3636
3737use Fcntl;
3838use File::Basename;
39+ use File::Copy;
3940use Getopt::Long;
4041use Sys::Hostname;
4142use Data::Dumper;
@@ -44,6 +45,7 @@ use strict;
4445Getopt::Long::Configure(" pass_through" );
4546
4647my @args ; # Argument list filled in
48+ my $basedir ;
4749
4850# #############################################################################
4951#
@@ -165,20 +167,11 @@ sub parse_arguments
165167
166168 usage() if $opt -> {help };
167169
168- # To make these options acceptable for mysqld, we must prepend the name to the value -
169- # but only if they are set, and have not been fixed already.
170- if ( $opt -> {' defaults-file' } && $opt -> {' defaults-file' } !~ / ^--/ )
170+ if ( $opt -> {' no-defaults' } && ( $opt -> {' defaults-extra-file' } ||
171+ $opt -> {' defaults-file' } ) )
171172 {
172- $opt -> {' defaults-file' } = ' --defaults-file=' . $opt -> {' defaults-file' };
173- }
174- if ( $opt -> {' defaults-extra-file' } && $opt -> {' defaults-extra-file' } !~ / ^--/ )
175- {
176- $opt -> {' defaults-extra-file' } = ' --defaults-extra-file=' . $opt -> {' defaults-extra-file' };
177- }
178- # This option has no value, so we just have to spell it in full.
179- if ( $opt -> {' no-defaults' } && $opt -> {' no-defaults' } !~ / ^--/ )
180- {
181- $opt -> {' no-defaults' } = ' --no-defaults=' ;
173+ error($opt ,
174+ " Cannot use both --no-defaults and --defaults-[extra-]file" );
182175 }
183176
184177 @args = @ARGV if $pick_args ;
@@ -206,7 +199,7 @@ sub find_in_basedir
206199 foreach my $part ( " $file " ," $file .exe" ," release/$file .exe" ,
207200 " debug/$file .exe" ," relwithdebinfo/$file .exe" )
208201 {
209- my $path = " $opt ->{ basedir} /$dir /$part " ;
202+ my $path = " $basedir /$dir /$part " ;
210203 if ( -f $path )
211204 {
212205 return $mode eq " dir" ? dirname($path ) : $path ;
@@ -388,6 +381,17 @@ sub generate_random_password {
388381my $opt = {};
389382parse_arguments($opt , ' PICK-ARGS-FROM-ARGV' , @ARGV );
390383
384+ # ----------------------------------------------------------------------
385+ # Actual basedir, not to be confused with --basedir option
386+ # ----------------------------------------------------------------------
387+
388+ if ( $opt -> {srcdir } ) {
389+ $basedir = $opt -> {builddir };
390+ } else {
391+ $basedir = $opt -> {basedir };
392+ }
393+ $basedir = " @prefix @" if ! $basedir ; # Default
394+
391395# ----------------------------------------------------------------------
392396# We can now find my_print_defaults. This script supports:
393397#
@@ -408,25 +412,64 @@ if ( $opt->{srcdir} )
408412 $opt -> {builddir } = $opt -> {srcdir } unless $opt -> {builddir };
409413 $print_defaults = " $opt ->{builddir}/extra/my_print_defaults" ;
410414}
411- elsif ( $opt -> { basedir } )
415+ else
412416{
413417 $print_defaults = find_in_basedir($opt ," file" ," my_print_defaults" ," bin" ," extra" );
414418}
415- else
419+ if ( ! $print_defaults )
416420{
417421 $print_defaults =' @bindir@/my_print_defaults' ;
418422}
419423
420424-x $print_defaults or -f " $print_defaults .exe"
421425 or cannot_find_file($print_defaults );
422426
427+ my $config_file ;
428+ my $copy_cfg_file ;
429+
430+ # ----------------------------------------------------------------------
431+ # This will be the default config file
432+ # ----------------------------------------------------------------------
433+
434+ $config_file = " $basedir /my.cnf" ;
435+
436+ my $cfg_template = find_in_basedir($opt ," file" ," my-default.cnf" ,
437+ " share" ," share/mysql" ," support-files" );
438+ -e $cfg_template or cannot_find_file(" my-default.cnf" );
439+
440+ $copy_cfg_file = $config_file ;
441+ if (-e $copy_cfg_file )
442+ {
443+ $copy_cfg_file =~ s / my.cnf/ my-new.cnf/ ;
444+ # Too early to print warning here, the user may not notice
445+ }
446+ open (TEMPL, $cfg_template ) or error(" Could not open config template" );
447+ open (CFG, " > $copy_cfg_file " ) or error(" Could not open config file" );
448+ while (<TEMPL>)
449+ {
450+ # Remove lines beginning with # *** which are template comments
451+ print CFG $_ unless / ^# \*\*\* / ;
452+ }
453+ close CFG;
454+ close TEMPL;
455+
423456# ----------------------------------------------------------------------
424457# Now we can get arguments from the groups [mysqld] and [mysql_install_db]
425458# in the my.cfg file, then re-run to merge with command line arguments.
426459# ----------------------------------------------------------------------
427460
461+ my $print_def_file ;
462+ if ( $opt -> {' defaults-file' } )
463+ {
464+ $print_def_file = $opt -> {' defaults-file' };
465+ }
466+ else
467+ {
468+ $print_def_file = $config_file ;
469+ }
470+
428471my @default_options ;
429- my $cmd = quote_options($print_defaults ,$opt -> { ' defaults-file' } ,
472+ my $cmd = quote_options($print_defaults ," -- defaults-file= $print_def_file " ,
430473 " mysqld" ," mysql_install_db" );
431474open (PIPE, " $cmd |" ) or error($opt ," can't run $cmd : $! " );
432475while ( <PIPE> )
@@ -475,10 +518,9 @@ my ($bindir,$extra_bindir,$mysqld,$pkgdatadir,$mysqld_opt,$scriptdir);
475518
476519if ( $opt -> {srcdir } )
477520{
478- $opt -> {basedir } = $opt -> {builddir };
479- $bindir = " $opt ->{basedir}/client" ;
480- $extra_bindir = " $opt ->{basedir}/extra" ;
481- $mysqld = " $opt ->{basedir}/sql/mysqld" ;
521+ $bindir = " $basedir /client" ;
522+ $extra_bindir = " $basedir /extra" ;
523+ $mysqld = " $basedir /sql/mysqld" ;
482524 $mysqld_opt = " --language=$opt ->{srcdir}/sql/share/english" ;
483525 $pkgdatadir = " $opt ->{srcdir}/scripts" ;
484526 $scriptdir = " $opt ->{srcdir}/scripts" ;
@@ -497,7 +539,6 @@ elsif ( $opt->{basedir} )
497539}
498540else
499541{
500- $opt -> {basedir } = ' @prefix@' ;
501542 $bindir = ' @bindir@' ;
502543 $extra_bindir = $bindir ;
503544 $mysqld = ' @libexecdir@/mysqld' ;
@@ -604,12 +645,26 @@ push(@args, "--user=$opt->{user}") if $opt->{user};
604645
605646# FIXME use --init-file instead of --bootstrap ?!
606647
648+ my $defaults_option = " " ;
649+ if ( $opt -> {' no-defaults' } )
650+ {
651+ $defaults_option = " --no-defaults" ;
652+ }
653+ elsif ( $opt -> {' defaults-file' } )
654+ {
655+ $defaults_option = " --defaults-file=$opt ->{'defaults-file'}" ;
656+ }
657+
658+ my $defaults_extra = " --defaults-extra-file=$opt ->{'defaults-extra-file'}"
659+ if $opt -> {' defaults-extra-file' };
660+
607661my $mysqld_bootstrap = $ENV {MYSQLD_BOOTSTRAP } || $mysqld ;
608662my $mysqld_install_cmd_line = quote_options($mysqld_bootstrap ,
609- $opt -> {' defaults-file' },
663+ $defaults_option ,
664+ $defaults_extra ,
610665 $mysqld_opt ,
611666 " --bootstrap" ,
612- " --basedir=$opt ->{ basedir} " ,
667+ " --basedir=$basedir " ,
613668 " --datadir=$opt ->{ldata}" ,
614669 " --log-warnings=0" ,
615670 " --loose-skip-ndbcluster" ,
@@ -660,8 +715,8 @@ if ( open(PIPE, "| $mysqld_install_cmd_line") )
660715 {
661716 # using the implicit variable $_ !
662717 s / ABC123xyz/ $escaped_password / e ; # Replace placeholder by random password
663- print PIPE $_ ;
664- }
718+ print PIPE $_ ;
719+ }
665720 close SQL3;
666721 tell_root_password();
667722 }
@@ -728,22 +783,22 @@ if ( open(PIPE, "| $mysqld_install_cmd_line") )
728783 " " ,
729784 " See the manual for more instructions." );
730785 } else {
731- report($opt ,
732- " PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !" ,
733- " To do so, start the server, then issue the following commands:" ,
734- " " ,
735- " $bindir /mysqladmin -u root password 'new-password'" ,
736- " $bindir /mysqladmin -u root -h $hostname password 'new-password'" ,
737- " " ,
738- " Alternatively you can run:" ,
739- " " ,
740- " $bindir /mysql_secure_installation" ,
741- " " ,
742- " which will also give you the option of removing the test" ,
743- " databases and anonymous user created by default. This is" ,
744- " strongly recommended for production servers." ,
745- " " ,
746- " See the manual for more instructions." );
786+ report($opt ,
787+ " PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !" ,
788+ " To do so, start the server, then issue the following commands:" ,
789+ " " ,
790+ " $bindir /mysqladmin -u root password 'new-password'" ,
791+ " $bindir /mysqladmin -u root -h $hostname password 'new-password'" ,
792+ " " ,
793+ " Alternatively you can run:" ,
794+ " " ,
795+ " $bindir /mysql_secure_installation" ,
796+ " " ,
797+ " which will also give you the option of removing the test" ,
798+ " databases and anonymous user created by default. This is" ,
799+ " strongly recommended for production servers." ,
800+ " " ,
801+ " See the manual for more instructions." );
747802 }
748803
749804 if ( !$opt -> {rpm } )
@@ -765,6 +820,28 @@ if ( open(PIPE, "| $mysqld_install_cmd_line") )
765820 " http://www.mysql.com" ,
766821 " " ,
767822 " Support MySQL by buying support/licenses at http://shop.mysql.com" );
823+
824+ if ($copy_cfg_file eq $config_file )
825+ {
826+ report($opt ,
827+ " New default config file was created as $config_file and" ,
828+ " will be used by default by the server when you start it." ,
829+ " You may edit this file to change server settings" );
830+ }
831+ else
832+ {
833+ warning($opt ,
834+ " Found existing config file $config_file on the system." ,
835+ " Because this file might be in use, it was not replaced," ,
836+ " but was used in bootstrap (unless you used --defaults-file)" ,
837+ " and when you later start the server." ,
838+ " The new default config file was created as $copy_cfg_file ," ,
839+ " please compare it with your file and take the changes you need." );
840+ }
841+ foreach my $cfg ( " /etc/my.cnf" , " /etc/mysql/my.cnf" )
842+ {
843+ check_sys_cfg_file ($opt , $cfg );
844+ }
768845 }
769846 exit 0
770847}
@@ -801,6 +878,21 @@ else
801878#
802879# #############################################################################
803880
881+ sub check_sys_cfg_file
882+ {
883+ my $opt = shift ;
884+ my $fname = shift ;
885+
886+ if ( -e $fname )
887+ {
888+ warning($opt ,
889+ " Default config file $fname exists on the system" ,
890+ " This file will be read by default by the MySQL server" ,
891+ " If you do not want to use this, either remove it, or use the" ,
892+ " --defaults-file argument to mysqld_safe when starting the server" );
893+ }
894+ }
895+
804896sub report_verbose
805897{
806898 my $opt = shift ;
0 commit comments