507

I want to copy a live production database into my local development database. Is there a way to do this without locking the production database?

I'm currently using:

mysqldump -u root --password=xxx -h xxx my_db1 | mysql -u root --password=xxx -h localhost my_db1

But it's locking each table as it runs.

1
  • Another late solution: You could also use Percona XtraBackup to dump your production database without interruption regarding transaction processing. It allows to do a hot backup, i.e. it doesn't affect current activites. See here: percona.com/software/mysql-database/percona-xtrabackup (I have no affiliation in any way with Percona.)
    – delx
    Aug 7, 2016 at 13:05

14 Answers 14

725

Does the --lock-tables=false option work?

According to the man page, if you are dumping InnoDB tables you can use the --single-transaction option:

--lock-tables, -l

Lock all tables before dumping them. The tables are locked with READ
LOCAL to allow concurrent inserts in the case of MyISAM tables. For
transactional tables such as InnoDB and BDB, --single-transaction is
a much better option, because it does not need to lock the tables at
all.

For innodb DB:

mysqldump --single-transaction=TRUE -u username -p DB
5
  • 28
    for innodb DB mysqldump --single-transaction=TRUE -u username -p DB
    – user1642018
    Oct 11, 2015 at 7:13
  • 23
    What if you have innodb and myisam? Dec 13, 2015 at 14:06
  • Is this on by default? Dec 13, 2015 at 14:11
  • obviously on (ie. locked) ?
    – evandrix
    Apr 16, 2020 at 17:57
  • I don't think you can pass an option to --lock-tables, so don't think --lock-tables=false would work
    – Ian
    Nov 23, 2020 at 16:03
308

This is ages too late, but good for anyone that is searching the topic. If you're not innoDB, and you're not worried about locking while you dump simply use the option:

--lock-tables=false
7
  • 1
    Thanks for the response Warren, this was very helpful and worked like a charm.
    – Gavin
    Feb 23, 2010 at 20:13
  • 7
    using '--lock-table=false --quick' uses the least server resources Mar 14, 2013 at 1:56
  • 50
    But you should be worried about locking tables. If multiple tables are written to while mysqldump is running (and you use foreign keys), you're dump may be inconsistent. You won't know until you restore it and happen to run JOIN queries on the inconsistent data. It may take a while for the inconsistent data to be discovered because the JOINs are used by your application not Mysql (with MyISAM tables); the restore will work just fine, mysql will not warn you about the inconsistencies. So: MyIsam -> always lock your tables. InnoDB -> use --single-transaction.
    – Costa
    Jun 2, 2013 at 3:47
  • 13
    @Costa I don't think locking tables is even sufficient for MyISAM tables. If mysqldump locks the tables in between queries executed by the application then you wind up with the same inconsistencies. The answer is even simpler: MyISAM -> use InnoDB instead.
    – cdhowie
    Aug 3, 2015 at 14:03
  • @Costa you definitely should be worried about locking tables, but only if you do need a consistent dump. There are some rare cases when you don't. For example, a crude fgrep on database-wide dump (debugging): I bet one don't want users to wait for ~20 minutes to create dump of production database (true story). If the point is to get dump not only ASAP, but also CONSISTENT, one should dump replicated slave or use lower-level snapshotting (lvm, zfs, btrfs, etc), keeping in mind FLUSH TABLES WITH READ LOCK stuff. Apr 11, 2019 at 10:59
47

The answer varies depending on what storage engine you're using. The ideal scenario is if you're using InnoDB. In that case you can use the --single-transaction flag, which will give you a coherent snapshot of the database at the time that the dump begins.

45

--skip-add-locks helped for me

6
  • 4
    or also --compact to include skip locks with other optimizations.
    – ppostma1
    Nov 20, 2013 at 18:43
  • 94
    This removes LOCK TABLES and UNLOCK TABLES statements from the dump file, it does not affect the locking during export.
    – dabest1
    Jan 30, 2015 at 1:34
  • 16
    No, it's not what you're looking for! See dabest1's comment. This does NOTHING to keep your tables from getting locked while doing a mysqldump. This is NOT an answer to the question.
    – orrd
    Jul 28, 2016 at 23:22
  • 3
    @dabest and @orrd are correct: --skip-add-locks would just make the dump restore faster. This is not a correct answer.
    – dr_
    Nov 4, 2016 at 10:51
  • Thankfully this is what I needed for my specific use-case. AWS aurora, when dumping data with mysql packaged mysqldump (I'm on driver 5.7.~something) will lock tables on inserts, including log tables. This will raise error You can't use locks with log tables. while re-inserting. I'm only dumping data for a migration so I think this will elegantly solve my issue. Mar 4, 2022 at 19:34
18

To dump large tables, you should combine the --single-transaction option with --quick.

http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_single-transaction

1
  • 5
    Option --quick is part of option --opt now. And --opt is enabled by default. Mar 9, 2018 at 10:48
14

For InnoDB tables use flag --single-transaction

it dumps the consistent state of the database at the time when BEGIN was issued without blocking any applications

MySQL DOCS

http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_single-transaction

1
  • 2
    Maybe along with --skip-lock-tables. Jan 29, 2020 at 18:49
13

This is about as late compared to the guy who said he was late as he was to the original answer, but in my case (MySQL via WAMP on Windows 7), I had to use:

--skip-lock-tables
2
  • This is what worked for me to dump information_schema without having the error "Access denied for user 'debian-sys-maint'@'localhost' to database 'information_schema' when using LOCK TABLES" Jan 27, 2016 at 10:37
  • @miken32 you mean the answer that suggested --skip-add-locks? That's definitely not the same as --skip-lock-tables. Thanks for taking the time to incorrectly point this out 9 years later though. :)
    – dtbarne
    Aug 27, 2021 at 21:44
10

Honestly, I would setup replication for this, as if you don't lock tables you will get inconsistent data out of the dump.

If the dump takes longer time, tables which were already dumped might have changed along with some table which is only about to be dumped.

So either lock the tables or use replication.

4
  • This whole DB is almost entirely read only so I'm not too worried about it changing.
    – Greg
    Sep 19, 2008 at 20:12
  • 2
    This comment is incorrect. MVCC allows for reading consistent state without locks on InnoDB. Mar 20, 2012 at 14:52
  • 6
    If you don't have replication already set up, then you need to do a dump to set it up. Same problem exists. Nov 25, 2013 at 0:58
  • 3
    If you don't have replication already setup, then you will need to lock the tables to do the dump to ensure data integrity though. So it's a catch 22.
    – JordanC
    Aug 6, 2014 at 2:26
6
    mysqldump -uuid -ppwd --skip-opt --single-transaction --max_allowed_packet=1G -q db |   mysql -u root --password=xxx -h localhost db
2
  • Up vote, this one worked for me just add the parameters --skip-opt --single-transaction --max_allowed_packet=1G Oct 18, 2014 at 9:04
  • 2
    I don't recommend "--skip-opt" for this purpose. That does a lot more than what the original question asked for. It turns off quick mode, it doesn't include the charset, etc etc.
    – orrd
    Jul 28, 2016 at 23:30
3

When using MySQL Workbench, at Data Export, click in Advanced Options and uncheck the "lock-tables" options.

enter image description here

1

Due to https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_lock-tables :

Some options, such as --opt (which is enabled by default), automatically enable --lock-tables. If you want to override this, use --skip-lock-tables at the end of the option list.

1

If you use the Percona XtraDB Cluster - I found that adding --skip-add-locks
to the mysqldump command Allows the Percona XtraDB Cluster to run the dump file without an issue about LOCK TABLES commands in the dump file.

0

Another late answer:

If you are trying to make a hot copy of server database (in a linux environment) and the database engine of all tables is MyISAM you should use mysqlhotcopy.

Acordingly to documentation:

It uses FLUSH TABLES, LOCK TABLES, and cp or scp to make a database backup. It is a fast way to make a backup of the database or single tables, but it can be run only on the same machine where the database directories are located. mysqlhotcopy works only for backing up MyISAM and ARCHIVE tables.

The LOCK TABLES time depends of the time the server can copy MySQL files (it doesn't make a dump).

-3

As none of these approaches worked for me, I simply did a:

mysqldump [...] | grep -v "LOCK TABLE" | mysql [...]

It will exclude both LOCK TABLE <x> and UNLOCK TABLES commands.

Note: Hopefully your data doesn't contain that string in it!

1
  • 4
    --skip-add-locks during the dump does that too Feb 16, 2017 at 3:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.