我有一个名为 db.sql
的文件;该文件包含一个数据库的数据。我想将此数据导入到名为 db
的数据库中在新建立的主/副本结构中。
我用过mysql -u USER -p'PASS' db < db.sql
命令将数据库导入主服务器。导入后,日期被插入到 master
服务器成功但未插入replica
服务器。 (但是仍然可以正常工作)
我检查了db.sql
的内容文件,其中包括:
SET @MYSQLDUMP_TEMP_LOG_BIN = @@SESSION.SQL_LOG_BIN;
SET @@SESSION.SQL_LOG_BIN= 0;
SET @@GLOBAL.GTID_PURGED='server-1:1-376,
server-2:1-2086154';
#end of the file
SET @@SESSION.SQL_LOG_BIN = @MYSQLDUMP_TEMP_LOG_BIN;
我知道原因是禁用 binlog
,但我有三个问题:
- 我有很多这样导出的备份文件;如何将它们导入到我当前的
master
中服务器,然后它会转到replica
服务器。 (我想如果我删除这些行,它会解决我的问题,但是有没有办法直接通过 MySQL 来完成它?) - 对于新导出的备份,如何通过可以直接导入的方式来防止此问题?下面的脚本生成
db.sql
文件。
MYSQL_CONN="-u${MYSQL_USER} -p${MYSQL_PASS}"
mysql ${MYSQL_CONN} -ANe"STOP SLAVE" 2> /dev/null
mysqldump ${MYSQL_CONN} --single-transaction --quick ${DB_NAME} 2> /dev/null > ${OUTPUT_PATH}/db.sql
mysql ${MYSQL_CONN} -ANe"START SLAVE" 2> /dev/null
- 备份文件来自不同的主/副本,具有不同的
GLOBAL.GTID_PURGED
;这会引起任何问题吗?如果有,如何解决?
最佳答案
https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html说:
If you do not set the
--set-gtid-purged
option, the default is that aSET @@GLOBAL.gtid_purged
statement is included in the dump output if GTIDs are enabled on the server you are backing up, and the set of GTIDs in the global value of thegtid_executed
system variable is not empty. A SET@@SESSION.sql_log_bin=0
statement is also included if GTIDs are enabled on the server.
这与您在转储输出中看到的内容相符。
稍后,同一页面显示:
The possible values for the --set-gtid-purged option are as follows:
...
OFF
SET @@GLOBAL.gtid_purged
is not added to the output, and SET@@SESSION.sql_log_bin=0
is not added to the output. For a server where GTIDs are not in use, use this option orAUTO
. Only use this option for a server where GTIDs are in use if you are sure that the required GTID set is already present ingtid_purged
on the target server and should not be changed, or if you plan to identify and add any missing GTIDs manually.
我建议在运行mysqldump
时设置--set-gtid-purged=OFF
。
关于mysql - 如何将数据导入MySQL主/副本结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75970834/