The return status from mysqladmin is 0 if the server is running, 1 if it is not. Here is what the official documentation has to say about the ping subcommand:Ĭheck whether the server is available. It is not necessary to query an arbitrary table and test the result to determine if the database is alive. This will ping the database every minute, and issue a "restart" if it does not respond. I suggest you put the following into your system crontab (edit the file /etc/crontab): * * * * * root /usr/bin/mysqladmin -host="localhost" -user="root" -password="password" ping || /usr/sbin/service mysql restart MySQL provides a command for that: mysqladmin ping Want you want to do is check if the database server process is still alive. # Some strange behave, after mysql restart, apache will die, so should add httpd restart # It should be pkill /usr/local/bin/mysql, not pkill mysql, wrong write will cause below code not working After a long time test, I shared final working code to all. given the answer is more nearer than other's. answer write better than I, but still not working. method maybe good, but not working in my situation. Is this problem caused by PATH? OR privileges? How to solve it? thanks. It works well in SSH part (return mysql select ok)īut not working in cronjob(every minute write error in /home/myspace/restart_log.txt and mysql server never start) RESULT=`mysql -host="localhost" -user="root" -password="password" -database="test" -execute="select browser from test limit 1"` root : sorry, you must have a tty to run sudo TTY=unknown PWD=/root USER=root COMMAND=/sbin/service mysql start. In /var/log/sucure, it show root : sorry, you must have a tty to run sudo TTY=unknown PWD=/root USER=root COMMAND=/sbin/service mysql stop. not working? I mean the mysql server has no problem, it can execute "select id from test limit 1" and get result, but the script still run the code in else case. Mysql -host="localhost" -user="root" -password="password" -database="test" -execute="select id from test limit 1"Įcho "error $(date)" > /home/myspace/restart_log.txt I put it in /home/myspace/mysql.sh chown root:root /home/myspace/mysql.sh then run every minute by crontab. I write a simple bash shell to check if mysql is crashed, restart service again.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |