You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

612 lines
20 KiB

make_win_bin_dist CMakeLists.txt resolveip.c Makefile.am: Better Windows support in the scripts directory mysql_config.pl.in, mysql_install_db.pl.in: New Perl version of Unix shell script, mainly for Windows Many files in scripts directory: Use default Perl location "#!/usr/bin/perl" instead of the build host path extra/CMakeLists.txt: Added target for executable "resolveip" extra/resolveip.c: Exclude Unix specific headers when compiling on Windows scripts/CMakeLists.txt: On Windows, filter Perl scripts and change name from ".sh" to ".pl" mysql_convert_table_format.sh mysql_explain_log.sh mysql_secure_installation.sh mysql_tableinfo.sh mysqld_multi.sh mysqldumpslow.sh mysqlhotcopy.sh Do the same for the new Windows specific Perl versions of shell scripts mysql_config.pl.in mysql_install_db.pl.in In CMake, set reasonable values for 'CFLAGS', 'prefix', 'datadir' and so on. scripts/Makefile.am: Include "mysql_config.pl.in" and "mysql_install_db.pl.in" in the source TAR scripts/make_win_bin_dist: Only include explicitly listed scripts from the "scripts" directory scripts/mysql_convert_table_format.sh: Use default Perl location "#!/usr/bin/perl" instead of the build host path scripts/mysql_explain_log.sh: Use default Perl location "#!/usr/bin/perl" instead of the build host path scripts/mysql_tableinfo.sh: Use default Perl location "#!/usr/bin/perl" instead of the build host path scripts/mysqld_multi.sh: Use default Perl location "#!/usr/bin/perl" instead of the build host path scripts/mysqldumpslow.sh: Use default Perl location "#!/usr/bin/perl" instead of the build host path scripts/mysqlhotcopy.sh: Use default Perl location "#!/usr/bin/perl" instead of the build host path scripts/mysql_config.pl.in: New Perl version of Unix shell script, mainly for Windows scripts/mysql_install_db.pl.in: New Perl version of Unix shell script, mainly for Windows
18 years ago
  1. #!/usr/bin/perl
  2. # -*- cperl -*-
  3. #
  4. # Copyright (C) 2007 MySQL AB
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; version 2 of the License.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. ##############################################################################
  19. #
  20. # This scripts creates the MySQL Server system tables.
  21. #
  22. # This script try to match the shell script version as close as possible,
  23. # but in addition being compatible with ActiveState Perl on Windows.
  24. #
  25. # All unrecognized arguments to this script are passed to mysqld.
  26. #
  27. # NOTE: This script in 5.0 doesn't really match the shell script
  28. # version 100%, it is more close to the 5.1 version.
  29. #
  30. # NOTE: This script was deliberately written to be as close to the shell
  31. # script as possible, to make the maintenance of both in parallel
  32. # easier.
  33. #
  34. ##############################################################################
  35. use File::Basename;
  36. use Getopt::Long;
  37. use Sys::Hostname;
  38. use Data::Dumper;
  39. use strict;
  40. Getopt::Long::Configure("pass_through");
  41. my @args; # Argument list filled in
  42. ##############################################################################
  43. #
  44. # Usage information
  45. #
  46. ##############################################################################
  47. sub usage
  48. {
  49. print <<EOF;
  50. Usage: $0 [OPTIONS]
  51. --basedir=path The path to the MySQL installation directory.
  52. --builddir=path If using --srcdir with out-of-directory builds, you
  53. will need to set this to the location of the build
  54. directory where built files reside.
  55. --cross-bootstrap For internal use. Used when building the MySQL system
  56. tables on a different host than the target.
  57. --datadir=path The path to the MySQL data directory.
  58. --force Causes mysql_install_db to run even if DNS does not
  59. work. In that case, grant table entries that normally
  60. use hostnames will use IP addresses.
  61. --ldata=path The path to the MySQL data directory. Same as --datadir.
  62. --rpm For internal use. This option is used by RPM files
  63. during the MySQL installation process.
  64. --skip-name-resolve Use IP addresses rather than hostnames when creating
  65. grant table entries. This option can be useful if
  66. your DNS does not work.
  67. --srcdir=path The path to the MySQL source directory. This option
  68. uses the compiled binaries and support files within the
  69. source tree, useful for if you don't want to install
  70. MySQL yet and just want to create the system tables.
  71. --user=user_name The login username to use for running mysqld. Files
  72. and directories created by mysqld will be owned by this
  73. user. You must be root to use this option. By default
  74. mysqld runs using your current login name and files and
  75. directories that it creates will be owned by you.
  76. All other options are passed to the mysqld program
  77. EOF
  78. exit 1;
  79. }
  80. ##############################################################################
  81. #
  82. # Parse an argument list
  83. #
  84. # We only need to pass arguments through to the server if we don't
  85. # handle them here. So, we collect unrecognized options (passed on
  86. # the command line) into the args variable.
  87. #
  88. ##############################################################################
  89. sub parse_arguments
  90. {
  91. my $opt = shift;
  92. my @saved_ARGV = @ARGV;
  93. @ARGV = @_; # Set ARGV so GetOptions works
  94. my $pick_args;
  95. if (@ARGV and $ARGV[0] eq 'PICK-ARGS-FROM-ARGV')
  96. {
  97. $pick_args = 1;
  98. shift @ARGV;
  99. }
  100. GetOptions(
  101. $opt,
  102. "force",
  103. "basedir=s",
  104. "builddir=s", # FIXME not documented
  105. "srcdir=s",
  106. "ldata|datadir=s",
  107. # Note that the user will be passed to mysqld so that it runs
  108. # as 'user' (crucial e.g. if log-bin=/some_other_path/
  109. # where a chown of datadir won't help)
  110. "user=s",
  111. "skip-name-resolve",
  112. "verbose",
  113. "rpm",
  114. "help",
  115. "defaults-file|defaults-extra-file|no-defaults:s",
  116. # Used when building the MySQL system tables on a different host than
  117. # the target. The platform-independent files that are created in
  118. # --datadir on the host can be copied to the target system.
  119. #
  120. # The most common use for this feature is in the Windows installer
  121. # which will take the files from datadir and include them as part of
  122. # the install package. See top-level 'dist-hook' make target.
  123. #
  124. # --windows is a deprecated alias
  125. "cross-bootstrap|windows", # FIXME undocumented, even needed?
  126. ) or usage();
  127. usage() if $opt->{help};
  128. @args = @ARGV if $pick_args;
  129. @ARGV = @saved_ARGV; # Set back ARGV
  130. }
  131. ##############################################################################
  132. #
  133. # Try to find a specific file within --basedir which can either be a binary
  134. # release or installed source directory and return the path.
  135. #
  136. ##############################################################################
  137. sub find_in_basedir
  138. {
  139. my $opt = shift;
  140. my $mode = shift; # "dir" or "file"
  141. my $files = shift;
  142. foreach my $file ( @{ref($files) ? $files : [$files]} )
  143. {
  144. foreach my $dir ( @_ )
  145. {
  146. foreach my $part ( "$file","$file.exe","release/$file.exe",
  147. "debug/$file.exe","relwithdebinfo/$file.exe" )
  148. {
  149. my $path = "$opt->{basedir}/$dir/$part";
  150. if ( -f $path )
  151. {
  152. return $mode eq "dir" ? dirname($path) : $path;
  153. }
  154. }
  155. }
  156. }
  157. }
  158. ##############################################################################
  159. #
  160. # Just a function to write out an error report
  161. #
  162. ##############################################################################
  163. sub cannot_find_file
  164. {
  165. my $file = shift;
  166. print "FATAL ERROR: Could not find $file\n";
  167. print "\n";
  168. print "If you compiled from source, you need to run 'make install' to\n";
  169. print "copy the software into the correct location ready for operation.\n";
  170. print "\n";
  171. print "If you are using a binary release, you must either be at the top\n";
  172. print "level of the extracted archive, or pass the --basedir option\n";
  173. print "pointing to that location.\n";
  174. print "\n";
  175. exit 1;
  176. }
  177. ##############################################################################
  178. #
  179. # Form a command line that can handle spaces in paths and arguments
  180. #
  181. ##############################################################################
  182. # FIXME this backslash escaping needed if using '"..."' ?
  183. # This regexp makes sure that any special chars are quoted,
  184. # so the arg gets passed exactly to the server.
  185. # XXX: This is broken; true fix requires using eval and proper
  186. # quoting of every single arg ($opt->{basedir}, $opt->{ldata}, etc.)
  187. # join(" ", map {s/([^\w\_\.\-])/\\$1/g}
  188. sub quote_options {
  189. my @cmd;
  190. foreach my $opt ( @_ )
  191. {
  192. next unless $opt; # If undefined or empty, just skip
  193. push(@cmd, "\"$opt\""); # Quote argument
  194. }
  195. return join(" ", @cmd);
  196. }
  197. ##############################################################################
  198. #
  199. # Ok, let's go. We first need to parse arguments which are required by
  200. # my_print_defaults so that we can execute it first, then later re-parse
  201. # the command line to add any extra bits that we need.
  202. #
  203. ##############################################################################
  204. my $opt = {};
  205. parse_arguments($opt, 'PICK-ARGS-FROM-ARGV', @ARGV);
  206. # ----------------------------------------------------------------------
  207. # We can now find my_print_defaults. This script supports:
  208. #
  209. # --srcdir=path pointing to compiled source tree
  210. # --basedir=path pointing to installed binary location
  211. #
  212. # or default to compiled-in locations.
  213. # ----------------------------------------------------------------------
  214. my $print_defaults;
  215. if ( $opt->{srcdir} and $opt->{basedir} )
  216. {
  217. error("Specify either --basedir or --srcdir, not both");
  218. }
  219. if ( $opt->{srcdir} )
  220. {
  221. $opt->{builddir} = $opt->{srcdir} unless $opt->{builddir};
  222. $print_defaults = "$opt->{builddir}/extra/my_print_defaults";
  223. }
  224. elsif ( $opt->{basedir} )
  225. {
  226. $print_defaults = find_in_basedir($opt,"file","my_print_defaults","bin","extra");
  227. }
  228. else
  229. {
  230. $print_defaults='@bindir@/my_print_defaults';
  231. }
  232. -x $print_defaults or -f "$print_defaults.exe"
  233. or cannot_find_file($print_defaults);
  234. # ----------------------------------------------------------------------
  235. # Now we can get arguments from the groups [mysqld] and [mysql_install_db]
  236. # in the my.cfg file, then re-run to merge with command line arguments.
  237. # ----------------------------------------------------------------------
  238. my @default_options;
  239. my $cmd = quote_options($print_defaults,$opt->{'defaults-file'},
  240. "mysqld","mysql_install_db");
  241. open(PIPE, "$cmd |") or error($opt,"can't run $cmd: $!");
  242. while ( <PIPE> )
  243. {
  244. chomp;
  245. next unless /\S/;
  246. push(@default_options, $_);
  247. }
  248. close PIPE;
  249. $opt = {}; # Reset the arguments FIXME ?
  250. parse_arguments($opt, @default_options);
  251. parse_arguments($opt, 'PICK-ARGS-FROM-ARGV', @ARGV);
  252. # ----------------------------------------------------------------------
  253. # Configure paths to support files
  254. # ----------------------------------------------------------------------
  255. # FIXME $extra_bindir is not used
  256. my ($bindir,$extra_bindir,$mysqld,$pkgdatadir,$mysqld_opt,$scriptdir);
  257. if ( $opt->{srcdir} )
  258. {
  259. $opt->{basedir} = $opt->{builddir};
  260. $bindir = "$opt->{basedir}/client";
  261. $extra_bindir = "$opt->{basedir}/extra";
  262. $mysqld = "$opt->{basedir}/sql/mysqld";
  263. $mysqld_opt = "--language=$opt->{srcdir}/sql/share/english";
  264. $pkgdatadir = "$opt->{srcdir}/scripts";
  265. $scriptdir = "$opt->{srcdir}/scripts";
  266. }
  267. elsif ( $opt->{basedir} )
  268. {
  269. $bindir = "$opt->{basedir}/bin";
  270. $extra_bindir = $bindir;
  271. $mysqld = find_in_basedir($opt,"file",["mysqld-nt","mysqld"],
  272. "libexec","sbin","bin") || # ,"sql"
  273. find_in_basedir($opt,"file","mysqld-nt",
  274. "bin"); # ,"sql"
  275. $pkgdatadir = find_in_basedir($opt,"dir","fill_help_tables.sql",
  276. "share","share/mysql"); # ,"scripts"
  277. $scriptdir = "$opt->{basedir}/scripts";
  278. }
  279. else
  280. {
  281. $opt->{basedir} = '@prefix@';
  282. $bindir = '@bindir@';
  283. $extra_bindir = $bindir;
  284. $mysqld = '@libexecdir@/mysqld';
  285. $pkgdatadir = '@pkgdatadir@';
  286. $scriptdir = '@scriptdir@';
  287. }
  288. unless ( $opt->{ldata} )
  289. {
  290. $opt->{ldata} = '@localstatedir@';
  291. }
  292. if ( $opt->{srcdir} )
  293. {
  294. $pkgdatadir = "$opt->{srcdir}/scripts";
  295. }
  296. # ----------------------------------------------------------------------
  297. # Set up paths to SQL scripts required for bootstrap
  298. # ----------------------------------------------------------------------
  299. my $fill_help_tables = "$pkgdatadir/fill_help_tables.sql";
  300. my $create_system_tables = "$pkgdatadir/mysql_system_tables.sql";
  301. my $fill_system_tables = "$pkgdatadir/mysql_system_tables_data.sql";
  302. foreach my $f ( $fill_help_tables,$create_system_tables,$fill_system_tables )
  303. {
  304. -f $f or cannot_find_file($f);
  305. }
  306. -x $mysqld or -f "$mysqld.exe" or cannot_find_file($mysqld);
  307. # Try to determine the hostname
  308. my $hostname = hostname();
  309. # ----------------------------------------------------------------------
  310. # Check if hostname is valid
  311. # ----------------------------------------------------------------------
  312. my $resolved;
  313. if ( !$opt->{'cross-bootstrap'} and !$opt->{rpm} and !$opt->{force} )
  314. {
  315. my $resolveip;
  316. $resolved = `$resolveip $hostname 2>&1`;
  317. if ( $? != 0 )
  318. {
  319. $resolved=`$resolveip localhost 2>&1`;
  320. if ( $? != 0 )
  321. {
  322. error($opt,
  323. "Neither host '$hostname' nor 'localhost' could be looked up with",
  324. "$bindir/resolveip",
  325. "Please configure the 'hostname' command to return a correct",
  326. "hostname.",
  327. "If you want to solve this at a later stage, restart this script",
  328. "with the --force option");
  329. }
  330. warning($opt,
  331. "The host '$hostname' could not be looked up with resolveip.",
  332. "This probably means that your libc libraries are not 100 % compatible",
  333. "with this binary MySQL version. The MySQL daemon, mysqld, should work",
  334. "normally with the exception that host name resolving will not work.",
  335. "This means that you should use IP addresses instead of hostnames",
  336. "when specifying MySQL privileges !");
  337. }
  338. }
  339. # FIXME what does this really mean....
  340. if ( $opt->{'skip-name-resolve'} and $resolved and $resolved =~ /\s/ )
  341. {
  342. $hostname = (split(' ', $resolved))[5];
  343. }
  344. # ----------------------------------------------------------------------
  345. # Create database directories mysql & test
  346. # ----------------------------------------------------------------------
  347. foreach my $dir ( $opt->{ldata}, "$opt->{ldata}/mysql", "$opt->{ldata}/test" )
  348. {
  349. # FIXME not really the same as original "mkdir -p", but ok?
  350. mkdir($dir, 0700) unless -d $dir;
  351. chown($opt->{user}, $dir) if -w "/" and !$opt->{user};
  352. }
  353. push(@args, "--user=$opt->{user}") if $opt->{user};
  354. # ----------------------------------------------------------------------
  355. # Configure mysqld command line
  356. # ----------------------------------------------------------------------
  357. # FIXME use --init-file instead of --bootstrap ?!
  358. my $mysqld_bootstrap = $ENV{MYSQLD_BOOTSTRAP} || $mysqld;
  359. my $mysqld_install_cmd_line = quote_options($mysqld_bootstrap,
  360. $opt->{'defaults-file'},
  361. $mysqld_opt,
  362. "--bootstrap",
  363. "--basedir=$opt->{basedir}",
  364. "--datadir=$opt->{ldata}",
  365. "--skip-innodb",
  366. "--skip-bdb",
  367. "--skip-ndbcluster",
  368. "--max_allowed_packet=8M",
  369. "--net_buffer_length=16K",
  370. @args,
  371. );
  372. # ----------------------------------------------------------------------
  373. # Create the system and help tables by passing them to "mysqld --bootstrap"
  374. # ----------------------------------------------------------------------
  375. report_verbose_wait($opt,"Installing MySQL system tables...");
  376. open(SQL, $create_system_tables)
  377. or error($opt,"can't open $create_system_tables for reading: $!");
  378. # FIXME > /dev/null ?
  379. if ( open(PIPE, "| $mysqld_install_cmd_line") )
  380. {
  381. print PIPE "use mysql;\n";
  382. while ( <SQL> )
  383. {
  384. # When doing a "cross bootstrap" install, no reference to the current
  385. # host should be added to the system tables. So we filter out any
  386. # lines which contain the current host name.
  387. next if $opt->{'cross-bootstrap'} and /\@current_hostname/;
  388. print PIPE $_;
  389. }
  390. close PIPE;
  391. close SQL;
  392. report_verbose($opt,"OK");
  393. # ----------------------------------------------------------------------
  394. # Pipe fill_help_tables.sql to "mysqld --bootstrap"
  395. # ----------------------------------------------------------------------
  396. report_verbose_wait($opt,"Filling help tables...");
  397. open(SQL, $fill_help_tables)
  398. or error($opt,"can't open $fill_help_tables for reading: $!");
  399. # FIXME > /dev/null ?
  400. if ( open(PIPE, "| $mysqld_install_cmd_line") )
  401. {
  402. print PIPE "use mysql;\n";
  403. while ( <SQL> )
  404. {
  405. print PIPE $_;
  406. }
  407. close PIPE;
  408. close SQL;
  409. report_verbose($opt,"OK");
  410. }
  411. else
  412. {
  413. warning($opt,"HELP FILES ARE NOT COMPLETELY INSTALLED!",
  414. "The \"HELP\" command might not work properly");
  415. }
  416. report_verbose($opt,"To start mysqld at boot time you have to copy",
  417. "support-files/mysql.server to the right place " .
  418. "for your system");
  419. if ( !$opt->{'cross-bootstrap'} )
  420. {
  421. # This is not a true installation on a running system. The end user must
  422. # set a password after installing the data files on the real host system.
  423. # At this point, there is no end user, so it does not make sense to print
  424. # this reminder.
  425. report($opt,
  426. "PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !",
  427. "To do so, start the server, then issue the following commands:",
  428. "",
  429. " $bindir/mysqladmin -u root password 'new-password'",
  430. " $bindir/mysqladmin -u root -h $hostname password 'new-password'",
  431. "",
  432. "Alternatively you can run:",
  433. "",
  434. " $bindir/mysql_secure_installation",
  435. "",
  436. "which will also give you the option of removing the test",
  437. "databases and anonymous user created by default. This is",
  438. "strongly recommended for production servers.",
  439. "",
  440. "See the manual for more instructions.");
  441. if ( !$opt->{rpm} )
  442. {
  443. report($opt,
  444. "You can start the MySQL daemon with:",
  445. "",
  446. " cd " . '@prefix@' . " ; $bindir/mysqld_safe &",
  447. "",
  448. "You can test the MySQL daemon with mysql-test-run.pl",
  449. "",
  450. " cd mysql-test ; perl mysql-test-run.pl");
  451. }
  452. report($opt,
  453. "Please report any problems with the " . '@scriptdir@' . "/mysqlbug script!",
  454. "",
  455. "The latest information about MySQL is available on the web at",
  456. "",
  457. " http://www.mysql.com",
  458. "",
  459. "Support MySQL by buying support/licenses at http://shop.mysql.com");
  460. }
  461. exit 0
  462. }
  463. else
  464. {
  465. error($opt,
  466. "Installation of system tables failed!",
  467. "",
  468. "Examine the logs in $opt->{ldata} for more information.",
  469. "You can try to start the mysqld daemon with:",
  470. "$mysqld --skip-grant &",
  471. "and use the command line tool",
  472. "$bindir/mysql to connect to the mysql",
  473. "database and look at the grant tables:",
  474. "",
  475. "shell> $bindir/mysql -u root mysql",
  476. "mysql> show tables",
  477. "",
  478. "Try 'mysqld --help' if you have problems with paths. Using --log",
  479. "gives you a log in $opt->{ldata} that may be helpful.",
  480. "",
  481. "The latest information about MySQL is available on the web at",
  482. "http://www.mysql.com",
  483. "Please consult the MySQL manual section: 'Problems running mysql_install_db',",
  484. "and the manual section that describes problems on your OS.",
  485. "Another information source is the MySQL email archive.",
  486. "Please check all of the above before mailing us!",
  487. "And if you do mail us, you MUST use the " . '@scriptdir@' . "/mysqlbug script!")
  488. }
  489. ##############################################################################
  490. #
  491. # Misc
  492. #
  493. ##############################################################################
  494. sub report_verbose
  495. {
  496. my $opt = shift;
  497. my $text = shift;
  498. report_verbose_wait($opt, $text, @_);
  499. print "\n\n";
  500. }
  501. sub report_verbose_wait
  502. {
  503. my $opt = shift;
  504. my $text = shift;
  505. if ( $opt->{verbose} or (!$opt->{rpm} and !$opt->{'cross-bootstrap'}) )
  506. {
  507. print "$text";
  508. map {print "\n$_"} @_;
  509. }
  510. }
  511. sub report
  512. {
  513. my $opt = shift;
  514. my $text = shift;
  515. print "$text\n";
  516. map {print "$_\n"} @_;
  517. print "\n";
  518. }
  519. sub error
  520. {
  521. my $opt = shift;
  522. my $text = shift;
  523. print "FATAL ERROR: $text\n";
  524. map {print "$_\n"} @_;
  525. exit 1;
  526. }
  527. sub warning
  528. {
  529. my $opt = shift;
  530. my $text = shift;
  531. print "WARNING: $text\n";
  532. map {print "$_\n"} @_;
  533. print "\n";
  534. }