1. 程式人生 > >MySQL高可用群集

MySQL高可用群集

cti system 不同 刷新 修改網卡 repl lin 主主復制 chang

MySQL高可用群集

技能目標

  • 熟悉MMM高可用架構
  • 熟悉MHA高可用架構
  • 學會配置MMM高可用
  • 學會配置MHA高可用

關於MMM高可用架構說明

  • mmm_mon:監控進程負責所有監控工作、決定和處理節點角色活動
  • mmm_agent:運行在每個MySQL服務器上的代理進程完成監控的探針工作和執行簡單的遠端服務設置
  • mmm_control:一個簡單的腳本,提供管理mmm_mond的進程命令
  • mysql-mmm的監管會提供多個虛擬IP(VIP),包括一個可寫VIP,多個可讀VIP,通過監管的管理綁定在可用MySQL之上,當某一臺MySQL關閉時,監管會將VIP遷移至其他MySQL

實驗環境

mysql-monitor 是監控服務器
主機 操作系統 IP地址 只要軟件
mysql-master1 Centos 7 x86_64 192.168.32.222 MySQL-MMM
mysql-master2 Centos 7 x86_64 192.168.32.223 MySQL-MMM
mysql-slave1 Centos 7 x86_64 192.168.32.224 MySQL-MMM
mysql-slave2 Centos 7 x86_64 192.168.32.225 MySQL-MMM
mysql-monitor Centos 7 x86_64 192.168.32.226 MySQL-MMM
master1,2 Centos 7 x86_64 VIP:192.168.32.221 MySQL-MMM
slave1 Centos 7 x86_64 VIP1:192.168.32.220 MySQL-MMM
slave2 Centos 7 x86_64 VIP1:192.168.32.199 MySQL-MMM

搭建主服務器master1

修改master1主配置文件

[root@localhost ~] vim /etc/my.cnf              #刪除從第一行開始到第九行刪除添加下列語句
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1                                               #server-id每臺數據庫server值都是唯一的
log-error=/usr/local/mysql/data/mysql_error.log         #開啟錯誤日誌
general_log=ON                                          #開啟訪問日誌
general_log_file=/usr/local/mysql/data/mysql_general.log
log_bin=mysql-bin                                       #開啟二進制日誌
slow_query_log=ON                                       #開啟慢日誌
slow_query_log_file=mysql_slow_query.log
long_query_time=5               
log_slave_updates=true
sync_binlog=1                                               #同步
auto_increment_increment=2                                  #增量值
auto_increment_offset=1                                     #起點值
binlog-ignore-db=mysql,information_schema                   #關閉mysql,information_schema 這兩個二進制日誌
[root@localhost ~]# systemctl restart mysqld.service #重啟數據庫
[root@localhost data]# netstat -anpt | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      3074/mysqld   
#查看端口有沒開啟

master2、slave1、slave2復制master1的配置文件把server-id改掉改成不一樣的

做主從同步 主主復制,主從同步

我在這是一臺一臺配的有一條命令可以節省改配置文件的時間

scp /etc/my.cnf [email protected]:/etc/

配置主主復制-兩臺主服務器相互復制

在m1上為m2授予從的權限,在m2上也要為m1授予從的權限
#先用show master status 查看兩臺主服務器的二進制日誌文件和偏移量
#master1
mysql> show master status;
+------------------+----------+--------------+--------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql-bin.000004 |      154 |              | mysql,information_schema |                   |
+------------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.00 sec)
#master2
mysql> show master status;
+------------------+----------+--------------+--------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql-bin.000004 |      154 |              | mysql,information_schema |                   |
+------------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.00 sec)

先把master2授權給master1

mysql> grant replication slave on *.* to ‘replication‘@‘192.168.32.%‘ identified by ‘123456‘;  
Query OK, 0 rows affected, 1 warning (0.03 sec)

mysql> change master to master_host=‘192.168.32.184‘,master_user=‘replication‘,master_passwordd=‘123456‘,master_log_file=‘mysql_bin.000002‘,master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.04 sec)

### 開啟同步功能 #slave代表開啟同步不是代表從服務器,我上面設置的只是個名稱與這邊的意思不同

mysql> start slave;
Query OK, 0 rows affected (0.03 sec)

先把master1授權給master2

mysql> grant replication slave on *.* to ‘replication‘@‘192.168.32.%‘ identified by ‘123456‘;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to master_host=‘192.168.32.151‘,master_user=‘replication‘,master_password=‘123456‘,master_log__file=‘mysql_bin.000004‘,master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

#開啟同步功能
mysql> start slave;
Query OK, 0 rows affected (0.03 sec)

#查看IO線程和SQL線程是否都已經開啟是否顯示為yes
mysql>show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.32.151
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 154
               Relay_Log_File: localhost-relay-bin.000011
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql-bin.000005
             Slave_IO_Running: Yes   
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 154
              Relay_Log_Space: 625
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 111eb6e9-78dd-11e8-9337-000c2940c252
             Master_Info_File: /usr/local/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)
如果只開啟了SQL線程而IO線程沒有開啟
那麽檢查防火墻有無關閉 增強性安全模塊有沒有關閉
再執行下列語句
mysql>stop slave; #先停止slave
mysql>reset slave; #刷新slave同步功能
mysql>start slave; #最後再開啟然後查看

創建一個數據庫看是否能同步

  • 主1
MariaDB [(none)]> create database info;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| info               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
  • 主2
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| info               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)
  • 從1
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| info               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)
  • 從2
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| info               |
| info1              |
| mysql              |
| performance_schema |
| test               |
+--------------------+
6 rows in set (0.00 sec)

在從2 上創建數據庫看主1 主2 從1 會不會同步不會同步就說明從服務器只會同步主服務器、主服務器不會同步從服務器

配置從服務器

兩個從服務器要能同時能同步兩個主服務器
安裝MMM在所有服務器上安裝註意,epel源要配置好
先配置epel源
配置ALI雲源,然後安裝epel-release源。

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all && yum makecache
yum -y install epel-release
yum -y install mysql-mmm*
修改配置文件 //所有主機上都要配置,直接復制多份
[root@localhost etc]# cd /etc/mysql-mmm/
[root@localhost mysql-mmm]# vim mmm_common.conf
active_master_role      writer

<host default>
    cluster_interface       ens33 #修改網卡默認是eth0
    pid_path                /run/mysql-mmm-agent.pid
    bin_path                /usr/libexec/mysql-mmm/
    replication_user        replicant    #登陸用戶是replicat
    replication_password    123456  #復制同步登陸密碼
    agent_user              mmm_agent
    agent_password          123456 #登陸代理密碼
</host>

<host db1>
    ip      192.168.32.222  ——> 主服務器1的IP地址
    mode    master ——>主服務器
    peer    db2
</host>

<host db2>
    ip      192.168.32.223  ——>主服務器2的IP地址
    mode    master ——>主服務器
    peer    db1
</host>
<host db3>
    ip      192.168.32.224  ——>從服務器1的IP地址
    mode    slave ——>從服務器
</host>

<host db4>
    ip      192.168.32.225 ——>從服務器2的IP地址
    mode    slave ——>從服務器
</host>

<role writer> #權限可寫
    hosts   db1, db2
    ips     192.168.32.221 ——>主服務器1、2的虛擬IP
    mode    exclusive
</role>

<role reader> #權限可讀
    hosts   db3, db4 #主機名
    ips     192.168.32.220 192.168.32.199 #從服務器1、2的虛擬IP
    mode    balanced
</role>

上傳配置文件

scp mmm_common.conf [email protected]:/etc/mysql-mmm/
scp mmm_common.conf [email protected]:/etc/mysql-mmm/
scp mmm_common.conf [email protected]:/etc/mysql-mmm/

------------------在monitor(監聽)服務器上配置----

#修改IP兩主兩從的IP地址在ping_ips後面,auto_set_online 自動設置在線時長為10秒
<monitor>
    ip                  127.0.0.1
    pid_path            /run/mysql-mmm-monitor.pid
    bin_path            /usr/libexec/mysql-mmm
    status_path         /var/lib/mysql-mmm/mmm_mond.status
    ping_ips             192.168.32.222,192.168.32.223,192.168.32.224,192.168.32.225
    auto_set_online     10

cd /etc/mysql-mmm/ //改密碼

vi mmm_mon.conf

<host default>
    monitor_user        mmm_monitor
    monitor_password    123456
</host>
--------------在所有數據庫上為mmm_moniter和mmm_agent授權-----
#為mmm_agent授權
MariaDB [(none)]> grant super, replication client, process on *.* to ‘mmm_agent‘@‘192.168.32.%‘ identified by ‘123456‘;
Query OK, 0 rows affected (0.01 sec)
#為mmm_moniter授權
MariaDB [(none)]> grant replication client on *.* to ‘mmm_monitor‘@‘192.168.32.%‘ identified by ‘123456‘; 
Query OK, 0 rows affected (0.01 sec)
# 刷新
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.01 sec)

---------------修改所有數據庫的mmm_agent.conf----

vi /etc/mysql-mmm/mmm_agent.conf

this db1 //根據規劃進行逐一調整

include mmm_common.conf

# The ‘this‘ variable refers to this server.  Proper operation requires
# that ‘this‘ server (db1 by default), as well as all other servers, have the
# proper IP addresses set in mmm_common.conf.
this db4

--------------在所有數據庫服務器上啟動mysql-mmm-agent---

#開啟服務
systemctl start mysql-mmm-agent.service
#設置開機自啟動
systemctl enable mysql-mmm-agent.service
#用 mmm_control show 查看各個節點
[root@localhost mysql-mmm]# mmm_control show 
  db1(192.168.32.222) master/ONLINE. Roles: writer(192.168.32.221)
  db2(192.168.32.223) master/ONLINE. Roles: 
  db3(192.168.32.224) slave/ONLINE. Roles: reader(192.168.32.199)
  db4(192.168.32.225) slave/ONLINE. Roles: reader(192.168.32.220)

故障測試

停止db1 確認 虛擬地址 221 是否移動到 db2 上。註意:主不會搶占

[root@localhost mysql-mmm]# systemctl stop mariadb.service 
[root@localhost mysql-mmm]# mmm_control show 
  db1(192.168.32.222) master/HARD_OFFLINE. Roles: 
  db2(192.168.32.223) master/ONLINE. Roles: writer(192.168.32.221)
  db3(192.168.32.224) slave/ONLINE. Roles: reader(192.168.32.199)
  db4(192.168.32.225) slave/ONLINE. Roles: reader(192.168.32.220)

再開一臺mysql服務器作為客戶端

在db1服務器上為最後一臺客戶端服務器地址授權登錄

MariaDB [(none)]> grant all on *.* to ‘testdba‘@‘192.168.32.222‘ identified by ‘123456‘;
MariaDB [(none)]> flush privileges;

#客戶端登陸虛擬IP
[root@localhost ~]# mysql -utestdba -p -h 192.168.32.221 
Enter password:  #設置代理授權是的登陸密碼
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 1358
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

#在客戶端創建數據庫,測試同步
MariaDB [(none)]> create database info; #在客戶端創建的
Query OK, 1 row affected (0.01 sec)

MariaDB [(none)]> show databases; 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| info               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

#在服務端查詢
MariaDB [(none)]> show databases; #db1
+--------------------+
| Database           |
+--------------------+
| information_schema |
| info               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]> show databases; #db2
+--------------------+
| Database           |
+--------------------+
| information_schema |
| info               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]> show databases; #db3
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| info               |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> show databases; #db4
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| info               |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)
#在客戶端創建就是在主服務器上創建從服務器同步

---驗證將從服務器停止一臺,另一臺將接管兩個虛擬IP,以保證業務不停止-----

[root@localhost mysql-mmm]# mmm_control show 
  db1(192.168.32.222) master/ONLINE. Roles: 
  db2(192.168.32.223) master/ONLINE. Roles: writer(192.168.32.221)
  db3(192.168.32.224) slave/ONLINE. Roles: reader(192.168.32.199), reader(192.168.32.220)
  db4(192.168.32.225) slave/HARD_OFFLINE. Roles: 

==上述同步做完了存在一個問題在做主從同步的時候同步的是主服務器上面的真實IP但是在down掉同步的那個主服務器的時候從服務器將不會在同步所以我們再設置主從同步的時候IP地址用他們設置的VIP(虛擬IP)==

MySQL高可用群集