When you run MySQL queries, sometimes you may encounter an error saying you lost connection to the MySQL server as follows: Show
The error above commonly happens when you run a long or complex MySQL query that runs for more than a few seconds. To fix the error, you may need to change the timeout-related global settings in your MySQL database server. Increase the connection timeout from the command line using –connect-timeout optionIf you’re accessing MySQL from the command line, then you can increase the number of seconds MySQL will wait for a connection response using the By default, MySQL will wait for 10 seconds before responding with a connection timeout error. You can increase the number to 120 seconds to wait for two minutes:
You can adjust the number Once you’re inside the Using the If you want to make a permanent change to the connection timeout variable, then you need to adjust the settings from either your MySQL database server or the GUI tool you used to access your database server. Let’s see how to change the timeout global variables in your MySQL database server first. Adjust the timeout global variables in your MySQL database serverMySQL database stores timeout-related global variables that you can access using the following query:
Here’s the result from my local database. The highlighted variables are the ones you need to change to let MySQL run longer queries:
To change the variable values, you can use the 0 query as shown below:
The above query should adjust the 1 variable value to 2 seconds. You can adjust the numbers as you see fit.Adjust the timeout variables in your MySQL configuration filesAlternatively, if you’re using a MySQL configuration file to control the settings of your connections, then you can edit the my.cnf file (Mac) or my.ini file (Windows) used by your MySQL connection. Open that configuration file using the text editor of your choice and try to find the following variables in mysqld :
The 3 and 4 variables shouldn’t cause any problem because they usually have 28800 seconds (or 8 hours) as their default value.To prevent the timeout error, you need to increase the 1 and 6 variable values. I’d suggest setting it to at least 2 seconds (10 minutes)Adjust timeout related variables in your MySQL GUI toolsIf you’re using GUI MySQL tools like MySQL Workbench, Sequel Ace, or PHPMyAdmin, then you can also find timeout-related variables that are configured by these tools in their settings or preferences menu. For example, in MySQL Workbench for Windows, you can find the timeout-related settings in Edit > Preferences > SQL Editor as shown below: MySQL Workbench Windows timeout settings If you’re using Mac, then the menu should be in MySQLWorkbench > Preferences > SQL Editor as shown below: MySQL Workbench Mac timeout settings If you’re using Sequel Ace like me, then you can find the connection timeout option in the Preferences > Network menu. Here’s a screenshot from Sequel Ace Network settings: Sequel Ace connection timeout option For other GUI tools, you need to find the option yourself. You can try searching the term 8 in Google to find the option. |