mariadb的主從復制、主主復制、半同步復制

主從服務器的時間要同步,數據庫版本最好是一致的,以免造成函數處理、日志讀取、日志解析等發生異常。
以下三個主從復制的設置是獨立的。
注意防火墻和selinux的影響。

1、簡單主從復制的實現

(1)主服務器的配置

1)安裝mariadb-server

[root@localhost ~]# yum -y install mariadb-server

2)編輯/etc/my.cnf文件

[root@localhost ~]# vim /etc/my.cnf
    在[mysqld]段的最后添加以下內容
    skip_name_resolve = ON
    innodb_file_per_table = ON
    server-id = 1 (id號不能跟從服務器相同)
    log-bin = master-log (自定義二進制日志文件名)

3)授權可以復制本地數據庫信息的主機

[root@localhost ~]# systemctl start mariadb.service (啟動mariadb server)

[root@localhost ~]# mysql
    MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'10.1.51.%' identified by 'replpasswd';
    MariaDB [(none)]> flush privileges;

MariaDB [(none)]> show master status\G (查看主服務器的狀態信息,在從服務器中要用到)
*************************** 1. row ***************************
            File: master-log.000003 (正在使用的二進制日志文件)
        Position: 497 (所處的位置)
    Binlog_Do_DB: 
Binlog_Ignore_DB:

(2)從服務器的配置

1)安裝mariadb-server

[root@localhost ~]# yum -y install mariadb-server

2)編輯/etc/my.cnf文件

[root@localhost ~]# vim /etc/my.cnf
    在[mysqld]段的最后添加以下內容
    skip_name_resolve = ON
    innodb_file_per_table = ON
    server-id = 2 (id號不能跟主服務器相同)
    relay-log = slave-log (自定義二進制日志文件名)

3)設置要從哪個主服務器的那個位置開始同步

[root@localhost ~]# systemctl start mariadb.service

[root@localhost ~]# mysql
    MariaDB [(none)]> change master to master_host='10.1.51.60',master_user='repluser',master_password='replpasswd',master_log_file='master-log.000003',master_log_pos=497;

MariaDB [(none)]> start slave; (啟動復制功能)
MariaDB [(none)]> show slave status\G (查看從服務器的狀態,下面顯示的是部分內容)
    Master_Host: 10.1.51.60
    Master_User: repluser
    Master_Port: 3306
    Connect_Retry: 60
    Master_Log_File: master-log.000003
    Read_Master_Log_Pos: 497
    Relay_Log_File: slave-log.000002
    Relay_Log_Pos: 530
    Relay_Master_Log_File: master-log.000003
    Slave_IO_Running: Yes 
    Slave_SQL_Running: Yes
    Master_Server_Id: 1

(3)測試

1)在主服務器導入事先準備好的數據庫

[root@localhost ~]# mysql < hellodb.sql

2)在從服務器查看是否同步

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |(數據庫已經同步)
| mysql              |
| performance_schema |
| test               |
+--------------------+
MariaDB [(none)]> use hellodb;
MariaDB [hellodb]> show tables; (hellodb數據庫的表也是同步的)
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes           |
| coc               |
| courses           |
| scores            |
| students          |
| teachers          |
| toc               |
+-------------------+

2、雙主復制的實現

(1)服務器1的配置

1)安裝mariadb-server

[root@localhost ~]# yum -y install mariadb-server

2)編輯/etc/my.cnf文件

[root@localhost ~]# vim /etc/my.cnf
    在[mysqld]段的最后添加以下內容
    skip_name_resolve = ON
    innodb_file_per_table = ON
    server-id = 1 (id號不能跟從服務器相同)
    log-bin = master-log (自定義主服務器的二進制日志文件名)
    relay-log = slave-log (自定義從服務器的二進制日志文件名)
    auto_increment_offset = 1 
    auto_increment_increment = 2

3)在服務器2上查看的master狀態

MariaDB [(none)]> show master status\G
*************************** 1. row ***************************
            File: master-log.000003
        Position: 422
    Binlog_Do_DB: 
Binlog_Ignore_DB:

4)啟動mariadb server并進行如下配置

[root@localhost ~]# systemctl start mariadb.service

[root@localhost ~]# mysql

    MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'10.1.51.%' identified by 'replpasswd';

    MariaDB [(none)]> change master to master_host='10.1.51.50',master_user='repluser',master_password='replpasswd',master_log_file='master-log.000003',master_log_pos=422;

    MariaDB [(none)]> start slave;

    MariaDB [(none)]> SHOW SLAVE STATUS\G (僅是部分內容)
        Master_Host: 10.1.51.50
        Master_User: repluser
        Master_Port: 3306
        Connect_Retry: 60
        Master_Log_File: master-log.000003
        Read_Master_Log_Pos: 422
        Relay_Log_File: slave-log.000002
        Relay_Log_Pos: 530
        Relay_Master_Log_File: master-log.000003
        Slave_IO_Running: Yes
        Slave_SQL_Running: Yes
        Master_Server_Id: 2

(2)服務器2的配置

1)安裝mariadb-server

[root@localhost ~]# yum -y install mariadb-server

2)編輯/etc/my.cnf文件

[root@localhost ~]# vim /etc/my.cnf
    skip_name_resolve = ON
    innodb_file_per_table = ON
    server-id = 2
    relay-log = slave-log
    lob-bin = master-log
    auto_increment_offset = 2 
    auto_increment_increment = 2

3)在服務器1查看master狀態

MariaDB [(none)]> show master status\G
*************************** 1. row ***************************
            File: master-log.000003
        Position: 245
    Binlog_Do_DB: 
Binlog_Ignore_DB:

4)啟動mariadb server并配置

[root@localhost ~]# systemctl start mariadb.service

[root@localhost ~]# mysql

    MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'10.1.51.%' identified by 'replpasswd';

    MariaDB [(none)]> change master to master_host='10.1.51.60',master_user='repluser',master_password='replpasswd',master_log_file='master-log.000003',master_log_pos=245;

    MariaDB [(none)]> start slave;

    MariaDB [(none)]> show slave status\G (僅是部分內容)  
        Master_Host: 10.1.51.60
        Master_User: repluser
        Master_Port: 3306
        Connect_Retry: 60
        Master_Log_File: master-log.000003
        Read_Master_Log_Pos: 422
        Relay_Log_File: slave-log.000003
        Relay_Log_Pos: 530
        Relay_Master_Log_File: master-log.000003
        Slave_IO_Running: Yes
        Slave_SQL_Running: Yes
        Master_Server_Id: 1

(3)測試

1)在任意一臺服務器上創建mydb數據庫

MariaDB [(none)]> create database mydb;

2)在另一臺服務器上查看

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| test               |
+--------------------+

3、半同步復制的實現

(1)在主服務器上的配置

1)安裝mariadb-server

[root@localhost ~]# yum -y install mariadb-server

2)編輯/etc/my.cnf

[root@localhost ~]# vim /etc/my.cnf
    skip_name_resolve = ON
    innodb_file_per_table = ON
    server-id = 1
    log-bin = master-log

3)授權可以復制本地數據庫信息的主機

[root@localhost ~]# systemctl start mariadb.service (啟動mariadb server)

[root@localhost ~]# mysql
    MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'10.1.51.%' identified by 'replpasswd';
    MariaDB [(none)]> flush privileges;

MariaDB [(none)]> show master status\G (查看主服務器的狀態信息,在從服務器中要用到)
*************************** 1. row ***************************
            File: master-log.000003 (正在使用的二進制日志文件)
        Position: 245 (所處的位置)
    Binlog_Do_DB: 
Binlog_Ignore_DB:

4)安裝rplsemisync_master插件,并啟用

[root@localhost ~]# mysql

MariaDB [(none)]> install plugin rpl_semi_sync_master soname 'semisync_master.so';
MariaDB [(none)]> set global rpl_semi_sync_master_enabled = ON; 

補充:
MariaDB [(none)]> show plugins;(可查看插件是否激活)
MariaDB [(none)]> show global variables like 'rpl_semi%';(可查看安裝的插件是否啟用)
MariaDB [(none)]> show global status like '%semi%';(可查看從服務器的個數,此時是0個)

(2)從服務器的配置

1)安裝mariadb-server

[root@localhost ~]# yum -y install mariadb-server

2)編輯/etc/my.cnf文件

[root@localhost ~]# vim /etc/my.cnf
    在[mysqld]段的最后添加以下內容
    skip_name_resolve = ON
    innodb_file_per_table = ON
    server-id = 2 (id號不能跟主服務器相同)
    relay-log = slave-log (自定義二進制日志文件名)

3)設置要從哪個主服務器的那個位置開始同步

[root@localhost ~]# systemctl start mariadb.service

[root@localhost ~]# mysql

    MariaDB [(none)]> change master to master_host='10.1.51.60',master_user='repluser',master_password='replpasswd',master_log_file='master-log.000003',master_log_pos=245;

4)安裝rplsemisync_slave插件并啟用

[root@localhost ~]# mysql   

    MariaDB [(none)]> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
    MariaDB [(none)]> set global rpl_semi_sync_slave_enabled = ON;
    MariaDB [(none)]> start slave;

完成上面配置后,可以在主服務器上查看半同步復制的相關信息,命令如下:

MariaDB [(none)]> show global status like '%semi%';
    Rpl_semi_sync_master_clients    1 (從服務器有一臺)

(3)測試

測試以個人實際情況而定

1)在主服務器上導入事先準備好的數據庫hellodb.sql

MariaDB [hellodb]> source /root/hellodb.sql;

2)在主服務器上查看半同步復制的狀態

MariaDB [hellodb]> show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-log.000003 |     8102 |              |                  |
+-------------------+----------+--------------+------------------+

MariaDB [hellodb]> show global status like '%semi%';
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 1     |
| Rpl_semi_sync_master_net_avg_wait_time     | 1684  |
| Rpl_semi_sync_master_net_wait_time         | 60630 |
| Rpl_semi_sync_master_net_waits             | 36    |
| Rpl_semi_sync_master_no_times              | 1     |
| Rpl_semi_sync_master_no_tx                 | 1     |
| Rpl_semi_sync_master_status                | ON    |
| Rpl_semi_sync_master_timefunc_failures     | 0     |
| Rpl_semi_sync_master_tx_avg_wait_time      | 1884  |
| Rpl_semi_sync_master_tx_wait_time          | 65965 |
| Rpl_semi_sync_master_tx_waits              | 35    |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
| Rpl_semi_sync_master_wait_sessions         | 0     |
| Rpl_semi_sync_master_yes_tx                | 35    |
+--------------------------------------------+-------+

3)在從服務器上查看是否同步

MariaDB [(none)]> show databases;
MariaDB [(none)]> use hellodb;
MariaDB [hellodb]> select * from students;

補充:基于上面的半同步復制配置復制的過濾器,復制過濾最好在從服務器上設置,步驟如下

(1)從服務器的配置

1)關閉mariadb server

[root@localhost ~]# systemctl stop mariadb.service

2)編輯/etc/my.cnf文件

[root@localhost ~]# vim /etc/my.cnf
    skip_name_resolve = ON
    innodb_file_per_table = ON
    server-id = 2
    relay-log = slave-log
    replicate-do-db = mydb (只復制mydb數據庫的內容)

補充:常用的過濾選項如下
    Replicate_Do_DB=
    Replicate_Ignore_DB=
    Replicate_Do_Table=
    Replicate_Ignore_Table=
    Replicate_Wild_Do_Table=
    Replicate_Wild_Ignore_Table=

3)重啟mariadb server

[root@localhost ~]# systemctl start mariadb.service

4)重啟mariadb server后,半同步復制功能將被關閉,因此要重新啟動

MariaDB [(none)]> show global variables like '%semi%';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled     | OFF   |
| rpl_semi_sync_slave_trace_level | 32    |
+---------------------------------+-------+

MariaDB [(none)]> set global rpl_semi_sync_slave_enabled = ON;
MariaDB [(none)]> stop slave;(需先關閉從服務器復制功能再重啟)
MariaDB [(none)]> start slave;

(2)測試

1)主服務器上的hellodb數據庫創建一個新表semitable

MariaDB [hellodb]> create table semitable (id int);

2)在從服務器上查看hellodb數據庫是否有semitable

MariaDB [(none)]> use hellodb
MariaDB [hellodb]> show tables;(并沒有)
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes           |
| coc               |
| courses           |
| scores            |
| students          |
| teachers          |
| toc               |
+-------------------+

3)在主服務器上創建mydb數據庫,并為其創建一個tbl1表

MariaDB [hellodb]> create database mydb;

4)在從服務器上查看mydb數據庫的是否有tbl1表

MariaDB [hellodb]> use mydb;
MariaDB [mydb]> show tables; (可以查看到)
+----------------+
| Tables_in_mydb |
+----------------+
| tbl1           |
+----------------+

原創文章,作者:pao,如若轉載,請注明出處:http://www.www58058.com/60221

(0)
paopao
上一篇 2016-11-20
下一篇 2016-11-21

相關推薦

  • 什么是文件系統

    文件系統:層級結構;有索引; /: 原初起點; 倒置樹狀結構; /dev/pts/2: 最左側/: 表示根目錄 其它的/: 表示路徑分隔符 Linux的路徑分隔符是/ Windows的是\ 文件的路徑表示: 絕對路徑:從根開始表示出的路徑  相對路徑:從當前位置開始表示出的路徑 文件名使用法則: 嚴格區分字符大小寫:file1, File1, FI…

    Linux干貨 2016-10-29
  • linux時間設置、screen使用、命令分類、hash作用、命令引用及history命令

    一、 生產環境發現一臺服務器系統時間產生偏差,造成服務異常 解決方法             a、 如果服務器硬件時間準備的話,可使用命令:~#] hwclock -s              將硬件時鐘同步到系統 &…

    Linux干貨 2016-08-02
  • 三.Linux博客-2016年7月24日幫助、history、別名、tree

    格式說明: 操作 概念 命令 說明及舉例 三-1.幫助、history、別名、tree touch /etc/nologin 使普通用戶不能登錄(創建了一個文件,刪掉就可以登陸)   ll /etc/nologin 查看那個文件 -rm -f /etc/  刪…

    Linux干貨 2016-08-23
  • 第一周n28

    1.計算機的組成及功能 計算機由五大部件組成。控制器、運算器、存儲器、輸入輸出設備。 1.控制器(Control):是整個計算機的中樞神經,其功能是對程序規定的控制信息進行解釋,根據其要求進行控制,調度程序、數據、地址,協調計算機各部分工作及內存與外設的訪問等。 2.運算器(Datapath):運算器的功能是對數據進行各種算術運算和邏輯運算,即對數據進行加工…

    Linux干貨 2017-12-03
  • MySQL入門命令知識

    簡單介紹下吧,MySQL應用的場景大多數互聯網公司第一次賣身是賣個了sun好像是10億,第二次是連同sun自己,以74億美元被賣給了Orecle~后面MySQL原作者站出來說,MySQL會存在閉源風險,整了個MariaDB~我也是醉了,也不考慮下我們的痛苦!下面簡要介紹下MySQL的入門知識。    一、MySQL有三種定義語言 &nbs…

    2016-12-05
  • 馬哥教育網絡班21期+第6周課程練習

    請詳細總結vim編輯器的使用并完成以下練習題 1、復制/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#; [root@localhost ~]# cp /etc/rc.d/rc.sysinit /tmp [root@localhost tmp]# vim rc.sysin…

    Linux干貨 2016-08-02
欧美性久久久久