設置從庫的master信息change master to master_host='172.16.18.114',master_user='user',master_password='pass';
3. 將master當前數據全量備份到slavemysqldump --single-transaction --databases 3alogic boss cbvacs elitel_access wollar wxqyh percona --master-data=1 | mysql --host=172.16.18.165
#--single-transaction參數會將當前事務設置為repeat-read
#--master-data會在備份文件中增加change master master_log_file=XXX,master_log_pos=xxx
or
mysqldump --single-transaction --databases 3alogic boss cbvacs elitel_access wollar wxqyh percona --master-data=1 > /tmp/114.sql
mysql --host=172.16.18.165 < /tmp/114.sql
4. start slave
在從庫mysql上start slavestart slave;
show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.18.114
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 14950195
Relay_Log_File: localhost-relay-bin.000003
Relay_Log_Pos: 26245
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: 3alogic,boss,elitel_access,cbvacs,wollar,wxqyh,percona
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 14950195
Relay_Log_Space: 26405
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.01 sec)
說明slave運行正常,可以在master上show master status,對比下Master_Log_Pos
5. 監控slave延遲
借助percona tookit工具pt-heartbeat來檢查slave落后master時間
該工具是在master運行
pt-heartbeat -D 3alogic --update --user=user --password=pass -h172.16.18.114 --create-table --interval=1 --daemonize
#上面這條命令會在master的3alogic庫下建立一個heartbeat的表,並每1S更新一次該表數據
#可以加參數--interval=60來指定更新頻率,該值越小監控延時情況越精確
pt-heartbeat -D 3alogic --monitor --user=user --password=user -h172.16.18.165 --interval=1
0.01s [ 0.77s, 0.15s, 0.05s ]
0.01s [ 0.77s, 0.15s, 0.05s ]
0.01s [ 0.77s, 0.15s, 0.05s ]
0.01s [ 0.77s, 0.15s, 0.05s ]
0.01s [ 0.77s, 0.15s, 0.05s ]
0.01s [ 0.77s, 0.15s, 0.05s ]
0.01s [ 0.77s, 0.15s, 0.05s ]
0.01s [ 0.77s, 0.15s, 0.05s ]
0.01s [ 0.77s, 0.15s, 0.05s ]
0.01s [ 0.77s, 0.15s, 0.05s ]
0.01s [ 0.77s, 0.15s, 0.05s ]
#上面這條命令會連接到slave檢查延遲情況
#--interval=1的情況下,延遲應該小於1S
6. 定期檢查主從數據是否一致
借助percona tookit工具pt-table-checksum來檢查主從數據是否一致,用pt-table-sync來同步不一致的數據。該工具是在master運行。
pt-table-checksumpt-table-checksum --nocheck-replication-filters --databases 3alogic --replicate percona.checksums --create-replicate-table --no-check-binlog-format --replicate-check-only
#--nocheck-replication-filters 從庫有Replicate_Do_DB時需要增加該參數
#--no-check-binlog-format 如果binlog_format=ROW需要設置該參數
#--replicate percona.checksums
#--create-replicate-table
#上面兩個參數,第一次運行時需要,會在主庫上建立庫percona和表checksums並同步到slave上,因此slave上Replicate_Do_DB必須含有percona,下次運行時可以不加這兩個參數
#--replicate-check-only 只顯示不同結果,該參數只是從slave的checksums表中獲取獲取不同結果的數據,並不會再次檢查主從數據是否一致,因些使用該參數檢查之前最好先執行一條不帶該選項的檢查命令
pt-table-checksum --nocheck-replication-filters --databases 3alogic --recursion-method=dsn=h=172.16.18.114,D=test,t=dsns
#默認情況下該工具會在master上利用show processlist得到slave連接信息
#上面的命令是將slave信息保存在test庫的dsns表中,表結構如下:
CREATE TABLE `dsns` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parent_id` int(11) DEFAULT NULL,
`dsn` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO dsns (parent_id,dsn) values(1,'h=172.16.18.165,u=user,p=pass,P=3306');
#可以用這種方式來修改slave特殊的端口
pt-table-syncpt-table-sync --print --user=user --password=pass h=localhost,D=3alogic,t=log h=172.16.18.165
#只同步一個表
#這種默認狀態下,工具會直接操作如果備庫更新數據,不安全,如果備庫開啟了log_bin則會出錯,要加上--replicate或--sync-to-master選項
pt-table-sync --print --user=user --password=pass --replicate=percona.checksums --databases=3alogic --tables=log h=localhost h=172.16.18.165
# 只同步一個表
pt-table-sync --print --replicate=percona.checksums --databases=3alogic h=localhost h=172.16.18.165
#同步一個庫
pt-table-sync --execute --user=root --password=cpyf --replicate=percona.checksums h=localhost,D=3alogic,t=log h=172.16.18.116
#--print 是只打印出結果
#--execute 才真正同步
#前一個h是master,后一個h是slave
#--replicate=percona.checksums依賴於percona.checksums的結果
./pt-table-sync --print --sync-to-master --databases=3alogic --tables=log 172.16.18.116
./pt-table-sync --execute --sync-to-master --databases=3alogic --tables=log 172.16.18.116
#這個命令不依賴於percona.checksums