溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

初識MariaDB之6——半同步復制

發(fā)布時間:2020-07-20 01:23:17 來源:網絡 閱讀:1054 作者:qiao645 欄目:MySQL數據庫

一、背景介紹

MySQL主從復制能解決一定的單點故障問題,但其異步的工作特性存在一定的隱患,比如主節(jié)點事務提交后還未寫入binlog,此時主節(jié)點故障后,但主節(jié)點認為事務已提交,從節(jié)點無法從主節(jié)點獲取到該事務的信息,提升為主節(jié)點后就會造成數據丟失,為了解決這一問題,谷歌為mysql在5.5之后的版本引進了半同步復制

二、原理介紹

所謂半同步復制就是一主多從,或一主一從的時候,主節(jié)點事務提交后至少等待一個從節(jié)點寫入中繼日志,這樣就保證了當主節(jié)點故障后,所有的操作都在中繼日志中有保存,當等待從節(jié)點的時間超出設置范圍,會回復異步的方式

三、環(huán)境介紹

本次實驗使用CentOS7.4系統(tǒng),5.5.56-MariaDB,實驗拓撲如下圖所示:

初識MariaDB之6——半同步復制初識MariaDB之6——半同步復制

使用一個日志服務器(也可以是從服務器)作為與主節(jié)點半同步的服務器,其他從服務器通過日志服務器同步binlog,由于日志服務器自身只需要記錄binlog,所以不需要數據在其上執(zhí)行一遍,存儲引擎可以使用blackhole

四、操作步驟

1.主節(jié)點服務器操作

(1)啟動主節(jié)點服務,配置文件如下

[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)創(chuàng)建一個主從復制的賬號

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;

初識MariaDB之6——半同步復制初識MariaDB之6——半同步復制

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,創(chuàng)建一個主從復制的賬號后再開啟

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;

初識MariaDB之6——半同步復制初識MariaDB之6——半同步復制

(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)啟動從節(jié)點(可以指啟動指定線程類型,不指定為都啟動)

MariaDB [hellodb]> start slave [IO_THREAD | SQL_THREAD];

(7)查看從節(jié)點狀態(tài)(Slave_IO_Running和Slave_SQL_Running顯示Yes成功)

MariaDB [(none)]> show slave status \G

3.驗證日志服務器blackhole存儲引擎是否生效

(1)主節(jié)點創(chuàng)建一個數據庫及表

MariaDB [(none)]> create database ark;

MariaDB [(none)]> use ark;

MariaDB [ark]> CREATE TABLE `students` (   `StuID` int(10) primary key,   `Name` varchar(50) );

(2)確認當前主節(jié)點的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)主服務器上任意插入條數據,再次觀察主節(jié)點的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引擎工作正常

初識MariaDB之6——半同步復制初識MariaDB之6——半同步復制

4.從節(jié)點服務器操作

從節(jié)點就是將日志服務器當成主節(jié)點,所有的同步是指向日志服務器

(1)啟動從節(jié)點的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)啟動從節(jié)點(可以指啟動指定線程類型,不指定為都啟動)

MariaDB [(none)]> start slave [IO_THREAD | SQL_THREAD];

(5)查看從節(jié)點狀態(tài)(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)在日志服務器查看上游節(jié)點

MariaDB [(none)]> show slave hosts;

+-----------+------+------+-----------+

| Server_id | Host | Port | Master_id |

+-----------+------+------+-----------+

|        20 |      | 3306 |        10 |

+-----------+------+------+-----------+

(2)在從節(jié)點查看上游節(jié)點

MariaDB [(none)]> show slave hosts;

+-----------+------+------+-----------+

| Server_id | Host | Port | Master_id |

+-----------+------+------+-----------+

|        30 |      | 3306 |        20 |

+-----------+------+------+-----------+

(3)主庫上創(chuàng)建個數據庫,進行驗證,至此操作完成

MariaDB [(none)]> create database wahaha;

MariaDB [(none)]> show status like '%semi%';

初識MariaDB之6——半同步復制初識MariaDB之6——半同步復制

Rpl_semi_sync_master_no_tx 沒有成功接收slave提交的次數

補充說明:

1.blackhole存儲引擎會將所有關于數據的操作丟棄,但是關于數據庫結構的操作會執(zhí)行(如創(chuàng)建數據庫,表)

2.Mariadb5.5或者MySQL5.7以后的版本支持多主一從,從服務器可以成為多臺MySQL服務器的統(tǒng)一日志服務器,但要求是多臺MySQL服務器上數據庫名稱必須不相同,同時所有從服務器使用不同的復制賬號,因為主服務器上每個I/O dump thread都需要用不同的賬號發(fā)起

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.如果是先開啟了主從復制,再切換到半同步,只需要執(zhí)行

MariaDB [(none)]> stop slave io_thread;

MariaDB [(none)]> start slave io_thread;

4.如果服務器重啟,啟動MySQL服務器后,plugin插件和主從復制功能會自動啟動,但是半同步功能未啟動,此時是默認的異步同步(或者配置完成半同步后將該選項寫入配置文件),需要手動啟動半同步功能和io_thread,執(zhí)行命令為:

主節(jié)點:

MariaDB [(none)]> set global rpl_semi_sync_master_enabled=ON;

從節(jié)點:

MariaDB [(none)]> set global rpl_semi_sync_slave_enabled=ON;

MariaDB [(none)]> stop slave io_thread;

MariaDB [(none)]> start slave io_thread;


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI