Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"MySQL server has gone away" in mysqli

Tags:

mysql

My web threw the error "MySQL server has gone away". I google it, find a method that use mysql_ping function, but I use extension mysqli, not mysql.

How can I fix this problem?

like image 445
user1514160 Avatar asked Jul 20 '12 09:07

user1514160


People also ask

How do I reconnect to MySQL?

To ensure this, call mysql_options() with the MYSQL_OPT_RECONNECT option: my_bool reconnect = 0; mysql_options(&mysql, MYSQL_OPT_RECONNECT, &reconnect); If the connection has gone down, the effect of mysql_ping() depends on the auto-reconnect state. If auto-reconnect is enabled, mysql_ping() performs a reconnect.

Why is MySQL not connecting to server?

Here are some reasons the Can't connect to local MySQL server error might occur: mysqld is not running on the local host. Check your operating system's process list to ensure the mysqld process is present. You're running a MySQL server on Windows with many TCP/IP connections to it.


2 Answers

There is a mysqli_ping method for this, as well:

http://us3.php.net/manual/en/mysqli.ping.php

However, in my experience, ping was not needed. All I had to do was add mysqli.reconnect = "1" to my php.ini.

Ref. check if mysql connection is valid

like image 117
user1477388 Avatar answered Oct 25 '22 00:10

user1477388


You may get this error when your query is crashing MySQL server check MySQL error log file for that.

If values for wait_timeout & interactive_timeout MySQL server variables is set to very low in MySQL configuration file. Try increasing values for them and then restart MySQL server.

This is basically a time between two queries and after opening new connection if you don't execute next query before this timeout then your connection will be automatically closed by the MySQL server and you will get this error.

like image 39
Omesh Avatar answered Oct 25 '22 00:10

Omesh