Mysql主從複製(GTID),一主兩從
複製原理:
Mysql中有一種日誌叫做bin日誌(二進位制日誌)。這個日誌會記錄下所有修改了資料庫的SQL語句(insert,update,delete,ALTER TABLE,grant等等)。 主從複製的原理其實就是把主伺服器上的BIN日誌複製到從伺服器上執行一遍,這樣從伺服器上的資料就和主伺服器上的資料相同了。
MySQL 支援單向、非同步複製,複製過程中一個伺服器充當主伺服器,而一個或多個其它伺服器充當從伺服器。主伺服器將更新寫入二進位制日誌檔案,並維護檔案的一個索引 以跟蹤日誌迴圈。這些日誌可以記錄傳送到從伺服器的更新。當一個從伺服器連線主伺服器時,它通知主伺服器從伺服器在日誌中讀取的最後一次成功更新的位置。 從伺服器接收從那時起發生的任何更新,然後封 鎖並等待主伺服器通知新的更新。
複製過程:
- 主節點必須啟用二進位制日誌,記錄任何修改資料庫資料的事件。
- 從節點開啟一個執行緒(I/O Thread)把自己扮演成mysql的客戶端,通過mysql協議,請求主節點的二進位制日誌檔案中的事件
- 主節點啟動一個執行緒(dump Thread),檢查自己二進位制日誌中的事件,跟對方請求的位置對比,如果不帶請求位置引數,則主節點就會從第一個日誌檔案中的第一個事件一個一個傳送給從節點。
- 從節點接收到主節點發送過來的資料把它放置到中繼日誌(Relay log)檔案中。並記錄該次請求到主節點的具哪個二進位制日誌檔案的哪個位置。
- 從節點啟動另外一個執行緒(sql Thread ),把replaylog中的事件讀取出來,並在本地再執行一次。
複製特點:
- 非同步複製:主節點中一個使用者請求一個寫操作時,主接點不需要把寫的資料在本地操作完成同時傳送給從伺服器並等待從伺服器反饋寫入完成,在響應用 戶,主機點只需要把寫入操作在本地完成,就響應使用者。但是,從節點中的資料有可能會落後主服務,可以使用(很多軟體來檢查是否落後)
- 主從資料不一致。
單向複製有利於健壯性、速度和系統管理: 1. 主伺服器/從伺服器設定增加了健壯性。主伺服器出現問題時,你可以切換到從伺服器作為備份, 2. 通過在主伺服器和從伺服器之間切分處理客戶查詢的負荷,可以得到更好的客戶響應時間。SELECT 查詢可以傳送到從伺服器以降低主伺服器的查詢處理負荷。但修改資料的語句仍然應傳送到主伺服器,以便主伺服器和從伺服器保持同步。如果非更新查詢為主,該負載均衡策略很有效,但一般是更新查詢。 3. 使用複製的另一個好處是可以使用一個從伺服器執行備份,而不會干擾主伺服器。在備份過程中主伺服器可以繼續處理更新。
注意:當你進行復制時,所有對複製中的表的更新必須在主伺服器上進行。否則,你必須要小心,以避免使用者對主伺服器上的表進行的更新與對從伺服器上的表所進行的更新之間的衝突!
一.主從複製配置過程
主節點:172.25.0.121(master)
從節點:172.25.0.122(slave)
所需的安裝包:
mysql-community-libs-5.7.17-1.el6.x86_64.rpm mysql-community-libs-compat-5.7.17-1.el6.x86_64.rpm
mysql-community-server-5.7.17-1.el6.x86_64.rpm
mysql-community-common-5.7.17-1.el6.x86_64.rpm
mysql-community-client-5.7.17-1.el6.x86_64.rpm
master上:
1.安裝mysql
[[email protected] ~]# yum install * -y
[[email protected] ~]# /etc/init.d/mysqld start #開啟資料庫
[[email protected] ~]# grep password /var/log/mysqld.log ##檢視初始密碼
[[email protected] ~]# mysql_secure_installation # 修改密碼
Enter password for user root: #輸入初始化密碼
New password: #輸入新密碼(字母大小寫,數字,字元,大於8位!)
Re-enter new password:
Change the password for root ? ((Press y|Y for Yes, any other key for No) :
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y #刪除匿名
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y #不允許root遠端登陸
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y #測試資料庫並訪問
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y #重新載入資料表
2.修改主配置檔案
[[email protected] ~]# vim /etc/my.cnf
[[email protected] ~]# /etc/init.d/mysqld restart
server-id=5 #伺服器Id
log-bin=mysql-bin ##開啟二進位制日誌
3.配置資料庫
[[email protected] ~]# mysql -p
Enter password: #輸入設定的新密碼
mysql> grant replication slave on *.* to [email protected]'172.25.0.%' identified by 'Westos+123'; #建立使用者並授權
mysql> show master status; #檢視主庫master的狀態
slave上:
1.安裝mysql並設定密碼
[[email protected] ~]# yum install *.rpm -y
[[email protected] ~]# /etc/init.d/mysqld start
[[email protected] ~]# grep password /var/log/mysqld.log
[[email protected] ~]# mysql_secure_installation
2.修改主配置檔案
[[email protected] ~]# vim /etc/my.cnf
3.配置資料庫
[[email protected] ~]# mysql -p
Enter password: #輸入新密碼
mysql>change master to master_host='172.25.0.121',master_user='user',master_password='Westos+123',master_log_file='mysql-bin.000001',master_log_pos=446; #從庫上建立使用者並授權
mysql> start slave ; #開啟從庫
mysql> show slave status\G; #檢視從庫狀態
當看到Slave_IO_Running: Yes以及Slave_SQL_Running: Yes,則表示slave庫已經正常運行了 當出現Slave_IO_Running: Connecting的提示時,說明主庫和從庫沒有連線上,有以下三點原因: 1.網路問題:檢查網路連線是否能夠連線上 2.密碼或POS號錯誤:檢視pos號和主庫的號是否對應 3.防火牆的問題:檢視主庫防火牆的策略,資料庫是否拒絕外來連線,然後做相應的改動
主從測試:
(1)主庫:
mysql> show databases;
mysql> create database westos;
mysql> use westos;
mysql> create table usertable( -> username varchar(11) not null, -> password varchar(11) not null);
mysql> desc userlist;
mysql> insert into usertable values('user1','111');
mysql> insert into usertable values('user2','123');
(2)從庫:
mysql> show databases;
mysql> use westos;
mysql> show tables;
mysql> select * from usertable;
可以在slave從庫上看到在master主庫上建立的庫和表資訊。
GTID主從複製
1.在master主庫和slave從庫修改配置檔案
[[email protected] ~]# vim /etc/my.cnf
gtid_mode=ON enforce-gtid-consistency=true
[[email protected] ~]# /etc/init.d/mysqld restart
2.檢視主庫masterf資料
mysql> show master status;
master_log_file和master_log_pos都會發生改變。
3.配置從資料庫
mysql> stop slave;
mysql> change master to master_host='172.25.0.121',master_user='user',master_password='Westos+123',master_auto_position = 1;
mysql> start slave;
mysql> show slave status\G;
4.測試: 主庫中:
mysql> show databases;
mysql> use westos;
mysql> select * from usertable;
mysql> delete from usertable where username='user2';
mysql> select * from usertable;
從庫中:
mysql> show databases;
mysql> use westos;
mysql> select * from usertable;
一主兩從:
server5:主master
server6:次master,slave
server7:slave
1.server7中安裝資料庫,同上設定
[[email protected] ~]# yum install *.rpm -y
[[email protected] ~]# vim /etc/my.cnf
[[email protected] ~]# /etc/init.d/mysqld start
[[email protected] ~]# grep password /var/log/mysqld.log
修改密碼:[[email protected] ~]# mysql_secure_installation
2.將server6設為從master
[[email protected] ~]# vim /etc/my.cnf
[[email protected] ~]# /etc/init.d/mysqld restart
[[email protected] ~]# mysqldump -p westos > westos.sql
[[email protected] ~]# scp westos.sql 172.25.0.123:/root
[[email protected] ~]# mysql -p
mysql> show slave status\G; #檢視是否還是兩個YES
mysql> grant replication slave on *.* to [email protected]'172.25.0.%' identified by 'Westos+123'; #master上建立使用者並授權
mysql> show master status;
3:server7中
[[email protected] ~]# vim westos.sql
[[email protected] ~]# mysql -p < westos.sql
[[email protected] ~]# mysql -p
mysql> change master to master_host='172.25.0.122',master_user='user1',master_password='Westos+123',master_auto_position = 1;
mysql> start slave;
mysql> show slave status\G;
測試 :server5中新增資訊
[[email protected] ~]# mysql -p
mysql> insert into westos.usertable values('tom','111');
server6/7中檢視:
mysql> select * from westos.usertable;
server6/7中成功複製了主mater的資訊。