27

I am doing bulk inserting and getting error as Mysql2::Error: Lost connection to MySQL server during query:

I searched for this error on the Internet and most of the blogs/articles asking to increase net_read_timeout value.

I searched on the Internet about net_read_timeout but not getting any article/blog which describes it in easy to understandable language. On MySQL website net_read_timeout is describe as "The number of seconds to wait for more data from a connection before aborting the read". I am totally confused with this statement and not getting it.

I also want to know about net_write_timeout and wait_timeout variable.

1
  • Have you considered consulting the MySQL documentation? Commented Aug 8, 2023 at 7:42

3 Answers 3

39

MySQL uses different timeout variables for various stages.

  • When connection is established it uses connection_timeout
  • When it waits for the next query it uses wait_timeout
  • When it doesn't receive the query in the specific time it uses net_read_timeout and net_write_timeout
  • And so on...

Usually net_read_timeout shouldn't be a problem but when you have some network trouble, especially when communicating with the server this timeout could be raised because instead of a single packet for the query, that you sent to the Database, MySQL waits for the entire query to be read but, due to the network problem, it doesn't receive the rest of the query. MySQL doesn't allow client to talk the server until the query result is fetched completely.

You cannot properly change those two variable, which are sessions variable after all.

Also from the MySQL Doc you can read

net_read_timeout:

The number of seconds to wait for more data from a connection before aborting the read. When the server is reading from the client, net_read_timeout is the timeout value controlling when to abort. When the server is writing to the client, net_write_timeout is the timeout value controlling when to abort. See also slave_net_timeout.

net_write_timeout:

The number of seconds to wait for a block to be written to a connection before aborting the write. See also net_read_timeout.

You can check the defaults variable within MySQL itself using

> mysql show variables like '%timeout';

Sign up to request clarification or add additional context in comments.

5 Comments

i set wait_timeout to 30 seconds and execute query "select sleep(40)" which sleeps for 40 seconds then i should get wait_timeout error, but still i am not getting wait timeout error.
can you give me example of how to manually raise wait_timeout,net_read_timeout and net_write_timeout exception, so that i can understand it better.
That's kinda weird, have you tried the query on my answer?
yes i tried it, it showing timeout values of different variables and nothing else. can you give me any example to manually raise wait_timeout and net_read_timeout error.
If I understood, select sleep(40) won’t trigger wait_timeout because this is the timeout to get the next query after the connection or after the last completed query, not the timeout to complete the query. Your query is sent, so it’s ok. Were you using unbuffered queries?
4

This is happening when you are using the non-buffering connection to the mysql database, and after executing your query, you are not consuming the data. client python connect with non-buffering SSDictCursor

connection = pymysql.connect(host='localhost',
                             user='xxxx',
                             password='xxxx',
                             db='employees',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.SSDictCursor)
sql = " select * from employees"
cursor = connection.cursor()
cursor.execute(sql)
cursor.fetchone()

doing nothing and your connection will be timed out or if not fetching all data within 60 seconds(net_write_timeout), your connection will be aborted.
quote from doc: When the server is writing to the client, net_write_timeout is the timeout value controlling when to abort

2019-08-14T15:20:26.465498Z 28440 Query select * from employees

2019-08-14T15:21:26.584634Z 28440 [Note] Aborted connection 28440 to db: 'employees' user: 'xxxx' host: 'localhost' (Got timeout writing communication packets)

Comments

4

I understood about wait_timeout settings. MySQL default wait_timeout is 28800 seconds which 8 hours. now to understand how wait_timeout works execute following SQL statement.

set wait_timeout = 10;

After executing above statement if MySQL server has not received any SQL statement withing 10 seconds then it will automatically close the connection.

To test it wait for 10 seconds and then execute any SQL query it will give you error like "MySQL closed connection during query execution"

will update my answer for net_read_timeout and net_write_timeout shortly.

1 Comment

I have a similar issue, my site keeps dropping as so many inserts are happening into db, how can I improve this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.