您好,登錄后才能下訂單哦!
一、背景介紹
MySQL主從復制能解決一定的單點故障問題,但其異步的工作特性存在一定的隱患,比如主節點事務提交后還未寫入binlog,此時主節點故障后,但主節點認為事務已提交,從節點無法從主節點獲取到該事務的信息,提升為主節點后就會造成數據丟失,為了解決這一問題,谷歌為mysql在5.5之后的版本引進了半同步復制
二、原理介紹
所謂半同步復制就是一主多從,或一主一從的時候,主節點事務提交后至少等待一個從節點寫入中繼日志,這樣就保證了當主節點故障后,所有的操作都在中繼日志中有保存,當等待從節點的時間超出設置范圍,會回復異步的方式
三、環境介紹
本次實驗使用CentOS7.4系統,5.5.56-MariaDB,實驗拓撲如下圖所示:
使用一個日志服務器(也可以是從服務器)作為與主節點半同步的服務器,其他從服務器通過日志服務器同步binlog,由于日志服務器自身只需要記錄binlog,所以不需要數據在其上執行一遍,存儲引擎可以使用blackhole
四、操作步驟
1.主節點服務器操作
(1)啟動主節點服務,配置文件如下
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip_name_resolve=ON
log_bin=mysql-binlog
slow_query_log=ON
server-id=10
innodb_file_per_table=ON
binlog_format=ROW
(2)創建一個主從復制的賬號
MariaDB [mysql]> grant replication slave on *.* to 'bak'@'172.16.10.%' identified by 'bakpass';
MariaDB [mysql]> flush privileges;
(3)安裝并啟動半同步插件(可寫入配置文件永久生效)
MariaDB [(none)]> install plugin rpl_semi_sync_master soname 'semisync_master.so';
MariaDB [(none)]> set global rpl_semi_sync_master_enabled=ON;
(4)確認插件和功能以正常啟動
MariaDB [(none)]> show global variables like '%semi%';
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| rpl_semi_sync_master_enabled | ON |
| rpl_semi_sync_master_timeout | 10000 |
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_no_slave | ON |
+------------------------------------+-------+
MariaDB [(none)]> show plugins;
2.日志服務器操作
(1)啟動日志服務器,配置文件如下
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip_name_resolve=ON
relay_log=mysql-relaylog
relay_log_index=mysql-relaylog
read_only=ON
relay_log_purge=ON
slow_query_log=ON
server-id=20
innodb_file_per_table=ON
default_storage_engine=blackhole
binlog_format=ROW
log_bin=mysql-binlog
log_slave_updates=ON
(2)關閉read_only,創建一個主從復制的賬號后再開啟
MariaDB [(none)]> set global read_only=0;
MariaDB [(none)]> grant replication slave on *.* to 'bak'@'172.16.10.%' identified by 'bakpass';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> set global read_only=1;
(3)安裝并啟動半同步插件
MariaDB [(none)]> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
MariaDB [(none)]> set global rpl_semi_sync_slave_enabled=ON;
(4)確認插件和功能以正常啟動
MariaDB [(none)]> show global variables like '%semi%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled | ON |
| rpl_semi_sync_slave_trace_level | 32 |
+---------------------------------+-------+
MariaDB [(none)]> show plugins;
(5)指定主服務器及主服務器當前的binlog日志和position
MariaDB [hellodb]> change master to
-> master_host='172.16.10.30',
-> master_user='bak',
-> master_password='bakpass',
-> master_port=3306,
-> master_log_file='mysql-binlog.000003',
-> master_log_pos=7805,
-> master_connect_retry=10;
(6)啟動從節點(可以指啟動指定線程類型,不指定為都啟動)
MariaDB [hellodb]> start slave [IO_THREAD | SQL_THREAD];
(7)查看從節點狀態(Slave_IO_Running和Slave_SQL_Running顯示Yes成功)
MariaDB [(none)]> show slave status \G
3.驗證日志服務器blackhole存儲引擎是否生效
(1)主節點創建一個數據庫及表
MariaDB [(none)]> create database ark;
MariaDB [(none)]> use ark;
MariaDB [ark]> CREATE TABLE `students` ( `StuID` int(10) primary key, `Name` varchar(50) );
(2)確認當前主節點的binlog日志及position
MariaDB [ark]> show master status;
+---------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------------+----------+--------------+------------------+
| mysql-binlog.000003 | 8638 | | |
+---------------------+----------+--------------+------------------+
(3)確認日志服務器當前的binlog日志及position
MariaDB [ark]> show master status;
+---------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------------+----------+--------------+------------------+
| mysql-binlog.000003 | 1104 | | |
+---------------------+----------+--------------+------------------+
(4)主服務器上任意插入條數據,再次觀察主節點的binlog日志及position
MariaDB [ark]> insert into students (stuid,name) values (3,'fu');
MariaDB [ark]> show master status;
+---------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------------+----------+--------------+------------------+
| mysql-binlog.000003 | 8818 | | |
+---------------------+----------+--------------+------------------+
(5)在日志服務器上查看中繼日志以更新
MariaDB [ark]> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.10.30
Master_User: bak
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-binlog.000003
Read_Master_Log_Pos: 8818
Relay_Log_File: mysql-relaylog.000002
Relay_Log_Pos: 1545
Relay_Master_Log_File: mysql-binlog.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Exec_Master_Log_Pos: 8818
(6)并且日志服務器上的binlog日志也完成了更新
MariaDB [ark]> show master status;
+---------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------------+----------+--------------+------------------+
| mysql-binlog.000003 | 1309 | | |
+---------------------+----------+--------------+------------------+
(7)但此時表中卻無任何數據,說明blackhole引擎工作正常
4.從節點服務器操作
從節點就是將日志服務器當成主節點,所有的同步是指向日志服務器
(1)啟動從節點的mysql服務,啟動relaylog日志,更改serverid
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip_name_resolve=ON
relay_log=mysql-relaylog
relay_log_index=mysql-relaylog
read_only=ON
relay_log_purge=ON
slow_query_log=ON
server-id=30
innodb_file_per_table=ON
(2)指定主服務器及主服務器當前的binlog日志和position
MariaDB [(none)]> change master to
-> master_host='172.16.10.40',
-> master_user='bak',
-> master_password='bakpass',
-> master_port=3306,
-> master_log_file='mysql-binlog.000003',
-> master_log_pos=1309,
-> master_connect_retry=10;
(3)啟動從節點(可以指啟動指定線程類型,不指定為都啟動)
MariaDB [(none)]> start slave [IO_THREAD | SQL_THREAD];
(5)查看從節點狀態(Slave_IO_Running和Slave_SQL_Running顯示Yes成功)
MariaDB [(none)]> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.10.40
Master_User: bak
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-binlog.000003
Read_Master_Log_Pos: 1309
Relay_Log_File: mysql-relaylog.000002
Relay_Log_Pos: 532
Relay_Master_Log_File: mysql-binlog.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
五、結果驗證
(1)在日志服務器查看上游節點
MariaDB [(none)]> show slave hosts;
+-----------+------+------+-----------+
| Server_id | Host | Port | Master_id |
+-----------+------+------+-----------+
| 20 | | 3306 | 10 |
+-----------+------+------+-----------+
(2)在從節點查看上游節點
MariaDB [(none)]> show slave hosts;
+-----------+------+------+-----------+
| Server_id | Host | Port | Master_id |
+-----------+------+------+-----------+
| 30 | | 3306 | 20 |
+-----------+------+------+-----------+
(3)主庫上創建個數據庫,進行驗證,至此操作完成
MariaDB [(none)]> create database wahaha;
MariaDB [(none)]> show status like '%semi%';
Rpl_semi_sync_master_no_tx 沒有成功接收slave提交的次數
補充說明:
1.blackhole存儲引擎會將所有關于數據的操作丟棄,但是關于數據庫結構的操作會執行(如創建數據庫,表)
2.Mariadb5.5或者MySQL5.7以后的版本支持多主一從,從服務器可以成為多臺MySQL服務器的統一日志服務器,但要求是多臺MySQL服務器上數據庫名稱必須不相同,同時所有從服務器使用不同的復制賬號,因為主服務器上每個I/O dump thread都需要用不同的賬號發起
MariaDB [(none)]> show processlist \G
*************************** 3. row ***************************
Id: 7
User: bak
Host: 172.16.10.40:47972
db: NULL
Command: Binlog Dump
Time: 15921
State: Master has sent all binlog to slave; waiting for binlog to be updated
Info: NULL
Progress: 0.000
*************************** 5. row ***************************
Id: 17
User: qwe
Host: 172.16.10.50:58968
db: NULL
Command: Binlog Dump
Time: 1866
State: Master has sent all binlog to slave; waiting for binlog to be updated
Info: NULL
Progress: 0.000
3.如果是先開啟了主從復制,再切換到半同步,只需要執行
MariaDB [(none)]> stop slave io_thread;
MariaDB [(none)]> start slave io_thread;
4.如果服務器重啟,啟動MySQL服務器后,plugin插件和主從復制功能會自動啟動,但是半同步功能未啟動,此時是默認的異步同步(或者配置完成半同步后將該選項寫入配置文件),需要手動啟動半同步功能和io_thread,執行命令為:
主節點:
MariaDB [(none)]> set global rpl_semi_sync_master_enabled=ON;
從節點:
MariaDB [(none)]> set global rpl_semi_sync_slave_enabled=ON;
MariaDB [(none)]> stop slave io_thread;
MariaDB [(none)]> start slave io_thread;
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。