题目:
A MySQL database uses all InnoDB tables and is configured as follows:
shell>cat/ etc/ my.cnf
[mysqld]
log-bin
server-id=1
You will be setting up a replication slave by using mysqldump. You will need a consistent backup
taken from your running production server. The process should have minimal impact to active
database connections.
Which two arguments will you pass to mysqldump to achieve this?
A. --skip-opt
B. --lock-all-tables
C. --create-apply-log
D. --single-transaction
E. --master-data
参数含义:
--skip-opt Disable --opt. Disables --add-drop-table, --add-locks,
--create-options, --quick, --extended-insert,
--lock-tables, --set-charset, and --disable-keys.
执行备份数据库表结构时,指定了 –skip-opt 选项,相当于:
--add-drop-table, --add-locks,
--create-options, --quick, --extended-insert,
--lock-tables, --set-charset, and --disable-keys
-x, --lock-all-tables
Locks all tables across all databases. This is achieved
by taking a global read lock for the duration of the
whole dump. Automatically turns --single-transaction and
--lock-tables off.
此参数使用时保证整个数据库【所有schema】的数据具有一致性快照。
在mysqldump导出的整个过程中以read方式锁住所有表(锁表方式类似 flush tables with read lock 的全局锁)。
--single-transaction
Creates a consistent snapshot by dumping all tables in a
single transaction. Works ONLY for tables stored in
storage engines which support multiversioning (currently
only InnoDB does); the dump is NOT guaranteed to be
consistent for other storage engines. While a
--single-transaction dump is in process, to ensure a
valid dump file (correct table contents and binary log
position), no other connection should use the following
statements: ALTER TABLE, DROP TABLE, RENAME TABLE,
TRUNCATE TABLE, as consistent snapshot is not isolated
from them. Option automatically turns off --lock-tables.
该参数通过在一个事务中导出所有表从而创建一个一致性的快照,当前版本的MySQL只可以对innodb 引擎保证一致性,导出过程中不会锁表,其他引擎,如MyISAM 在导出期间会锁表,为保证有效的dump文件,即正确的表内容和二进制日志位置,
在导出的过程中不能有如下操作
- ALTER TABLE
- DROP TABLE
- RENAME TABLE
- TRUNCATE TABLE
如指定了 --lock-tables参数则会自动将其关闭
--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.
这个参数在建立slave数据库的时候会用到,当这个参数的值为1的时候,mysqldump出来的文件就会包括这个语句,后面紧接着就是file和position的记录,file和position记录的位置就是slave从master端复制文件的起始位置;使用此选项转储主复制服务器以生成转储文件,该文件可用于将另一台服务器设置为主复制服务器的从属服务器。它使转储输出包含一个语句,该语句指示转储服务器的二进制日志坐标(文件名和位置)。这些是从服务器开始复制的主服务器坐标。
如果选项值为2,则语句将作为SQL注释编写,因此仅提供信息;当转储文件被重新加载时,它不起作用。如果选项值为1,则该语句在重新加载转储文件时生效。如果未指定选项值,则默认值为1。
根据题目意思:答案选项为D,E
--single-transaction && --master-data
在使用 --single-transaction 参数后,会在FLUSH TABLES WITH READ LOCK 后添加START TRANSACTION 语句,用来开启单一事务 ,这个时候的加锁,仅仅是为了确定master-data中的binlog的具体位置和开启事务,开启事务后,就已经把读锁释放了,而且在由日志可以看出,在日志的回滚过程中,是回滚到单一的TRANSACTION,也是sp点,每次进行对表和参数的改动后,都会对事务进行回滚。 通过对备份log的分析,可以发现,所有的的备份阶段完成后,都是rollback到sp点的。也就是返回到SAVEPOINT sp时间点,也就是备份完成后,实际上备份库仍然是在sp点,而这个所谓的sp回滚,其实是调用的undo中的数据快照来实现的。
使用--single-transaction 可以保证在备份过程中,整个备份集的数据一致性。
--single-transaction备份对于数据库的影响时间比较长,影响范围比较大 ,但是加入该参数后,对于多张大表的备份速度肯定会加快。
要实现点对点的备份恢复,或者对于某点的数据完整备份,必须使用--single-transaction 和--master-data=2两个参数,比如主从搭建时候的,因为在--single-transaction参数中,没可以保证数据备份的备份时间点的事务一致性。
本文详细介绍了mysqldump的--skip-opt和--single-transaction参数。--skip-opt会导致全局读锁,确保所有数据库的一致性快照;而--single-transaction则在不锁定表的情况下提供一致性快照,适用于InnoDB存储引擎。--master-data选项用于生成用于创建从服务器的复制信息,当值为1或2时,会包含不同的二进制日志信息。在使用--single-transaction时,可以减少对数据库的影响并提高大表备份速度。
561

被折叠的 条评论
为什么被折叠?



