MySQL主從同步
主從同步概述
MySQL主從同步
- 實現數據自動同步的服務結構
- 主服務器:接受客戶端訪問連接
- 從服務器:自動同步主服務器的數據
主從同步的工作原理
主庫有1個線程叫dump線程,從庫有2個線程(io+sql),當主庫有數據發生改變的時候,那么主庫會通過自己的dump線程去給從庫發個信號,然后從庫再去請求主庫的binlog日志文件并保存在本機的中繼日志文件里面,然后sql線程執行中繼日志文件里面的SQL信息,完成同步。
- Master
- 啟用binlog日志
- Slave
- Slave_IO:復制master主機binlog日志文件里的SQL命令到本機的relay-log文件里
- Slave_SQL:執行本機的relay-log文件里的SQL語句,實現與Master數據一致
構建主從同步
構建思路
- 配置主服務器
- 啟用binlog日志、授權用戶、查看binlog日志
- 配置從服務器
- 設置server_id
- 確保與主服務器數據一致
- 指定主庫信息
- 啟動slave程序
- 查看狀態信息
拓撲結構圖
構建主服務器
- 修改主配置文件
啟動binlog日志,并重啟服務
[root@host51 ~
]
4
[mysqld
]
5 server_id
=51
6 log_bin
=master51
[root@host51 ~
]
- 授權用戶
用戶權限replication slave
用戶名自定義
客戶端地址允許從服務器連接
密碼自定義,但要符合密碼策略
[root@host51 ~
]
mysql
> grant replication slave on *.* to repluser@
"%" identified by
"123qqq...A";
mysql
> select user ,host from mysql.user where user
="repluser";
+----------+------+
| user
| host
|
+----------+------+
| repluser
| %
|
+----------+------+
1 row
in set (0.00 sec
)
mysql
> show master status
;
+-----------------+----------+--------------+------------------+-------------------+
| File
| Position
| Binlog_Do_DB
| Binlog_Ignore_DB
| Executed_Gtid_Set
|
+-----------------+----------+--------------+------------------+-------------------+
| master51.000001
| 441
| | | |
+-----------------+----------+--------------+------------------+-------------------+
1 row
in set (0.00 sec
)
配置從服務器
- 修改主配置文件my.cnf
server_id不允許與主服務器server_id相同
[root@host52 ~
]4
[mysqld
]5 server_id
=52
[root@host52 ~
]
- 確保與主服務器數據一致
1. 在主服務器上備份數據,將備份文件拷貝給從服務器
2. 從服務器使用備份文件恢復數據
3. 在從服務器上查看備份文件記錄的binlog日志
[root@host51 ~
]
[root@host51 ~
]
/allbak.sql
[root@host51 ~
]
[root@host52 ~
]
allbak.sql
[root@host52 ~
]
[root@host52 ~
]
CHANGE MASTER TO MASTER_LOG_FILE
='master51.000001', MASTER_LOG_POS
=441
;
- 指定主服務器信息
[root@host52 ~
]
mysql
> show slave status
;
Empty
set (0.00 sec
)mysql
> change master to-
> master_host
="192.168.4.51" , -
> master_user
="repluser" , -
> master_password
="123qqq...A" , -
> master_log_file
="master51.000001" , -
> master_log_pos
=441
;
Query OK, 0 rows affected, 2 warnings
(0.06 sec
)
mysql
> start slave
;
Query OK, 0 rows affected
(0.00 sec
)1. Master信息會自動保存到/var/lib/mysql/master.info文件
2. 若更改主服務器信息時,應先執行stop slave
;修改后在執行start slave
;- 查看Slave狀態
確認IO線程,SQL線程都是Yes狀態
mysql
> show slave status \G
*************************** 1. row ***************************Slave_IO_State: Waiting
for master to send eventMaster_Host: 192.168.4.51 Master_User: repluserMaster_Port: 3306Connect_Retry: 60Master_Log_File: master51.000001Read_Master_Log_Pos: 441Relay_Log_File: host52-relay-bin.000004Relay_Log_Pos: 319Relay_Master_Log_File: master51.000001Slave_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: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 441Relay_Log_Space: 692Until_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
Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 51Master_UUID: 6d7b632c-4e3f-11ea-8a8d-000c29875030Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has
read all relay log
; waiting
for more updatesMaster_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: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
1 row
in set (0.00 sec
)
如果報錯檢查Last_IO_Error以及Last_SQL_Error,然后根據錯誤進行修改,stop slave ,再次重新寫change master,然后重新start slave,查看信息狀態
change master to master_user
="用戶名"
change master to master_host
="主服務器地址"
change master to master_password
="主服務器設置的密碼"
change master to master_log_file
="主服務器的binlog日志名"
change master to master_log_pos
=主服務器的偏移量
;
(ps:當如果報錯號碼為2003,此時檢查防火墻與selinux的狀態,確保防火墻關閉,selinux為permissive狀態)
將從服務器恢復成獨立的數據庫
- 存放在數據庫目錄下的相關文件
- 將以下文件全部刪除,重啟服務,即可恢復成獨立的數據庫
文件名說明
| master.info | 主庫信息 |
| relay-log.info | 中繼日志信息 |
| 主機名-relay-bin.xxxxxx | 中繼日志 |
| 主機名-relay-bin.index | 索引文件 |
[root@host52 ~
]
[root@host52 mysql
]
auto.cnf ibdata1 mysql sys
host52-relay-bin.000003 ib_logfile0 mysql.sock xtrabackup_binlog_pos_innodb
host52-relay-bin.000004 ib_logfile1 mysql.sock.lock xtrabackup_info
host52-relay-bin.index ibtmp1 performance_schema
ib_buffer_pool master.info relay-log.info
[root@host52 mysql
]
25
master51.000001
441
192.168.4.51
repluser
123qqq
...A
3306
60
00
30.0000
6d7b632c-4e3f-11ea-8a8d-000c29875030
864000
[root@host52 mysql
]
[root@host52 mysql
]
[root@host52 mysql
]
[root@host52 mysql
]
[root@host52 ~
]
mysql
>show slave status
;
mysql
> change master to-
> master_host
="192.168.4.51" ,-
> master_user
="repluser" ,-
> master_password
="123qqq...A" ,-
> master_log_file
="master51.000001" ,-
> master_log_pos
=441
;
Query OK, 0 rows affected, 2 warnings
(0.06 sec
)
mysql
>start slave
;
mysql
>show slave status
; 測試:
在主服務器上新寫入數據,在從服務器上面查看
[root@host51 ~
]
mysql
> create database db5
;
Query OK, 1 row affected
(0.00 sec
)mysql
> create table db5.user
(name char
(12
));
Query OK, 0 rows affected
(0.01 sec
)mysql
> insert into db5.user values
("haha");
Query OK, 1 row affected
(0.02 sec
)mysql
> show master status
;
+-----------------+----------+--------------+------------------+-------------------+
| File
| Position
| Binlog_Do_DB
| Binlog_Ignore_DB
| Executed_Gtid_Set
|
+-----------------+----------+--------------+------------------+-------------------+
| master51.000001
| 1024
| | | |
+-----------------+----------+--------------+------------------+-------------------+
1 row
in set (0.00 sec
)在host52上面查看
mysql
> show databases
;
+--------------------+
| Database
|
+--------------------+
| information_schema
|
| db1
|
| db5
|
| mysql
|
| performance_schema
|
| sys
|
+--------------------+
6 rows
in set (0.05 sec
)mysql
> select * from db5.user
;
+------+
| name
|
+------+
| haha
|
+------+
1 row
in set (0.00 sec
)
構建主從同步結構模式
結構類型
- 一主多從:從 <-- 主 --> 從
- 鏈式復制:主 <-- 從 <-- 從
- 互為主從:主 < – > 主
配置一主多從結構
- 配置從服務器53
1. 修改配置文件
2. 確保與主服務器數據一致
3. 指定主庫信息
4. 啟動slave進程
[root@host53 ~
]4
[mysqld
]5 server_id
=53
[root@host51 ~
]
[root@host51 ~
]
[root@host51 ~
]
mysql
> show master status
;
+-----------------+----------+--------------+------------------+-------------------+
| File
| Position
| Binlog_Do_DB
| Binlog_Ignore_DB
| Executed_Gtid_Set
|
+-----------------+----------+--------------+------------------+-------------------+
| master51.000001
| 1024
| | | |
+-----------------+----------+--------------+------------------+-------------------+
1 row
in set (0.00 sec
)
[root@host53 ~
]
mysql
> create database db5
;
[root@host53 ~
][root@host53 ~
]
mysql
> change master to -
> master_host
="192.168.4.51" ,-
> master_user
="repluser" ,-
> master_password
="123qqq...A" ,-
> master_log_file
="master51.000001" ,-
> master_log_pos
=1024
;
Query OK, 0 rows affected, 2 warnings
(0.07 sec
)
mysql
> show slave status \G
*************************** 1. row ***************************Slave_IO_State: Waiting
for master to send eventMaster_Host: 192.168.4.51Master_User: repluserMaster_Port: 3306Connect_Retry: 60Master_Log_File: master51.000001Read_Master_Log_Pos: 1024Relay_Log_File: host53-relay-bin.000003Relay_Log_Pos: 319Relay_Master_Log_File: master51.000001Slave_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: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 1024Relay_Log_Space: 527Until_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
Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 51Master_UUID: 6d7b632c-4e3f-11ea-8a8d-000c29875030Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has
read all relay log
; waiting
for more updatesMaster_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: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
1 row
in set (0.00 sec
)UUID問題:如果報錯UUID沖突,可能是由于之前做過的實驗將所有數據備份過來導致uid也是相同的,解決辦法如下:進到/var/lib/mysql數據庫目錄,修改/var/lib/mysql/auto.cnf, 改一個新的值,但與原來的長度一定要相同,重啟數據庫服務即可,或者也可以進到數據庫select uid
();獲取新的uid復制到/var/lib/mysql/auto.cnf中
[root@host51 ~
]
mysql
> select * from db5.user
;
+---------+
| name
|
+---------+
| haha
|
| heihei
|
| lalalla
|
| jkfjf
|
| uuuuu
|
| sss
|
| ggg
|
+---------+
7 rows
in set (0.00 sec
)[root@host52 ~
]
mysql
> select * from db5.user
;
+---------+
| name
|
+---------+
| haha
|
| heihei
|
| lalalla
|
| jkfjf
|
| uuuuu
|
| sss
|
| ggg
|
+---------+
7 rows
in set (0.01 sec
)[root@host53 ~
]
mysql
> select * from db5.user
;
+---------+
| name
|
+---------+
| haha
|
| heihei
|
| lalalla
|
| jkfjf
|
| uuuuu
|
| sss
|
| ggg
|
+---------+
7 rows
in set (0.00 sec
)[root@host53 mysql
]
auto.cnf host53-relay-bin.000003 master.info relay-log.info
ca-key.pem host53-relay-bin.index mysql server-cert.pem
ca.pem ib_buffer_pool mysql.sock server-key.pem
client-cert.pem ibdata1 mysql.sock.lock sys
client-key.pem ib_logfile0 performance_schema
db5 ib_logfile1 private_key.pem
host53-relay-bin.000002 ibtmp1 public_key.pem
[root@host53 ~
]
25
master51.000001
2547
192.168.4.51
repluser
123qqq
...A
3306
60
00
30.0000
6d7b632c-4e3f-11ea-8a8d-000c29875030
864000
配置主從從模式
為了在啟用binlog日志以及通不值錢保持主、從庫的一致性,主從同步未配置之前,要保證從庫上要有主庫上的數據,禁用selinux,關閉防火墻服務,保證物理連接正常。
- 配置主服務器
1.將53主機恢復成獨立的數據庫
2. 啟用binlog日志
3. 重啟數據庫服務
4. 用戶授權
[root@host53 ~
]
mysql:
[Warning
] Using a password on the
command line interface can be insecure.Master_Host: 192.168.4.51Last_IO_Error: error connecting to master
'repluser@192.168.4.51:3306' - retry-time: 60 retries: 14
[root@host53 ~
]
cd [root@host53 ~
]
[root@host53 mysql
]
auto.cnf db5 ib_logfile0 private_key.pem sys
ca-key.pem host53-relay-bin.000004 ib_logfile1 public_key.pem
ca.pem host53-relay-bin.index master.info relay-log.info
client-cert.pem ib_buffer_pool mysql server-cert.pem
client-key.pem ibdata1 performance_schema server-key.pem
[root@host53 mysql
]
[root@host53 mysql
]
[root@host53 mysql
]
[root@host53 mysql
]
5
[root@host53 mysql
]
[root@host53 mysql
]
mysql
> show databases
;
+--------------------+
| Database
|
+--------------------+
| information_schema
|
| db5
|
| mysql
|
| performance_schema
|
| sys
|
+--------------------+
5 rows
in set (0.10 sec
)mysql
> drop database db5
;
Query OK, 1 row affected
(0.12 sec
)mysql
> show databases
;
+--------------------+
| Database
|
+--------------------+
| information_schema
|
| mysql
|
| performance_schema
|
| sys
|
+--------------------+
4 rows
in set (0.00 sec
)mysql
> show master status
;
Empty
set (0.00 sec
)mysql
> show slave status
;
Empty
set (0.00 sec
)[root@host53 mysql
]
[mysqld
]
server_id
=53
log_bin
=host53
[root@host53 mysql
]
[root@host53 mysql
]
用戶授權
mysql
> grant replication slave on *.* to tian@
"%" identified by
"123qqq...A";
Query OK, 0 rows affected, 1 warning
(0.01 sec
)查看日志信息
mysql
> show master status
;
+---------------+----------+--------------+------------------+-------------------+
| File
| Position
| Binlog_Do_DB
| Binlog_Ignore_DB
| Executed_Gtid_Set
|
+---------------+----------+--------------+------------------+-------------------+
| host53.000001
| 441
| | | |
+---------------+----------+--------------+------------------+-------------------+
1 row
in set (0.00 sec
)- 配置服務器54(既做主服務器又做從服務器)
級聯復制:指在主服務器寫的數據,會同步到從服務器的從服務器中
1. 修改配置文件
2. 用戶授權
3. 指定主庫信息
4. 啟動slave進程
[root@host54 ~
]
[mysqld
]
log_bin
=host54
server_id
=54
log_slave_updates
[root@host54 ~
]
[root@host54 ~
]
mysql
> grant replication slave on *.* to tian@
"%" identified by
"123qqq...A";
Query OK, 0 rows affected, 1 warning
(0.01 sec
)mysql
> show master status
;
+---------------+----------+--------------+------------------+-------------------+
| File
| Position
| Binlog_Do_DB
| Binlog_Ignore_DB
| Executed_Gtid_Set
|
+---------------+----------+--------------+------------------+-------------------+
| host54.000003
| 437
| | | |
+---------------+----------+--------------+------------------+-------------------+
1 row
in set (0.00 sec
)mysql
> change master to-
> master_host
="192.168.4.53",-
> master_user
="tian",-
> master_password
="123qqq...A",-
> master_log_file
="host53.000001",-
> master_log_pos
=437
;
Query OK, 0 rows affected, 2 warnings
(0.08 sec
)mysql
> start slave
;
Query OK, 0 rows affected
(0.01 sec
)mysql
> show slave status\G
*************************** 1. row ***************************Slave_IO_State: Waiting
for master to send eventMaster_Host: 192.168.4.53Master_User: tianMaster_Port: 3306Connect_Retry: 60Master_Log_File: host53.000001Read_Master_Log_Pos: 437Relay_Log_File: host54-relay-bin.000002Relay_Log_Pos: 317Relay_Master_Log_File: host53.000001Slave_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: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 437Relay_Log_Space: 525Until_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
Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 53Master_UUID: 5240ae91-533a-11ea-a67b-000c296c0929Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has
read all relay log
; waiting
for more updatesMaster_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: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
1 row
in set (0.00 sec
)*****************************************************************- 配置從服務器55
1. 修改配置文件
2. 指定主庫信息
3. 啟動slave進程
[root@host55 ~
]
5 server_id
=55
[root@host55 ~
]
[root@host55 ~
]
mysql
> show master status
;
Empty
set (0.00 sec
)mysql
> change master to-
> master_host
="192.168.4.54",-
> master_user
="tian",-
> master_password
="123qqq...A",-
> master_log_file
="host54.000003",-
> master_log_pos
=437
;
Query OK, 0 rows affected, 2 warnings
(0.08 sec
)mysql
> start slave
;
Query OK, 0 rows affected
(0.00 sec
)mysql
> show slave status\G
*************************** 1. row ***************************Slave_IO_State: Waiting
for master to send eventMaster_Host: 192.168.4.54Master_User: tianMaster_Port: 3306Connect_Retry: 60Master_Log_File: host54.000003Read_Master_Log_Pos: 437Relay_Log_File: host55-relay-bin.000002Relay_Log_Pos: 317Relay_Master_Log_File: host54.000003Slave_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: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 437Relay_Log_Space: 525Until_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
Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 54Master_UUID: 8b4a89b4-531c-11ea-883c-000c290db2a4Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has
read all relay log
; waiting
for more updatesMaster_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: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
1 row
in set (0.00 sec
)客戶端測試,在主庫上授權訪問數據的連接用戶,客戶端連接主庫執行權限匹配的SQL操作,授權用戶連接第一臺從庫,可以看到主庫的數據,授權用戶連接第二臺從庫,可以看到主庫的數據在host53上授權用戶登錄
mysql
> grant all on gamedb.* to adminuser@
"192.168.4.50" identified by
"123qqq...A";
Query OK, 0 rows affected, 1 warning
(0.11 sec
)mysql
> show master status
;
+---------------+----------+--------------+------------------+-------------------+
| File
| Position
| Binlog_Do_DB
| Binlog_Ignore_DB
| Executed_Gtid_Set
|
+---------------+----------+--------------+------------------+-------------------+
| host53.000001
| 751
| | | |
+---------------+----------+--------------+------------------+-------------------+
1 row
in set (0.00 sec
)用host50測試,只要下載mariadb即可,不需要mysql
[root@host50 ~
]
/usr/bin/which: no mysql
in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
)
[root@host50 ~
][root@host50 ~
]
MySQL
[(none
)]> create database gamedb
;
Query OK, 1 row affected
(0.00 sec
)MySQL
[(none
)]> create table gamedb.user
(name char
(10
));
Query OK, 0 rows affected
(0.07 sec
)MySQL
[(none
)]> insert into gamedb.user values
("bob");
Query OK, 1 row affected
(0.01 sec
)MySQL
[(none
)]> quit
Bye
[root@host50 ~
]
+--------------------+
| Database
|
+--------------------+
| information_schema
|
| gamedb
|
+--------------------+
[root@host50 ~
]
+------+
| name
|
+------+
| bob
|
+------+
[root@host50 ~
]
+--------------------+
| Database
|
+--------------------+
| information_schema
|
| gamedb
|
+--------------------+
[root@host50 ~
]
+------+
| name
|
+------+
| bob
|
+------+
配置主主模式
將數據庫服務器20、21配置為主主結構
20為主服務器 21為從服務器
20為從服務器 21為主服務器
在20、21上面都能看到啟動了binlog日志,以及slave status
互相同步對方的數據
1.準備環境:在兩臺主機上都要安裝mysql的軟件包
20主機:
[root@20 ~
]
mysql-5.7.17.tar
[root@20 ~
]
[root@20 ~
]
mysql-5.7.17.tar
mysql-community-client-5.7.17-1.el7.x86_64.rpm
mysql-community-common-5.7.17-1.el7.x86_64.rpm
mysql-community-devel-5.7.17-1.el7.x86_64.rpm
mysql-community-embedded-5.7.17-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.17-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.17-1.el7.x86_64.rpm
mysql-community-libs-5.7.17-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm
mysql-community-minimal-debuginfo-5.7.17-1.el7.x86_64.rpm
mysql-community-server-5.7.17-1.el7.x86_64.rpm
mysql-community-test-5.7.17-1.el7.x86_64.rpm
[root@20 ~
]
[root@20 ~
]
[root@20 ~
]21主機:
[root@21 ~
]
mysql-5.7.17.tar
[root@21 ~
]
[root@21 ~
]
mysql-5.7.17.tar
mysql-community-client-5.7.17-1.el7.x86_64.rpm
mysql-community-common-5.7.17-1.el7.x86_64.rpm
mysql-community-devel-5.7.17-1.el7.x86_64.rpm
mysql-community-embedded-5.7.17-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.17-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.17-1.el7.x86_64.rpm
mysql-community-libs-5.7.17-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm
mysql-community-minimal-debuginfo-5.7.17-1.el7.x86_64.rpm
mysql-community-server-5.7.17-1.el7.x86_64.rpm
mysql-community-test-5.7.17-1.el7.x86_64.rpm
[root@21 ~
]
[root@21 ~
]
[root@21 ~
]2.起服務,修改密碼(兩臺主機做一樣的操作,以下以21為例)
[root@21 ~
]
[root@21 ~
][root@21 ~
]
auto.cnf client-key.pem ib_logfile1 mysql.sock.lock server-cert.pem
ca-key.pem ib_buffer_pool ibtmp1 performance_schema server-key.pem
ca.pem ibdata1 mysql private_key.pem sys
client-cert.pem ib_logfile0 mysql.sock public_key.pem
[root@21 ~
]
2020-02-23T00:03:35.923408Z 1
[Note
] A temporary password is generated
for root@localhost: To5?l6Vphjee
[root@21 ~
]
mysql
> show variables like
"%password%";
+---------------------------------------+--------+
| Variable_name
| Value
|
+---------------------------------------+--------+
| default_password_lifetime
| 0
|
| disconnect_on_expired_password
| ON
|
| log_builtin_as_identified_by_password
| OFF
|
| mysql_native_password_proxy_users
| OFF
|
| old_passwords
| 0
|
| report_password
| |
| sha256_password_proxy_users
| OFF
|
| validate_password_check_user_name
| OFF
|
| validate_password_dictionary_file
| |
| validate_password_length
| 8
|
| validate_password_mixed_case_count
| 1
|
| validate_password_number_count
| 1
|
| validate_password_policy
| MEDIUM
|
| validate_password_special_char_count
| 1
|
+---------------------------------------+--------+
14 rows
in set (0.00 sec
)
mysql
> set global validate_password_length
=6
;
Query OK, 0 rows affected
(0.00 sec
)mysql
> set global validate_password_policy
=0
;
Query OK, 0 rows affected
(0.00 sec
)mysql
> alter user root@
"localhost" identified by
"123456";
Query OK, 0 rows affected
(0.00 sec
)[root@21 ~
]
[mysqld
]
validate_password_length
=6
validate_password_policy
=0
[root@21 ~
]3.將20主機設為主服務器,21主機設為從服務器
[root@20 ~
]
[mysqld
]
validate_password_length
=6
validate_password_policy
=0
server_id
=20
log_bin
=master20
[root@20 ~
]
mysql
> show master status
;
+-----------------+----------+--------------+------------------+-------------------+
| File
| Position
| Binlog_Do_DB
| Binlog_Ignore_DB
| Executed_Gtid_Set
|
+-----------------+----------+--------------+------------------+-------------------+
| master20.000001
| 154
| | | |
+-----------------+----------+--------------+------------------+-------------------+
1 row
in set (0.00 sec
)mysql
> grant replication slave on *.* to tian@
"%" identified by
"123456";
Query OK, 0 rows affected, 1 warning
(0.01 sec
)mysql
> show master status
;
+-----------------+----------+--------------+------------------+-------------------+
| File
| Position
| Binlog_Do_DB
| Binlog_Ignore_DB
| Executed_Gtid_Set
|
+-----------------+----------+--------------+------------------+-------------------+
| master20.000001
| 437
| | | |
+-----------------+----------+--------------+------------------+-------------------+
1 row
in set (0.00 sec
)[root@21 ~
]
[mysqld
]
validate_password_length
=6
validate_password_policy
=0
server_id
=21
[root@21 ~
]
[root@21 ~
]
mysql
> show master status
;
Empty
set (0.00 sec
)
mysql
> change master to-
> master_host
="192.168.4.20",-
> master_user
="tian" ,-
> master_password
="123456" ,-
> master_log_file
="master20.000001" ,-
> master_log_pos
=437
;
Query OK, 0 rows affected, 2 warnings
(0.01 sec
)mysql
> start slave
;
Query OK, 0 rows affected
(0.01 sec
)mysql
> show slave status \G
*************************** 1. row ***************************Slave_IO_State: Waiting
for master to send eventMaster_Host: 192.168.4.20Master_User: tianMaster_Port: 3306Connect_Retry: 60Master_Log_File: master20.000001Read_Master_Log_Pos: 437Relay_Log_File: 21-relay-bin.000002Relay_Log_Pos: 319Relay_Master_Log_File: master20.000001Slave_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: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 437Relay_Log_Space: 523Until_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
Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 20Master_UUID: fd553082-55cf-11ea-a325-000c29fd476fMaster_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has
read all relay log
; waiting
for more updatesMaster_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: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
1 row
in set (0.00 sec
)4.同樣,將21作為主服務器,20做為從服務器
[root@21 ~
]
[mysqld
]
validate_password_length
=6
validate_password_policy
=0
server_id
=21
log_bin
=master21
[root@21 ~
]
[root@21 ~
]
mysql
> show master status
;
+-----------------+----------+--------------+------------------+-------------------+
| File
| Position
| Binlog_Do_DB
| Binlog_Ignore_DB
| Executed_Gtid_Set
|
+-----------------+----------+--------------+------------------+-------------------+
| master21.000002
| 154
| | | |
+-----------------+----------+--------------+------------------+-------------------+
1 row
in set (0.00 sec
)mysql
> grant replication slave on *.* to tian@
"%" identified by
"123456";
Query OK, 0 rows affected, 1 warning
(0.00 sec
)mysql
> show master status
;
+-----------------+----------+--------------+------------------+-------------------+
| File
| Position
| Binlog_Do_DB
| Binlog_Ignore_DB
| Executed_Gtid_Set
|
+-----------------+----------+--------------+------------------+-------------------+
| master21.000002
| 437
| | | |
+-----------------+----------+--------------+------------------+-------------------+
1 row
in set (0.00 sec
)[root@20 ~
]
mysql
> change master to-
> master_host
="192.168.4.21" ,-
> master_user
="tian" ,-
> master_password
="123456" ,-
> master_log_file
="master21.000002" ,-
> master_log_pos
=437
;
Query OK, 0 rows affected, 2 warnings
(0.03 sec
)mysql
> start slave
;
Query OK, 0 rows affected
(0.01 sec
)mysql
> show slave status \G
*************************** 1. row ***************************Slave_IO_State: Waiting
for master to send eventMaster_Host: 192.168.4.21Master_User: tianMaster_Port: 3306Connect_Retry: 60Master_Log_File: master21.000002Read_Master_Log_Pos: 437Relay_Log_File: 20-relay-bin.000002Relay_Log_Pos: 319Relay_Master_Log_File: master21.000002Slave_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: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 437Relay_Log_Space: 523Until_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
Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 21Master_UUID: efdf9bdc-55cf-11ea-a2c4-000c293d6c33Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has
read all relay log
; waiting
for more updatesMaster_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: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
1 row
in set (0.00 sec
)5.測試
在20主機上寫入數據,在21上也能查看,在21主機上寫入數據,在20主機上也能查看,且20與21主機同時都能查看到主服務器的binlog日志以及從服務器的信息20主機:
mysql
> create database aaa
;
Query OK, 1 row affected
(0.00 sec
)mysql
> create table aaa.z
(name char
(10
));
Query OK, 0 rows affected
(0.02 sec
)mysql
> insert into aaa.z values
("qqq");
Query OK, 1 row affected
(0.04 sec
)mysql
> select * from aaa.z
;
+------+
| name
|
+------+
| qqq
|
+------+
1 row
in set (0.00 sec
)21主機:
mysql
> select * from aaa.z
;
+------+
| name
|
+------+
| qqq
|
+------+
1 row
in set (0.00 sec
)mysql
> insert into aaa.z values
("www");
Query OK, 1 row affected
(0.01 sec
)mysql
> select * from aaa.z
;
+------+
| name
|
+------+
| qqq
|
| www
|
+------+
2 rows
in set (0.00 sec
)
mysql
> show master status
;
+-----------------+----------+--------------+------------------+-------------------+
| File
| Position
| Binlog_Do_DB
| Binlog_Ignore_DB
| Executed_Gtid_Set
|
+-----------------+----------+--------------+------------------+-------------------+
| master21.000002
| 686
| | | |
+-----------------+----------+--------------+------------------+-------------------+
1 row
in set (0.00 sec
)[root@21 ~
]
21-relay-bin.000001 client-cert.pem master21.000001 performance_schema
21-relay-bin.000002 client-key.pem master21.000002 private_key.pem
21-relay-bin.index ib_buffer_pool master21.index public_key.pem
aaa ibdata1 master.info relay-log.info
auto.cnf ib_logfile0 mysql server-cert.pem
ca-key.pem ib_logfile1 mysql.sock server-key.pem
ca.pem ibtmp1 mysql.sock.lock sys再次回到20主機查看:mysql
> select * from aaa.z
;
+------+
| name
|
+------+
| qqq
|
| www
|
+------+
2 rows
in set (0.00 sec
)
mysql
> show master status
;
+-----------------+----------+--------------+------------------+-------------------+
| File
| Position
| Binlog_Do_DB
| Binlog_Ignore_DB
| Executed_Gtid_Set
|
+-----------------+----------+--------------+------------------+-------------------+
| master20.000001
| 1012
| | | |
+-----------------+----------+--------------+------------------+-------------------+
1 row
in set (0.00 sec
)[root@20 ~
]
20-relay-bin.000001 client-cert.pem master20.000001 private_key.pem
20-relay-bin.000002 client-key.pem master20.index public_key.pem
20-relay-bin.index ib_buffer_pool master.info relay-log.info
aaa ibdata1 mysql server-cert.pem
auto.cnf ib_logfile0 mysql.sock server-key.pem
ca-key.pem ib_logfile1 mysql.sock.lock sys
ca.pem ibtmp1 performance_schema也可以用客戶端進行測試:
在20與21主機上添加授權用戶:
mysql
> grant all on aaa.* to tian@
"192.168.4.53" identified by
"123456";
Query OK, 0 rows affected, 1 warning
(0.01 sec
)
客戶端登錄,分別在20與21主機的服務器上寫入數據:
[root@host53 ~
]
mysql
> show grants
;
+----------------------------------------------------------+
| Grants
for tian@192.168.4.53
|
+----------------------------------------------------------+
| GRANT USAGE ON *.* TO
'tian'@
'192.168.4.53' |
| GRANT ALL PRIVILEGES ON
`aaa`.* TO
'tian'@
'192.168.4.53' |
+----------------------------------------------------------+
2 rows
in set (0.00 sec
)
mysql
> insert into aaa.z values
("sss");
Query OK, 1 row affected
(0.01 sec
)mysql
> select * from aaa.z
;
+------+
| name
|
+------+
| qqq
|
| www
|
| sss
|
+------+
3 rows
in set (0.00 sec
)[root@host53 ~
]
mysql
> insert into aaa.z values
("eee");
Query OK, 1 row affected
(0.01 sec
)mysql
> select * from aaa.z
;
+------+
| name
|
+------+
| qqq
|
| www
|
| sss
|
| eee
|
+------+
4 rows
in set (0.00 sec
)在20主機和21主機上分別查看:
20主機:
mysql
> select * from aaa.z
;
+------+
| name
|
+------+
| qqq
|
| www
|
| sss
|
| eee
|
+------+
4 rows
in set (0.01 sec
)21主機:
mysql
> select * from aaa.z
;
+------+
| name
|
+------+
| qqq
|
| www
|
| sss
|
| eee
|
+------+
4 rows
in set (0.00 sec
)
復制模式
復制模式介紹
- 異步模式(Asynchronous replication)
- 主服務器執行完一次事務后,立即將結果返回給客戶端,不關心從服務器是否已經同步
- 如果配置一主多從模式,默認為異步模式,當客戶端寫入數據時,主服務器直接返回給客戶端告知數據已經存儲成功,而不等從服務器進行數據同步
- 好處:用戶的體驗度高
- 壞處:當主服務器壞掉時,有可能一個服務器上都沒有客戶端寫過的數據,造成數據丟失
- 介于異步復制和完全同步復制之間
- 主服務器在執行完一次事務后,等待至少一臺從服務器同步數據完成,才將結果返回給客戶端
- 當客戶端存儲數據時,只要有一臺從服務器同步主服務器的數據時,主服務器就會直接回復客戶端數據存儲已經完成,客戶端的體驗度高,也可以保證至少有一臺從服務器有同步數據
- 等所有的從服務器存儲完數據后,主服務器才回復給客戶端告知數據存儲完成,客戶端的體驗度低,但每個從服務器中都有主服務器中的數據,當主服務器壞掉也可以正常使用
配置半同步模式
- 步驟:
1. 安裝模塊
2. 查看安裝模塊
3. 啟用模塊
命令行配置(馬上生效,但重啟服務會失效)host53主
查看是否允許動態加載模塊(默認允許)
mysql
> show variables like
"have_dynamic_loading";
+----------------------+-------+
| Variable_name
| Value
|
+----------------------+-------+
| have_dynamic_loading
| YES
|
+----------------------+-------+
1 row
in set (0.03 sec
)命令行安裝模塊
mysql
> install plugin rpl_semi_sync_master soname
"semisync_master.so";
Query OK, 0 rows affected
(0.07 sec
)查看模塊
mysql
> use information_schema
mysql
> desc PLUGINS
;
+------------------------+-------------+------+-----+---------+-------+
| Field
| Type
| Null
| Key
| Default
| Extra
|
+------------------------+-------------+------+-----+---------+-------+
| PLUGIN_NAME
| varchar
(64
) | NO
| | | |
| PLUGIN_VERSION
| varchar
(20
) | NO
| | | |
| PLUGIN_STATUS
| varchar
(10
) | NO
| | | |
| PLUGIN_TYPE
| varchar
(80
) | NO
| | | |
| PLUGIN_TYPE_VERSION
| varchar
(20
) | NO
| | | |
| PLUGIN_LIBRARY
| varchar
(64
) | YES
| | NULL
| |
| PLUGIN_LIBRARY_VERSION
| varchar
(20
) | YES
| | NULL
| |
| PLUGIN_AUTHOR
| varchar
(64
) | YES
| | NULL
| |
| PLUGIN_DESCRIPTION
| longtext
| YES
| | NULL
| |
| PLUGIN_LICENSE
| varchar
(80
) | YES
| | NULL
| |
| LOAD_OPTION
| varchar
(64
) | NO
| | | |
+------------------------+-------------+------+-----+---------+-------+
11 rows
in set (0.00 sec
)
查看模塊是否安裝成功
mysql
> select PLUGIN_NAME , PLUGIN_STATUS from PLUGINS where PLUGIN_NAME like
"%semi%";
+----------------------+---------------+
| plugin_name
| plugin_status
|
+----------------------+---------------+
| rpl_semi_sync_master
| ACTIVE
|
+----------------------+---------------+
1 row
in set (0.00 sec
)啟用master半同步復制模式
mysql
> set global rpl_semi_sync_master_enabled
=1
;
Query OK, 0 rows affected
(0.01 sec
)查看半同步復制模式是否已啟用
mysql
> show variables like
"rpl_semi_sync_%_enabled";
+------------------------------+-------+
| Variable_name
| Value
|
+------------------------------+-------+
| rpl_semi_sync_master_enabled
| ON
|
+------------------------------+-------+
1 row
in set (0.24 sec
)host54主從
安裝模塊
主:
mysql
> install plugin rpl_semi_sync_master soname
"semisync_master.so";
Query OK, 0 rows affected
(0.08 sec
)從:
mysql
> install plugin rpl_semi_sync_slave soname
"semisync_slave.so";
Query OK, 0 rows affected
(0.03 sec
)mysql
> use information_schema
mysql
> select plugin_name ,plugin_status from plugins where plugin_name like
"%semi%" ;
+----------------------+---------------+
| plugin_name
| plugin_status
|
+----------------------+---------------+
| rpl_semi_sync_master
| ACTIVE
|
| rpl_semi_sync_slave
| ACTIVE
|
+----------------------+---------------+
2 rows
in set (0.10 sec
)mysql
> set global rpl_semi_sync_master_enabled
=1
;
Query OK, 0 rows affected
(0.10 sec
)mysql
> set global rpl_semi_sync_slave_enabled
=1
;
Query OK, 0 rows affected
(0.00 sec
)mysql
> show variables like
"rpl_semi_sync_%_enabled";
+------------------------------+-------+
| Variable_name
| Value
|
+------------------------------+-------+
| rpl_semi_sync_master_enabled
| ON
|
| rpl_semi_sync_slave_enabled
| ON
|
+------------------------------+-------+
2 rows
in set (0.09 sec
)host55從mysql
> install plugin rpl_semi_sync_slave soname
"semisync_slave.so";
Query OK, 0 rows affected
(0.06 sec
)
mysql
> use information_schema
mysql
> select plugin_name ,plugin_status from plugins where plugin_name like
"%semi%" ;
+---------------------+---------------+
| plugin_name
| plugin_status
|
+---------------------+---------------+
| rpl_semi_sync_slave
| ACTIVE
|
+---------------------+---------------+
1 row
in set (0.00 sec
)mysql
> set global rpl_semi_sync_slave_enabled
=1
;
Query OK, 0 rows affected
(0.00 sec
)mysql
> show variables like
"rpl_semi_sync_%_enabled";
+-----------------------------+-------+
| Variable_name
| Value
|
+-----------------------------+-------+
| rpl_semi_sync_slave_enabled
| ON
|
+-----------------------------+-------+
1 row
in set (0.01 sec
)************************************************************
永久配置(/etc/my.cnf)
在host54主機上面操作,永久啟用半同步復制模式
[root@host54 mysql
]
[mysqld
]
server_id
=54
log_bin
=host54
log_slave_updates
plugin-load
="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl_semi_sync_master_enabled
=1
rpl_semi_sync_slave_enabled
=1重啟數據庫服務,并查看狀態信息
[root@host54 mysql
]
[root@host54 mysql
]
mysql
> select plugin_name, plugin_status from information_schema.plugins where plugin_name like
'%semi%';
+----------------------+---------------+
| plugin_name
| plugin_status
|
+----------------------+---------------+
| rpl_semi_sync_master
| ACTIVE
| //模塊已加載
| rpl_semi_sync_slave
| ACTIVE
|
+----------------------+---------------+
2 rows
in set (0.00 sec
)
mysql
> show variables like
"rpl_semi_sync_%_enabled";
+------------------------------+-------+
| Variable_name
| Value
|
+------------------------------+-------+
| rpl_semi_sync_master_enabled
| ON
| //模式已啟用
| rpl_semi_sync_slave_enabled
| ON
|
+------------------------------+-------+
2 rows
in set (0.00 sec
)
總結
- 實時備份,用于故障切換
- 讀寫分離,提供查詢服務
- 備份,避免影響業務
- 主庫開啟binlog日志(設置binlog參數)
- 主從server_id不同
- 從庫服務器要與主庫數據相同,可以連通主庫
- 主庫宕機后,數據可能會丟失
- 從庫只有一個SQL Thread,主庫寫的壓力大,復制可能會延時
- 解決辦法:
- 半同步復制,可以解決數據丟失的問題
總結
以上是生活随笔為你收集整理的DBA(三):MySQL主从同步、复制模式的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。