4
# Copyright (C) 2007 MySQL AB
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.
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.
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
19
##############################################################################
21
# This scripts creates the MySQL Server system tables.
23
# This script try to match the shell script version as close as possible,
24
# but in addition being compatible with ActiveState Perl on Windows.
26
# All unrecognized arguments to this script are passed to mysqld.
28
# NOTE: This script in 5.0 doesn't really match the shell script
29
# version 100%, it is more close to the 5.1 version.
31
# NOTE: This script was deliberately written to be as close to the shell
32
# script as possible, to make the maintenance of both in parallel
35
##############################################################################
43
Getopt::Long::Configure("pass_through");
45
my @args; # Argument list filled in
47
##############################################################################
51
##############################################################################
57
--basedir=path The path to the MySQL installation directory.
58
--builddir=path If using --srcdir with out-of-directory builds, you
59
will need to set this to the location of the build
60
directory where built files reside.
61
--cross-bootstrap For internal use. Used when building the MySQL system
62
tables on a different host than the target.
63
--datadir=path The path to the MySQL data directory.
64
--force Causes mysql_install_db to run even if DNS does not
65
work. In that case, grant table entries that normally
66
use hostnames will use IP addresses.
67
--ldata=path The path to the MySQL data directory. Same as --datadir.
68
--rpm For internal use. This option is used by RPM files
69
during the MySQL installation process.
70
--skip-name-resolve Use IP addresses rather than hostnames when creating
71
grant table entries. This option can be useful if
72
your DNS does not work.
73
--srcdir=path The path to the MySQL source directory. This option
74
uses the compiled binaries and support files within the
75
source tree, useful for if you don't want to install
76
MySQL yet and just want to create the system tables.
77
--user=user_name The login username to use for running mysqld. Files
78
and directories created by mysqld will be owned by this
79
user. You must be root to use this option. By default
80
mysqld runs using your current login name and files and
81
directories that it creates will be owned by you.
83
All other options are passed to the mysqld program
89
##############################################################################
91
# Parse an argument list
93
# We only need to pass arguments through to the server if we don't
94
# handle them here. So, we collect unrecognized options (passed on
95
# the command line) into the args variable.
97
##############################################################################
103
my @saved_ARGV = @ARGV;
104
@ARGV = @_; # Set ARGV so GetOptions works
107
if (@ARGV and $ARGV[0] eq 'PICK-ARGS-FROM-ARGV')
117
"builddir=s", # FIXME not documented
121
# Note that the user will be passed to mysqld so that it runs
122
# as 'user' (crucial e.g. if log-bin=/some_other_path/
123
# where a chown of datadir won't help)
130
"defaults-file|defaults-extra-file|no-defaults:s",
132
# Used when building the MySQL system tables on a different host than
133
# the target. The platform-independent files that are created in
134
# --datadir on the host can be copied to the target system.
136
# The most common use for this feature is in the Windows installer
137
# which will take the files from datadir and include them as part of
138
# the install package. See top-level 'dist-hook' make target.
140
# --windows is a deprecated alias
141
"cross-bootstrap|windows", # FIXME undocumented, even needed?
144
usage() if $opt->{help};
146
@args = @ARGV if $pick_args;
148
@ARGV = @saved_ARGV; # Set back ARGV
151
##############################################################################
153
# Try to find a specific file within --basedir which can either be a binary
154
# release or installed source directory and return the path.
156
##############################################################################
161
my $mode = shift; # "dir" or "file"
164
foreach my $file ( @{ref($files) ? $files : [$files]} )
166
foreach my $dir ( @_ )
168
foreach my $part ( "$file","$file.exe","release/$file.exe",
169
"debug/$file.exe","relwithdebinfo/$file.exe" )
171
my $path = "$opt->{basedir}/$dir/$part";
174
return $mode eq "dir" ? dirname($path) : $path;
181
##############################################################################
183
# Just a function to write out an error report
185
##############################################################################
191
print "FATAL ERROR: Could not find $file\n";
193
print "If you compiled from source, you need to run 'make install' to\n";
194
print "copy the software into the correct location ready for operation.\n";
196
print "If you are using a binary release, you must either be at the top\n";
197
print "level of the extracted archive, or pass the --basedir option\n";
198
print "pointing to that location.\n";
204
##############################################################################
206
# Form a command line that can handle spaces in paths and arguments
208
##############################################################################
210
# FIXME this backslash escaping needed if using '"..."' ?
211
# This regexp makes sure that any special chars are quoted,
212
# so the arg gets passed exactly to the server.
213
# XXX: This is broken; true fix requires using eval and proper
214
# quoting of every single arg ($opt->{basedir}, $opt->{ldata}, etc.)
215
# join(" ", map {s/([^\w\_\.\-])/\\$1/g}
219
foreach my $opt ( @_ )
221
next unless $opt; # If undefined or empty, just skip
222
push(@cmd, "\"$opt\""); # Quote argument
224
return join(" ", @cmd);
227
##############################################################################
229
# Ok, let's go. We first need to parse arguments which are required by
230
# my_print_defaults so that we can execute it first, then later re-parse
231
# the command line to add any extra bits that we need.
233
##############################################################################
236
parse_arguments($opt, 'PICK-ARGS-FROM-ARGV', @ARGV);
238
# ----------------------------------------------------------------------
239
# We can now find my_print_defaults. This script supports:
241
# --srcdir=path pointing to compiled source tree
242
# --basedir=path pointing to installed binary location
244
# or default to compiled-in locations.
245
# ----------------------------------------------------------------------
249
if ( $opt->{srcdir} and $opt->{basedir} )
251
error("Specify either --basedir or --srcdir, not both");
253
if ( $opt->{srcdir} )
255
$opt->{builddir} = $opt->{srcdir} unless $opt->{builddir};
256
$print_defaults = "$opt->{builddir}/extra/my_print_defaults";
258
elsif ( $opt->{basedir} )
260
$print_defaults = find_in_basedir($opt,"file","my_print_defaults","bin","extra");
264
$print_defaults='@bindir@/my_print_defaults';
267
-x $print_defaults or -f "$print_defaults.exe"
268
or cannot_find_file($print_defaults);
270
# ----------------------------------------------------------------------
271
# Now we can get arguments from the groups [mysqld] and [mysql_install_db]
272
# in the my.cfg file, then re-run to merge with command line arguments.
273
# ----------------------------------------------------------------------
276
my $cmd = quote_options($print_defaults,$opt->{'defaults-file'},
277
"mysqld","mysql_install_db");
278
open(PIPE, "$cmd |") or error($opt,"can't run $cmd: $!");
283
push(@default_options, $_);
286
$opt = {}; # Reset the arguments FIXME ?
287
parse_arguments($opt, @default_options);
288
parse_arguments($opt, 'PICK-ARGS-FROM-ARGV', @ARGV);
290
# ----------------------------------------------------------------------
291
# Configure paths to support files
292
# ----------------------------------------------------------------------
294
# FIXME $extra_bindir is not used
295
my ($bindir,$extra_bindir,$mysqld,$pkgdatadir,$mysqld_opt,$scriptdir);
297
if ( $opt->{srcdir} )
299
$opt->{basedir} = $opt->{builddir};
300
$bindir = "$opt->{basedir}/client";
301
$extra_bindir = "$opt->{basedir}/extra";
302
$mysqld = "$opt->{basedir}/sql/mysqld";
303
$mysqld_opt = "--language=$opt->{srcdir}/sql/share/english";
304
$pkgdatadir = "$opt->{srcdir}/scripts";
305
$scriptdir = "$opt->{srcdir}/scripts";
307
elsif ( $opt->{basedir} )
309
$bindir = "$opt->{basedir}/bin";
310
$extra_bindir = $bindir;
311
$mysqld = find_in_basedir($opt,"file",["mysqld-nt","mysqld"],
312
"libexec","sbin","bin") || # ,"sql"
313
find_in_basedir($opt,"file","mysqld-nt",
315
$pkgdatadir = find_in_basedir($opt,"dir","fill_help_tables.sql",
316
"share","share/mysql"); # ,"scripts"
317
$scriptdir = "$opt->{basedir}/scripts";
321
$opt->{basedir} = '@prefix@';
322
$bindir = '@bindir@';
323
$extra_bindir = $bindir;
324
$mysqld = '@libexecdir@/mysqld';
325
$pkgdatadir = '@pkgdatadir@';
326
$scriptdir = '@scriptdir@';
329
unless ( $opt->{ldata} )
331
$opt->{ldata} = '@localstatedir@';
334
if ( $opt->{srcdir} )
336
$pkgdatadir = "$opt->{srcdir}/scripts";
339
# ----------------------------------------------------------------------
340
# Set up paths to SQL scripts required for bootstrap
341
# ----------------------------------------------------------------------
343
my $fill_help_tables = "$pkgdatadir/fill_help_tables.sql";
344
my $create_system_tables = "$pkgdatadir/mysql_system_tables.sql";
345
my $fill_system_tables = "$pkgdatadir/mysql_system_tables_data.sql";
347
foreach my $f ( $fill_help_tables,$create_system_tables,$fill_system_tables )
349
-f $f or cannot_find_file($f);
352
-x $mysqld or -f "$mysqld.exe" or cannot_find_file($mysqld);
353
# Try to determine the hostname
354
my $hostname = hostname();
356
# ----------------------------------------------------------------------
357
# Check if hostname is valid
358
# ----------------------------------------------------------------------
361
if ( !$opt->{'cross-bootstrap'} and !$opt->{rpm} and !$opt->{force} )
365
$resolved = `$resolveip $hostname 2>&1`;
368
$resolved=`$resolveip localhost 2>&1`;
372
"Neither host '$hostname' nor 'localhost' could be looked up with",
374
"Please configure the 'hostname' command to return a correct",
376
"If you want to solve this at a later stage, restart this script",
377
"with the --force option");
380
"The host '$hostname' could not be looked up with resolveip.",
381
"This probably means that your libc libraries are not 100 % compatible",
382
"with this binary MySQL version. The MySQL daemon, mysqld, should work",
383
"normally with the exception that host name resolving will not work.",
384
"This means that you should use IP addresses instead of hostnames",
385
"when specifying MySQL privileges !");
389
# FIXME what does this really mean....
390
if ( $opt->{'skip-name-resolve'} and $resolved and $resolved =~ /\s/ )
392
$hostname = (split(' ', $resolved))[5];
395
# ----------------------------------------------------------------------
396
# Create database directories mysql & test
397
# ----------------------------------------------------------------------
399
foreach my $dir ( $opt->{ldata}, "$opt->{ldata}/mysql", "$opt->{ldata}/test" )
401
# FIXME not really the same as original "mkdir -p", but ok?
402
mkdir($dir, 0700) unless -d $dir;
403
chown($opt->{user}, $dir) if -w "/" and !$opt->{user};
406
push(@args, "--user=$opt->{user}") if $opt->{user};
408
# ----------------------------------------------------------------------
409
# Configure mysqld command line
410
# ----------------------------------------------------------------------
412
# FIXME use --init-file instead of --bootstrap ?!
414
my $mysqld_bootstrap = $ENV{MYSQLD_BOOTSTRAP} || $mysqld;
415
my $mysqld_install_cmd_line = quote_options($mysqld_bootstrap,
416
$opt->{'defaults-file'},
419
"--basedir=$opt->{basedir}",
420
"--datadir=$opt->{ldata}",
424
"--max_allowed_packet=8M",
425
"--net_buffer_length=16K",
429
# ----------------------------------------------------------------------
430
# Create the system and help tables by passing them to "mysqld --bootstrap"
431
# ----------------------------------------------------------------------
433
report_verbose_wait($opt,"Installing MySQL system tables...");
435
open(SQL, $create_system_tables)
436
or error($opt,"can't open $create_system_tables for reading: $!");
437
# FIXME > /dev/null ?
438
if ( open(PIPE, "| $mysqld_install_cmd_line") )
440
print PIPE "use mysql;\n";
443
# When doing a "cross bootstrap" install, no reference to the current
444
# host should be added to the system tables. So we filter out any
445
# lines which contain the current host name.
446
next if $opt->{'cross-bootstrap'} and /\@current_hostname/;
453
report_verbose($opt,"OK");
455
# ----------------------------------------------------------------------
456
# Pipe fill_help_tables.sql to "mysqld --bootstrap"
457
# ----------------------------------------------------------------------
459
report_verbose_wait($opt,"Filling help tables...");
460
open(SQL, $fill_help_tables)
461
or error($opt,"can't open $fill_help_tables for reading: $!");
462
# FIXME > /dev/null ?
463
if ( open(PIPE, "| $mysqld_install_cmd_line") )
465
print PIPE "use mysql;\n";
473
report_verbose($opt,"OK");
477
warning($opt,"HELP FILES ARE NOT COMPLETELY INSTALLED!",
478
"The \"HELP\" command might not work properly");
481
report_verbose($opt,"To start mysqld at boot time you have to copy",
482
"support-files/mysql.server to the right place " .
485
if ( !$opt->{'cross-bootstrap'} )
487
# This is not a true installation on a running system. The end user must
488
# set a password after installing the data files on the real host system.
489
# At this point, there is no end user, so it does not make sense to print
492
"PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !",
493
"To do so, start the server, then issue the following commands:",
495
" $bindir/mysqladmin -u root password 'new-password'",
496
" $bindir/mysqladmin -u root -h $hostname password 'new-password'",
498
"Alternatively you can run:",
500
" $bindir/mysql_secure_installation",
502
"which will also give you the option of removing the test",
503
"databases and anonymous user created by default. This is",
504
"strongly recommended for production servers.",
506
"See the manual for more instructions.");
511
"You can start the MySQL daemon with:",
513
" cd " . '@prefix@' . " ; $bindir/mysqld_safe &",
515
"You can test the MySQL daemon with mysql-test-run.pl",
517
" cd mysql-test ; perl mysql-test-run.pl");
520
"Please report any problems with the " . '@scriptdir@' . "/mysqlbug script!",
522
"The latest information about MySQL is available on the web at",
524
" http://www.mysql.com",
526
"Support MySQL by buying support/licenses at http://shop.mysql.com");
533
"Installation of system tables failed!",
535
"Examine the logs in $opt->{ldata} for more information.",
536
"You can try to start the mysqld daemon with:",
537
"$mysqld --skip-grant &",
538
"and use the command line tool",
539
"$bindir/mysql to connect to the mysql",
540
"database and look at the grant tables:",
542
"shell> $bindir/mysql -u root mysql",
543
"mysql> show tables",
545
"Try 'mysqld --help' if you have problems with paths. Using --log",
546
"gives you a log in $opt->{ldata} that may be helpful.",
548
"The latest information about MySQL is available on the web at",
549
"http://www.mysql.com",
550
"Please consult the MySQL manual section: 'Problems running mysql_install_db',",
551
"and the manual section that describes problems on your OS.",
552
"Another information source is the MySQL email archive.",
553
"Please check all of the above before mailing us!",
554
"And if you do mail us, you MUST use the " . '@scriptdir@' . "/mysqlbug script!")
557
##############################################################################
561
##############################################################################
568
report_verbose_wait($opt, $text, @_);
572
sub report_verbose_wait
577
if ( $opt->{verbose} or (!$opt->{rpm} and !$opt->{'cross-bootstrap'}) )
580
map {print "\n$_"} @_;
590
map {print "$_\n"} @_;
599
print "FATAL ERROR: $text\n";
600
map {print "$_\n"} @_;
609
print "WARNING: $text\n";
610
map {print "$_\n"} @_;