'Restart mysqld from php fails

From some days a website I work on starts being very slow every days, I tried to see if there were queries running for long or many connections but I see nothing strange:

MariaDB [(none)]> show status like '%conn%';
+-----------------------------------------------+-------+
| Variable_name                                 | Value |
+-----------------------------------------------+-------+
| Aborted_connects                              | 10    |
| Connection_errors_accept                      | 0     |
| Connection_errors_internal                    | 0     |
| Connection_errors_max_connections             | 0     |
| Connection_errors_peer_address                | 0     |
| Connection_errors_select                      | 0     |
| Connection_errors_tcpwrap                     | 0     |
| Connections                                   | 2538  |
| Max_used_connections                          | 71    |
| Performance_schema_session_connect_attrs_lost | 0     |
| Slave_connections                             | 0     |
| Slaves_connected                              | 0     |
| Ssl_client_connects                           | 0     |
| Ssl_connect_renegotiates                      | 0     |
| Ssl_finished_connects                         | 0     |
| Threads_connected                             | 67    |
| wsrep_connected                               | OFF   |
+-----------------------------------------------+-------+

Just mysqld using 300% of CPU, then I found that doing /etc/init.d/mysql restart make the website fast again until the next time.

Also, I know this is not a solution but I don't know if I will find one and when, also I need a workaround until that. Then, I created a php script that check if the website is slow and tries to call that command itself, but I receive that error:

Restarting mysql (via systemctl): mysql.service failed! 

As apache uses the user www-data I added that to /etc/sudoers:

www-data ALL = NOPASSWD: /etc/init.d/mysql

But still have the same error, isn't that line correct ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source