Restore from backup - Lost connection to MySQL server during query?

@sebadamus continuing from Newbie: How to backup? - #15 by sebadamus


Any idea why I get this error? “(2013, ‘Lost connection to MySQL server during query’)”

Its an almost empty site, the sql db backup uncompressed is 8mb, private/public files just 1 jpg.

I run this command to restore DB and its public/private files from the backup created within ERPNext, but it takes something like 20 minutes until the error appears.

Will try to restore manually, restoring db with phpmyadmin and coping the files manually… but would be good to be able to restore with bench commands.

bench --site site1-test.local restore ../DB-Backups/Backups_Manuales/20180318_132828-site1_local-database.sql --with-public-files ../DB-Backups/Backups_Manuales/20180318_132828-site1_local-files.tar --with-private-files ../DB-Backups/Backups_Manuales/20180318_132828-site1_local-private-files.tar

frappe@erpnext:~/frappe-bench$ bench --site site1-test.local restore …/DB-Backups/Backups_Manuales/20180318_132828-site1_local-database.sql --with-public-files …/DB-Backups/Backups_Manuales/20180318_132828-site1_local-files.tar --with-private-files …/DB-Backups/Backups_Manuales/20180318_132828-site1_local-private-files.tar
MySQL root password:
Traceback (most recent call last):
File “/usr/lib/python2.7/”, line 174, in _run_module_as_main
"main", fname, loader, pkg_name)
File “/usr/lib/python2.7/”, line 72, in _run_code
exec code in run_globals
File “/home/frappe/frappe-bench/apps/frappe/frappe/utils/”, line 94, in 
File “/home/frappe/frappe-bench/apps/frappe/frappe/utils/”, line 18, in main
File “/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/click/”, line 722, in call
return self.main(*args, **kwargs)
File “/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/click/”, line 697, in main
rv = self.invoke(ctx)
File “/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/click/”, line 1066, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File “/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/click/”, line 1066, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File “/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/click/”, line 895, in invoke
return ctx.invoke(self.callback, **ctx.params)
File “/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/click/”, line 535, in invoke
return callback(*args, **kwargs)
File “/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/click/”, line 17, in new_func
return f(get_current_context(), *args, **kwargs)
File “/home/frappe/frappe-bench/apps/frappe/frappe/commands/”, line 24, in _func
ret = f(frappe._dict(ctx.obj), *args, **kwargs)
File “/home/frappe/frappe-bench/apps/frappe/frappe/commands/”, line 115, in restore
File “/home/frappe/frappe-bench/apps/frappe/frappe/commands/”, line 65, in _new_site
admin_password=admin_password, verbose=verbose, source_sql=source_sql,force=force, reinstall=reinstall)
File “/home/frappe/frappe-bench/apps/frappe/frappe/”, line 43, in install_db
if not ‘tabDefaultValue’ in frappe.db.get_tables():
File “/home/frappe/frappe-bench/apps/frappe/frappe/”, line 784, in get_tables
return [d[0] for d in self.sql(“show tables”)]
File “/home/frappe/frappe-bench/apps/frappe/frappe/”, line 176, in sql
File “/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/pymysql/”, line 165, in execute
result = self._query(query)
File “/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/pymysql/”, line 321, in _query
File “/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/pymysql/”, line 860, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File “/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/pymysql/”, line 1061, in _read_query_result
File “/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/pymysql/”, line 1349, in read
first_packet = self.connection._read_packet()
File “/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/pymysql/”, line 991, in _read_packet
packet_header = self._read_bytes(4)
File “/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/pymysql/”, line 1037, in _read_bytes
CR.CR_SERVER_LOST, “Lost connection to MySQL server during query”)
pymysql.err.OperationalError: (2013, ‘Lost connection to MySQL server during query’)

For a sanity check connect by other means?

Say manually

mysql -u root -p

or via the bench

bench mysql

Thanks Clarkej,

First command response right and asks for root mysql pass, then just mysql prompt.

Second command opens bench mysql prompt (didnt knew this command, useful!)

But the ending is the same pymysql.err.OperationalError: (2013, ‘Lost connection to MySQL server during query’)

its a ubuntu 16.04 LTS server, everything running locally on the same server…

increase max_allowed_packet in mysql settings

I had this problem repeatedly until I logged into mariadb and imported the sql file.

Then ran bench --site sitename migrate from the frappe-bench folder.

and it worked.

it is 16 MB by default.
I have 3 GB back up file need to restore.
So what is the value we need to put in my sql packet size?

I have increased to 32 MB , but getting above error again.

I got the same problem today with a v11 instance. Also used the same command to restore the db (with the --force option).

as …

… did not help I figured that

  1. login in to the mysql console mysql -u root -p -h localhost
  2. identifying the db name with show databases;
  3. dropping the database (still in the mysql console) `drop database [db-name];
  4. logging out of the mysql console, and finally
  5. restoring the backup with bench --site [site-name] restore /path/to/backup-file.sql


show databsesnd then doing


did you able to solve this issue? please let me know how you did.

thanks and reg,