views:

11150

answers:

6

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.

+8  A: 

Does the --no-lock-tables 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.
John Millikin
--no-lock-tables does not seem to work. Instead, as noted below, one could use --lock-tables=false
Brian Phillips
+3  A: 

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.

michal kralik
This whole DB is almost entirely read only so I'm not too worried about it changing.
Greg
+3  A: 

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.

dvorak
+1  A: 

I'm all innoDB. --single-transaction works perfectly, thanks so much!

Greg
+29  A: 

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
Warren Krewenki
Thanks for the response Warren, this was very helpful and worked like a charm.
Robotsu
A: 

The different between "--single-transaction" and "--lock-tables" when using innodb tables?If you know,mail to me:[email protected],thanks.I come from China.

Beni