MySQL主主同步方案


  MySQL主主+Keepalived

  MySQL+DRBD+Heartbeat

在企業中,數據庫高可用一直是企業的重中之重,中小企業很多都是使用mysql主主方案,一主多從,讀寫分離等,但是單主存在單點故障,從庫切換成主庫需要作改動。因此,如果是雙主或者多主,就會增加mysql入口,增加高可用。不過多主需要考慮自增長ID問題,這個需要特別設置配置文件,比如雙主,可以使用奇偶,總之,主之間設置自增長ID相互不沖突就能完美解決自增長ID沖突問題。

 

主主方案實現思路

1、  兩台mysql都可讀寫,互為主備。默認只使用一台masterA負責數據的寫入,另一台masterB備用處於備用狀態;

2、  masterA是masterB的主庫,masterB又是masterA的主庫,它們互為主從;

3、  兩台主庫之間做高可用,可以采用keepalived等方案,使用VIP對外提供服務;

4、所有提供服務的從服務器與masterB進行主從同步(雙主多從);

5、建議采用高可用策略的時候,masterA或masterB均不因宕機恢復后而搶占VIP(非搶占模式);

這樣做可以在一定程度上保證主庫的高可用,在一台主庫down掉之后,可以在極短的時間內切換到另一台主庫上,盡可能減少主庫宕機對業務造成的影響,減少了主從同步給生產主庫帶來的壓力;

 

但是也有幾個不足的地方:

  • masterB可能會一直處於空閑狀態(可以用它當從庫,負責部分查詢);
  • 主庫后面提供服務的從庫要等masterB先同步完了數據后才能去masterB上去同步數據,這樣可能會造成一定程度的同步延時;

 

第1台機器

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

[mysqld]

server-id=1

log-bin=mysql-binlog                               #打開二進制功能

log-slave-updates=true

max_binlog_size=1024M                        #binlog單文件最大值 

auto_increment_offset = 1

auto_increment_increment = 2              #奇數ID

 

replicate-ignore-db = information_schema #忽略不同步主從的數據庫

replicate-ignore-db = performance_schema

replicate-ignore-db = test

replicate-ignore-db = mysql

 

max_connections = 3000

max_connect_errors = 30

 

skip-character-set-client-handshake     #忽略應用程序想要設置的其他字符集

init-connect='SET NAMES utf8'              #連接時執行的SQL

character-set-server=utf8                        #服務端默認字符集

wait_timeout=1800                                  #請求的最大連接時間

interactive_timeout=1800                      #和上一參數同時修改才會生效

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES        #sql模式

 

relay-log=relay-log-bin                             #開啟中繼日志

relay-log-index=slave-relay-bin.index

[root@localhost ~]# systemctl restart mariadb

[root@localhost ~]# mysql

MariaDB [(none)]> grant replication slave on *.* to 'repl'@'192.168.200.112' identified by '123456';

Query OK, 0 rows affected (0.00 sec)

 

MariaDB [(none)]> flush privileges;

Query OK, 0 rows affected (0.00 sec)

 

MariaDB [(none)]> show master status;

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

| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-binlog.000004 |      486 |              |                  |

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

1 row in set (0.00 sec)

 

第2台機器

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

[mysqld]

server-id       = 2

log-bin=mysql-binlog

log-slave-updates=true

max_binlog_size=1024M

auto_increment_offset = 2

auto_increment_increment = 2                               #偶數ID

 

replicate-ignore-db = information_schema

replicate-ignore-db = performance_schema

replicate-ignore-db = test

replicate-ignore-db = mysql

 

max_connections = 3000

max_connect_errors = 30

 

skip-character-set-client-handshake

init-connect='SET NAMES utf8'

character-set-server=utf8

wait_timeout=1800

interactive_timeout=1800

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

 

relay-log=relay-log-bin

relay-log-index=slave-relay-bin.index

[root@localhost ~]# systemctl restart mariadb

[root@localhost ~]# mysql

MariaDB [(none)]> grant replication slave on *.* to 'repl'@'192.168.200.111' identified by '123456';

Query OK, 0 rows affected (0.00 sec)

 

MariaDB [(none)]> flush privileges;

Query OK, 0 rows affected (0.00 sec)

 

MariaDB [(none)]> show master status;

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

| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-binlog.000001 |      889 |              |                  |

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

1 row in set (0.00 sec)

特別參數說明

log-slave-updates = true     #將復制事件寫入binlog,一台服務器既做主庫又做從庫此選項必須要開啟

masterA自增長ID

auto_increment_offset = 1

auto_increment_increment = 2    #奇數ID

masterB自增加ID

auto_increment_offset = 2

auto_increment_increment = 2    #偶數ID

 

測試環境,可以保證沒數據寫入。否則需要的步驟是:先masterA鎖表-->masterA備份數據-->masterA解鎖表 -->masterB導入數據-->masterB設置主從-->查看主從

第1台機器

MariaDB [(none)]> stop slave;

Query OK, 0 rows affected, 1 warning (0.00 sec)

 

MariaDB [(none)]> change master to master_host='192.168.200.112',master_port=3306,master_user='repl',master_password='123456',master_log_file='mysql-binlog.000001',master_log_pos=889;

Query OK, 0 rows affected (0.04 sec)

 

MariaDB [(none)]> start slave;

Query OK, 0 rows affected (0.01 sec)

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

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.200.112

                  Master_User: repl

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-binlog.000001

          Read_Master_Log_Pos: 889

               Relay_Log_File: relay-log-bin.000002

                Relay_Log_Pos: 532

        Relay_Master_Log_File: mysql-binlog.000001

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

 

第2台機器

MariaDB [(none)]> stop slave;

Query OK, 0 rows affected (0.01 sec)

 

MariaDB [(none)]> change master to master_host='192.168.200.111',master_port=3306,master_user='repl',master_password='123456',master_log_file='mysql-binlog.000004',master_log_pos=486;

Query OK, 0 rows affected (0.13 sec)

 

MariaDB [(none)]> start slave;

Query OK, 0 rows affected (0.01 sec)

 

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

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.200.111

                  Master_User: repl

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-binlog.000004

          Read_Master_Log_Pos: 486

               Relay_Log_File: relay-log-bin.000002

                Relay_Log_Pos: 532

        Relay_Master_Log_File: mysql-binlog.000004

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

 

第1台機器

MariaDB [(none)]> create database test01;

Query OK, 1 row affected (0.01 sec)

第2台機器

MariaDB [(none)]> show databases;

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

| Database           |

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

| information_schema |

| mysql              |

| performance_schema |

| test               |

| test01             |

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

5 rows in set (0.00 sec)

 

MariaDB [(none)]> create database test02;

Query OK, 1 row affected (0.00 sec)

 

第1台機器

MariaDB [(none)]> show databases;

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

| Database           |

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

| information_schema |

| mysql              |

| performance_schema |

| test               |

| test01             |

| test02             |

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

6 rows in set (0.00 sec)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM