Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a rackspace server that I have been renting to run my personal projects upon. Since I am cheap, it has 256Mb of RAM and honestly can't handle alot. Every once in a while, when there is a sharp uptick in traffic, the server decides to start killing processes and it seems that mysqld is a popular one for it to kill. I try to visit my site and am greeted with the message that there was an error establishing the database connection. Inspection of the logs reveals that mysqld was killed due to lack of memory.

Since I am still as poor as I was yesterday and don't want to upgrade my rackspace VM's RAM, is there a way I can tell it to automagically restart mysqld when it dies?

I have a thought to use something like crontab, but alas, I don't know exactly what to do there either. I guess I am product of the "Linux on your desktop" generation since I can do most things on my desktop and laptop (which run Linux almost exclusively), but still lack a lot of server administration skills for Linux.

The server runs CentOS 6.3

share|improve this question
    
what distro? I think upstart would do this on ubuntu –  Journeyman Geek Jun 23 '13 at 23:59
    
Whoops. Forgot to mention that detail: CentOS –  Los Frijoles Jun 24 '13 at 0:16
    
eh. might want to include the version too –  Journeyman Geek Jun 24 '13 at 2:29
    
Not to detract from the question, but perhaps a long-term solution would be to avoid the condition instead :) If you just want a simple unmanaged VPS for your personal projects, you may be able to pull a good price on a bigger box from a provider that specializes in low end. I have done this with great satisfaction. There are many places to find such VPS providers, e.g. lowendtalk.com. –  John Chadwick Jun 24 '13 at 3:10
add comment

2 Answers

This is not a clean solution, it would obviously be better to avoid the problem in the first place. Anyway, I am not sure how CentOS manages services but I think it uses service. If so, you can check if the mysql service is running with

/sbin/service mysql status

This command will exit successfully if mysql is running and return a non 0 exit status if i is not. You can therefore start the service if it is not running with this command:

/sbin/service mysql status || service mysql start

You can add this line to /etc/crontab to launch thes command every minute:

* * * * * /sbin/service mysql status || service mysql start
share|improve this answer
add comment

This is a little disturbing.

mysqld is always restarted by mysqld_safe because there is an infinite loop in the bottom of mysqld_safe to check for abnormal shutdowns. If the error is too severe, not event mysqld_safe can restart mysqld on subsequent tries.

Given that situation that mysqld_safe is designed for, it may not be a good idea to force mysqld to start if mysqld_safe will reject it anyway.

You need to locate the error log in my.cnf it will be under

[mysqld]
log-error=log-filename

or

[mysqld_safe]
log-error=log-filename

Read the text file (probably by running tail -30 log-filename) and find the source of the mysqld processing shutting down.

share|improve this answer
    
Interesting...looking at my log it says that mysqld was restarted, but then roughly 1-2 hours later it will say that the process ended. After that the next record is my manual start. Perhaps I have mysqld_safe configured wrong? –  Los Frijoles Jun 29 '13 at 5:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.