Hey , From time to time I am having problem with "Too many connections on mysql" and need to restart mysql server manually, but it happens that I am not near computer to do that , so I decide to make small script which would watch website for error and in case could restart mysql server. Tried couple things to do that , but doesnt really works. Examples: <? system("/etc/init.d/mysql restart"); ?> Some times it works, but most of the time - dont.
You can use exec to run external programs or command line stuff in php, but it must be enabled to use.
// STOP + START = RESTART... he he system('net stop "MySQL"'); /* STOP */ system('net start "MySQL"'); /* START */ PHP:
You cannot do that if accessing php through web, as it doesn't have root access to do so. What you need is a ssh server, then login ssh and issue the command.
nabil_kadimi's proposal is for Windows, so if you are on linux/unix it will not get you very far. On linux/unix you can stop MySQL without being root (mysqladmin shutdown) but to start it you have to be root since it does a setuid to the MySQL user. If you have root access to the server, you could add the Apache user to the sudoers file with a line that allows a no-password invocation of mysqld_safe. Then you will be able to stop and restart it. Honestly, though, I think you're better off figuring out why you have all these open connections. I have high-traffic MySQL instances that have been up literally for years. Also, explore using mysqladmin to kill off individual unwanted processes rather than blasting away the whole thing.
See above. You can easily do it using sudo, no need for root. Could also write a tiny setuid wrapper in C.
Sudo is even worse, have to supply password in the command itself, and sudo is just a wrapper for root access.
Maybe you are not very familiar with sudo. I recommend reading the documentation to refresh your understanding. 1) You can specify that a particular user can run a particular application without providing a password, by using the "NOPASSWD:" parameter. No need to put a password anywhere. 2) Sudo allows you to restrict the use to a specific command (e.g., mysqld_safe), so saying "it's just a wrapper for root access" is misleading at best. It's a way to avoid root access by providing a setuid facility constrained to a specific task rather than just granting blanket superuser access. It is in fact the perfect tool for what this user needs, as long as this user has the ability to edit the sudoers file.