1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to Restart Mysql From PHP Script

Discussion in 'PHP' started by deriklogov, Mar 10, 2009.

  1. #1
    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.
     
    deriklogov, Mar 10, 2009 IP
  2. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #2
    You can use exec to run external programs or command line stuff in php, but it must be enabled to use.
     
    exodus, Mar 10, 2009 IP
  3. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #3
    
    // STOP + START = RESTART... he he
    
    system('net stop "MySQL"'); /* STOP */
    system('net start "MySQL"'); /* START */
    
    PHP:
     
    nabil_kadimi, Mar 10, 2009 IP
  4. deriklogov

    deriklogov Well-Known Member

    Messages:
    1,078
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    130
    #4
    I dont know why but net doesnt work on this system.


     
    deriklogov, Mar 11, 2009 IP
  5. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #5
    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.
     
    Kaizoku, Mar 11, 2009 IP
  6. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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.
     
    SmallPotatoes, Mar 12, 2009 IP
  7. mighty_falcon

    mighty_falcon Member

    Messages:
    67
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #7
    You cannot do this unless you run PHP as root (highly not recommended!!)
     
    mighty_falcon, Mar 12, 2009 IP
  8. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #8
    See above. You can easily do it using sudo, no need for root. Could also write a tiny setuid wrapper in C.
     
    SmallPotatoes, Mar 12, 2009 IP
  9. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #9
    Sudo is even worse, have to supply password in the command itself, and sudo is just a wrapper for root access.
     
    Kaizoku, Mar 13, 2009 IP
  10. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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.
     
    SmallPotatoes, Mar 13, 2009 IP