一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|數(shù)據(jù)庫(kù)技術(shù)|

服務(wù)器之家 - 數(shù)據(jù)庫(kù) - mariadb - mariadb的主從復(fù)制、主主復(fù)制、半同步復(fù)制配置詳解

mariadb的主從復(fù)制、主主復(fù)制、半同步復(fù)制配置詳解

2020-06-05 20:01運(yùn)維部落 mariadb

這篇文章主要詳細(xì)介紹了mariadb的主從復(fù)制、主主復(fù)制、半同步復(fù)制的概念和方法,有需要的小伙伴可以參考下

主從服務(wù)器的時(shí)間要同步,數(shù)據(jù)庫(kù)版本最好是一致的,以免造成函數(shù)處理、日志讀取、日志解析等發(fā)生異常。

以下三個(gè)主從復(fù)制的設(shè)置是獨(dú)立的。

注意防火墻和selinux的影響。

1、簡(jiǎn)單主從復(fù)制的實(shí)現(xiàn)

(1)主服務(wù)器的配置

1)安裝mariadb-server

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

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

[root@localhost ~]# vim /etc/my.cnf

    在[mysqld]段的最后添加以下內(nèi)容

    skip_name_resolve = ON
    innodb_file_per_table = ON
    server-id = 1 (id號(hào)不能跟從服務(wù)器相同)
    log-bin = master-log (自定義二進(jìn)制日志文件名)

3)授權(quán)可以復(fù)制本地?cái)?shù)據(jù)庫(kù)信息的主機(jī)

?
1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost ~]# systemctl start mariadb.service (啟動(dòng)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 (查看主服務(wù)器的狀態(tài)信息,在從服務(wù)器中要用到)
*************************** 1. row ***************************
   File: master-log.000003 (正在使用的二進(jìn)制日志文件)
  Position: 497 (所處的位置)
 Binlog_Do_DB:
Binlog_Ignore_DB:

(2)從服務(wù)器的配置

1)安裝mariadb-server

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

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

[root@localhost ~]# vim /etc/my.cnf

    在[mysqld]段的最后添加以下內(nèi)容

    skip_name_resolve = ON
    innodb_file_per_table = ON
    server-id = 2 (id號(hào)不能跟主服務(wù)器相同)
    relay-log = slave-log (自定義二進(jìn)制日志文件名)

3)設(shè)置要從哪個(gè)主服務(wù)器的那個(gè)位置開始同步

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[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; (啟動(dòng)復(fù)制功能)
MariaDB [(none)]> show slave status\G (查看從服務(wù)器的狀態(tài),下面顯示的是部分內(nèi)容)
 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)測(cè)試

1)在主服務(wù)器導(dǎo)入事先準(zhǔn)備好的數(shù)據(jù)庫(kù)

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
MariaDB [(none)]> show databases;
+--------------------+
| Database   |
+--------------------+
| information_schema |
| hellodb   |(數(shù)據(jù)庫(kù)已經(jīng)同步)
| mysql    |
| performance_schema |
| test    |
+--------------------+
MariaDB [(none)]> use hellodb;
MariaDB [hellodb]> show tables; (hellodb數(shù)據(jù)庫(kù)的表也是同步的)
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes   |
| coc    |
| courses   |
| scores   |
| students   |
| teachers   |
| toc    |
+-------------------+

2、雙主復(fù)制的實(shí)現(xiàn)

(1)服務(wù)器1的配置

1)安裝mariadb-server

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

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

[root@localhost ~]# vim /etc/my.cnf

    在[mysqld]段的最后添加以下內(nèi)容

    skip_name_resolve = ON
    innodb_file_per_table = ON
    server-id = 1 (id號(hào)不能跟從服務(wù)器相同)
    log-bin = master-log (自定義主服務(wù)器的二進(jìn)制日志文件名)
    relay-log = slave-log (自定義從服務(wù)器的二進(jìn)制日志文件名)
    auto_increment_offset = 1
    auto_increment_increment = 2

3)在服務(wù)器2上查看的master狀態(tài)

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

4)啟動(dòng)mariadb server并進(jìn)行如下配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[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 (僅是部分內(nèi)容)
  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)服務(wù)器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)在服務(wù)器1查看master狀態(tài)

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

4)啟動(dòng)mariadb server并配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[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 (僅是部分內(nèi)容)
  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)測(cè)試

1)在任意一臺(tái)服務(wù)器上創(chuàng)建mydb數(shù)據(jù)庫(kù)

MariaDB [(none)]> create database mydb;

2)在另一臺(tái)服務(wù)器上查看

?
1
2
3
4
5
6
7
8
9
10
MariaDB [(none)]> show databases;
+--------------------+
| Database   |
+--------------------+
| information_schema |
| mydb    |
| mysql    |
| performance_schema |
| test    |
+--------------------+

3、半同步復(fù)制的實(shí)現(xiàn)

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

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)授權(quán)可以復(fù)制本地?cái)?shù)據(jù)庫(kù)信息的主機(jī)

?
1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost ~]# systemctl start mariadb.service (啟動(dòng)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 (查看主服務(wù)器的狀態(tài)信息,在從服務(wù)器中要用到)
*************************** 1. row ***************************
   File: master-log.000003 (正在使用的二進(jìn)制日志文件)
  Position: 245 (所處的位置)
 Binlog_Do_DB:
Binlog_Ignore_DB:

4)安裝rpl semi sync_master插件,并啟用

?
1
2
3
4
[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;

補(bǔ)充:

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

(2)從服務(wù)器的配置

1)安裝mariadb-server

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

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

[root@localhost ~]# vim /etc/my.cnf

    在[mysqld]段的最后添加以下內(nèi)容

    skip_name_resolve = ON
    innodb_file_per_table = ON
    server-id = 2 (id號(hào)不能跟主服務(wù)器相同)
    relay-log = slave-log (自定義二進(jìn)制日志文件名)

3)設(shè)置要從哪個(gè)主服務(wù)器的那個(gè)位置開始同步

?
1
2
3
4
5
[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)安裝rpl semi sync_slave插件并啟用

?
1
2
3
4
5
[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;

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

?
1
2
MariaDB [(none)]> show global status like '%semi%';
 Rpl_semi_sync_master_clients 1 (從服務(wù)器有一臺(tái))

(3)測(cè)試

測(cè)試以個(gè)人實(shí)際情況而定
1)在主服務(wù)器上導(dǎo)入事先準(zhǔn)備好的數(shù)據(jù)庫(kù)hellodb.sql

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

2)在主服務(wù)器上查看半同步復(fù)制的狀態(tài)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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)在從服務(wù)器上查看是否同步

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

補(bǔ)充:基于上面的半同步復(fù)制配置復(fù)制的過濾器,復(fù)制過濾最好在從服務(wù)器上設(shè)置,步驟如下

(1)從服務(wù)器的配置

1)關(guān)閉mariadb server

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

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

?
1
2
3
4
5
6
[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 (只復(fù)制mydb數(shù)據(jù)庫(kù)的內(nèi)容)

補(bǔ)充:常用的過濾選項(xiàng)如下

    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后,半同步復(fù)制功能將被關(guān)閉,因此要重新啟動(dòng)

?
1
2
3
4
5
6
7
8
9
10
11
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;(需先關(guān)閉從服務(wù)器復(fù)制功能再重啟)
MariaDB [(none)]> start slave;

(2)測(cè)試

1)主服務(wù)器上的hellodb數(shù)據(jù)庫(kù)創(chuàng)建一個(gè)新表semitable

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

2)在從服務(wù)器上查看hellodb數(shù)據(jù)庫(kù)是否有semitable

?
1
2
3
4
5
6
7
8
9
10
11
12
13
MariaDB [(none)]> use hellodb
MariaDB [hellodb]> show tables;(并沒有)
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes   |
| coc    |
| courses   |
| scores   |
| students   |
| teachers   |
| toc    |
+-------------------+

3)在主服務(wù)器上創(chuàng)建mydb數(shù)據(jù)庫(kù),并為其創(chuàng)建一個(gè)tbl1表

MariaDB [hellodb]> create database mydb;

4)在從服務(wù)器上查看mydb數(shù)據(jù)庫(kù)的是否有tbl1表

?
1
2
3
4
5
6
7
MariaDB [hellodb]> use mydb;
MariaDB [mydb]> show tables; (可以查看到)
+----------------+
| Tables_in_mydb |
+----------------+
| tbl1   |
+----------------+

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 午夜国产精品视频 | 我们中文在线观看免费完整版 | 国内精品久久久久小说网 | 色婷婷综合和线在线 | 日本人交换乱理伦片 | 久久亚洲精选 | 亚洲日本va中文字幕 | 国产欧美一区二区三区精品 | beeg日本高清xxxx18| 激情偷拍网| 三星w999| 国产欧美一区二区成人影院 | 亚洲国产成人精品 | 日本网络视频www色高清免费 | 成人资源在线观看 | 四虎免费在线观看 | 色综合天天综合网看在线影院 | 亚洲精品综合一二三区在线 | 亚洲国产精品高清在线 | 亚洲成人影院在线 | 欧美人在线一区二区三区 | 天美传媒影视在线免费观看 | 国产精品久久久久不卡绿巨人 | 水多多凹凸福利视频导航 | 日本在线你懂的 | 91理论片午午伦夜理片久久 | 天天色天天综合 | 4s4s4s4s色大众影视 | 超h 超重口 高h 污肉1v1 | 午夜精品免费 | 香蕉久草在线 | 欧美贵妇vs高跟办公室 | 男女18一级大黄毛片免 | 非洲一级毛片又粗又长aaaa | 无人区在线观看免费视频国语 | 动漫美女羞羞视频 | 小浪妇奶真大水多 | 免费观看视频在线播放 | 午夜DY888国产精品影院 | 黄版快手 | 麻生希在线 |