Skip to content

Instantly share code, notes, and snippets.

@jasondavis
Last active December 12, 2020 01:34
Show Gist options
  • Save jasondavis/dbecdbd93707ea8cd6ba5610fb697c84 to your computer and use it in GitHub Desktop.
Save jasondavis/dbecdbd93707ea8cd6ba5610fb697c84 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 - [email protected]</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