Why MySQL Auto Restart Failing in WordPress Cloud Server Despite Your Efforts of Trying Various Optimization and Scripts to Auto-Restart? We already talked around fixing Error Establishing a Database Connection in WordPress, Autostart MySQL After a Crash, other MySQL fixes for Ubuntu and latest fixes for Ubuntu 18.04. Various updates on the part of different software partially force us to discover new fix.
The discussions in the above links clearly would say – properly configured MySQL should automatically restart. That is theoritically true for the cloud servers running WordPress. Virtual environment, bad coding and attacks make the equation to auto-restart not so easy. These can be causes of failing to restart :
Errors in configuration (in
/etc/my.cnf like file)
Corrupt MySQL binary
Wrong permissions/ownership of binaries and folders
Busy MySQL port to bind
Unable to write to
Lack of disk space
Out of memory
Various type of networking attacks on WordPress
Badly coded plugin
WordPress is targeted because for being based on PHP and MySQL. The clever solution is to increase your cloud server’s memory. We can open the crontab editor in the terminal with
crontab -e command and add the following line to check to auto-restart :
*/5 * * * * service mysql status > /dev/null || service mysql start
This checks if MySQL is running every five-minute interval and redirects stdout to null. But it is commonly seen by many of the users that such scripts decrease the number of failed auto-restart but never makes to zero. With an ongoing massive XML-RPC attack, the server likely to have not much memory to execute auto-restart function. Situation can be worse with a badly coded plugin.
You can use this kind of bash script :
# cronjob need clear where is the path
# mysql under path /usr/local/bin
mysql --host="localhost" --user="root" --password="password" --database="test" --execute="select id from test limit 1"
if [ $? -eq 0 ]; then echo "";
# mysql run well nothing to do, make a
sudo /etc/init.d/mysql stop
# It should be pkill /usr/local/bin/mysql, not pkill mysql, wrong write will cause below code not working
sudo /etc/init.d/mysql start
sudo /etc/init.d/httpd restart
# Some strange behave, after mysql restart, apache will die, so should add httpd restart
echo "error $(date)" >> /home/myspace/restart_log.txt
You’ll get PATH with this command :
env | grep "^PATH="
Alternate option is to use MySQL Ping function via cron :
* * * * * root /usr/bin/mysqladmin --host="localhost" --user="root" --password="password" ping || /usr/sbin/service mysql restart
The above methods or Monit like tools are better to be run from another server to monitor your server running WordPress and automatically take actions like restarting a process or forcing to restart the VPS/cloud server. Within a server with some error it may be difficult to resolve it in automated way.Tagged With wordpress mysql database optimization , ZFE4 , wordpress vps optimization