Skip to content

Instantly share code, notes, and snippets.

@lastk
Forked from jasondavis/reboot_mysql_database.php
Created December 12, 2020 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lastk/4a48a60da42f0a575cd720020cb1cac6 to your computer and use it in GitHub Desktop.
Save lastk/4a48a60da42f0a575cd720020cb1cac6 to your computer and use it in GitHub Desktop.
Start / Stop / Reboot MySQL Database server using SSH with PHP
<?php
// Start / Stop / Reboot MySQL Database server using SSH with PHP
// Requires the library from phpseclib.sourceforge.net / https://github.com/phpseclib/phpseclib
// Server user login data
$ssh_user = '';
$ssh_password = '';
$ssh_server_ip = '';
?>
<!DOCTYPE html>
<html>
<body>
<title>Re-boot MySQL Database Server</title>
<?php
function didMySqlDbRestart($sshCommandResponseString){
if(preg_match('/process \d+$/', trim($sshCommandResponseString)) > 0){
return true;
}else if($sshCommandResponseString == 'start: Job is already running: mysql'){
return true;
}else{
return false;
}
}
// phpseclib.sourceforge.net / https://github.com/phpseclib/phpseclib
include('Net/SSH2.php');
if(isset($_GET['restart-mysql']) && $_GET['restart-mysql'] === 'yes'){
$ssh = new Net_SSH2($ssh_server_ip);
if (!$ssh->login($ssh_user, $ssh_password)) {
exit('Login Failed');
}
ob_start();
echo $ssh->exec('service mysql restart');
$sshCommandResponseString = ob_get_contents();
ob_end_clean();
//echo $sshCommandResponseString;
if(didMySqlDbRestart($sshCommandResponseString)){
echo 'SUCCESS: MySQL Database Rebooted';
} else{
echo 'ERROR: MySQL Database Did Not Reboot';
}
}else if(isset($_GET['stop-mysql']) && $_GET['stop-mysql'] === 'yes'){
$ssh = new Net_SSH2($ssh_server_ip);
if (!$ssh->login($ssh_user, $ssh_password)) {
exit('Login Failed');
}
ob_start();
echo $ssh->exec('service mysql stop');
$sshCommandResponseString = ob_get_contents();
ob_end_clean();
//echo $sshCommandResponseString;
if(didMySqlDbRestart($sshCommandResponseString)){
echo 'SUCCESS: MySQL Database Stopped';
} else{
echo 'ERROR: MySQL Database Did Not Stop';
}
}else if(isset($_GET['start-mysql']) && $_GET['start-mysql'] === 'yes'){
$ssh = new Net_SSH2($ssh_server_ip);
if (!$ssh->login($ssh_user, $ssh_password)) {
exit('Login Failed');
}
ob_start();
echo $ssh->exec('service mysql start');
$sshCommandResponseString = ob_get_contents();
ob_end_clean();
if(didMySqlDbRestart($sshCommandResponseString)){
echo 'SUCCESS: MySQL Database Started';
} else{
echo 'ERROR: MySQL Database Did Not Start';
}
}else{
?>
<h1>Reboot MySQL Database Server</h1>
<p>If the MySQL Database crashes then you can run this script which will login to the server via SSH and execute the restart commands to start the database server.</p>
<p>If this does not resolve the issue then please contact Jason Davis - jason.davis.fl@gmail.com</p>
<form action="" method="get">
<input type="hidden" name="restart-mysql" value="yes">
<input type="submit" value="Restart MySQL Database">
</form><br>
<form action="" method="get">
<input type="hidden" name="stop-mysql" value="yes">
<input type="submit" value="Stop MySQL Database">
</form><br>
<form action="" method="get">
<input type="hidden" name="start-mysql" value="yes">
<input type="submit" value="Start MySQL Database">
</form>
<?php
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment