MySQL MMM High Availability

1. Introduction to MMM

MMM (Master-Master replication manager for MvSQL, MySQL master-master replication manager) is a set of script programs that support dual-master failover and dual-master daily management. MMM is developed in Perl language and is mainly used to monitor and manage MySQL Master-Master (dual-master) replication. Although it is called dual-master replication, only one master is allowed to write at the same time in business, and the other backup master provides Part of the read service to speed up the warm-up of the standby master during the master-master switchover. It can be said that the script program of MMM realizes the function of failover on the one hand, and on the other hand, its internal additional tool scripts can also implement multiple slaves. read load balancing.

MMM provides both automatic and manual methods to remove the virtual ip of a server with a high replication delay in a group of servers. At the same time, it can also back up data and realize data synchronization between two nodes. Since MMM cannot fully guarantee data consistency, MMM is suitable for scenarios that do not require high data consistency but want to ensure business availability to the greatest extent.

MMM is a set of flexible script programs, implemented based on perl, used to monitor and failover mysql replication, and manage the configuration of MySQL Master-Master replication.

The description of the MMM high-availability architecture is as follows:

  • mmm_mon: monitoring process, responsible for all monitoring work, determining and processing all node role activities. This script needs to be run on the monitoring host.
  • mmm_agent: The agent process running on each MySQL server, completes the monitoring probe work and performs simple remote service settings. This script needs to be run on the supervised machine.
  • mmm_control: A simple script that provides commands to manage the mmm_mon process.
  • The supervisory side of mysql-mmm will provide multiple virtual IPs (VIPs), including one writable VIP and multiple readable VIPs. Through supervisory management, these IPs will be bound to the available MySQL. When the machine crashes, the supervisor will migrate the VIP to another MySQL.

During the entire supervision process, relevant authorized users need to be added in MySQL so that MySQL can support the maintenance of the monitoring host. Authorized users include an mmm_monitor user and an mmm_agent user.

2. Build MySQL's MMM High Availability

1. Prepare the environment

master01(db1)			192.168.80.20                 mysql5.7、mysql-mmm
master02(db2)			192.168.80.30                 mysql5.7、mysql-mmm
slave01(db3)			192.168.80.13                 mysql5.7、mysql-mmm
slave02(db4)			192.168.80.14                 mysql5.7、mysql-mmm
monitor					192.168.80.15                 mysql-mmm



systemctl stop firewalld 
setenforce 0

2. Build MySQL multi-master and multi-slave mode

//修改 master01 配置文件
vim /etc/my.cnf
......
[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                                               #每台 Mysql 主机的 server-id 不能相同
log-error=/usr/local/mysql/data/mysql_error.log             #错误日志
general_log=ON                                              #通用查询日志
general_log_file=/usr/local/mysql/data/mysql_general.log
slow_query_log=ON                                           #慢查询日志
slow_query_log_file=mysql_slow_query.log
long_query_time=5
binlog-ignore-db=mysql,information_schema        #不需要同步的库名
log_bin=mysql_bin                                #开启二进制日志用于主从数据复制
log_slave_updates=true                           #允许slave从master复制数据时可以写入到自己的二进制日志
sync_binlog=1                            #"双1设置",MySQL 在每写一次二进制日志时都会同步到磁盘中去    
innodb_flush_log_at_trx_commit=1         #"双1设置",每次事务提交时MySQL都会把缓存的数据写入日志文件,并且刷到磁盘中去
auto_increment_increment=2               #自增字段一次递增多少
auto_increment_offset=1                  #自增字段的起始值


//把配置文件复制到其它 3 台数据库服务器上并启动服务器,注意:配置文件中的 server_id 要修改
scp /etc/my.cnf [email protected]:/etc/
scp /etc/my.cnf [email protected]:/etc/
scp /etc/my.cnf [email protected]:/etc/

systemctl restart mysqld


//配置主主复制,两台主服务器相互复制
#在两台主服务器上都执行授予从的权限,从服务器上不需要执行
grant replication slave on *.* to 'replication'@'192.168.80.%' identified by '123456';

#在两台主服务器上查看,记录日志文件名称和同步点
show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000002 |    154   |              |                  |
+-------------------+----------+--------------+------------------+

#在 master01 上配置同步
change master to master_host='192.168.80.30',master_user='replication',master_password='123456',master_log_file='mysql_bin.000002',master_log_pos=154;

start slave;

show slave status\G
    Slave_IO_Running: Yes
    Slave_SQL_Running: Yes

#在 master02 上配置同步
change master to master_host='192.168.80.20',master_user='replication',master_password='123456',master_log_file='mysql_bin.000002',master_log_pos=154;

start slave;

show slave status\G
    Slave_IO_Running: Yes
    Slave_SQL_Running: Yes


//配置主从复制,在两台从服务器上做
change master to master_host='192.168.80.20',master_user='replication',master_password='123456',master_log_file='mysql_bin.000002',master_log_pos=154;

start slave;

show slave status\G
    Slave_IO_Running: Yes
    Slave_SQL_Running: Yes


//测试主主、主从 同步情况
create database db_test;

3. Install and configure MySQL-MMM

//在所有服务器上安装 MySQL-MMM
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*

//在 master01 上对 MySQL-MMM 进行配置
cd /etc/mysql-mmm/
vim mmm_common.conf
……
<host default>
    cluster_interface       ens33
    ……
    replication_user        replication
    replication_password    123456
    agent_user              mmm_agent
    agent_password          123456

<host db1>
    ip      192.168.80.20
    mode    master
    peer    db2
</host>

<host db2>
    ip      192.168.80.30
    mode    master
    peer    db1
</host>

<host db3>
    ip      192.168.80.13
    mode    slave
</host>

<host db4>
    ip      192.168.80.14
    mode    slave
</host>

<role writer>
    hosts   db1, db2
    ips     192.168.80.188
    mode    exclusive           #只有一个 host 可以进行写操作模式
</role>

<role reader>
    hosts   db3, db4
    ips     192.168.80.198, 192.168.80.199
    mode    balanced            #多个 slave 主机可以进行读操作模式
</role>

//把配置文件复制到其它 4 台主机,所有主机该配置文件内容都是一样的
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/
scp mmm_common.conf [email protected]:/etc/mysql-mmm/


//修改所有数据库服务器的代理配置文件 mmm_agent.conf
vim /etc/mysql-mmm/mmm_agent.conf
include mmm_common.conf
this db1				#根据不同的主机分别修改为 db1,db2,db3,db4


//在 monitor 监控服务器上修改监控配置文件 mmm_mon.conf
vim /etc/mysql-mmm/mmm_mon.conf
include mmm_common.conf
<monitor>
.....
    ping_ips        	192.168.80.20,192.168.80.30,192.168.80.13,192.168.80.14    #指定所有数据库服务器的 IP
	auto_set_online		10				#指定自动上线时间
</monitor>

<host default>
    monitor_user        mmm_monitor		#指定 mmm_monitor 的用户名
    monitor_password    123456          #指定 mmm_monitor 的密码
</host>


//在所有数据库上为 mmm_agent(代理进程)授权
grant super, replication client, process on *.* to 'mmm_agent'@'192.168.80.%' identified by '123456';


//在所有数据库上为 mmm_moniter(监控进程)授权
grant replication client on *.* to 'mmm_monitor'@'192.168.80.%' identified by '123456';

flush privileges;


//在所有数据库服务器上启动 mysql-mmm-agent
systemctl start mysql-mmm-agent.service
systemctl enable mysql-mmm-agent.service


//在 monitor 服务器上启动 mysql-mmm-monitor
systemctl start mysql-mmm-monitor.service   


//在 monitor 服务器上测试群集
#查看各节点的情况
mmm_control show
  db1(192.168.80.20) master/ONLINE. Roles: writer(192.168.80.188)
  db2(192.168.80.30) master/ONLINE. Roles: 
  db3(192.168.80.13) slave/ONLINE. Roles: reader(192.168.80.198)
  db4(192.168.80.14) slave/ONLINE. Roles: reader(192.168.80.199)

#检测监控功能是否都完善,需要各种OK
mmm_control checks all

#指定绑定 VIP 的主机
mmm_control move_role writer db2

4. Failure test

mmm_control move_role writer db1

#停止 master01 确认 VIP 是否移动到 master02 上。注意:master01 主服务器恢复服务后,不会抢占

mmm_control show
  db1(192.168.80.20) master/HARD_OFFLINE. Roles:
  db2(192.168.80.30) master/ONLINE. Roles: writer(192.168.80.188)

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

5. Client testing

#在 master01 服务器上为 monitor 服务器地址授权登录
grant all on *.* to 'testdba'@'192.168.80.15' identified by '123456';
flush privileges;

#在 monitor 服务器上使用 VIP 登录
yum install -y mariadb-server mariadb
systemctl start mariadb.service

mysql -utestdba -p -h 192.168.235.188

#创建数据,测试同步情况
create database testdba;

Guess you like

Origin blog.csdn.net/weixin_60917414/article/details/131431533