標籤:
簡介:
Mysql 的主從同步功能,這種解決方案是企業很常見的一種。常用於備份資料庫,當用戶端操作主庫時,主庫會產生binlog記錄檔,
從庫通過複製主庫的binlog記錄檔,然後解析成相應的 SQL 陳述式在從庫執行,實現主從一致的效果。
這種解決方案只提供了日誌的同步執行功能,而從庫只能提供讀操作,當主伺服器發生故障時,必須手動處理容錯移轉,一般情況下的做法是將一台從伺服器改為主伺服器。
Master : 192.168.1.88
Slave : 192.168.1.80
一、配置 Master
[[email protected] ~]# vim /etc/my.cnflog-bin = mysql-bin # 開啟 binlog 日誌記錄server-id = 1 # 設定 id 號,此 識別碼全域唯一[[email protected] ~]# service mysqld start[[email protected] ~]# netstat -anpt | grep 3306 # 檢測 Mysql 是否啟動成功[[email protected] ~]# chkconfig --add mysqld[[email protected] ~]# chkconfig --level 35 mysqld on[[email protected] ~]# iptables -I INPUT -s 192.168.1.80 -p tcp --dport 3306 -j ACCEPT[[email protected] ~]# service iptables save
二、配置 Slave
[[email protected] ~]# vim /etc/my.cnfserver-id = 2 # 設定 id 號,此 識別碼全域唯一relay-log = mysql-relay-bin # 指定 relay-log 記錄檔的命名格式replicate-wild-ignore-table = mysql.% # 過濾不需要複製的表replicate-wild-ignore-table = test.%replicate-wild-ignore-table = information_schema.%replicate-wild-ignore-table = performance_schema.%[[email protected] ~]# service mysqld start[[email protected] ~]# chkconfig --add mysqld[[email protected] ~]# chkconfig --level 35 mysqld on[[email protected] ~]# iptables -I INPUT -s 192.168.1.88 -p tcp --dport 3306 -j ACCEPT[[email protected] ~]# service iptables save
三、小插曲
四、Master 建立複製使用者並授權
[[email protected] ~]# mysql -u root -p123456mysql> grant replication slave on *.* to ‘rep1_user‘@‘192.168.1.80‘ identified by ‘rep1_passwd‘;mysql> show master status;+------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000008 | 324 | | |+------------------+----------+--------------+------------------+1 row in set (0.00 sec)
五、Slave 添加 Master 資訊
[[email protected] ~]# mysql -u root -p123456mysql> change master to -> master_host=‘192.168.1.88‘,-> master_user=‘rep1_user‘,-> master_password=‘rep1_passwd‘,-> master_log_file=‘mysql-bin.000008‘,-> master_log_pos=324;mysql> start slave;mysql> show slave status\G*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.1.88Master_User: rep1_userMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000008Read_Master_Log_Pos: 324Relay_Log_File: mysql-relay-bin.000002Relay_Log_Pos: 267Relay_Master_Log_File: mysql-bin.000008Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table: mysql.%,test.%,information_schema.%,performance_schema.%Last_Errno: 0Last_Error:Skip_Counter: 0Exec_Master_Log_Pos: 324Relay_Log_Space: 430Until_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: 0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id: 1Master_UUID: b5e20203-86b5-11e4-b69c-000c29d099faMaster_Info_File: /usr/local/mysql/data/master.infoSQL_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:1 row in set (0.00 sec)
## 重點關註:Slave_IO_Running 跟 Slave_SQL_Running 這兩個主從複製線程,正常情況下這兩項都為 YES !
## 還需要關註:Slave_IO_State、Master_Host、Master_Log_File、Read_Master_Log_Pos、Relay_Log_File、Relay_Log_Pos、Relay_Master_Log_File
## Replicate_Wild_Ignore_Table 這項可以看到同步過程中過濾了哪些庫(表)。
附:
三、小插曲
## 在做主從同步前,主庫已經有資料時,需要做以下處理。
mysql> flush tables with read lock; # 主庫鎖表操作,讓資料庫只能讀,不能寫
## 保留此終端,重新開啟一個 tty ,如果關閉此終端則鎖表失效。
[[email protected] ~]# cd /usr/local/[[email protected] local]# tar zcf mysql.tar.gz mysql # 備份整個資料庫[[email protected] local]# rsync -a mysql.tar.gz 192.168.1.80:/usr/local/ # 將資料傳到從庫[[email protected] local]# service mysqld stop # 關閉從庫的 Mysql[[email protected] local]# mv mysql old.mysql # 將原有 mysql 目錄改名[[email protected] local]# tar zxf mysql.tar.gz # 解壓出主庫的 mysql 目錄[[email protected] local]# service mysqld restart # 重啟主庫 Mysql 服務[[email protected] local]# service mysqld start # 啟動從庫 Mysql 服務
Mysql Replication 主從同步