Skip to content

Commit c8777cb

Browse files
authored
Updates: changes for database initilization. (#408)
* Updates: changes for database initialization.
1 parent 70f8125 commit c8777cb

File tree

2 files changed

+38
-34
lines changed

2 files changed

+38
-34
lines changed

randgen/lib/DBServer/MySQL/MySQLd.pm

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use strict;
3131
use Carp;
3232
use Data::Dumper;
3333
use File::Path qw(mkpath rmtree);
34+
use File::Spec::Functions qw(catfile);
3435

3536
use constant MYSQLD_BASEDIR => 0;
3637
use constant MYSQLD_VARDIR => 1;
@@ -194,8 +195,8 @@ sub new {
194195
"--datadir=".$self->datadir,
195196
$self->_messages,
196197
"--character-sets-dir=".$self->[MYSQLD_CHARSETS],
197-
"--default-storage-engine=myisam",
198-
"--log-warnings=0",
198+
"--default-storage-engine=innodb",
199+
"--log_error_verbosity=1",
199200
"--tmpdir=".$self->tmpdir];
200201

201202
if ($self->[MYSQLD_START_DIRTY]) {
@@ -310,7 +311,6 @@ sub addServerOptions {
310311

311312
sub createMysqlBase {
312313
my ($self) = @_;
313-
314314
## 1. Clean old db if any
315315
if (-d $self->vardir) {
316316
rmtree($self->vardir);
@@ -320,38 +320,31 @@ sub createMysqlBase {
320320
mkpath($self->vardir);
321321
mkpath($self->tmpdir);
322322
mkpath($self->datadir);
323-
mkpath($self->datadir."/mysql");
324-
mkpath($self->datadir."/test");
325-
326-
## 3. Create boot file
327-
my $boot = $self->vardir."/boot.sql";
328-
open BOOT,">$boot";
329323

330-
## Set curren database
331-
print BOOT "use mysql;\n";
332-
foreach my $b (@{$self->[MYSQLD_BOOT_SQL]}) {
333-
open B,$b;
334-
while (<B>) { print BOOT $_;}
335-
close B;
336-
}
337-
## Don't want empty users
338-
print BOOT "DELETE FROM user WHERE `User` = '';\n";
339-
close BOOT;
324+
my $init_options = [
325+
"--no-defaults",
326+
"--initialize-insecure", # Creates root user without password
327+
"--datadir=" . $self->datadir,
328+
"--basedir=" . $self->basedir
329+
];
340330

341-
## 4. Boot database
331+
## 4. Initialize database
332+
my $exit_code;
333+
my $initlog;
342334
if (osWindows()) {
343-
my $command = $self->generateCommand(["--no-defaults","--bootstrap"],
344-
$self->[MYSQLD_STDOPTS]);
345-
346-
my $bootlog = $self->vardir."/boot.log";
347-
system("$command < \"$boot\" > \"$bootlog\"");
335+
# Todo Untested code (WINDOWS).
336+
my $command = $self->generateCommand($init_options, $self->[MYSQLD_STDOPTS]);
337+
338+
$initlog = catfile($self->vardir, "init.log");
339+
$exit_code = system(qq{$command > "$initlog" 2>&1});
348340
} else {
349-
my $boot_options = ["--no-defaults","--bootstrap"];
350-
push(@$boot_options,"--loose-skip-innodb") if $self->_olderThan(5,6,3);
351-
my $command = $self->generateCommand($boot_options,
352-
$self->[MYSQLD_STDOPTS]);
353-
my $bootlog = $self->vardir."/boot.log";
354-
system("cat \"$boot\" | $command > \"$bootlog\" 2>&1 ");
341+
my $command = $self->generateCommand($init_options, $self->[MYSQLD_STDOPTS]);
342+
$initlog = catfile($self->vardir, "init.log");
343+
$exit_code = system(qq{$command > "$initlog" 2>&1});
344+
}
345+
346+
if ($exit_code != 0) {
347+
croak("MySQL initialization failed. Check log: $initlog");
355348
}
356349
}
357350

@@ -430,6 +423,7 @@ sub startServer {
430423
$self->printInfo;
431424
say("Starting MySQL ".$self->version.": $command");
432425
$self->[MYSQLD_AUXPID] = fork();
426+
croak("Could not fork: $!") unless defined $self->[MYSQLD_AUXPID];
433427
if ($self->[MYSQLD_AUXPID]) {
434428
## Wait for the pid file to have been created
435429
my $wait_time = 0.2;
@@ -443,8 +437,14 @@ sub startServer {
443437
croak("Could not start mysql server, waited ".($waits*$wait_time)." seconds for pid file");
444438
}
445439
my $pidfile = $self->pidfile;
446-
my $pid = `cat \"$pidfile\"`;
447-
$pid =~ m/([0-9]+)/;
440+
open(my $fh, '<', $pidfile) or croak("Cannot open pidfile '$pidfile': $!");
441+
my $pid = <$fh>;
442+
close($fh);
443+
444+
chomp($pid) if defined $pid;
445+
if (!defined $pid || $pid !~ /^(\d+)$/) {
446+
croak("Invalid or empty PID in pidfile '$pidfile'");
447+
}
448448
$self->[MYSQLD_SERVERPID] = int($1);
449449
} else {
450450
exec("$command > \"$errorlog\" 2>&1") || croak("Could not start mysql server");

randgen/runall-new.pl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,18 +411,22 @@
411411
}
412412
croak("Could not start all servers");
413413
}
414-
414+
415415
if (
416416
($server_id == 0) ||
417417
($rpl_mode eq '')
418418
) {
419+
# Create default test database.
420+
$server[$server_id]->dbh()->do("CREATE DATABASE IF NOT EXISTS $database");
421+
419422
$dsns[$server_id] = $server[$server_id]->dsn($database);
420423
}
421424

422425
if ((defined $dsns[$server_id]) && (defined $engine)) {
423426
my $dbh = DBI->connect($dsns[$server_id], undef, undef, { mysql_multi_statements => 1, RaiseError => 1 } );
424427
$dbh->do("SET GLOBAL storage_engine = '$engine'");
425428
}
429+
426430
}
427431
}
428432

0 commit comments

Comments
 (0)