How to restart sql connections in php

1

I am programming a system in php that connects to sql , but it has become very frequent that I get this error message:

  

Warning: mysqli :: mysqli (): (HY000 / 1040): Too many connections in   /home/u442095798/public_html/conexion.php on line 5 Failed Connection:   Too many connections

When researching on the internet I found that you can see the connections you can write in SQL the following command:

show status like '%onn%';

Shows that 175342417 connections have been used, this number of connections increases even if the website is not open.

I have not been able to find a solution to the connection problem.

Could you tell me if it is possible to reset the values of the connections from php?

    
asked by Stravos77 03.12.2016 в 15:56
source

2 answers

3

The PDO libraries ( assigning null the variable ) and MySQLi ( mysqli::close() ) allow connections to be closed after being used. As a general rule they close automatically after the completion of the script.

If you are using a dedicated server you should find out where connections are made with a SHOW [FULL] PROCESSLIST to discard a possible socket from your server. In the list you will see the IP or host from which the connection is established as well as the user they have used for it.

If you are using shared hosting, it is likely that the problem is not yours, but the provider that gives you service. Maybe it has made a bad sizing of the server, you should claim it to fix it.

Please, do not use the persistent connections of mysql_pconnect() as recommended or, in general, the functions mysql_* . They were marked as obsolete in PHP 5.5 and completely removed in PHP 7.0 , mainly because they are a source of security holes and a source of bad practices.

If you confirm if your server is dedicated or shared, I can continue to help you in more depth.

    
answered by 03.12.2016 / 17:17
source
2

You could use persistent connections to mysql

mysql_pconnect() Here

These allow you to make a permanent connection to the Mysql server

Another thing you could do would be to change the time of the variable interactive_timeout of Mysql. Since it may happen that some process is taking longer than necessary and leave the connection of Mysql active.

Interactive Timeout

    
answered by 03.12.2016 в 16:38