Error Analysis and Solution-MySQL server has gone away

Source: Internet
Author: User
Tags mysql manual php mysql
Applications (such as PHP) execute MySQL statements in batches for a long time. Execute an SQL statement, but the SQL statement is too large or the statement contains blob or longblob fields. For example, processing image data. MySQL server has gone away is easy to cause. In a similar situation today, MySQL is just a cold saying: MySQL server has gone away. You probably browsed it for the following reasons: One possibility is that the SQL statement sent is too long and exceeds the size of max_allowed_packet. For this reason, you only need to modify my. CNF: Increase the value of max_allowed_packet. There is another possibility that timeout may be caused due to some reasons. For example, Singleton is used in the program to obtain the database connection. Although the program connects to the database multiple times, it actually uses the same connection, in addition, if the interval between two database operations in the program exceeds wait_timeout (this setting can be seen in show status), a problem may occur. The simplest way to deal with it is to increase wait_timeout. Of course, you can also use mysql_ping () in the program from time to time, so that MySQL knows that it is not a battle. Solve MySQL server has gone away 1. Execute MySQL statements in batches for a long time in applications (such as PHP. The most common is collection or conversion of New and Old data. Solution: In the my. CNF file, add or modify the following two variables: wait_timeout = 2880000 interactive_timeout = 2880000. For details about the two variables, Google or read the official manual. If you cannot modify my. CNF, you can set client_interactive when connecting to the database, for example: SQL = "set interactive_timeout = 24*3600"; mysql_real_query (...) 2. Execute an SQL statement, but the SQL statement is too large or the statement contains blob or longblob fields. For example, in my. add or modify the following variables in the CNF file: max_allowed_packet = 10 m (you can also set the size you need) The max_allowed_packet parameter is used to control the maximum length of the Communication buffer. Recently, a website was designed to use the web page collector function. When a PHP script requests a URL, the requested webpage may be slow, exceeding the wait-Timeout time of MySQL, then, when the webpage content is captured and you are about to insert it to MySQL, you will find that the MySQL connection times out and is closed, so an error message such as "MySQL server has gone away" appears, to solve this problem, my experience has the following two points that may be useful to everyone: the first method is to increase your wait-Timeout value, which is in my. CNF (my. INI), my database load is slightly larger, so the value I set is 10 (The unit of this value is seconds, this means that when a database connection does not perform any operation within 10 seconds, it will be forcibly closed. I am not using a permanent link (mysql_pconnect), and I am using mysql_connect, you can find the wait-Timeout effect in mys. Show processlist In The QL process list. You can set this wait-timeout to a larger value, for example, 300 seconds. In general, 300 seconds is enough, in fact, you do not need to set it. The default value of MySQL is 8 hours. The situation depends on your server and site. Method 2: This is also the best method I personally think, that is, to check the connection status of MySQL and relink it. We may all know that there is a function like mysql_ping. In many materials, this mysql_ping API will check whether the database is connected. If it is disconnected, it will try again, however, I found that this is not the case in my test. It is conditional and must pass relevant parameters through the C API mysql_options, mySQL has the option to disconnect the automatic connection (MySQL does not automatically connect by default), but I found that this function is not included in the PHP MySQL API during the test. Edit MySQL again, haha. However, the mysql_ping function can still be used at last, but there is a small operation skill in it: this is a function in the middle of my database operation class to copy the Code as follows: function Ping () {If (! Mysql_ping ($ this-> link) {mysql_close ($ this-> link); // Note: you must first disable the database, this is the key $ this-> connect () ;}} the code that I need to call this function may be like this to copy the Code as follows: $ STR = file_get_contents ('HTTP: // www.jb51.net '); $ db-> Ping (); // The database connection may be closed after the previous web page is crawled, check and reconnect $ db-> query ('select * From table'); Ping () This function first checks whether the data connection is normal. If it is disabled, the MySQL instance of the current script is closed and then reconnected. After such processing, it can effectively solve problems such as MySQL server has gone away without causing additional overhead to the system. Which of the following statements about MySQL server has gone away are as follows. You probably browsed it for the following reasons: One possibility is that the SQL statement sent is too long and exceeds the size of max_allowed_packet. For this reason, you only need to modify my. CNF: Increase the value of max_allowed_packet. There is another possibility that timeout may be caused due to some reasons. For example, Singleton is used in the program to obtain the database connection. Although the program connects to the database multiple times, it actually uses the same connection, in addition, if the interval between two database operations in the program exceeds wait_timeout (this setting can be seen in show status), a problem may occur. The simplest way to deal with it is to increase wait_timeout. Of course, you can also use mysql_ping () in the program from time to time, so that MySQL knows that it is not a battle. Solve MySQL server has gone away 1. Execute MySQL statements in batches for a long time in applications (such as PHP. The most common is collection or conversion of New and Old data. Solution: In the my. CNF file, add or modify the following two variables: wait_timeout = 2880000 interactive_timeout = 2880000. For details about the two variables, Google or read the official manual. If you cannot modify my. CNF, you can set client_interactive when connecting to the database, for example: SQL = "set interactive_timeout = 24*3600"; mysql_real_query (...) 2. Execute an SQL statement, but the SQL statement is too large or the statement contains blob or longblob fields. For example, in my. add or modify the following variables in the CNF file: max_allowed_packet = 10 m (you can also set the size you need) The max_allowed_packet parameter is used to control the maximum length of the Communication buffer. 1. applications (such as PHP) execute MySQL statements in batches for a long time. The most common is collection or conversion of New and Old data. Solution: In the my. ini file, add or modify the following two variables: wait_timeout = 2880000interactive_timeout = 2880000. For more information about the two variables, see the official manual. If you cannot modify my. CNF, you can set client_interactive when connecting to the database, for example: SQL = "set interactive_timeout = 24*3600"; mysql_real_query (...) 2. Execute an SQL statement, but the SQL statement is too large or the statement contains blob or longblob fields. For example, in my. add or modify the following variables in the CNF file: max_allowed_packet = 10 m (you can also set the size you need) The max_allowed_packet parameter is used to control the maximum length of the Communication buffer. ------------ The following is the network search information ----------------- maybe other people may encounter this problem, not necessarily because of this, so I will find a more comprehensive analysis on the Internet below: there are two articles: the first one is more intuitive and the second one is more profound. MySQL server has gone away 16:23:22 from: http://www.webjx.com/database/mysql-8817.htmlthis day when we encounter such a situation, mysqlis just a cold saying: MySQL server has gone away. You probably browsed it for the following reasons: One possibility is that the SQL statement sent is too long and exceeds the size of max_allowed_packet. For this reason, you only need to modify my. CNF: Increase the value of max_allowed_packet. There is another possibility that timeout may be caused due to some reasons. For example, Singleton is used in the program to obtain the database connection. Although the program connects to the database multiple times, it actually uses the same connection, in addition, if the interval between two database operations in the program exceeds wait_timeout (this setting can be seen in show status), a problem may occur. The simplest way to deal with it is to increase wait_timeout. Of course, you can also use mysql_ping () in the program from time to time, so that MySQL knows that it is not a battle. Solve MySQL server has gone away1 and execute MySQL statements in batches in applications (such as PHP) for a long time. The most common is collection or conversion of New and Old data. Solution: In the my. CNF file, add or modify the following two variables: wait_timeout = 2880000interactive_timeout = 2880000. For more information about the two variables, see the official manual. If you cannot modify my. CNF, you can set client_interactive when connecting to the database, for example: SQL = "set interactive_timeout = 24*3600"; mysql_real_query (...) 2. Execute an SQL statement, but the SQL statement is too large or the statement contains blob or longblob fields. For example, in my. add or modify the following variables in the CNF file: max_allowed_packet = 10 m (you can also set the size you need) The max_allowed_packet parameter is used to control the maximum length of its communication buffer MYSQL: strange MySQL server has gone away and its solution from: http://fz9493.blog.sohu.com/38472203.htmljimmy | 15 March, 2007 in MySQL execute show status, usually pay more attention to the cache effect, the number of processes, etc., often ignore the two values: variable_name value aborted_clients 3792 aborted_connects 376 usually accounts for only 0.0x % of the query, so it is not highly valued. In addition, in traditional Web applications, query errors do not affect users much, but it is okay to refresh the page again. In the recent basic transformation, many applications run as services and users cannot be prompted to refresh them. In this case, the service quality may be affected. The main error message is "MySQL server has gone away" in log tracking of program scripts ". The official explanation is: The most common reason for the MySQL server has gone away error is that the server timed out and closed the connection. some other common reasons for the MySQL server has gone away error are: you (or the DB administrator) has killed the running thread with a kill statement or a mysqladmin kill command. you tried to run a query after closing the connection to the server. this indicates a logic E Rror in the application that shoshould be corrected. A client application running on a different host does not have the necessary privileges to connect to the MySQL server from that host. you got a timeout from the TCP/IP connection on the client side. this may happen if you have been using the commands: mysql_options (..., mysql_opt_read_timeout ,...) or mysql_options (..., mysql_opt_write_timeout ,...). In this case increasing the timeout may help solve the problem. you have encountered a timeout on the server side and the automatic reconnection in the client is disabled (the reconnect flag in the MySQL structure is equal to 0 ). you are using a Windows client and the server had dropped the connection (probably because wait_timeout expired) before the command was issued. the problem on Windows is tha T in some cases MySQL doesn't get an error from the OS when writing to the TCP/IP connection to the server, but instead gets the error when trying to read the answer from the connection. in this case, even if the reconnect flag in the MySQL structure is equal to 1, mySQL does not automatically reconnect and re-issue the query as it doesn' t know if the server did get the original query or not. the so Lution to this is to either do a mysql_ping on the connection if there has been a long time since the last query (this is what myodbc does) or set wait_timeout on the mysqld server so high that it in practice never times out. you can also get these errors if you send a query to the server that is incorrect or too large. if mysqld has es a packet that is too large or out of order, it assumes that Something has gone wrong with the client and closes the connection. if you need big queries (for example, if you are working with big blob columns), you can increase the query limit by setting the server's max_allowed_packet variable, which has a default value of 1 MB. you may also need to increase the maximum packet size on the client end. more information on setting the packet size is given in SE Ction a.1.2.9, "Packet Too large ". an insert or replace statement that inserts a great tables rows can also cause these sorts of errors. either one of these statements sends a single request to the server irrespective of the number of rows to be inserted; thus, you can often avoid the error by grouping the number of rows sent per insert or replace. you also get a lost connection if you are sending a p Acket 16 MB or larger if your client is older than 4.0.8 and your server is 4.0.8 and above, or the other way around. it is also possible to see this error if hostname lookups fail (for example, if the DNS server on which your server or network relies goes down ). this is because MySQL is dependent on the host system for name resolution, but has no way of knowing whether it is working-from MySQL's Point of view the problem is indistinguishable from any other network timeout. you may also see the MySQL server has gone away error if MySQL is started with the -- skip-networking option. another networking issue that can cause this error occurs if the MySQL port (default 3306) is blocked by your firewall, thus preventing any connections at all to the MySQL server. you can also encounter this error W Ith applications that fork child processes, all of which try to use the same connection to the MySQL server. this can be avoided by using a separate connection for each child process. you have encountered a bug where the server died while executing the query. according to this analysis, the possible cause is. the MySQL server and the Client Version do not match. 2. the MySQL server configuration is defective or the optimization is insufficient. 3. You need to improve the program script by replacing multiple server and client versions, and find that only some errors can be reduced, which cannot be completely solved. Exclude 1. The server has been thoroughly optimized and cannot achieve the desired results. In the Value Setting of timeout, from 10 of the experience value to 60 of the default PHP value, multiple attempts were made. The official MySQL Default Value (8 hours) is obviously impossible. So as to exclude 2. (More optimization experience is shared and will be provided in the future) analyze 3 pairs of program code and find that a large number of programs have applied the code similar to the following (for ease of understanding, use the original API description): $ conn = mysql_connect (......);............ if (! $ Conn) {// reconnect $ conn = mysql_connect (......);} mysql_query ($ SQL, $ conn); the meaning of this Code is consistent with the methods and ideas officially recommended by MySQL [if you have a script, you just have to issue the query again for the client to do an automatic reconnection.]. In actual analysis, if (! $ Conn) is not reliable. The program passes if (! $ Conn), the above error will still be returned. Rewrite the program: If (! Conn) {// connect...} elseif (! Mysql_ping ($ conn) {// reconnect...} mysql_query ($ SQL, $ conn); after actual observation, the MySQL server has gone away error is basically solved. BTW: With A Reconnect question, in the old environment of php4x + client3x + mysql4x, The reconnet code: $ conn = mysql_connect (...) can work normally. However, in the new environment of php5x + client4x + mysql4x, $ conn returned by $ conn = mysql_connect (...) is unavailable in some cases. It must be written as mysql_close ($ conn); $ conn = mysql_connect (...); the $ conn returned can be used normally. The reason is unknown. No further research has been conducted and no further discussions have been made. Maybe there will be an official MySQL bug report .~~ Haha ~~ This article is from the csdn blog. For more information, see http://blog.csdn.net/brightsnow/archive/2009/03/17/3997705.aspxdescription: remember that your MySQL "max_allowed_packet" configuration setting (default 1 MB) by default, MySQL can process up to 1 MB of data. If you use large text or BLOB data in SQL, this problem occurs. Note when trying to insert or update and trying to put a large amount of text or data (BLOB) into a MySQL table you might run into problems in the PHP manual. in MySQL. err you might see: Packet Too large (73904) to fix you just have to start up MySQL with the option-O max_allowed_packet = maxsize you woshould just replace maxsize with the Max size you want to insert, the default is 65536 MySQL manual says both the client And the server have their own max_allowed_packet variable, so if you want to handle big packets, you must increase this variable both in the client and in the server. if you are using the mysql client program, its default max_allowed_packet variable is 16 Mb. to set a larger value, start MySQL like this: shell> MySQL -- max_allowed_packet = 32 m that sets the packet size to 32 MB. the server's default m Ax_allowed_packet value is 1 MB. you can increase this if the server needs to handle big queries (for example, if you are working with big blob columns ). for example, to set the variable to 16 MB, start the server like this: shell> mysqld -- max_allowed_packet = 16 m you can also use an option file to set max_allowed_packet. for example, to set the size for the server to 16 MB, add the following lines in An option file: [mysqld] max_allowed_packet = 16 m when MySQL is used for database restoration, the following error occurs due to the large data size: the MySQL server returned this error: mySQL error nr.2006-MySQL server has gone away. This error occurs when I restore a MB backup. The solution is to find the MySQL installation directory, find the my. ini file, and add max_allowed_packet = 10 m at the end of the file (you can also set the desired size ). The max_allowed_packet parameter is used to control the maximum length of the Communication buffer.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.