auto restart mysql services

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

auto restart mysql services

Post by gurjit »

i tried to auto restart the mysql server if it was stuck doing a large query. i used the following code. It emailed me fine but never resterted the services.

Code: Select all

<?php
$dbh = mysql_connect('localhost','username','password');
if (!$dbh){ 
mail('user@mydomain.co.uk','MySQL Restarted Successfully',mysql_error()); 
exec('/etc/rc.d/init.d/mysqld restart'); 
exit;
} 
mysql_close($dbh);
?>
I was hoping to get this working and to email the log file of queries in the que, to identify the query causing the server to o be busy.

After getting this to work I will create a cron to run every 5 mins to check the mysql services.

Any ideas why this did not work.

My server runs
OS: Linux 2.6.10
Version: FedoraCore 2
MySQL 3.26
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

Probably a permissions issue. You familiar with SUDO, and setting it up for different users?
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

I always use the "root" login details (which is the administrator).

"root" is the administrator, who has all permissions

However i'm not familiar with SUDO


I can run the same command in "putty" using command line and it works fine.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Running PHP as root is a bad idea.

I assume you mean you log into MySQL as root, which isn't the same thing (the OS restarts MySQL, MySQL doesn't do it itself).
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

I assume you mean you log into MySQL as root, which isn't the same thing (the OS restarts MySQL, MySQL doesn't do it itself).
Thats what I mean (In this instance I logged in as root, just too see what happens and nothing happened).


How in PHP can I run the '/etc/rc.d/init.d/mysqld restart' command. Just like I can run it using "command line"
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

If you're trying to run that command inside a web page, then the user is going to be whatever is defined in httpd.conf. Apache starts as root, but the child processes start as the user defined in httpd.conf

To be honest, if you're running into a problem with a query taking too on a web page, modify your php.ini file to restrict the query execution time.

Look for the Resource Limits section of the php.ini file
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

makes sense what your saying.

Is there any other way I can run the same command as a webpage outside the httpdocs folders on apache servers?

I was thinking in the cron commands them selves?

The query that was taking too long has been found and ammended. In the future if this was too happen I would rather the server restart itself and send me a log file and email to notify me of the problem. I'm just trying to prevent the same thing happening in the future.

PHP execution time is set too 7 mins. The reason is that I am in the process of uploading large csv files to upload some data. I need the extra time for processing. This is for something I am doing internal. So no choice in cutting that limit down.
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

You can run a php script from the command line, like so:

php myPHPpage.php

That will run with the current user permissions.
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

by running a php script in the command line I will need to put the php script in a httpdocs folder (web directory). The httpd.conf files for the web directory will need to be changed (How can I avoid this to give admin permissions to restart the services) - this is where i'm getting confused.

Is there away around, rather than changing the httpd.conf files
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

You don't have to have the php script in the httpdocs folder in order to run it.

Try this: php -v

If you get a response back, you're all set. If not, then you either need to set the path, or use /path/to/php/executable
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

i got a response back.

Does this mean I can put the code anywhere on the server and in the cron line just call the php file like

*/5 * * * * /usr/bin/php /path/to/script/check_restart_db.php

and it should work?

Code: Select all

<?php 
// file check_restart_db.php
$dbh = mysql_connect('localhost','username','password'); 
if (!$dbh){ 
mail('user@mydomain.co.uk','MySQL Restarted Successfully',mysql_error()); 
exec('/etc/rc.d/init.d/mysqld restart'); 
exit; 
} 
mysql_close($dbh); 
?>
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

Yes, that should do it, assuming the user that the cron job runs under can restart the server.
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

i'll give it a go first thing monday morning.

Thanks blackbeard
Post Reply