標籤:
MYSQL主從複製:
1,主從伺服器都啟用二進位日誌,設定唯一ID,並相互建立串連的帳號並授權;
2,從伺服器主動向主伺服器請求查詢當前同步狀態,並拉回資料;
主要相關命令:
查看主伺服器狀態:show master status\G;查看從伺服器狀態:show slave status\G;查看mysql I/O線程:show processlist\G;配置從伺服器複製:change master to {***}詳見後面說明啟動從伺服器複製:start slave;授權使用者:grant 許可權 on 資料庫.* to 使用者名稱@‘登入主機‘ identified by "密碼";重新整理系統許可權表:flush privileges;鎖定資料庫:flush tables with read lock;解除鎖定庫:unlock tables;
相關設定主伺服器:
#vi /etc/my.cnf[mysqld]log-bin=mysql-bin //[必須]啟用二進位日誌server-id=222 //[必須]伺服器唯一ID,預設是1,一般取IP最後一段binlog-do-db=wordpress //表示只備份wordpressbinlog_ignore_db=mysql //表示忽略備份mysql不加binlog-do-db和binlog_ignore_db,那就表示備份全部資料庫。mysql>GRANT REPLICATION SLAVE ON *.* to ‘copy_user‘@‘%‘ identified by ‘copy_password‘; --這裡要注意:REPLICATION SLAVE,指主伺服器複製許可權mysql>show processlist; --查看已串連的從伺服器
從伺服器:
#vi /etc/my.cnf[mysqld]log-bin=mysql-bin //[不是必須] 啟用二進位日誌 ,當從伺服器是其他伺服器的主伺服器時,必須要設定server-id=111 //[必須] 伺服器唯一ID,預設是1,一般取IP最後一段slave-skip-errors = 1062 //[不是必須]忽略重複索引值錯誤replicate-do-db=testreplicate-ignore-db=mysqlmysql>GRANT REPLICATION CLIENT ON *.* to ‘copy_user‘@‘%‘ identified by ‘copy_password‘; --這裡要注意:REPLICATION CLIENT指從伺服器複製許可權mysql>change master to --配置串連到哪個主伺服器 ->master_host=‘192.168.2.12‘, --主伺服器IP地址 ->master_user=‘copy_user‘, --主伺服器中配置的使用者名稱 ->master_password=‘copy_password‘, --主伺服器中的密碼 ->master_log_file=‘mysql-bin.000039‘, --就是當前主伺服器記錄檔名,就是從這個檔案開始複製,不大於前邊的File值。 ->master_log_pos=32176; --從上面記錄檔中的相應位置開始,前邊的Position值 ,mysql>start slave; --啟動從伺服器複製功能]mysql>stop slave;mysql>SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1; --忽略1步錯誤
mysql>stop slave;mysql>change master to master_host=‘192.168.1.21‘,master_user=‘copy_user‘, master_password=‘copy_password‘,master_log_file=‘mysql-bin.000031‘, master_log_pos=108506;mysql>start slave;
查看主伺服器狀態,在主伺服器上運行:
mysql> show master status\G;*************************** 1. row *************************** File: mysql-bin.000008 Position: 154 Binlog_Do_DB: Binlog_Ignore_DB: mysql,sys,information_schema,performance_schemaExecuted_Gtid_Set: 1 row in set (0.00 sec)ERROR: No query specifiedmysql>
查看從伺服器當前複製狀態,在從伺服器上運行:
mysql> show slave status\G;*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.2.12 //主伺服器地址Master_User: copy_king //授權帳戶名稱Master_Port: 3306 //資料庫連接埠Connect_Retry: 60 //當串連錯誤,間隔多長時間重試Master_Log_File: mysql-bin.000038 //當前主伺服器的最大日誌IDRead_Master_Log_Pos: 95990 //當前主伺服器進行到的日誌位置,這與主伺服器中的值相同Relay_Log_File: kq126-relay-bin.000053 //自己的日誌位置Relay_Log_Pos: 96153 //同步讀取二進位日誌的位置,大於等於Exec_Master_Log_PosRelay_Master_Log_File: mysql-bin.000038 //已同步到主伺服器的位置Slave_IO_Running: Yes //此狀態必須YES,串連到主庫是否正在運行Slave_SQL_Running: Yes //此狀態必須YES, 本庫內寫入狀態是否運行,這兩個都必須是YES,只要有一個是NO,就是出問題了Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 95990 //Relay_Log_Space: 96489Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 從伺服器資料比主伺服器延遲多少秒,一般是0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 9Master_UUID: 7d911e39-3a98-11e5-8b9e-000c29168ea6Master_Info_File: /usr/local/mysql/var/master.info --當前配置主庫的檔案SQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update itMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 01 row in set (0.00 sec)ERROR: No query specified
查看日誌,在linux中:
/usr/local/mysql/bin/mysqlbinlog /usr/local/mysql/var/mysql-bin.000050
通過日誌還原:
通過時間還原:
–start-datetime=”還原資料的起始時間”
–stop-datetime=”還原資料的結束時間”
mysqlbinlog –start-datetime=“時間” 記錄檔路徑 | mysql -u使用者 -p密碼 –從規定的起始時間還原到現在
mysqlbinlog –stop-datetime=“時間” 記錄檔路徑 | mysql -u使用者 -p密碼 –從最開始還原到規定的結束時間
mysqlbinlog –start-datetime=“時間” –stop-datetime=“時間” 記錄檔路徑 | mysql -u使用者 -p密碼 –指定時間段
[root@localhost bin]# /usr/local/mysql/bin/mysqlbinlog --stop-datetime=‘2015-10-19 17:30:0‘ /usr/local/mysql/var/mysql-bin.000001 | mysql -uroot -proot
通過位置還原:
–start-position=”還原資料的起始位置”
–stop-position=”還原資料的結束位置”
mysqlbinlog –start-position=“位置” 記錄檔路徑 | mysql -u使用者 -p密碼 –從規定的起始位置還原到現在
mysqlbinlog –stop-position=“位置” 記錄檔路徑 | mysql -u使用者 -p密碼 –從最開始還原到規定的結束位置
mysqlbinlog –start-position=“位置” –stop-position=“位置” 記錄檔路徑 | mysql -u使用者 -p密碼 –指定位置段
MYSQL資料同步、複製、分發