MySQL 5.6.10跨平臺(tái)GTID復(fù)制實(shí)踐
在官方文檔中提到,最保險(xiǎn)可靠的復(fù)制方式,是基于row的復(fù)制,所以寧可犧牲一些性能也要保證數(shù)據(jù)的安全。
現(xiàn)實(shí)環(huán)境中,master主數(shù)據(jù)庫(kù)MySQL 5.6.10(msi安裝方式)安裝在Windows 2008 Server x64上,slave從服務(wù)器是一臺(tái)老舊的DELL服務(wù)器,運(yùn)行CentOS 6.4 x64系統(tǒng),源碼編譯安裝MySQL 5.6.10的Linux版本,安裝過(guò)程可以參考我以前的博文:http://www.cnblogs.com/jlzhou/archive/2013/03/09/2951544.html
不同平臺(tái)下,MySQL是有一些差異的,要小心處理。
第一個(gè)問(wèn)題是,Windows平臺(tái)下,文件名大小寫不敏感,造成對(duì)應(yīng)的MySQL的數(shù)據(jù)表名稱默認(rèn)都采用小寫字母方式,同時(shí)大小不寫敏感,參考我以前的博文:http://www.cnblogs.com/jlzhou/archive/2013/03/18/2966106.html 為了能將數(shù)據(jù)同步復(fù)制到Linux平臺(tái)的MySQL,我們需要設(shè)置Linux平臺(tái)下MySQL的數(shù)據(jù)表名稱設(shè)置:(修改my.cnf文件)
- [mysqld]
- lower_case_table_names=1
第二個(gè)問(wèn)題是,自增字段0值的問(wèn)題。因?yàn)楝F(xiàn)有數(shù)據(jù)庫(kù)是MSSQL,業(yè)務(wù)邏輯需要某些表的自增字段從0開始。參考我以前的博文:http://www.cnblogs.com/jlzhou/archive/2013/03/18/2965384.html 為了在Windows平臺(tái)和Linux平臺(tái)的MySQL之間復(fù)制數(shù)據(jù),增加全局變量設(shè)置,在my.ini和my.cnf中分別添加NO_AUTO_VALUE_ON_ZERO設(shè)置到sql-mode行:
- //my.ini 該文件默認(rèn)在Windows7或Windows2008操作系統(tǒng)中位于 C:\ProgramData\MySQL\MySQL Server 5.6 目錄下(采用MSI安裝方式),如果你自定義了數(shù)據(jù)目錄,則該配置文件在數(shù)據(jù)目錄下。
- # Set the SQL mode to strict
- sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_AUTO_VALUE_ON_ZERO"
現(xiàn)在開始配置GTID復(fù)制,先配置master端的my.ini文件,加入下述配置,然后重啟master的MySQL服務(wù):
- binlog-format=ROW
- log-bin=master-bin.log
- log-bin-index=master-bin.index
- log-slave-updates=true
- gtid-mode=on
- enforce-gtid-consistency=true
- master-info-repository=TABLE
- relay-log-info-repository=TABLE
- sync-master-info=1
- slave-parallel-workers=2
- binlog-checksum=CRC32
- master-verify-checksum=1
- slave-sql-verify-checksum=1
- binlog-rows-query-log-events=1
- server-id=1
- sync_binlog=1
再修改slave端的my.cnf文件,加入下述配置,然后重啟slave的MySQL服務(wù):
- binlog-format=ROW
- log-bin=slave-bin.log
- log-bin-index=slave-bin.index
- log-slave-updates=true
- gtid-mode=on
- enforce-gtid-consistency=true
- master-info-repository=TABLE
- relay-log-info-repository=TABLE
- sync-master-info=1
- slave-parallel-workers=2
- binlog-checksum=CRC32
- master-verify-checksum=1
- slave-sql-verify-checksum=1
- binlog-rows-query-log-events=1
- server-id=2
- sync_binlog=1
其實(shí),并不需要在slave端啟用binlog,但是為了在master故障時(shí),方便的轉(zhuǎn)換slave到master,并且方便建立slave的slave,所以采用和主服務(wù)器類似的配置。
復(fù)制設(shè)置會(huì)將用于復(fù)制的用戶和密碼以明文形式保存在master.info文件中,最好為復(fù)制建立專用的用戶,授予 REPLICATION SLAVE 權(quán)限。
在master端執(zhí)行:
GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.1.101' IDENTIFIED BY '12345678';
最后,在slave執(zhí)行指向master的命令,并開啟slave復(fù)制。
CHANGE MASTER TO MASTER_HOST='192.168.1.100', MASTER_PORT=3306, MASTER_USER='repluser',MASTER_PASSWORD='12345678', master_auto_position=1; START slave;
這時(shí)就可以測(cè)試在master上建立數(shù)據(jù)庫(kù),建表,然后監(jiān)控slave的復(fù)制狀態(tài)了。
本文中沒(méi)有涉及到已有數(shù)據(jù)庫(kù)在master上的情況,請(qǐng)參考這篇博文:http://www.zhaokunyao.com/archives/4131
后記:
附上備份和恢復(fù)腳本:命令行執(zhí)行
- Backup from remote server scripts:
- // for test database:
- "C:\MySQL\MySQL Server 5.6\bin\mysqldump.exe" --user=root --max_allowed_packet=1G --host=10.192.8.105 --port=3306 --default-character-set=utf8 --set-gtid-purged=OFF --password --databases test > "C:\\Backup\\test.dump.sql"
- Restore to local dev machine scripts:
- // for test database:
- "C:\MySQL\MySQL Server 5.6\bin\mysql.exe" --host=localhost --user=root --port=3306 --password --default-character-set=utf8 --comments < "C:\\Backup\\test.dump.sql"
注意,上述腳本中,備份的部分要加入--set-gtid-purged=OFF參數(shù),防止在備份出的sql腳本中生成 SET @@global.gtid_purged 語(yǔ)句:
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.
官方文檔關(guān)于set-gtid-purged是這樣寫的:
This option enables control over global transaction ID (GTID) information written to the dump file, by indicating whether to add a SET @@global.gtid_purged statement to the output.
原文鏈接:http://www.cnblogs.com/jlzhou/archive/2013/03/22/2975913.html
【編輯推薦】