Solve Lost connection to MySQL server during query error method / Mysql closed strict mode

Use the time to import Navicat MySQL database, there has been a serious mistake, Lost connection to MySQL server during query, literally means the loss of connection to MySQL server during a query.

[Msg] Decompressing...
[Msg] Table Created: wp_wiki_copy
[Msg] Importing Data...
[Msg] 2013 - Lost connection to MySQL server during query
[Msg] Table Restored: wp_wiki_copy
[Msg] Finished - Stopped before completion

My data about the amount of 400 000, database backup file size has 1.15G, which is compressed, is a bit big.

MySQL preliminary judgment is likely to hang, in view of MySQL system service process did not stop there.

Most began to consider the structure of the database it is wrong, but I import data Navicat for MySQL backup and restore the backup, the table should have structural problems in the backup file inside, should not the database structure.

Network environment are local. It can not be the problem of network links and database servers.

Finally in the morning to find a solution, in my.ini configuration file to add the node mysqld

max_allowed_packet = 2048M

MySQL is configured to allow maximum packet size, above 500M based on your project you can modify for your own value, as long as larger than the backup file to import it.

================================ knowledge supplement ================ ================

 

mysql appears ERROR: (2006, 'MySQL server has gone away') means that the question refers to the link between the client and MySQL server disconnected.

The reason for this is generally too long time sql operation, or the data transfer is too large (e.g., using insert ... values ​​statements too, this situation can be avoided by modifying the configuration parameters max_allowed_packed, the program may be insertion of data in the batch).

There are many reasons for this problem, the analysis of online summarize:

One reason. MySQL service the dang

Methods to determine whether the reason is very simple, enter the mysql console, long-run view of mysql

mysql> show global status like 'uptime';
+---------------+---------+
| Variable_name | Value   |
+---------------+---------+
| Uptime        | 3414707 |
+---------------+---------+

1 row in set or view the MySQL error log to see if there is information to restart

If the uptime great value, indicating that mysql service is running for a long time. Description Recent service is not restarted before.
If the log information is not relevant, but also the name mysql table service had not recently restarted, you can continue to check the following several things.

Two reasons. Mysql connection timeout

Namely, a long connection mysql long time no new requests initiated, the server-side reached timeout, server to be forcibly closed.
Through this connection thereafter initiate an inquiry and they will error has Gone Away Server
(most of the PHP script that fall into this category)

mysql> show global variables like '%timeout';
+----------------------------+----------+
| Variable_name              | Value    |
+----------------------------+----------+
| connect_timeout            | 10       |
| delayed_insert_timeout     | 300      |
| innodb_lock_wait_timeout   | 50       |
| innodb_rollback_on_timeout | OFF      |
| interactive_timeout        | 28800    |
| lock_wait_timeout          | 31536000 |
| net_read_timeout           | 30       |
| net_write_timeout          | 60       |
| slave_net_timeout          | 3600     |
| wait_timeout               | 28800    |
+----------------------------+----------+
10 rows in set

wait_timeout is 28800 seconds, i.e. link mysql no operation is automatically closed after 28,800 seconds

Three reasons. Mysql request link process is actively kill

This situation and the reasons for two similar, but a man-made one MYSQL own operation

mysql> show global status like 'com_kill';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Com_kill      | 21    |
+---------------+-------+
1 row in set原因四. Your SQL statement was too large.

When the result set of a query more than max_allowed_packet this error will occur. Positioning method is to play error-related statements.

Export using select * into outfile way to the file to see if the file size exceeds the max_allowed_packet, if more than you need to adjust parameters or optimize statement.

mysql> show global variables like 'max_allowed_packet';
+--------------------+---------+
| Variable_name      | Value   |
+--------------------+---------+
| max_allowed_packet | 1048576 |
+--------------------+---------+
1 row in set (0.00 sec)

修改参数:

MySQL> SET Global the max_allowed_packet = 1024 * 1024 * 16;
MySQL> Show Global Variables like 'the max_allowed_packet';
+ ------ + -------------------- + ----
| variable_name | Value |
+ -------------------- + ---------- +
| max_allowed_packet | 16777216 |
+ - + ---------- + -------------------
1 Row in the SET (0.00 sec)

the following is added:

applications (such as PHP) long a bulk of the time MYSQL statement. The implementation of a SQL, but SQL statement is too large or statement contains BLOB or longblob field. For example, processing of picture data. Are prone to MySQL server has gone away. 

Today encounter a similar situation, MySQL simply said coldly: MySQL server has gone away. 

Probably browse a bit, mainly probably because of the following reasons: 
One possibility is to send SQL statements so long that exceeds the size of max_allowed_packet, if this is the reason, as long as you modify the my.cnf, increase the value of max_allowed_packet It can be. 

Another possibility is that for some reason the timeout, for example, the program uses the Singleton approach to obtain a database connection, although many connect to the database, but in fact are using the same connection, but the program twice a database operation the interval longer than wait_timeout (SHOW STATUS to see this setting), then it may be a problem. The simplest approach is to wait_timeout big change, of course, you can occasionally dig mysql_ping () about, so MySQL will know in the program it is not a person in combat. 

Gone Away has resolved Server MySQL 

1, the application (such as PHP) to perform the bulk of the long MYSQL statement. The most common is the acquisition or conversion of old and new data. 
Solution: 
Add my.cnf file or modify the following two variables: 
wait_timeout = 2.88 million 
interactive_timeout = 2.88 million 
specifically about these two variables can google or see the official manual. If not modify my.cnf, may be provided at the time of connection to the database CLIENT_INTERACTIVE, such as: 
SQL = "SET interactive_timeout seconds = 24 * 3600"; 
mysql_real_query (...) 


2, a SQL execution, but is too large or the SQL statement statement BLOB fields containing or longblob. For example, picture data processing 
solution: 
add or modify the following variables in my.cnf file: 
max_allowed_packet = 10M (you can also set the size of their own needs) 
max_allowed_packet effect parameter is used to control the maximum length of the communication buffer. 


A recent site has a collection station to use WEB page function, web page when a PHP script when the requested URL, the request may be this very slowly, over time the wait-timeout mysql, and then was arrested when web content Came back, ready to be inserted into the MySQL time and found MySQL connection timeout closed, so it was a "MySQL server has gone away" error message like this, to solve this problem, in my experience there are two things, perhaps we have use: 
the first method: 
of course, is to increase your wait-timeout value, this parameter is (in Windows to step down following is my.ini) set in the my.cnf, my database load a little bigger, so I set value of 10, (the values are in seconds, meaning that when a database connection without any operation within 10 seconds, it will be forced to close, I'm not using permanent link (mysql_pconnect), using a mysql_connect, on this the effect of wait-timeout you can see (show processlist)) in the list of MySQL process, you can put this wait-timeout is set to be larger, such as 300 seconds, huh, huh, in general, 300 seconds, sufficient, in fact, you can not set, MySQL default is eight hours. Situation to be made to your server and site. 
The second method: 
This is what I personally think the best way that MySQL checks the link status, link it again. 
May we all know there mysql_ping such a function, say mysql_ping of this API will check the database is linked in a lot of information, if you are disconnected, then tries to reconnect, but found that this was not like this in my testing process , is conditional, must be passed by the relevant parameters mysql_options the C API, let MYSQL have the option to automatically disconnect the link (MySQL by default to not automatically connect), but I have found in tests of the MySQL API PHP does not take this function, you re-edit MySQL, huh, huh. But mysql_ping This function is finally need them, just where you want to have a small operating skills: 
This is a function of my middle class database operations 

Copy the code  code is as follows:


of ping function () { 
IF {(mysql_ping ($ this-> Link)!) 
mysql_close ($ this-> Link); // Note: be sure to perform database closed, which is the key 
$ this-> Connect (); 


I need to call this function code may be like this 

Copy the code  code is as follows:


= file_get_contents STR $ ( 'http://www.jb51.net'); 
$ db-> of ping (); // After gripping the front page, or causes the database connection is closed, and reconnect check 
$ db- > query ( 'select * from table  ');


ping () function to detect a data connection is normal, if it is closed, the entire current script MYSQL instance closed, and then reconnect. 
After this treatment can be very effective to solve this problem MySQL server has gone away, and will not cause additional overhead on the system. 
__________________________________________________________________________________________________ 
  today encountered a similar situation, MySQL simply said coldly: MySQL server has gone away. 
  Probably browse a bit, mainly probably because of the following reasons: 
  One possibility is to send SQL statements so long that exceeds the size of max_allowed_packet, if this is the reason, as long as you modify the my.cnf, increase the value of max_allowed_packet It can be. 
  Another possibility is that for some reason the timeout, for example, the program uses the Singleton approach to obtain a database connection, although many connect to the database, but in fact are using the same connection, but the program twice a database operation the interval longer than wait_timeout (SHOW STATUS to see this setting), then it may be a problem. The simplest approach is to wait_timeout big change, of course, you can occasionally dig mysql_ping () about, so MySQL will know in the program it is not a person in combat. 
  Gone Away has resolved Server MySQL 
  1, the application (such as PHP) to perform the bulk of the long MYSQL statement. The most common is the acquisition or conversion of old and new data. 
  solution: 
  Add my.cnf file or modify the following two variables: 
wait_timeout = 2.88 million 
interactive_timeout = 2.88 million specifically about these two variables can google or see the official manual. If not modify my.cnf, may be provided at the time of connection to the database CLIENT_INTERACTIVE, such as: 
SQL = "SET interactive_timeout seconds = 24 * 3600"; 
mysql_real_query (...) 
  2, a SQL execution, but is too large or the SQL statement statement BLOB fields containing or longblob. For example, picture data processing 
  Solution: 
  Add my.cnf file or modify the following variables: 
max_allowed_packet = 1OM (may be required to set the size of their own) 
acting max_allowed_packet parameter is used to control the maximum length of the communication buffer.
1, the application (such as PHP) long MYSQL execute batch statement.
The most common is the acquisition or conversion of old and new data.

 

Solution:

Add in my.ini file or modify the following two variables:
wait_timeout = 2.88 million
interactive_timeout =  2.88 million

specifically about these two variables can google or see the official manual.
If not modify my.cnf, may be provided at the time of connection to the database CLIENT_INTERACTIVE, such as:
SQL = "SET interactive_timeout seconds = 24 * 3600";
mysql_real_query (...)

2, a SQL execution, but is too large or the SQL statement statement BLOB fields containing or longblob.
For example, picture data processing
solution


is added my.cnf file or modify the following variables:
max_allowed_packet =  1OM  (may be required to set the size of their own)

acting max_allowed_packet parameter is used to control the maximum length of the communication buffer.

Modification method

1) The method
can be modified edit my.cnf (the windows my.ini), be modified configuration server section [mysqld] segment or mysql.

max_allowed_packet = 20M
If you can not find the my.cnf

mysql --help | grep my.cnf
to find my.cnf file.

2) Method 2
(very uncompromising, very tangled way)

Enter the mysql server

Running mysql command line

set global max_allowed_packet = 2 * 1024 * 1024 * 10
and then close it out this mysql server link, and then enter.

show VARIABLES like '% max_allowed_packet%' ;
see the next max_allowed_packet whether to edit the success

------------  following is the information network search  ----------------- -


Maybe someone else encountered this problem, here is not necessarily the reason, then, I was found on the Internet into a more comprehensive analysis of the following:
there are two, the first chapter intuitive, The second more esoteric.
Solve the MySQL server has gone away 2009-01-09 16:23:22

today encountered a similar situation, MySQL simply said coldly: MySQL server has gone away.
Probably browse a bit, mainly probably because of the following reasons:
One possibility is to send SQL statements so long that exceeds the size of max_allowed_packet, if this is the reason, as long as you modify the my.cnf, increase the value of max_allowed_packet It can be.
Another possibility is that for some reason the timeout, for example, the program uses the Singleton approach to obtain a database connection, although many connect to the database, but in fact are using the same connection, but the program twice a database operation the interval longer than wait_timeout (SHOW STATUS to see this setting), then it may be a problem. The simplest approach is to wait_timeout big change, of course, you can occasionally dig mysql_ping () about, so MySQL will know in the program it is not a person in combat.
Gone Away has resolved Server MySQL

1, the application (such as PHP) to perform the bulk of the long MYSQL statement. The most common is the acquisition or conversion of old and new data.

Solution:

Add or modify the following two variables in my.cnf file:

wait_timeout = 2.88 million
interactive_timeout = 2.88 million  
Specifically about these two variables can google or see the official manual. If not modify my.cnf, may be provided at the time of connection to the database CLIENT_INTERACTIVE, such as:

SQL = "SET interactive_timeout seconds = 24 * 3600";
mysql_real_query (...)

2, a SQL execution, but is too large or the SQL statement statement BLOB fields containing or longblob. For example, picture data processing

Solution:

Add my.cnf file or modify the following variables:

the max_allowed_packet =  1OM
(may be required to set the size of their own)

the max_allowed_packet
effect parameter is used to control the maximum length of the communication buffer

MySQL : strange MySQL server has gone away and their solutions

in the implementation Mysql show status, usually more concerned about the effect of the cache, the number of processes, etc., tend to overlook the two values:

variable_name value 
Aborted_clients 3792 
Aborted_connects 376


usually only query of 0.0x%, so not well valued. But also in traditional Web applications, query on the error did not affect the user, just refresh the page again on OK. Recent basic transformation, the number of applications to run as a service, not prompt the user to refresh, in this case, might affect the quality of services.

通过程序脚本的日志跟踪,主要报错信息为“MySQL server has gone away”。官方的解释是:

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 error in the application that should 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 that 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 solution 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 receives 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 1MB. You may also need to increase the maximum packet size on the client end. More information on setting the packet size is given in Section A.1.2.9, “Packet too large”.

An INSERT or REPLACE statement that inserts a great many 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 reducing the number of rows sent per INSERT or REPLACE.

You also get a lost connection if you are sending a packet 16MB 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.

By You CAN Also Encounter the this error with Applications that fork Child Processes, All of Which the try to use at The Same, Connection to at The MySQL Server. This CAN BE avoided by a using A separate Connection for the each Child Process.

By You have have encountered A bug the WHERE at The Server died . while executing the query



that analysis, there are three possible reasons:

1, Mysql server does not match the client version.

2, Mysql server configuration is defective or insufficient optimization

3, need to improve the script

by replacing multiple service and client version and found that only partially reduce error, and can not fully resolved. Exclusion 1.

End of service a thorough optimization, also failed to achieve the desired effect. On the set timeout values, the value of the experience from 10, 60 to the default PHP carried out several attempts. The official Mysql default value (8 hours) is obviously impossible. Thus 2 have also been excluded. (More optimization experience sharing will be provided later finishing)

analyzed for three pairs of a program code, a large number of applications found in the program code similar to the following (for ease of understanding, the original described by API):

$ Conn = mysql_connect (.. . ...);

... ... ... ...

IF ($ Conn) {// Reconnect!

$ Conn = mysql_connect (... ...);

}

mysql_query ($ sql, $ conn) ;

the meaning of this code, the official suggested Mysql ideas and methods consistent [If you have a script, you just have to issue the query again for the client to do an automatic reconnection.]. In practice, the analysis found that, if (! $ Conn) is not reliable, the program passed if (! $ Conn) after the test, will still return the above error.

: The program has been rewritten

IF (! Conn) {// ...} Connect

(! Mysql_ping ($ conn)) ELSEIF the Reconnect {// ...}

mysql_query ($ SQL, $ conn);

by actual observation, MySQL server has gone away error is basically solved.

BTW: it comes with a question about reconnect, and

in php4x + client3x + mysql4x old environment, reconnet code:

$ conn = mysql_connect (...) can work.

However, in the new environment of php5x + client4x + mysql4x, $ conn = mysql_connect (...) returns $ Conn some cases there is not available. Need to write to:

mysql_close ($ conn);

$ conn = mysql_connect (...);

return $ conn to function properly. The reason is unknown. Without making in-depth study, and no discussion. Maybe mysql BUG official report will be in it.

Ha ha ~ ~ ~ ~


This article comes from CSDN blog, reproduced, please indicate the source: HTTP: //blog.csdn.net/brightsnow/archive/2009/03/17/3997705.aspx


the Description: 
Remember that your MySQL "max_allowed_packet" Setting the Configuration (default 1MB) 
MySQL default 1MB is the maximum that can be handled 
if you use a large text or BLOB data in sql, this problem occurs. php manual annotations on 

the When Trying to Trying to the INSERT and the UPDATE or PUT A Large Data AMOUNT of text or (BLOB) INTO MySQL Table A RUN INTO Problems Might you. 
the In mysql.err you Might See: 
Packet TOO Large (73904) 
the To you have have to Start the Just FIX up at the mysql with the Option -O max_allowed_packet = MAXSIZE 
by you Would the Just with the replace MAXSIZE at the max size you want to INSERT, IS 65536 at the default 


on the 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 16MB. To set a larger value, start mysql like this: 

shell> mysql --max_allowed_packet=32M That sets the packet size to 32MB. 

The server's default max_allowed_packet value is 1MB. 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 16MB, start the server like this: 

shell> mysqld --max_allowed_packet = 16M by You CAN AN Also use the Option File to the SET max_allowed_packet the For Example, to at The size for the SET at The Server to 16MB, the Add Lines at The following AN in the Option File:. 

[mysqld] max_allowed_packet = 16M 

use mysql to do database restore, because of some great data, this error can occur: the MySQL Server returned this error: MySQL error Nr.2006-MySQL server has gone away. One of my 150mb backup and restore when he appeared this error. The solution is to find the mysql installation directory and find my.ini file, and finally add in the file: max_allowed_packet = 10M (you can also set the size of their own needs). max_allowed_packet effect parameter is used to control the maximum length of the communication buffer.

Close Mysq strict mode

It may appear the error log when you import data, in addition to the data packet is too large than there MySQl settings under normal circumstances

Only need to turn off strict mode mysql most of the problems can be solved

Mysql way to find the configuration file my.ini

SQL-the MODE = "STRICT_TRANS_TABLES, NO_AUTO_CREATE_USER, NO_ENGINE_SUBSTITUTION"


modify the

sql-mode = "NO_AUTO_CREATE_USER, NO_ENGINE_SUBSTITUTION "

To ok

There is remember to avoid problems before modifying some applications do not take effect occurs in the open after the service manually shut down completely change Oh Mysql service

Today to share this ........

Guess you like

Origin www.cnblogs.com/C1own/p/12186318.html