How do you restart MySQL on UNIX.
May 9, 2000, 9:06 PM
Veteran / Moderator (6956 posts)
May 9, 2000, 9:06 PM
Post #2 of 6
Views: 2882
On solaris (I'm not sure about other Unices) I think I started it at a run level that the system monitors and restarts automatically if it dies, just like sendmail and some other processes. You'd need to consult your user manual for your Unix version about 'run levels' and start-up commands.
Looking at the script, I'm assuming you'd be using this as a 'start up' script? Then having the system run this script like cron, making sure this script doesn't die?? <G>??
Gets sort of complicated.
Looking at the script, I'm assuming you'd be using this as a 'start up' script? Then having the system run this script like cron, making sure this script doesn't die?? <G>??
Gets sort of complicated.
May 9, 2000, 11:07 PM
Veteran / Moderator (6956 posts)
May 9, 2000, 11:07 PM
Post #3 of 6
Views: 2879
May 10, 2000, 6:35 AM
Administrator (9387 posts)
May 10, 2000, 6:35 AM
Post #5 of 6
Views: 2884
It's worthwhile having a monitor script to restart mysql if it ever dies. We use something like:
my @dbi = ('DBI:mysql:test', '', '');
my $admin = 'you@yourserver.com';
my $mail = '/usr/sbin/sendmail -t';
my $pid_file = '/usr/local/mysql/data/mysql.pid';
print "Checking mysql ... \n" if $debug;
my $dbh = DBI->connect(@dbi);
if (! defined $dbh) {
print "failed.\n" if $debug;
print "trying to restart ... \n" if $debug;
&resurrect_mysql ($DBI::errstr, $my_pid, $my_start, $mail, $admin);
print "ok.\n\n" if $debug;
}
else {
print "ok.\n\n" if $debug;
$dbh->disconnect;
}
sub resurrect_mysql {
# -----------------------------------------------------------
my ($message, $pid_file, $my_start, $mail, $admin) = @_;
# Open Mail and redirect stderr to mail.
open (MAIL, "| $mail") | | die "mail: $!";
# Change to mysql.
chdir ("/usr/local/mysql") or die "chdir: $!";
my $date = localtime();
print MAIL <<END;
To: $admin
From: The Watchful Server Monitor <nobody>
Subject: Mysql server is down!!
I tried to connect to mysql on $date
but there was no answer! Error Message was:
$message
I am going to try to resurrect it now:
END
;
my $pid = `cat $pid_file 2>/dev/null`;
my $output = '';
if ($pid) {
print MAIL "Found pid $pid .. killing... ";
print MAIL `kill $pid`;
print MAIL "Done.\n\n";
}
eval {
local $SIG{ALRM} = sub { die "alarm\n" };
alarm(3);
$output = `$my_start 2>&1`;
alarm 0;
};
$@ ?
print MAIL "\nStartup Timed out! Output: $output\n\n" :
print MAIL "\nOutput: $output\n\n";
print MAIL <<END;
That's the best I could do. Hope it helped.
The Web Monitor
END
close MAIL;
}
Note: This assumes mysql is in /usr/local/mysql, you might need to alter if you've installed via RPM's.
Cheers,
Alex
Code:
my $debug = 0; my @dbi = ('DBI:mysql:test', '', '');
my $admin = 'you@yourserver.com';
my $mail = '/usr/sbin/sendmail -t';
my $pid_file = '/usr/local/mysql/data/mysql.pid';
print "Checking mysql ... \n" if $debug;
my $dbh = DBI->connect(@dbi);
if (! defined $dbh) {
print "failed.\n" if $debug;
print "trying to restart ... \n" if $debug;
&resurrect_mysql ($DBI::errstr, $my_pid, $my_start, $mail, $admin);
print "ok.\n\n" if $debug;
}
else {
print "ok.\n\n" if $debug;
$dbh->disconnect;
}
sub resurrect_mysql {
# -----------------------------------------------------------
my ($message, $pid_file, $my_start, $mail, $admin) = @_;
# Open Mail and redirect stderr to mail.
open (MAIL, "| $mail") | | die "mail: $!";
# Change to mysql.
chdir ("/usr/local/mysql") or die "chdir: $!";
my $date = localtime();
print MAIL <<END;
To: $admin
From: The Watchful Server Monitor <nobody>
Subject: Mysql server is down!!
I tried to connect to mysql on $date
but there was no answer! Error Message was:
$message
I am going to try to resurrect it now:
END
;
my $pid = `cat $pid_file 2>/dev/null`;
my $output = '';
if ($pid) {
print MAIL "Found pid $pid .. killing... ";
print MAIL `kill $pid`;
print MAIL "Done.\n\n";
}
eval {
local $SIG{ALRM} = sub { die "alarm\n" };
alarm(3);
$output = `$my_start 2>&1`;
alarm 0;
};
$@ ?
print MAIL "\nStartup Timed out! Output: $output\n\n" :
print MAIL "\nOutput: $output\n\n";
print MAIL <<END;
That's the best I could do. Hope it helped.
The Web Monitor
END
close MAIL;
}
Note: This assumes mysql is in /usr/local/mysql, you might need to alter if you've installed via RPM's.
Cheers,
Alex
May 10, 2000, 3:22 PM
Administrator (9387 posts)
May 10, 2000, 3:22 PM
Post #6 of 6
Views: 2892
Yes, this is run every two minutes in cron(and is actually just a snippet of the full one -- it also checks the mod_perl server, webserver, qmail, etc).
The advantadge of this, is that it mimics an actual request, where most other just monitor that the process is still running. That way if mysql hangs, or is unresponsive, the monitor script will restart it.
The only downside right now is if it dies in the night and can't restart it (only happened once) I end up with 400-500 emails in the morning.
Cheers,
Alex
The advantadge of this, is that it mimics an actual request, where most other just monitor that the process is still running. That way if mysql hangs, or is unresponsive, the monitor script will restart it.
The only downside right now is if it dies in the night and can't restart it (only happened once) I end up with 400-500 emails in the morning.
Cheers,
Alex