mysqldump一次备份多个库
一个数据库实例中有20+个库,本次备份需要备份其中的10+个库,使用mysqldump直接进行备份踩到一个warning和一个error。
数据库版本:5.7.26
mysqldump -hrm-2ze04c849v9m32bzj.mysql.rds.aliyuncs.com -uadminroot -p'BDm4w%qqGf3Zx!J5' --single-transaction --quick --databases db1 db2 db3 db4 ... > /tmp/test.sql
Warning:
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events.
mysqldump: Couldn't execute 'SELECT COLUMN_NAME, JSON_EXTRACT(HISTOGRAM, '$."number-of-buckets-specified"') FROM information_schema.COLUMN_STATISTICS WHERE SCHEMA_NAME = 'activity' AND TABLE_NAME = 'scores';': Unknown table 'column_statistics' in information_schema (1109)
原因:
关于GTID是5.6以后,加入了全局事务 ID (GTID) 来强化数据库的主备一致性,故障恢复,以及容错能力。 官方给的:A global transaction identifier (GTID) is a unique identifier created and associated with each transaction committed on the server of origin (master). 所以可能是因为在一个数据库里面唯一,但是当导入其他的库就有可能重复。所有会有一个提醒。
可以通过添加--set-gtid-purged=off 或者–gtid-mode=OFF这两个参数设置。
mysqldump -hrm-2ze04c849v9m32bzj.mysql.rds.aliyuncs.com -uadminroot -p'BDm4w%qqGf3Zx!J5' --set-gtid-purged=OFF --single-transaction --quick --databases db1 db2 db3 db4 ... > /tmp/test.sql
报错:
mysqldump: Couldn't execute 'SELECT COLUMN_NAME, JSON_EXTRACT(HISTOGRAM, '$."number-of-buckets-specified"') FROM information_schema.COLUMN_STATISTICS WHERE SCHEMA_NAME = 'activity' AND TABLE_NAME = 'scores';': Unknown table 'column_statistics' in information_schema (1109)
原因:
新版的mysqldump默认启用了一个新标志,通过- -column-statistics=0
来禁用他。
mysqldump -hrm-2ze04c849v9m32bzj.mysql.rds.aliyuncs.com -uadminroot -p'BDm4w%qqGf3Zx!J5' --column-statistics=0 --set-gtid-purged=OFF --single-transaction --quick --databases db1 db2 db3 db4 ... > /tmp/test.sql
成功!