We help IT Professionals succeed at work.

Restarting MySQL using PHP Script

tripat
tripat asked
on
Medium Priority
909 Views
Last Modified: 2010-04-21
I am writting a script to restart mysql at a certain check, but I am unable to run the following from PHP:

exec('/sbin/service mysqld restart');

/sbin/service mysqld restart, when run from SSH succussfully restarts mySQL.

Please help me, if I am doing any mistake and correct me.

Do we have to give any permissions to this file, as presently this file is owned by a user not root.
Comment
Watch Question

Commented:
Maybe exec is disabled in php.ini?
Search for "disable_functions" in php.ini and check if exec is disabled

Do you get an error message?

Author

Commented:
exec is enabled I am sure because, in same program I running this:

exec('uptime') and caculating load average from this.

Author

Commented:

I have tried this:

exec('/sbin/service mysqld restart ',$output);
print_r($output);

and I get the following output:

Array
(
    [0] => Shutting down MySQL...................................[FAILED]
    [1] => Starting MySQL[  OK  ]
)

does it mean PHP does not have rights to execute this command?

Commented:
to restart a service it needs root privs.

you would either have to setup what ever is running this php script, like apache to use root instead of wwwuser or whatever it's using.

Author

Commented:
thanks

Explore More ContentExplore courses, solutions, and other research materials related to this topic.