10.mysql-主從服務-M-S+M-M+M-S-S
MySQL主從服務
MySQL Replication
Replication介紹
Replication可以實現將資料從一臺資料庫伺服器(master)複製到一或多臺資料庫伺服器(slave)
預設情況下屬於非同步複製,無需維持長連線
通過配置,可以複製所有的庫或者幾個庫,甚至庫中的一些表
是MySQL內建的,本身自帶的
Replication原理
簡單的說就是master將資料庫的改變寫入二進位制日誌,slave同步這些二進位制日誌,並根據這些二進位制日誌進行資料操作
DML:SQL操作語句,update, insert,delete
Relay log :中繼日誌
Replication作用
1、Fail Over 故障切換
2、Backup Server 備份服務,無法對SQL語句執行產生的故障恢復(使用SQL語句刪除時,主從都將刪除。)
3、High Performance高效能,可以多臺slave,實現讀寫分離
Replication如何工作
示例圖:
mysql主從複製中:
第一步:master記錄二進位制日誌。在每個事務更新資料完成之前,master在二進位制日誌記錄這些改變。MySQL將事務寫入二進位制日誌,即使事務中的語句都是交叉執行的。在事件寫入二進位制日誌完成後,master通知儲存引擎提交事務。
第二步:slave將master的binary log拷貝到它自己的中繼日誌。首先,slave開始一個工作執行緒——I/O執行緒。I/O執行緒在master上開啟一個普通的連線,然後開始binlog dump process。Binlog dump process從master的二進位制日誌中讀取事件,如果已經執行完master產生的所有檔案,它會睡眠並等待master產生新的事件。I/O執行緒將這些事件寫入中繼日誌。
注意:首先Master服務中會將改變資料的記錄(date changes)存放到二進位制日誌(Binary log)中。Slave服務中I/O thread將會從Master的二進位制日誌(Binary log)中拉去記錄(這些記錄就是SQL語句)並同步到中繼日誌(Real log)中。再由SQL thread將中繼日誌記錄執行一遍,由此資料從master中同步到slave中。
主從同步,是從伺服器或中繼伺服器從主伺服器中抓取二進位制日誌的。
Replication常見方案
One master and Muti salve 一主多備
一般用來做讀寫分離的,master寫,其他slave讀,這種架構最大問題I/O壓力集中
在Master上<多臺同步影響IO>
M-S-S
使用一臺slave作為中繼,分擔Master的壓力,slave中繼需要開啟bin-log,並配置log-slave-updates
Slave中繼可使用Black-hole儲存引擎,不會把資料儲存到磁碟,只記錄二進位制日誌
M-M 雙主互備 (互為主從)
很多人誤以為這樣可以做到MySQL負載均衡,實際沒什麼好處,每個伺服器需要做同樣的同步更新,破壞了事物的隔離性和資料的一致性
M-M-M
監控三臺機器互相做對方的master
天生的缺陷:複製延遲,slave上同步要慢於master,如果大併發的情況那延遲更嚴重
Mysql在5.6已經自身可以實現fail over故障切換
One slave Muti master 一從對多主
好處:節省成本,將多個master資料自動化整合
缺陷:對庫和表資料的修改較多
部署mysql主從同步(M-S模型)
只能從master中同步到slave中,不能從slave中同步到master上。
準備環境
主機名 |
IP |
系統/MySQL版本 |
角色 |
root@131-server ~ |
192.168.234.131 |
CentOS7.6/5.7.30 |
Master |
root@130-client ~ |
192.168.234.130 |
CentOS7.6/5.7.30 |
slave |
模式:C/S架構
埠3306
master伺服器
建立資料庫及表
[root@131-server ~]# mysql -uroot –p
mysql> create database server_db;
Query OK, 1 row affected (0.01 sec)
mysql> use master_db;
Database changed
mysql> create table server_tb(id int(5), name varchar(20), age int(5), gender enum('W','M') default 'M');
Query OK, 0 rows affected, 2 warnings (0.02 sec)
配置slave授權並重新整理許可權
[root@131-server ~]# mysql -uroot -p
Enter password:
告訴master它可以被某個使用者、某個地址或地址段的slave抓取二進位制日誌。
mysql> grant replication slave on *.* to 'slave'@'192.168.234.130' identified by "3edc#EDC";
Query OK, 0 rows affected, 1 warning (0.00 sec)
sql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
注意:在mysql8.0.20中此授權語句不能使用。
解釋:
*.*:表示某個資料庫下的某個表;*.*表示所有資料庫下的所有表
'slave'@'192.168.234.130':slave表示從伺服器端的拷貝二進位制日誌的使用者名稱;192.168.234.130表示從伺服器地址,也可以是地址段如:192.168.234.%。
identified by "3edc#EDC":表示為從伺服器端拷貝二進位制日誌的使用者名稱設定密碼。
修改master中/etc/my.cnf配置檔案
[root@131-server ~]# vim /etc/my.cnf
在檔案最後根據需求新增:
#master_slave
#定義master服務的二進位制名稱
log-bin=mysql-bin-master
#啟用資料庫的ID,ID號不能重複.
server-id=1
#設定需要同步的資料庫(如果要同步多個庫,可將binlog-do-db此行寫多次。一般生產環境只同步一個數據庫。)
binlog-do-db=server_db
#設定不需要同步的資料庫(不能同步mysql這個庫,因為每個mysql的許可權設定都不一致,如果同步將會出現錯誤!)
binlog-ignore-db=mysql
其他選項:
#控制資料庫的binlog刷到磁碟上去,0 不控制,效能最好,1每次事物提交都會刷到日誌檔案中,效能最差,最安全
sync_binlog=1
#binlog日誌格式,mysql預設採用statement,建議使用mixed
binlog_format=mixed
#binlog過期清理時間
expire_logs_days=7
#binlog每個日誌檔案大小
max_binlog_size=100m
#binlog快取值大小
binlog_cache_size=4m
#最大binlog快取值
max_binlog_cache_size=512m
#自增值的偏移量
auto-increment-offset=1
#自增值的自增量
auto-increment-increment=1
#跳過從資料庫錯誤
slave-skip-errors=all
重啟mysql服務使/etc/my.cnf配置生效
[root@131-server ~]# systemctl restart mysqld
檢視master狀態
mysql> show master status \G
*************************** 1. row ***************************
File: mysql-bin-master.000001
Position: 154
Binlog_Do_DB: master_db
Binlog_Ignore_DB: mysql
Executed_Gtid_Set:
1 row in set (0.00 sec)
開啟二進位制日誌同步
預設情況二進位制日誌同步是自動開啟的,保險起見可以再次開啟。
mysql> set sql_log_bin=on;
Query OK, 0 rows affected (0.01 sec)
關閉二進位制日誌同步
mysql> set sql_log_bin=off;
Query OK, 0 rows affected (0.00 sec)
檢視二進位制日誌狀態
mysql> show binlog events \G
*************************** 1. row ***************************
Log_name: mysql-bin-master.000001
Pos: 4
Event_type: Format_desc
Server_id: 1
End_log_pos: 123
Info: Server ver: 5.7.30-log, Binlog ver: 4
*************************** 2. row ***************************
Log_name: mysql-bin-master.000001
Pos: 123
Event_type: Previous_gtids
Server_id: 1
End_log_pos: 154
Info:
2 rows in set (0.00 sec)
從master中匯出資料庫
做主從同步前,需要保持master與slave兩者資料一致。
[root@131-server ~]# mysqldump -uroot -p -B server_db > server_db.sql
[root@131-server ~]# scp server_db.sql [email protected]:/root
The authenticity of host '192.168.234.130 (192.168.234.130)' can't be established.
ECDSA key fingerprint is SHA256:aWS4iiSS+zdq8KnQoQTWGgfbrOkzXLzq1hVIQIsmqPU.
ECDSA key fingerprint is MD5:65:e6:cb:b1:e7:92:de:6e:9e:f7:79:d5:2d:88:09:bb.
Are you sure you want to continue connecting (yes/no)? yes
[email protected]'s password:
server_db.sql 100% 2043 1.1MB/s 00:00
[root@131-server ~]#
slave伺服器
測試slave能否登入master
[root@130-client ~]# mysql -uslave -p -h192.168.234.131
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.30-log MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
如果不能登入成功,可能是沒有重新整理許可權表。
master伺服器中重新整理許可權表;
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
在slave中匯入資料庫
[root@130-client ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> source /root/server_db.sql
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
修改slave中/etc/my.cnf配置檔案
slave中無須配置二進位制日誌,如果做中繼slave時必須配置log-bin=mysql-bin-slave
#slave_master
#啟用資料庫的ID,ID號不能重複.
server-id=2
重啟mysql服務使/etc/my.cnf配置生效
[root@130-client ~]# systemctl restart mysqld
將主從同步賬號密碼寫入資料庫,並重新整理許可權
root@130-client ~]# mysql -uroot -p
Enter password:
告訴slave,它的master地址、使用者名稱、密碼。
mysql> change master to master_host='192.168.234.131',master_user='slave',master_password='3edc#EDC';
Query OK, 0 rows affected, 2 warnings (0.01 sec)
開啟slave服務
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
關閉slave服務
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
開啟二進位制日誌同步功能
mysql> set sql_log_bin=on;
Query OK, 0 rows affected (0.00 sec)
檢視主從同步狀態
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.234.131 #master地址
Master_User: slave #master使用者名稱
Master_Port: 3306 #master埠
Connect_Retry: 60
Master_Log_File: mysql-bin-master.000008
Read_Master_Log_Pos: 154
Relay_Log_File: 130-client-relay-bin.000009
Relay_Log_Pos: 381
Relay_Master_Log_File: mysql-bin-master.000008
Slave_IO_Running: Yes #物理通訊狀態
Slave_SQL_Running: Yes #SQL同步狀態
Replicate_Do_DB:
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: 154
Relay_Log_Space: 820
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
Master_UUID: 995efba9-4803-11eb-bcdc-000c2922b449
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
插入資料測試
master
mysql> insert into server_tb values(3,'wangquanzizhan',27,'M');
Query OK, 1 row affected (0.00 sec)
mysql> select * from server_tb where id=3;
+------+----------------+------+--------+
| id | name | age | gender |
+------+----------------+------+--------+
| 3 | wangquanzizhan | 27 | M |
+------+----------------+------+--------+
1 row in set (0.00 sec)
slave
mysql> select * from server_tb where id=3;
+------+----------------+------+--------+
| id | name | age | gender |
+------+----------------+------+--------+
| 3 | wangquanzizhan | 27 | M |
+------+----------------+------+--------+
1 row in set (0.00 sec)
排錯思路
1.master/slave中二進位制日誌同步功能沒有開啟。
2.iptables防火牆的埠策略沒有開放。
3.對應的master/slave的IP地址填寫錯誤。
4.master/slave的資料庫表結構不一致。
部署mysql主主雙向主從同步(M-M模型)
master與slave雙向同步。
準備環境
主機名 |
IP |
系統/MySQL版本 |
角色 |
root@131-server ~ |
192.168.234.131 |
CentOS7.6/5.7.30 |
Master |
root@130-client ~ |
192.168.234.130 |
CentOS7.6/5.7.30 |
slave |
模式:C/S架構
埠3306
131-server新增配置
建立資料庫即表
[root@131-server ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database msdata;
Query OK, 1 row affected (0.00 sec)
mysql> use msdata;
Database changed
mysql> create table mstable (id int(5),name varchar(20));
Query OK, 0 rows affected (0.08 sec)
配置slave授權並重新整理許可權
告訴131,它的二進位制日誌可以被130地址的wangbin使用者抓取。
mysql> grant replication slave on *.* to 'wangbin'@'192.168.234.130' identified by '3edc#EDC';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
修改/etc/my.cnf配置檔案
[root@131-server ~]# vim /etc/my.cnf
新增如下內容
#master-master
log-bin = mysql-bin-master
server-id = 110
binlog-do-db = msdata
binlog-ignore-db = mysql
重啟mysql使/etc/my.cnf配置檔案生效
[root@131-server ~]# systemctl restart mysqld
130-client新增配置
必須使兩個伺服器的mysql為同一版本,表結構一致。
131中匯出資料庫並傳入130
[root@131-server ~]# mysqldump -uroot -p -B msdata > msdata.sql
Enter password:
[root@131-server ~]# scp msdata.sql [email protected]:/root
The authenticity of host '192.168.234.130 (192.168.234.130)' can't be established.
ECDSA key fingerprint is SHA256:aWS4iiSS+zdq8KnQoQTWGgfbrOkzXLzq1hVIQIsmqPU.
ECDSA key fingerprint is MD5:65:e6:cb:b1:e7:92:de:6e:9e:f7:79:d5:2d:88:09:bb.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.234.130' (ECDSA) to the list of known hosts.
[email protected]'s password:
msdata.sql 100% 1950 933.9KB/s 00:00
130資料庫匯入
[root@130-client ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> source /root/msdata.sql
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
配置slave授權並重新整理許可權
告訴130,它的二進位制日誌可以被131地址的changmengka使用者抓取。
mysql> grant replication slave on *.* to 'changmengka'@'192.168.234.131' identified by '4rfv$RFV';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
修改/etc/my.cnf配置檔案
[root@130-client ~]# vim /etc/my.cnf
新增如下內容:
#master-master
log-bin = mysql-bin-slave
server-id = 120
binlog-do-db = msdata
binlog-ignore-db = mysql
重啟mysql使/etc/my.cnf配置檔案生效
[root@130-client ~]# systemctl restart mysqld
131-server新增配置
將主從同步賬號密碼寫入資料庫,並重新整理許可權
告訴131,它的master地址是130,使用者是130上的changmengka
change master to master_host='192.168.234.130',master_user='changmengka',master_password='4rfv$RFV';
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
檢視master狀態
mysql> show master status \G
*************************** 1. row ***************************
File: mysql-master.000001
Position: 154
Binlog_Do_DB: msdata
Binlog_Ignore_DB: mysql
Executed_Gtid_Set:
1 row in set (0.00 sec)
開啟slave功能
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
檢視slave狀態
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.234.130
Master_User: changmengka
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-slave.000001
Read_Master_Log_Pos: 306
Relay_Log_File: 131-server-relay-bin.000002
Relay_Log_Pos: 523
Relay_Master_Log_File: mysql-slave.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
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: 306
Relay_Log_Space: 735
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: 120
Master_UUID: c9d20b75-4803-11eb-b4f6-000c2908cb00
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
130-client新增配置
將主從同步賬號密碼寫入資料庫,並重新整理許可權
告訴130,它的master地址是131,使用者是131上的wangbin
mysql> change master to master_host='192.168.234.131',master_user='wangbin',master_password='3edc#EDC';
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
檢視master狀態
mysql> show master status \G
*************************** 1. row ***************************
File: mysql-slave.000001
Position: 306
Binlog_Do_DB: msdata
Binlog_Ignore_DB: mysql
Executed_Gtid_Set:
1 row in set (0.00 sec)
開啟slave功能
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
檢視slave狀態
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.234.131
Master_User: wangbin
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-master.000001
Read_Master_Log_Pos: 154
Relay_Log_File: 130-client-relay-bin.000002
Relay_Log_Pos: 373
Relay_Master_Log_File: mysql-master.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
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: 154
Relay_Log_Space: 585
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: 110
Master_UUID: 995efba9-4803-11eb-bcdc-000c2922b449
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
插入資料測試
在131上insert into資料,在130上select資料
131上
mysql> use msdata
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> insert into mstable values(1,'wangbin');
Query OK, 1 row affected (0.00 sec)
mysql> select * from mstable;
+------+---------+
| id | name |
+------+---------+
| 1 | wangbin |
+------+---------+
1 row in set (0.00 sec)
130上
mysql> use msdata
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from mstable;
+------+---------+
| id | name |
+------+---------+
| 1 | wangbin |
+------+---------+
1 row in set (0.01 sec)
在130上insert into資料,在131上select資料
130上
mysql> insert into mstable values(2,'changmengka');
Query OK, 1 row affected (0.00 sec)
mysql> select * from mstable;
+------+-------------+
| id | name |
+------+-------------+
| 1 | wangbin |
| 2 | changmengka |
+------+-------------+
2 rows in set (0.00 sec)
131上
mysql> select * from mstable;
+------+-------------+
| id | name |
+------+-------------+
| 1 | wangbin |
| 2 | changmengka |
+------+-------------+
2 rows in set (0.00 sec)
排錯思路
1.網路不通
2.賬戶密碼錯誤
3.防火牆
4.mysql配置檔案問題
5.連線伺服器時語法
6.主伺服器mysql許可權
部署mysql一主一中繼一從主從同步(M-S-S)
一臺為主,一臺為中繼,一臺為從,中繼同步。
環境準備
主機名 |
IP |
系統/MySQL版本 |
角色 |
root@131-server ~ |
192.168.234.131 |
CentOS7.6/5.7.30 |
Master |
root@130-client ~ |
192.168.234.130 |
CentOS7.6/5.7.30 |
Slave |
root@129-relay ~ |
192.168.234.129 |
CentOS7.6/5.7.30 |
Relay |
131-server
建立資料庫及表
[root@131-server ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database mssdata;
Query OK, 1 row affected (0.01 sec)
mysql> use mssdata;
Database changed
mysql> create table msstable(id int(5), name varchar(20));
Query OK, 0 rows affected (0.03 sec)
配置slave授權並重新整理許可權
告訴131,它的二進位制日誌可以被129的chang使用者抓取。
mysql> grant replication slave on *.* to 'chang'@'192.168.234.129' identified by '4rfv$RFV';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
修改/etc/my.cnf配置檔案
[root@131-server ~]# vim /etc/my.cnf
新增如下內容
#M-S-S
server-id = 110
binlog-do-db = mssdata
binlog-ignore-db = mysql
log-bin = mysql-bin-master
sync-binlog = 1
binlog-format = row
重啟mysql服務使/etc/my.cnf配置檔案生效
[root@131-server ~]# systemctl restart mysqld
拷貝資料庫
需要確保master、relay、slave三個服務的mysql版本一致、表結構一致。
[root@131-server ~]# mysqldump -uroot -p -B mssdata > mssdata.sql
Enter password:
[root@131-server ~]# scp mssdata.sql [email protected]:/root
The authenticity of host '192.168.234.129 (192.168.234.129)' can't be established.
ECDSA key fingerprint is SHA256:aWS4iiSS+zdq8KnQoQTWGgfbrOkzXLzq1hVIQIsmqPU.
ECDSA key fingerprint is MD5:65:e6:cb:b1:e7:92:de:6e:9e:f7:79:d5:2d:88:09:bb.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.234.129' (ECDSA) to the list of known hosts.
[email protected]'s password:
mssdata.sql 100% 1961 185.7KB/s 00:00
[root@131-server ~]# scp mssdata.sql [email protected]:/root
The authenticity of host '192.168.234.130 (192.168.234.130)' can't be established.
ECDSA key fingerprint is SHA256:aWS4iiSS+zdq8KnQoQTWGgfbrOkzXLzq1hVIQIsmqPU.
ECDSA key fingerprint is MD5:65:e6:cb:b1:e7:92:de:6e:9e:f7:79:d5:2d:88:09:bb.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.234.130' (ECDSA) to the list of known hosts.
[email protected]'s password:
mssdata.sql 100% 1961 213.7KB/s 00:00
129-relay
修改/etc/my.cnf配置檔案
[root@129-relay ~]# vim /etc/my.cnf
新增如下內容
#M-S-S
server-id = 120
log-bin = mysql-bin-relay
log-slave-updates = 1
binlog-format = row
重啟mysql服務使/etc/my.cnf配置檔案生效
[root@129-relay ~]# systemctl restart mysqld
匯入資料庫
[root@129-relay ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> source /root/mssdata.sql
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
關閉slave
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
將主從同步賬號密碼寫入資料庫並重新整理許可權
告訴129,它的master是131,使用者時131上的chang
mysql> change master to master_host='192.168.234.131',master_user='chang',master_password='4rfv$RFV';
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
開啟slave
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
配置slave授權並重新整理許可權
告訴129,它的二進位制日誌可以被130的meng使用者抓取
mysql> grant replication slave on *.* to 'meng'@'192.168.234.130' identified by '5tgb%TGB';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
檢視slave狀態
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.234.131
Master_User: chang
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-master.000001
Read_Master_Log_Pos: 154
Relay_Log_File: 129-relay-relay-bin.000002
Relay_Log_Pos: 373
Relay_Master_Log_File: mysql-master.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
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: 154
Relay_Log_Space: 584
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: 110
Master_UUID: 995efba9-4803-11eb-bcdc-000c2922b449
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
關閉中繼的查詢
如果不需要在中繼器上查詢資料庫,可以進行關閉。
mysql> set sql_log_bin=off;
Query OK, 0 rows affected (0.00 sec)
mysql> alter table msstable engine=blackhole;
Query OK, 1 row affected (0.01 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> set sql_log_bin=on;
Query OK, 0 rows affected (0.00 sec)
130-client
修改/etc/my.cnf配置檔案
[root@130-client ~]# vim /etc/my.cnf
新增如下內容
#M-S-S
server-id = 130
log-bin = mysql-bin-slave
binlog-format = row
重啟mysql服務使/etc/my.cnf配置檔案生效
[root@130-client ~]# systemctl restart mysqld
匯入資料庫
[root@130-client ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30-log MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> source /root/mssdata.sql
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
關閉slave
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
將主從同步賬號密碼寫入資料庫並重新整理許可權
mysql> change master to master_host='192.168.234.129',master_user='meng',master_password='5tgb%TGB';
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
開啟slave
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
檢視slave狀態
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.234.129
Master_User: meng
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-relay.000001
Read_Master_Log_Pos: 755
Relay_Log_File: 130-client-relay-bin.000002
Relay_Log_Pos: 972
Relay_Master_Log_File: mysql-relay.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
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: 755
Relay_Log_Space: 1184
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: 120
Master_UUID: 5c01fa2f-48bd-11eb-b5a0-000c2958b5d5
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
插入資料測試
未關閉中繼器顯示查詢
131上
mysql> insert into msstable values(1,'wangbin');
Query OK, 1 row affected (0.00 sec)
mysql> select * from msstable;
+------+---------+
| id | name |
+------+---------+
| 1 | wangbin |
+------+---------+
1 row in set (0.00 sec)
129上
mysql> select * from msstable;
+------+---------+
| id | name |
+------+---------+
| 1 | wangbin |
+------+---------+
1 row in set (0.00 sec)
130上
mysql> select * from msstable;
+------+---------+
| id | name |
+------+---------+
| 1 | wangbin |
+------+---------+
1 row in set (0.00 sec)
關閉中繼器顯示查詢
131上
mysql> insert into msstable values(2,'changmengka');
Query OK, 1 row affected (0.00 sec)
mysql> select * from msstable;
+------+-------------+
| id | name |
+------+-------------+
| 1 | wangbin |
| 2 | changmengka |
+------+-------------+
2 rows in set (0.00 sec)
129上
mysql> select * from msstable;
Empty set (0.00 sec)
130上
mysql> select * from msstable;
+------+-------------+
| id | name |
+------+-------------+
| 1 | wangbin |
| 2 | changmengka |
+------+-------------+
2 rows in set (0.00 sec)