Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Prevent Connection Timeouts for Large MySQL Imports

Tags:

During development, how our local WAMP servers get up-to-date data from the test server is that a dump of the database is made and we upload that dump using the source command to load the .sql file.

Recently, at the very end of the import we have been getting errors about the @old variables which stored the original settings like foreign key constraints before they’re changed (so turning off foreign key constraints so that the import doesn’t throw errors when it recreates tables and attempts to create foreign keys when one of the tables has yet to be created). I have worked out that the cause is that the product table is getting more and more data and at a point the session has timed out during the import.

I’m wondering what setting can I set (either as part of the SQL query on in the my.ini file) that will stop all timeouts, in effect making a session last forever while we are signed in.

like image 763
Memor-X Avatar asked Jul 05 '13 05:07

Memor-X


People also ask

What is MySQL connection timeout?

MySqlConnection. ConnectionTimeout Property. Gets the time to wait while trying to establish a connection before terminating the attempt and generating an error.

How do I set query timeout in MySQL?

Or you can set a session-wide or global timeout: SET SESSION MAX_EXECUTION_TIME=2000; SET GLOBAL MAX_EXECUTION_TIME=2000; The timeouts only apply to read-only SELECT queries. You can handle this error centrally in your application and display message to users that the response took too long and was aborted.


2 Answers

Strategies for importing large MySQL databases

PHPMyAdmin Import

Chances are if you’re reading this, PHPMyAdmin was not an option for your large MySQL database import. Nonetheless it is always worth a try, right? The most common cause of failure for PHPMyAdmin imports is exceeding the import limit. If you’re working locally or have your own server, you can try changing the MySQL ini settings usually found in the my.ini file located in the MySQL install folder. If you’re working with WAMP on Windows, you can access that file using the WAMP control panel under MySQL > my.ini. Remember to restart WAMP so your new settings will be used. Settings you may want to increase here include:

max_allowed_packet
read_buffer_size

Even with enhanced MySQL import settings you may still find that imports time out due to PHP settings. If you have access to PHP.ini, you can make edits to the maximum execution time and related settings. In WAMP, access the PHP.ini file under the WAMP control panel at PHP > php.ini. Consider raising the limits on the following settings while trying large MySQL imports:

max_execution_time
max_input_time
memory_limit

Using Big Dump staggered MySQL dump importer

If basic PHPMyAdmin importing does not work, you may want to try the Big Dump script from Ozerov.de for staggered MySQL imports. What this useful script does is run your import in smaller blocks, which is exactly what is often needed to successfully import a large MySQL dump. It is a free download available at http://www.ozerov.de/bigdump/.

The process of using Big Dump is fairly simple: you basically position your SQL import file and the Big Dump script together on the server, set a few configs in the Big Dump script and then run the script. Big Dump handles the rest!

One key point about this otherwise great option, is that it will not work at all on MySQL exports that contain extended inserts. So if you have the option to prevent extended inserts, try it. Otherwise you will have to use another method for importing your large MySQL file.

Go command line with MySQL console

If you’re running WAMP (and even if you’re not) there is always the option to cut to the chase and import your large MySQL database using the MySQL console. I’m importing a 4GB database this way as I write this post. Which is actually why I have some time to spend writing, because even this method takes time when you have a 4GB SQL file to import!

Some developers (usually me) are intimidated by opening up a black screen and typing cryptic commands into it. But it can be liberating, and when it comes to MySQL databases it often the best route to take. In WAMP we access the MySQL console from the WAMP control panel at MySQL > MySQL Console. Now let’s learn the 2 simple MySQL Console commands you need to import a MySQL database, command-line style:

use `db_name`

Command use followed by the database name will tell the MySQL console which database you want to use. If you have already set up the database to which you are importing, then you start by issuing the use command. Suppose your database is named my_great_database. In this case, issue the following command in the MySQL Console. Note that commands must end with a semi-colon.

mysql-> use my_great_database;

mysql-> source sql_import_file.sql

Command source followed by the location of a SQL file will import the SQL file to the database you previously specified with the use command. You must provide the path, so if you’re using WAMP on your local server, start by putting the SQL file somewhere easy to get at such as C:\sql\my_import.sql. The full command with this example path would be:

mysql-> source C:\sql\my_import.sql;

After you run that command, the SQL file should begin to be imported. Let the queries run and allow the import to complete before closing the MySQL console.

Further documentation for MySQL command line can be found here: http://dev.mysql.com/doc/refman/5.5/en/mysql.html.

Another solution is to use MySQL Workbench.

like image 98
Mark Avatar answered Oct 23 '22 18:10

Mark


This solution worked for me:

max_allowed_packet  <-- --> upped size to 8M
read_buffer_size  <-- -->  upped from 256 to 512

Using Xampp control panel on localhost. After making the changes to the my.ini file in MySQL config, don’t forget to quit Xampp (or Wamp) and restart it for changes to take effect.

(Four days of head-banging and I finally got it fixed!)

Symptoms were on Import: #2006 MySql server went away. However, only 10 table rows were being imported out of 87 table rows.

like image 37
CloudyBright Avatar answered Oct 23 '22 18:10

CloudyBright