mysql-mmm高可用群集

 一、概述

       MMMMaster-Master replication manager for MySQL)是一套支持双主故障切换和双主日常管理的脚本程序。 MMM 使用 Perl 语言开发,主要用来监控和管理 MySQL Master-Master(双主)复制,可以说是 MySQL 主主复制管理器

虽然叫做双主复制,但是业务上同一时刻只运行对一个主进行写入,另一台备选主上提供部分读服务,以加速在主主切换时刻备选主的预热,可以说 MMM 这套脚本程序一方面实现了故障切换的功能,另一方面其内部附加的工具脚本也可以实现多个 SlaveRead 负载均衡

       MMM 提供了自动和手动两种方式移除一组服务器中复制延迟较高的服务器的虚拟 IP,同时它还可以备份数据,实现两节点之间的数据同步等。由于 MMM 无法完全的保证数据一致性,所以 MMM 适用于对数据的一致性要求不是很高,但是又想最大程度的保证业务可用性的场景

mysql-mmm高可用群集_第1张图片

mysql-mmm高可用群集_第2张图片

 二、MySQL-MMM 优缺点

优点:高可用性,扩展性好,出现故障自动切换,对于主主同步,在同一时间只提供一台数据库写操作,保证数据的一致性
缺点:Monitor 节点是单点,可以结合 Keepalived 实现高可用,对主机的数量有需求,需要实现读写分离,对程序来说是个挑战

三、MySQL-MMM 内部工作架构

(一)、进程类型

mmm_mond监控进程,负责所有的监控工作,决定和处理所有节点角色活动。此脚本需要在监控机上运行
mmm_agentd运行在每个 MySQL 服务器上(MasterSlave)的代理进程,完成监控的探针工作和执行简单的远端服务设置
mmm_control一个简单的脚本,提供管理 mmm_mond 进程的命令

(二)、工作架构

mysql-mmm高可用群集_第3张图片

(三)、工作原理

mysql-mmm 的监管端会提供多个虚拟 IPVIP),包括一个可写的 VIP,多个可读的 VIP
通过监管的管理,这些 IP 会绑定在可用的 MySQL 之上
当某一台 MySQL 宕机时,监管会将 VIP 迁移至其它 MySQL

四、案例

系统类型 名称 server ID IP VIP role(角色)
CentOS 7.4 monitor(监听)

192.168.2.8
CentOS 7.4 master1(主1) 1 192.168.2.9 192.168.2.100 writer
CentOS 7.4 master2(主1) 2 192.168.2.10 192.168.2.100 writer
CentOS 7.4 slave1(从1) 3 192.168.2.11 192.168.2.101 reader
CentOS 7.4 slave2(从2) 4 192.168.2.12 192.168.2.102 reader

(一)、 准备工作

1、 在monitor、master1、master2、slave1、slave2配置域名解析

直接复制修改ip或名称

echo '
192.168.2.8  monitor
192.168.2.9  master1
192.168.2.10 master2
192.168.2.11 slave1
192.168.2.12 slave2'>> /etc/hosts

2、 在master1上安装配置ntp服务


[root@master1 ~]# yum -y install ntp                         
.......
...
[root@master1 ~]# sed -i '/^server/s/^/#/g' /etc/ntp.conf                
[root@master1 ~]# echo ' 
server 127.127.1.0
fudge 127.127.1.0 stratum 8
' >> /etc/ntp.conf

[root@master1 ~]# systemctl start ntpd                
[root@master1 ~]# systemctl enable ntpd                        
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

3、 master2上配置ntp时间


[root@master2 ~]# yum -y install ntp                  
[root@master2 ~]# /usr/sbin/ntpdate 192.168.2.9                    
21 Dec 15:48:55 ntpdate[55094]: adjust time server 192.168.2.9 offset 0.000702 sec
[root@master2 ~]# echo "/usr/sbin/ntpdate 192.168.2.9" >> /etc/rc.local                  
[root@master2 ~]# chmod +x /etc/rc.local               

monitor、slave1、slave2同master2安装配置ntp

()、安装MySQL服务

注意:如果有一个完整的mysql服务可以按照(三)进行复制 

安装mysql文献:mysql的安装过程

 (三)、部署剩下的mysql 

 master1:已安装mysql

注意: 直接把 master1 安装好的MySQL复制到 master2

一定要关闭MySQL!!!!!!!!!!!

一定要关闭MySQL!!!!!!!!!!!

一定要关闭MySQL!!!!!!!!!!!


[root@master1 ~]# systemctl stop mysqld                                              #复制前先关闭mysql 服务
-------------------------------------
mysql 主文件复制
[root@master1 ~]# for i in 10 11 12;do scp -r /usr/local/mysql/ [email protected].$i:/usr/local/mysql;done  

mysql启动文件复制
[root@master1 ~]# for i in 10 11 12;do scp -r /usr/lib/systemd/system/mysqld.service [email protected].$i:/usr/lib/systemd/system/;done

mysql配置文件复制
[root@master1 ~]# for i in 10 11 12;do scp -r /etc/my.cnf [email protected].$i:/etc/;done

mysql系统文件复制
[root@master1 ~]# for i in 10 11 12;do scp -r /etc/init.d/mysql.server [email protected].$i:/etc/init.d/;done

master2 :没有安装;进行部署

 方法一:手动输入


[root@master2 ~]# yum -y install gcc gcc-c++ ncurses bison libgcrypt perl cmake ncurses-devel                       
...................
.........
..
[root@master2 ~]# groupadd mysql               
[root@master2 ~]# useradd -r -g mysql mysql         
[root@master2 ~]# chown -R mysql:mysql /usr/local/mysql                
[root@master2 ~]# echo "export PATH=$PATH:/usr/local/mysql/bin/" >>/etc/profile                   
[root@master2 ~]# source /etc/profile                   
[root@master2 ~]# chkconfig --add mysql.server                              
[root@master2 ~]# systemctl start mysqld  或者 systemctl start mysql                     
-------------------------------------------------------------------
[root@master2 ~]# netstat -anptul | grep mysql                       
tcp6       0      0 :::3306                 :::*                    LISTEN      68776/mysqld      

 方法二:懒人模式(复制粘贴完事)


echo '
yum -y install gcc gcc-c++ ncurses bison libgcrypt perl cmake ncurses-devel
groupadd mysql
useradd -r -g mysql mysql
chown -R mysql:mysql /usr/local/mysql
chkconfig --add mysql.server
systemctl start mysql
netstat -anptul | grep mysql 
rm -rf jb.sh' > jb.sh
bash jb.sh

直接回车


echo "export PATH=$PATH:/usr/local/mysql/bin/" >>/etc/profile
source /etc/profile

slave1(从1)、slave2(从2)  同上

 完成后查看数据库是否启动:master1、master2、slave1、slave2的mysql数据库

1、 删除UUID文件

原因:mysql 有个uuid , 然而uuid 是唯一标识的,所以复制过来的uuid是一样的,只需要删除重启就可以了


[root@master2 ~]# find / -iname "auto.cnf"           
/usr/local/mysql/data/auto.cnf
[root@master2 ~]# rm -rf /usr/local/mysql/data/auto.cnf
[root@master2 ~]# systemctl restart mysqld          #重启

 删除slave1、slave2的UUID


[root@slave1 ~]# rm -rf /usr/local/mysql/data/auto.cnf
[root@slave1 ~]# systemctl restart mysqld          #重启

[root@slave2 ~]# rm -rf /usr/local/mysql/data/auto.cnf
[root@slave2 ~]# systemctl restart mysqld          #重启

(四)、 实现双主复制

1、 配置master1


echo '
server-id=1
log-bin=mysql-bin
log-slave-updates
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
relay-log=relay1-log-bin
relay-log-index=slave-relay1-bin.index
' >> /etc/my.cnf
————————————————————说明——————————————————————————
server-id = 1                   #每台 Mysql 主机的 server-id 不能相同
log_bin=mysql_bin               #开启二进制日志用于主从数据复制
log_slave_updates=true          #允许slave从master复制数据时可以写入到自己的二进制日志
sync_binlog=1                   #主机每次提交事务的时候把二进制日志的内容同步到磁盘上,所以即使服务器崩溃,也会把时间写入到日志中 
auto_increment_increment=2      #自增字段一次递增多少                          
auto_increment_offset=1         #自增字段的起始值                                                             

[root@master1 ~]# systemctl restart mysqld                           

[root@master1 ~]# mysql -uroot -p123.com                
........
...
mysql> show master status;                  
+-----------------+----------+--------------+------------------+-------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      154 |              |                  |                   |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> grant replication slave on *.* to 'master'@'192.168.2.%' identified by '123.com';              
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;               
Query OK, 0 rows affected (0.00 sec)

2、 配置master2(同步master1)


echo '
server-id=2
log-bin=mysql-bin
log-slave-updates
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
relay-log=relay1-log-bin
relay-log-index=slave-relay1-bin.index
' >> /etc/my.cnf

[root@master2 ~]# systemctl restart mysqld                  
[root@master2 ~]# mysql -uroot -p123.com                  
.......
...
mysql> grant replication slave on *.* to 'master'@'192.168.2.%' identified by '123.com';                   
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;                
Query OK, 0 rows affected (0.01 sec)

mysql> change master to master_host='192.168.2.9',master_user='master',master_password='123.com',master_log_file='mysql-bin.000001',master_log_pos=154;              #建议输入154
Query OK, 0 rows affected, 2 warnings (0.00 sec)

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

mysql> show slave status\G                 
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.2.9
                  Master_User: master
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 611
               Relay_Log_File: relay1-log-bin.000002
                Relay_Log_Pos: 776
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
.............
......

 4、 配置master1(同步master2)

在 master1 上配置

mysql> change master to master_host='192.168.2.10',master_user='master',master_password='123.com',master_log_file='mysql-bin.000001',master_log_pos=154;                      #写154
Query OK, 0 rows affected, 2 warnings (0.00 sec)

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

mysql> show slave status\G                                          
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.10
                  Master_User: master
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1068
               Relay_Log_File: relay1-log-bin.000002
                Relay_Log_Pos: 776
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
.....................
..........

(五)、实现主从复制

1、配置 slave1从同步maser1

注意: 配置从服务器时从服务器不能同步旧数据

查看:maser1
mysql> flush logs;
Query OK, 0 rows affected (0.00 sec)

mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      154 |              |                  |                   |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

 slave1上配置


echo '
server-id=3
log-bin=mysql-bin
log-slave-updates
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
relay-log=relay1-log-bin
relay-log-index=slave-relay1-bin.index
' >> /etc/my.cnf

[root@slave1 ~]# systemctl restart mysqld             
[root@slave1 ~]# mysql -uroot -p123.com                             

mysql> change master to master_host='192.168.2.9',master_user='master',master_password='123.com',master_log_file='mysql-bin.000002',master_log_pos=154;                   
Query OK, 0 rows affected, 2 warnings (0.00 sec)

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

mysql> show slave status\G                  
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.9
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: relay1-log-bin.000004
                Relay_Log_Pos: 319
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
................
........

2、配置 slave2从同步maser2

 注意: 配置从服务器时从服务器不能同步旧数据

查看 master2 的 Position

mysql> flush logs;
Query OK, 0 rows affected (0.00 sec)

mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      154 |              |                  |                   |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

 slave2上配置


echo '
server-id=4
log-bin=mysql-bin
log-slave-updates
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
relay-log=relay1-log-bin
relay-log-index=slave-relay1-bin.index
' >> /etc/my.cnf

[root@slave2 ~]# systemctl restart mysqld            
[root@slave2 ~]# mysql -uroot -p123.com                      
                    
mysql> change master to master_host='192.168.2.10',master_user='master',master_password='123.com',master_log_file='mysql-bin.000002',master_log_pos=154;                                          
Query OK, 0 rows affected, 2 warnings (0.01 sec)

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

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.10
                  Master_User: master
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 154
               Relay_Log_File: relay1-log-bin.000002
                Relay_Log_Pos: 319
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

................
.......

如果I/O进程为NO  进行下面的操作

先重启服务在进行同步

systemctl restart mysqld

在主服务器上刷新日志
mysql> flush logs;
Query OK, 0 rows affected (0.00 sec)
在查看
mysql> show master status;

——————————————————————————————————————-
从服务器上执行

mysql> stop slave;                  
Query OK, 0 rows affected (0.00 sec)

mysql> change master to master_host='192.168.2.10',master_user='master',master_password='123.com',master_log_file='mysql-bin.000002',master_log_pos=154;                       
Query OK, 0 rows affected, 2 warnings (0.00 sec)

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

mysql> show slave status\G                       
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.10
                  Master_User: master
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 154
               Relay_Log_File: relay1-log-bin.000002
                Relay_Log_Pos: 319
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
.....................
..........

(六)、测试


在 master1 上创建 user 库

mysql> create database user;                 
Query OK, 1 row affected (0.00 sec)
                     
mysql> show databases;                      
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| user               |
+--------------------+
5 rows in set (0.00 sec)
————————————————————————————————————————
master2     

mysql> show databases;                      
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| user               |
+--------------------+
5 rows in set (0.00 sec)

————————————————————————————————————————
slave1

mysql> show databases;                  
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| user               |
+--------------------+
5 rows in set (0.00 sec)

————————————————————————————————————————
slave2

mysql> show databases;                     
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| user               |
+--------------------+
5 rows in set (0.00 sec)

(七)、在monitor、master1、master2、slave1、slave2节点上安装mysql-mmm软件程序

 1、在所有服务器上安装 MySQL-MMM

 添加一个网卡 设置为桥接模式 后执行下面的命令


systemctl restart network                     
yum -y install wget                     
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo                                 
yum -y install epel-release                 
yum -y install mysql-mmm*                                      

2、在 monitor 上对 MySQL-MMM 进行配置


[root@monitor ~]# cd /etc/mysql-mmm/                             
[root@monitor mysql-mmm]# vim mmm_common.conf                         
ctive_master_role      writer


    cluster_interface       ens33                      #修改网卡名称为内部网卡名称
    pid_path                /run/mysql-mmm-agent.pid
    bin_path                /usr/libexec/mysql-mmm/
    replication_user        master                     #数据库授权用户
    replication_password    123.com                    #数据库授权密码
    agent_user              mmm_agent                  #代理用户的用户名
    agent_password          12345                      #代理用户的密码



    ip      192.168.2.9                                #修改ip
    mode    master
    peer    db2



    ip      192.168.2.10                               #修改ip
    mode    master
    peer    db1



    ip      192.168.2.11                               #修改ip
    mode    slave



    ip      192.168.2.12                               #修改ip
    mode    slave



    hosts   db1, db2
    ips     192.168.2.100                              #mater的虚拟IP
    mode    exclusive



    hosts   db3, db4                                   #修改
    ips     192.168.2.101, 192.168.2.102               #slave1、slave2的虚拟IP
    mode    balanced


保存

3、把配置文件复制到其它 4 台主机,所有主机配置文件内容都一样


[root@monitor mysql-mmm]# for i in 9 10 11 12;do scp mmm_common.conf [email protected].$i:/etc/mysql-mmm/;done                        

4、修改所有数据库服务器的代理配置文件 mmm_agent.conf

 
[root@master1 ~]# vim /etc/mysql-mmm/mmm_agent.conf                      
include mmm_common.conf
......
this db1
保存
——————————————————————————————————————
[root@master2 ~]# vim /etc/mysql-mmm/mmm_agent.conf               
include mmm_common.conf
.....
this db2
保存
——————————————————————————————————————
[root@slave1 ~]# vim /etc/mysql-mmm/mmm_agent.conf                        
include mmm_common.conf
......
this db3
保存
——————————————————————————————————————
[root@slave2 ~]# vim /etc/mysql-mmm/mmm_agent.conf                 
include mmm_common.conf
......
this db4
保存

5、在 monitor 监控服务器上修改监控配置文件 mmm_mon.conf


[root@monitor ~]# cd /etc/mysql-mmm/             
[root@monitor mysql-mmm]# ls            
mmm_agent.conf  mmm_common.conf  mmm_mon.conf  mmm_mon_log.conf  mmm_tools.conf
[root@monitor mysql-mmm]# cp mmm_mon.conf{,.bak}             
[root@monitor mysql-mmm]# ls                 
mmm_agent.conf  mmm_common.conf  mmm_mon.conf  mmm_mon.conf.bak  mmm_mon_log.conf  mmm_tools.conf


[root@monitor mysql-mmm]# vim mmm_mon.conf               
include mmm_common.conf


    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.2.9,192.168.10,192.168.11,192.168.2.12       #指定数据库IP
    auto_set_online     10            #指定自动上线时间

.................
......



    monitor_user        mmm_monitor  #指定用户
    monitor_password    12345        #指定密码


debug 0

保存

6、在master 数据库上为 mmm_agent(代理进程)和 mmm_moniter(监控进程) 授权


grant super, replication client, process on *.* to 'mmm_agent'@'192.168.2.%' identified by '12345';                             

grant replication client on *.* to 'mmm_monitor'@'192.168.2.%' identified by '12345';                      

flush privileges;                         

7、monitor、master1、master2、slave1、slave2 启动 mysql-mmm

monitor 上启动mysql-mmm-monitor

[root@monitor ~]# systemctl start mysql-mmm-monitor.service                 
master1、master2、slave1、slave2 启动 mysql-mmm-agent

systemctl start mysql-mmm-agent.service        
systemctl enable mysql-mmm-agent.service        

(八)、在 monitor 服务器上进行测试群集

1、查看节点情况

[root@monitor ~]# mmm_control show
  db1(192.168.2.9) master/ONLINE. Roles: writer(192.168.2.100)
  db2(192.168.2.10) master/ONLINE. Roles: 
  db3(192.168.2.11) slave/ONLINE. Roles: reader(192.168.2.102)
  db4(192.168.2.12) slave/ONLINE. Roles: reader(192.168.2.101)
ONLINE. Roles 在线节点
HARD_OFFLINE. Roles ping不通 或者 mysql连接中断,会导致hard_offline状态
admin_offline 手动下线的状态

2、查看监控情况是否都完善,全为OK表示没有问题

[root@monitor ~]# mmm_control checks all
db4  ping         [last change: 2021/12/21 21:49:40]  OK
db4  mysql        [last change: 2021/12/21 21:49:40]  OK
db4  rep_threads  [last change: 2021/12/21 21:49:40]  OK
db4  rep_backlog  [last change: 2021/12/21 21:49:40]  OK: Backlog is null
db2  ping         [last change: 2021/12/21 21:49:40]  OK
db2  mysql        [last change: 2021/12/21 21:49:40]  OK
db2  rep_threads  [last change: 2021/12/21 21:49:40]  OK
db2  rep_backlog  [last change: 2021/12/21 21:49:40]  OK: Backlog is null
db3  ping         [last change: 2021/12/21 21:49:40]  OK
db3  mysql        [last change: 2021/12/21 21:49:40]  OK
db3  rep_threads  [last change: 2021/12/21 21:49:40]  OK
db3  rep_backlog  [last change: 2021/12/21 21:49:40]  OK: Backlog is null
db1  ping         [last change: 2021/12/21 21:49:40]  OK
db1  mysql        [last change: 2021/12/21 21:49:40]  OK
db1  rep_threads  [last change: 2021/12/21 21:49:40]  OK
db1  rep_backlog  [last change: 2021/12/21 21:49:40]  OK: Backlog is null

3、指定绑定 VIP 的主机

——————————————————修改前
[root@monitor ~]# mmm_control show
  db1(192.168.2.9) master/ONLINE. Roles: writer(192.168.2.100)
  db2(192.168.2.10) master/ONLINE. Roles: 
  db3(192.168.2.11) slave/ONLINE. Roles: reader(192.168.2.102)
  db4(192.168.2.12) slave/ONLINE. Roles: reader(192.168.2.101)

[root@monitor ~]# mmm_control move_role writer db2             #修改writer的主机
OK: Role 'writer' has been moved from 'db1' to 'db2'. Now you can wait some time and check new roles info!

——————————————————修改后
[root@monitor ~]# mmm_control show
  db1(192.168.2.9) master/ONLINE. Roles: 
  db2(192.168.2.10) master/ONLINE. Roles: writer(192.168.2.100)
  db3(192.168.2.11) slave/ONLINE. Roles: reader(192.168.2.102)
  db4(192.168.2.12) slave/ONLINE. Roles: reader(192.168.2.101)

——————————————————修改回来
[root@monitor ~]# mmm_control move_role writer db1
OK: Role 'writer' has been moved from 'db2' to 'db1'. Now you can wait some time and check new roles info!

[root@monitor ~]# mmm_control show
  db1(192.168.2.9) master/ONLINE. Roles: writer(192.168.2.100)
  db2(192.168.2.10) master/ONLINE. Roles: 
  db3(192.168.2.11) slave/ONLINE. Roles: reader(192.168.2.102)
  db4(192.168.2.12) slave/ONLINE. Roles: reader(192.168.2.101)

(九)、故障测试

1、停止 master01 确认 VIP 是否移动到 master02 上

注意:master1 主服务器恢复服务后,不会抢占


[root@master1 ~]# systemctl stop mysqld          
[root@master1 ~]# netstat -anptu|grep mysql        
[root@master1 ~]# 

mysql-mmm高可用群集_第4张图片


[root@monitor ~]# mmm_control show           
  db1(192.168.2.9) master/HARD_OFFLINE. Roles: 
  db2(192.168.2.10) master/ONLINE. Roles: writer(192.168.2.100)
  db3(192.168.2.11) slave/ONLINE. Roles: reader(192.168.2.102)
  db4(192.168.2.12) slave/ONLINE. Roles: reader(192.168.2.101)

[root@master1 ~]# systemctl start mysqld

结论:当master1 宕机之后,writer会自动切换到master2上,实现了高可用

2、 停止一台从服务器,另一台将接管两个虚拟IP,以保证业务不停止


[root@slave1 ~]# systemctl stop mysqld

mysql-mmm高可用群集_第5张图片


[root@monitor ~]# mmm_control show        
  db1(192.168.2.9) master/ONLINE. Roles: 
  db2(192.168.2.10) master/ONLINE. Roles: writer(192.168.2.100)
  db3(192.168.2.11) slave/HARD_OFFLINE. Roles: 
  db4(192.168.2.12) slave/ONLINE. Roles: reader(192.168.2.101), reader(192.168.2.102)      #db4接管了两个VIP

[root@slave1 ~]# systemctl start mysqld

(十)、客户端测试

 1、在 master1、master2、slave1、slave2 上配置VIP地址


[root@master1 ~]# ip addr add 192.168.2.100/24 dev ens33

[root@master2 ~]# ip addr add 192.168.2.100/24 dev ens33

[root@slave1 ~]# ip addr add 192.168.2.101/24 dev ens33

[root@slave2 ~]# ip addr add 192.168.2.102/24 dev ens33


    删除  VIP   


[root@slave2 ~]# ip addr del 192.168.2.102/32 dev ens33

如果没有删除就查看子网掩码是多少位

mysql-mmm高可用群集_第6张图片

2、在 master1 服务器上为 monitor 服务器地址授权登录


[root@master1 ~]# systemctl start mysqld                
......
..
mysql> grant all on *.* to 'jl'@'192.168.2.%' identified by '123456';    #授权monitor服务器上的 jl 用户远程登录权限
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;                 
Query OK, 0 rows affected (0.00 sec)

3、在 monitor 服务器上使用 VIP 登录进行测试


[root@monitor ~]# yum install -y mariadb-server mariadb               
.......
..
[root@monitor ~]# systemctl start mariadb.service                 

[root@monitor ~]# mysql -ujl -p123456 -h 192.168.2.100
...............
.....
MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| user               |
+--------------------+
5 rows in set (0.00 sec)

MySQL [(none)]> create database aaa;
Query OK, 1 row affected (0.00 sec)

MySQL [(none)]> quit
Bye

[root@monitor ~]# mysql -ujl -p123456 -h 192.168.2.102
..........
....
MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| aaa                |
| mysql              |
| performance_schema |
| sys                |
| user               |
+--------------------+
6 rows in set (0.00 sec)

4、关闭slave1和slave2节点的写功能,让其只能读取

没有关闭前

[root@monitor ~]# mysql -ujl -p123456 -h 192.168.2.102
.............
MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| aaa                |
| mysql              |
| performance_schema |
| sys                |
| user               |
+--------------------+
6 rows in set (0.00 sec)

MySQL [(none)]> create database user111;
Query OK, 1 row affected (0.00 sec)

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| aaa                |
| mysql              |
| performance_schema |
| sys                |
| user               |
| user111            |
+--------------------+
7 rows in set (0.00 sec)

在 slave1和slave2 上配置
echo 'read_only=1'>>/etc/my.cnf

重启mysql
systemctl restart mysqld

————————————————————————————————————————
master1 创建一个用户

mysql> grant select,insert on *.* to 'yg'@'192.168.2.%' identified by "123456";
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

——————————————————————————————
   客户端测试

[root@monitor ~]# mysql -uyg -p123456 -h 192.168.2.102
...........
MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| aaa                |
| mysql              |
| performance_schema |
| sys                |
| user               |
| user111            |
+--------------------+
7 rows in set (0.00 sec)

MySQL [(none)]> create database bbbbb;
ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement

你可能感兴趣的:(mysql,数据库,mysql-mmm高可用群集)