我们可以编写一个脚本来在每次网站关闭时重启MySQL吗?

时间:2016-06-06 13:27:53

标签: mysql shell amazon-web-services xampp lampp

我正在使用AWS上托管的Linux服务器,由于可用内存较少,网站出现故障,我得到一个"错误建立与数据库的连接"错误。

每次发生这种情况我都要重启MySQL。

我们可以自动化这个过程吗?

我用来重启LAMPP服务器的命令是:

sudo /opt/lampp/lampp restart

2 个答案:

答案 0 :(得分:1)

Use a bash script to monitor the mysql port and restart it if it is down

Just to demo how it works

The nc command checks that port 3306 (mysql port) is contactable If it is then $? is set to zero, so this counts as "up" otherwise $? is down. You may wish to use a different command instead of "nc", for example $('a[name=CondLink]').click(function(){ $('div[id=holder]').html('<ul style="list-style:none; margin:0; padding:0;"><li><a name="CondLink" id="onlink">Link1</a></li><li><a name="EffLink">Link2</a></li></ul>'); }); $('a[name=EffLink]').click(function(){ $('div[id=holder]').html('<ul style="list-style:none; margin:0; padding:0;"><li><a name="CondLink">Link1</a></li><li><a name="EffLink" id="onlink">Link2</a></li></ul>'); }); to attempt to run a do nothing mysql command

echo "select 1;" | mysql

The actual version you might use could look like this

while [ 1 ]
  do sleep 1
  nc -z localhost 3306
  if [ $? == 0 ]; then echo "up"
  else echo "down"
  fi
done

You can leave a window open (maybe using the "screens" command too) and run this 24x7

If you can increase the time interval to 1 minute then it would be ok to run out of cron like this

in crontab ( to run every minute)

while [ 1 ]
  do sleep 10
  nc -vz localhost 3306
  if [ $? != 0 ]; then /opt/lampp/lampp restart
  fi
done

script

* * * * * /path/to/script

答案 1 :(得分:0)

AWS EC2提供IOP突发,因此我使用大型SWAP / Cache配置Linux服务器。据我所知,这与InnoDB概念相反,MySQL希望将其全部存储在内存中。此外,假设您使用的是LAMP,则应扩展PHP的内存使用量。

http://charmingwebdesign.com/how-to-add-swap-partition-on-ec2-linux-instance/

http://charmingwebdesign.com/wordpress-support-plugin-woocommerce-excelling-ecommerce-what-is-ideal-php-memory-for-woocommerce/