我想将一个实时生产数据库复制到本地开发数据库中。是否有一种方法可以在不锁定生产数据库的情况下做到这一点?
我目前正在使用:
mysqldump -u root --password=xxx -h xxx my_db1 | mysql -u root --password=xxx -h localhost my_db1
但是它在运行时锁定了每个表。
我想将一个实时生产数据库复制到本地开发数据库中。是否有一种方法可以在不锁定生产数据库的情况下做到这一点?
我目前正在使用:
mysqldump -u root --password=xxx -h xxx my_db1 | mysql -u root --password=xxx -h localhost my_db1
但是它在运行时锁定了每个表。
当前回答
mysqldump -uuid -ppwd --skip-opt --single-transaction --max_allowed_packet=1G -q db | mysql -u root --password=xxx -h localhost db
其他回答
对于InnoDB表使用flag——single-transaction
它在BEGIN时转储数据库的一致状态 没有阻止任何申请
MySQL文档
http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_single-transaction
——lock-tables=false选项有效吗?
根据手册页,如果你正在转储InnoDB表,你可以使用——single-transaction选项:
--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.
对于innodb DB:
mysqldump --single-transaction=TRUE -u username -p DB
老实说,我会为此设置复制,因为如果不锁定表,就会从转储中获得不一致的数据。
如果转储需要较长的时间,则已经转储的表可能已经更改,而某些表可能即将被转储。
因此,要么锁定表,要么使用复制。
这和那个说他迟到的人比起来差不多晚了,但在我的情况下(在Windows 7上通过WAMP下载MySQL),我不得不使用:
--skip-lock-tables