te that if you dump many databases at once (using the option --databases= or
? ? ? ? --all-databases), the logs will be flushed for each database dumped. The exception is when using --lock-all-tables or
? ? ? ? --master-data: in this case the logs will be flushed only once, corresponding to the moment all tables are locked. So if you want your
? ? ? dump and the log flush to happen at the same exact moment you should use --lock-all-tables or --master-data with --flush-logs.
? ? ? 为了获得导出数据和刷新日志的一致性(同时发生),必须将 --flush-logs 选项和 --lock-all-tables 或者 --master-data 以前使用:
? ? ? mysqldump --flush-logs --lock-all-tables;? mysqldump --flush-logs --master-data=2 ;
5)--flush-privileges?
? ? ? Emit a FLUSH PRIVILEGES statement after dumping the mysql database.? This option should be used any time the dump contains the
? ? ? mysql database and any other database that depends on the data in the mysql database for proper restore.
? ? ? 如何导出包含了mysql数据,就应该启用该选项。该选项会在导出的 mysql 数据库的后面加上 flush privileges 语句,因为在向mysql数据库inert了语句
? ? ? 之后,必须使用 flush privileges,不然权限不生效。下面是例子:

6)? --master-data[=#]?
? ? ? This causes the binary log position and filename to be appended to the output. If equal to 1, will print it as a CHANGE MASTER
? ? ? command; if equal to 2, that command will be prefixed with a comment symbol. This option will turn --lock-all-tables on, unless
? ? ? --single-transaction is specified too (in which case a global read lock is only taken a short time at the beginning of the dump; don't
? ? ? forget to read about --single-transaction below). In all cases, any action on logs will happen at the exact moment of the dump.
? ? ? Option automatically turns --lock-tables off.
所以为了获得一致性的备份数据和在备份是同时刷新binary日志,我们应该如下结合使用这些选项(完美组合):
mysqldump -uxxx -pxxx --single-transaction --master-data=2 --flush-logs --routines --databases db1 > db1.sql;
(其中 --flush-logs 不是必须的; 搭建slave时,不要导出events,但是需要导出rountines.)
其中被?--master-data 打开的 --lock-all-tables 选项,又被 --single-transaction 关闭掉了。--flush-logs 借助于 --master-data 可以达到即使一次导出多个数据库时,其 flush 的二进制日志也是在同一个时间点的,不是每一个数据库flush一次的。并且这个时间点 和 --master-data 记录的 binary log position 和 binary log file是同一个时间点,这些都是利用了 --single-transaction 和 --master-data 合用时短暂的使用一个全局的读锁来达到目的的。
5. mysqldump 复制 的相关选项
1)? --master-data[=#]?
? ? ? This causes the binary log position and filename to be appended to the output. If equal to 1, will print it as a CHANGE MASTER
? ? ? command; if equal to 2, that command will be prefixed with a comment symbol. This option will turn --lock-all-tables on, unless
? ? ? --single-transaction is specified too (in which case a global read lock is only taken a short time at the beginning of the dump;
? ? ? don't forget to read about --single-transaction below). In all cases, any action on logs will happen at the exact moment of the dump.
? ? ? Option automatically turns --lock-tables off.
? ? ? 该选项,上面已经介绍了。--master-data=1 表示会导出 change master to 语句,--master-data=2 该语句放在注释中,默认是为 0 。
? ? ? 一般会和 --single-transaction一起使用,用于搭建master-slave环境。
下面是 --master-data=1 和 --master-data=2 的比较:


在导出文件的前30行左右可以看到 change master to 语句。可以使用 head -n 30 db1.sql 查看
2)? --dump-slave[=#]? ?
? ? ? This causes the binary log position and filename of the master to be appended to the dumped data output. Setting the value to 1, will
? ? ? printit as a CHANGE MASTER command in the dumped data output; if equal to 2, that command will be prefixed with a comment
? ? ? symbol. This option will turn --lock-all-tables on, unless --single-transaction is specified too (in which case a global read lock is only
? ? ? tak