MySQL instances deployment architecture --mmm

Mmm source architecture

  • As we all know, MySQL itself provides a master-slave replication, then you can easily achieve bi-directional master-master replication, and then for one of the Master node Slave to build a library. This realization of the basic architecture of MySQL-MMM: two-way replication between master1 and master2, is also a master-slave replication between Master1 and Slave1.
  • In this way the whole system there are two Master, only a master of foreign provide writing services under normal circumstances. If you provide services to external master unplanned downtime, which is MySQL itself does not have the ability to failover switching, although there is still a cluster master node normal, but the application is still available. mysql-mmm is to solve this problem born.

Mmm principle of architecture

  • MySQL-MMM is a Master-Master Replication Manager for MySQL (mysql master-master replication manager) for short, is Google's open source projects (Perl script), is mainly used to monitor and make mysql master-master replication failover
  • The principle is the real database node IP (RIP) is mapped to the virtual IP (VIP) set, focused on this virtual IP, there is a dedicated write the IP, the IP for multiple read, Write this for a VIP mapping the database cluster real IP two master's (RIP), in order to achieve switching Failover, the other can be used to read the VIP balanced reading (balance).

mmm agency advantages and disadvantages

advantage

  • Use Perl scripting language and completely open source
  • Provides read and write VIP (virtual IP), the server role change transparent to the front-end application
  • MMM provides latency monitoring from the server
  • MMM provides primary database failover re-sync from the server to the new primary
  • It is easy for the failed primary database back online

Shortcoming

  • Published earlier do not support the new MySQL replication capabilities (based GTID replication)
  • Did not read the load balancing function
  • Switching master and slave, likely to cause data loss
  • Single point of failure MMM Monitoring Service

mmm architecture diagram

MySQL instances deployment architecture --mmm

Pilot deployment

Environmental deployment

master1IP地址:192.168.144.167
master2IP地址:192.168.144.151
slave1IP地址:192.168.144.168
slave2IP地址:192.168.144.145
monitorIP地址:192.168.144.164

Install MYSQL database in master1 \ master2 \ slave1 \ slave2 server

[root@master1 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo   //获取源地址
[root@master1 ~]# yum -y install epel-release      //安装epel源
[root@master1 ~]# yum clean all && yum makecache     //yum缓存清空
[root@master1 ~]# yum -y install mariadb-server mariadb      //安装mariadb数据库
[root@master1 ~]# systemctl stop firewalld.service       //关闭防火墙
[root@master1 ~]# setenforce 0
[root@master1 ~]# systemctl start mariadb.service        //开启数据库

Master1 modify the database configuration file

[root@master1 ~]# vim /etc/my.cnf        //进入编辑配置文件
[mysqld]
log_error=/var/lib/mysql/mysql.err        //错误日志文件
log=/var/lib/mysql/mysql_log.log           //主从日志存放位置
log_slow_queries=/var/lib/mysql_slow_queris.log     //man日志
binlog-ignore-db=mysql,information_schema         //二进制文件
character_set_server=utf8        //字符集
log_bin=mysql_bin             //二进制日志文件
server_id=11            //服务id(不能相同)
log_slave_updates=true   //允许从服务器更新
sync_binlog=1            //同步日志
auto_increment_increment=2    //自增列
auto_increment_offset=1               //起始点
[root@master1 ~]# systemctl restart mariadb.service   //重启数据库

Use scp to copy files to another MySQL database configuration services, and modify server_id In other MySQL servers

[root@master1 ~]# scp /etc/my.cnf [email protected]:/etc/   (server-id=22)
[root@master1 ~]# scp /etc/my.cnf [email protected]:/etc/   (server-id=33)
[root@master1 ~]# scp /etc/my.cnf [email protected]:/etc/   (server-id=44)
[root@master1 ~]# netstat -anpt | grep 3306       //查看端口3306
tcp      0   0 0.0.0.0:3306     0.0.0.0:*         LISTEN      4235/mysqld  

Configure the master-master replication

master1

[root@master1 ~]# mysql   ##进入数据库

MariaDB [(none)]> show master status;   ##查看主服务器的状态信息
+------------------+----------+--------------+--------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000001 |      245 |              | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> grant replication slave on *.* to 'replication'@'192.168.144.%' identified by '123456';     //授权给144段的网段复制的权限用户名replication密码123456
Query OK, 0 rows affected (0.00 sec)

master2

MariaDB [(none)]> change master to master_host='192.168.144.167',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=245;     //在master2上同步master1服务器
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant replication slave on *.* to 'replication'@'192.168.144.%' identified by '123456';    //在master2上授权复制权限
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show master status;       //查看master2的服务器的状态信息
+------------------+----------+--------------+--------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000001 |      410 |              | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> flush privileges;     //刷新权限
Query OK, 0 rows affected (0.00 sec)

master1

MariaDB [(none)]> change master to master_host='192.168.144.151',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=410;       //master1同步master2服务器数据库
Query OK, 0 rows affected (0.03 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

master1, open synchronization master2

MariaDB [(none)]> start slave;              //开启同步
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show slave status\G;       //查看同步状态信息
                         ...
                         Slave_IO_Running: Yes
                        Slave_SQL_Running: Yes
                        ...

slave1, slave2 call the shots from the synchronization

MariaDB [(none)]> change master to master_host='192.168.144.167',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=245;       //在从服务器上同步master1主服务器
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;       //刷新权限
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> start slave;       //开启同步
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show slave status\G;        //查看同步的状态信息
                             ...
                             Slave_IO_Running: Yes
                            Slave_SQL_Running: Yes
                            ...

Master synchronization status test master, a master-slave

Master master1

MariaDB [(none)]> create database school;   //创建数据库
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;   //查看数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| test               |
+--------------------+
5 rows in set (0.00 sec)

View on the slave database servers

MariaDB [(none)]> show databases;    //实现主主,主从同步
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| test               |
+--------------------+
5 rows in set (0.00 sec)

Monitor configuration server epel-release source empty the cache, and then install MMM

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

yum -y install epel-release

yum clean all && yum makecache

yum -y install mysql-mmm*

Mmm installed on all MySQL servers

yum -y install mysql-mmm*

All the servers are configured to mmm

 [root@master1 ~]# vim /etc/mysql-mmm/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.144.167   //master1地址
        mode    master
        peer    db2
</host>

<host db2>
        ip      192.168.144.151   //master2地址
        mode    master
        peer    db1
</host>

<host db3>
        ip      192.168.144.168   //slave1地址
        mode    slave
</host>

<host db4>
        ip      192.168.144.145   //slave2地址
        mode    slave
</host>

<role writer>
        hosts   db1, db2      ##写服务器虚拟ip
        ips     192.168.144.250
        mode    exclusive
</role>

<role reader> 
        hosts   db3, db4    ##读服务器虚拟ip
        ips     192.168.144.251, 192.168.144.252
        mode    balanced
</role>

##复制到其他的服务器中
[root@master1 ~]# scp /etc/mysql-mmm/mmm_common.conf [email protected]:/etc/mysql-mmm/
[email protected]'s password: 
mmm_common.conf                               100%  836   267.1KB/s   00:00    
[root@master1 ~]# scp /etc/mysql-mmm/mmm_common.conf [email protected]:/etc/mysql-mmm/
[email protected]'s password: 
mmm_common.conf                               100%  836   863.2KB/s   00:00    
[root@master1 ~]# scp /etc/mysql-mmm/mmm_common.conf [email protected]:/etc/mysql-mmm/
[email protected]'s password: 
mmm_common.conf                               100%  836   904.7KB/s   00:00    
[root@master1 ~]# scp /etc/mysql-mmm/mmm_common.conf [email protected]:/etc/mysql-mmm/

On the monitor server configuration

[root@monitor ~]# vim /etc/mysql-mmm/mmm_mon.conf
<host default>
        monitor_user        mmm_monitor
        monitor_password    123456    ##修改monitor的密码
</host>

On all database mmm_agent proxy authorization, the authorization for the monitoring mmm_monitor

MariaDB [(none)]> grant super, replication client, process on *.* to 'mmm_agent'@'192.168.144.%' identified by '123456';     //授权代理
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant replication client on *.* to 'mmm_monitor'@'192.168.144.%' identified by '123456';        //授权监控
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;       //刷新权限
Query OK, 0 rows affected (0.00 sec)

Modify mmm_agent.conf all databases

[root@master1 ~]# vim /etc/mysql-mmm/mmm_agent.conf
this db1 //根据规划进行逐一调整
[root@master2 ~]# vim /etc/mysql-mmm/mmm_agent.conf
this db2 //根据规划进行逐一调整
[root@slave1 ~]# vim /etc/mysql-mmm/mmm_agent.conf
this db3 //根据规划进行逐一调整
[root@slave2 ~]# vim /etc/mysql-mmm/mmm_agent.conf
this db4 //根据规划进行逐一调整
##所有数据库开启
[root@master1 ~]systemctl start mysql-mmm-agent.service        //开启代理服务
[root@master1 ~]systemctl enable mysql-mmm-agent.service        //加入开机自启动

On the monitor configuration

[root@monitor ~]# vim /etc/mysql-mmm/mmm_mon.conf
<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.144.167,192.168.144.151,192.168.144.168,192.168.144.145    //所有数据库服务器地址
        auto_set_online     10        //自动在线时间
[root@monitor ~]# systemctl stop firewalld.service        //关闭防火墙
[root@monitor ~]# setenforce 0
[root@monitor ~]# systemctl start mysql-mmm-monitor.service       /开启监控服务
[root@monitor ~]# mmm_control show            //查看主从的飘逸地址
    db1(192.168.144.167) master/ONLINE. Roles: writer(192.168.144.250)
    db2(192.168.144.151) master/ONLINE. Roles: 
    db3(192.168.144.168) slave/ONLINE. Roles: reader(192.168.144.252)
    db4(192.168.144.145) slave/ONLINE. Roles: reader(192.168.144.251)

Test drift address

[root@master1 ~]# systemctl stop mariadb.service     //模拟停止master1服务器

Check the monitor server

[root@monitor ~]# mmm_control show
    db1(192.168.144.167) master/HARD_OFFLINE. Roles: 
    db2(192.168.144.151) master/ONLINE. Roles: writer(192.168.144.250)
    db3(192.168.144.168) slave/ONLINE. Roles: reader(192.168.144.252)
    db4(192.168.144.145) slave/ONLINE. Roles: reader(192.168.144.251)
(重启master1数据库服务,虚拟地址不会被抢占到master1)
mmm_control checks all //需要各种OK
mmm_control move_role writer db1      //可以切换虚拟地址

Installing MySQL on the monitor as a test virtual machine ip download the database

[root@monitor ~]# yum install mysql -y

//master1服务器上授权monitor地址访问//

MariaDB [(none)]> grant all on *.* to 'testdba'@'192.168.144.164' identified by '123456';
##授权monitor地址访问
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> flush privileges;   ##刷新权限
Query OK, 0 rows affected (0.00 sec)

[root@monitor ~]# mysql -utestdba -p -h 192.168.144.250   ##使用虚拟地址即可登录数据库
Enter password: 

MariaDB [(none)]> 

Guess you like

Origin blog.51cto.com/14473285/2458541
mmm