~drizzle-trunk/drizzle/development

1 by brian
clean slate
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
#
21
#  This scripts creates the MySQL Server system tables.
22
#
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.
25
#
26
#  All unrecognized arguments to this script are passed to mysqld.
27
#
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.
30
#
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
33
#        easier.
34
#
35
##############################################################################
36
37
use File::Basename;
38
use Getopt::Long;
39
use Sys::Hostname;
40
use Data::Dumper;
41
use strict;
42
43
Getopt::Long::Configure("pass_through");
44
45
my @args;                       # Argument list filled in
46
47
##############################################################################
48
#
49
#  Usage information
50
#
51
##############################################################################
52
53
sub usage
54
{
55
  print <<EOF;
56
Usage: $0 [OPTIONS]
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.
82
83
All other options are passed to the mysqld program
84
85
EOF
86
  exit 1;
87
}
88
89
##############################################################################
90
#
91
#  Parse an argument list
92
#
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.
96
#
97
##############################################################################
98
99
sub parse_arguments
100
{
101
  my $opt = shift;
102
103
  my @saved_ARGV = @ARGV;
104
  @ARGV = @_;                           # Set ARGV so GetOptions works
105
106
  my $pick_args;
107
  if (@ARGV and $ARGV[0] eq 'PICK-ARGS-FROM-ARGV')
108
  {
109
    $pick_args = 1;
110
    shift @ARGV;
111
  }
112
113
  GetOptions(
114
             $opt,
115
             "force",
116
             "basedir=s",
117
             "builddir=s",      # FIXME not documented
118
             "srcdir=s",
119
             "ldata|datadir=s",
120
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)
124
             "user=s",
125
126
             "skip-name-resolve",
127
             "verbose",
128
             "rpm",
129
             "help",
130
             "defaults-file|defaults-extra-file|no-defaults:s",
131
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.
135
             #
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.
139
             #
140
             # --windows is a deprecated alias
141
             "cross-bootstrap|windows", # FIXME undocumented, even needed?
142
  ) or usage();
143
144
  usage() if $opt->{help};
145
146
  @args =  @ARGV if $pick_args;
147
148
  @ARGV = @saved_ARGV;                  # Set back ARGV
149
}
150
151
##############################################################################
152
#
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.
155
#
156
##############################################################################
157
158
sub find_in_basedir
159
{
160
  my $opt   = shift;
161
  my $mode  = shift;            # "dir" or "file"
162
  my $files = shift;
163
164
  foreach my $file ( @{ref($files) ? $files : [$files]} )
165
  {
166
    foreach my $dir ( @_ )
167
    {
168
      foreach my $part ( "$file","$file.exe","release/$file.exe",
169
                         "debug/$file.exe","relwithdebinfo/$file.exe" )
170
      {
171
        my $path = "$opt->{basedir}/$dir/$part";
172
        if ( -f $path )
173
        {
174
          return $mode eq "dir" ? dirname($path) : $path;
175
        }
176
      }
177
    }
178
  }
179
}
180
181
##############################################################################
182
#
183
#  Just a function to write out an error report
184
#
185
##############################################################################
186
187
sub cannot_find_file
188
{
189
  my $file = shift;
190
191
  print "FATAL ERROR: Could not find $file\n";
192
  print "\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";
195
  print "\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";
199
  print "\n";
200
201
  exit 1;
202
}
203
204
##############################################################################
205
#
206
#  Form a command line that can handle spaces in paths and arguments
207
#
208
##############################################################################
209
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}
216
217
sub quote_options {
218
  my @cmd;
219
  foreach my $opt ( @_ )
220
  {
221
    next unless $opt;           # If undefined or empty, just skip
222
    push(@cmd, "\"$opt\"");     # Quote argument
223
  }
224
  return join(" ", @cmd);
225
}
226
227
##############################################################################
228
#
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.
232
#
233
##############################################################################
234
235
my $opt = {};
236
parse_arguments($opt, 'PICK-ARGS-FROM-ARGV', @ARGV);
237
238
# ----------------------------------------------------------------------
239
# We can now find my_print_defaults.  This script supports:
240
#
241
#   --srcdir=path pointing to compiled source tree
242
#   --basedir=path pointing to installed binary location
243
#
244
# or default to compiled-in locations.
245
# ----------------------------------------------------------------------
246
247
my $print_defaults;
248
249
if ( $opt->{srcdir} and $opt->{basedir} )
250
{
251
  error("Specify either --basedir or --srcdir, not both");
252
}
253
if ( $opt->{srcdir} )
254
{
255
  $opt->{builddir} = $opt->{srcdir} unless $opt->{builddir};
256
  $print_defaults = "$opt->{builddir}/extra/my_print_defaults";
257
}
258
elsif ( $opt->{basedir} )
259
{
260
  $print_defaults = find_in_basedir($opt,"file","my_print_defaults","bin","extra");
261
}
262
else
263
{
264
  $print_defaults='@bindir@/my_print_defaults';
265
}
266
267
-x $print_defaults or -f "$print_defaults.exe"
268
  or cannot_find_file($print_defaults);
269
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
# ----------------------------------------------------------------------
274
275
my @default_options;
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: $!");
279
while ( <PIPE> )
280
{
281
  chomp;
282
  next unless /\S/;
283
  push(@default_options, $_);
284
}
285
close PIPE;
286
$opt = {};                              # Reset the arguments FIXME ?
287
parse_arguments($opt, @default_options);
288
parse_arguments($opt, 'PICK-ARGS-FROM-ARGV', @ARGV);
289
290
# ----------------------------------------------------------------------
291
# Configure paths to support files
292
# ----------------------------------------------------------------------
293
294
# FIXME $extra_bindir is not used
295
my ($bindir,$extra_bindir,$mysqld,$pkgdatadir,$mysqld_opt,$scriptdir);
296
297
if ( $opt->{srcdir} )
298
{
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";
306
}
307
elsif ( $opt->{basedir} )
308
{
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",
314
                                  "bin");  # ,"sql"
315
  $pkgdatadir     = find_in_basedir($opt,"dir","fill_help_tables.sql",
316
                                    "share","share/mysql");  # ,"scripts"
317
  $scriptdir      = "$opt->{basedir}/scripts";
318
}
319
else
320
{
321
  $opt->{basedir} = '@prefix@';
322
  $bindir         = '@bindir@';
323
  $extra_bindir   = $bindir;
324
  $mysqld         = '@libexecdir@/mysqld';
325
  $pkgdatadir     = '@pkgdatadir@';
326
  $scriptdir      = '@scriptdir@';
327
}
328
329
unless ( $opt->{ldata} )
330
{
331
  $opt->{ldata} = '@localstatedir@';
332
}
333
334
if ( $opt->{srcdir} )
335
{
336
  $pkgdatadir = "$opt->{srcdir}/scripts";
337
}
338
339
# ----------------------------------------------------------------------
340
# Set up paths to SQL scripts required for bootstrap
341
# ----------------------------------------------------------------------
342
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";
346
347
foreach my $f ( $fill_help_tables,$create_system_tables,$fill_system_tables )
348
{
349
  -f $f or cannot_find_file($f);
350
}
351
352
-x $mysqld or -f "$mysqld.exe" or cannot_find_file($mysqld);
353
# Try to determine the hostname
354
my $hostname = hostname();
355
356
# ----------------------------------------------------------------------
357
# Check if hostname is valid
358
# ----------------------------------------------------------------------
359
360
my $resolved;
361
if ( !$opt->{'cross-bootstrap'} and !$opt->{rpm} and !$opt->{force} )
362
{
363
  my $resolveip;
364
365
  $resolved = `$resolveip $hostname 2>&1`;
366
  if ( $? != 0 )
367
  {
368
    $resolved=`$resolveip localhost 2>&1`;
369
    if ( $? != 0 )
370
    {
371
      error($opt,
372
            "Neither host '$hostname' nor 'localhost' could be looked up with",
373
            "$bindir/resolveip",
374
            "Please configure the 'hostname' command to return a correct",
375
            "hostname.",
376
            "If you want to solve this at a later stage, restart this script",
377
            "with the --force option");
378
    }
379
    warning($opt,
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 !");
386
  }
387
}
388
389
# FIXME what does this really mean....
390
if ( $opt->{'skip-name-resolve'} and $resolved and $resolved =~ /\s/ )
391
{
392
  $hostname = (split(' ', $resolved))[5];
393
}
394
395
# ----------------------------------------------------------------------
396
# Create database directories mysql & test
397
# ----------------------------------------------------------------------
398
399
foreach my $dir ( $opt->{ldata}, "$opt->{ldata}/mysql", "$opt->{ldata}/test" )
400
{
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};
404
}
405
406
push(@args, "--user=$opt->{user}") if $opt->{user};
407
408
# ----------------------------------------------------------------------
409
# Configure mysqld command line
410
# ----------------------------------------------------------------------
411
412
# FIXME use --init-file instead of --bootstrap ?!
413
414
my $mysqld_bootstrap = $ENV{MYSQLD_BOOTSTRAP} || $mysqld;
415
my $mysqld_install_cmd_line = quote_options($mysqld_bootstrap,
416
                                            $opt->{'defaults-file'},
417
                                            $mysqld_opt,
418
                                            "--bootstrap",
419
                                            "--basedir=$opt->{basedir}",
420
                                            "--datadir=$opt->{ldata}",
421
                                            "--skip-innodb",
422
                                            "--skip-bdb",
423
                                            "--skip-ndbcluster",
424
                                            "--max_allowed_packet=8M",
425
                                            "--net_buffer_length=16K",
426
                                            @args,
427
                                          );
428
429
# ----------------------------------------------------------------------
430
# Create the system and help tables by passing them to "mysqld --bootstrap"
431
# ----------------------------------------------------------------------
432
433
report_verbose_wait($opt,"Installing MySQL system tables...");
434
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") )
439
{
440
  print PIPE "use mysql;\n";
441
  while ( <SQL> )
442
  {
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/;
447
448
    print PIPE $_;
449
  }
450
  close PIPE;
451
  close SQL;
452
453
  report_verbose($opt,"OK");
454
455
  # ----------------------------------------------------------------------
456
  # Pipe fill_help_tables.sql to "mysqld --bootstrap"
457
  # ----------------------------------------------------------------------
458
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") )
464
  {
465
    print PIPE "use mysql;\n";
466
    while ( <SQL> )
467
    {
468
      print PIPE $_;
469
    }
470
    close PIPE;
471
    close SQL;
472
473
    report_verbose($opt,"OK");
474
  }
475
  else
476
  {
477
    warning($opt,"HELP FILES ARE NOT COMPLETELY INSTALLED!",
478
                 "The \"HELP\" command might not work properly");
479
  }
480
481
  report_verbose($opt,"To start mysqld at boot time you have to copy",
482
                      "support-files/mysql.server to the right place " .
483
                      "for your system");
484
485
  if ( !$opt->{'cross-bootstrap'} )
486
  {
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
490
    # this reminder.
491
    report($opt,
492
           "PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !",
493
           "To do so, start the server, then issue the following commands:",
494
           "",
495
           "  $bindir/mysqladmin -u root password 'new-password'",
496
           "  $bindir/mysqladmin -u root -h $hostname password 'new-password'",
497
           "",
498
           "Alternatively you can run:",
499
           "",
500
           "  $bindir/mysql_secure_installation",
501
           "",
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.",
505
           "",
506
           "See the manual for more instructions.");
507
508
    if ( !$opt->{rpm} )
509
    {
510
      report($opt,
511
             "You can start the MySQL daemon with:",
512
             "",
513
             "  cd " . '@prefix@' . " ; $bindir/mysqld_safe &",
514
             "",
515
             "You can test the MySQL daemon with mysql-test-run.pl",
516
             "",
517
             "  cd mysql-test ; perl mysql-test-run.pl");
518
    }
519
    report($opt,
520
           "Please report any problems with the " . '@scriptdir@' . "/mysqlbug script!",
521
           "",
522
           "The latest information about MySQL is available on the web at",
523
           "",
524
           "  http://www.mysql.com",
525
           "",
526
           "Support MySQL by buying support/licenses at http://shop.mysql.com");
527
  }
528
  exit 0
529
}
530
else
531
{
532
  error($opt,
533
        "Installation of system tables failed!",
534
         "",
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:",
541
        "",
542
        "shell> $bindir/mysql -u root mysql",
543
        "mysql> show tables",
544
        "",
545
        "Try 'mysqld --help' if you have problems with paths. Using --log",
546
        "gives you a log in $opt->{ldata} that may be helpful.",
547
        "",
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!")
555
}
556
557
##############################################################################
558
#
559
#  Misc
560
#
561
##############################################################################
562
563
sub report_verbose
564
{
565
  my $opt  = shift;
566
  my $text = shift;
567
568
  report_verbose_wait($opt, $text, @_);
569
  print "\n\n";
570
}
571
572
sub report_verbose_wait
573
{
574
  my $opt  = shift;
575
  my $text = shift;
576
577
  if ( $opt->{verbose} or (!$opt->{rpm} and !$opt->{'cross-bootstrap'}) )
578
  {
579
    print "$text";
580
    map {print "\n$_"} @_;
581
  }
582
}
583
584
sub report
585
{
586
  my $opt  = shift;
587
  my $text = shift;
588
589
  print "$text\n";
590
  map {print "$_\n"} @_;
591
  print "\n";
592
}
593
594
sub error
595
{
596
  my $opt  = shift;
597
  my $text = shift;
598
599
  print "FATAL ERROR: $text\n";
600
  map {print "$_\n"} @_;
601
  exit 1;
602
}
603
604
sub warning
605
{
606
  my $opt  = shift;
607
  my $text = shift;
608
609
  print "WARNING: $text\n";
610
  map {print "$_\n"} @_;
611
  print "\n";
612
}