mmm架構的來源
- 衆所周知,MySQL自身提供了主從複製,而後能夠很輕鬆實現master-master雙向複製,同時再爲其中一個Master節點搭建一個Slave庫。這樣就實現了MySQL-MMM架構的基礎:master1和master2之間雙向複製,同時Master1和Slave1之間是主從複製。
- 這樣整個體系中存在兩個Master,正常狀況下只有一個master對外提供寫服務。若是對外提供服務的master意外宕機了,這是MySQL自己並不具有failover切換的能力,儘管集羣中仍然有一個正常的master節點,但應用仍不可用。mysql-mmm就是爲了解決這個問題誕生的。
mmm架構的原理
- MySQL-MMM是Master-Master Replication Manager for MySQL(mysql主主複製管理器)的簡稱,是Google的開源項目(Perl腳本),主要用來監控mysql主主複製並作失敗轉移
- 其原理是將真實數據庫節點的IP(RIP)映射爲虛擬IP(VIP)集,在這個虛擬的IP集中,有一個專用於write的IP,多個用於read的IP,這個用於Write的VIP映射着數據庫集羣中的兩臺master的真實IP(RIP),以此來實現Failover的切換,其餘read的VIP能夠用來均衡讀(balance)。
mmm機構優缺點
優勢
- 使用Perl腳本語言開發及徹底開源
- 提供了讀寫VIP(虛擬IP),使服務器角色的變動對前端應用透明
- MMM提供了從服務器的延遲監控
- MMM提供了主數據庫故障轉移後從服務器對新主的從新同步功能
- 很容易對發生故障的主數據庫從新上線
缺點
- 發佈時間比較早不支持MySQL新的複製功能(基於GTID的複製)
- 沒有讀負載均衡的功能
- 在進行主從切換時,容易形成數據丟失
- MMM監控服務存在單點故障
mmm架構原理圖
實驗部署
環境部署
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
在master1\master2\slave1\slave2服務器中安裝MYSQL數據庫
[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數據庫配置文件
[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 //重啓數據庫
使用scp複製數據庫配置文件到其餘MySQL服務,並在其餘MySQL服務器中修改server_id
[root@master1 ~]# scp /etc/my.cnf root@192.168.144.151:/etc/ (server-id=22)
[root@master1 ~]# scp /etc/my.cnf root@192.168.144.168:/etc/ (server-id=33)
[root@master1 ~]# scp /etc/my.cnf root@192.168.144.145:/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
配置主主複製
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,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作主從同步
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
...
測試主主,主從的同步狀態
主服務器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)
slave服務器上查看數據庫
MariaDB [(none)]> show databases; //實現主主,主從同步
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| test |
+--------------------+
5 rows in set (0.00 sec)
monitor服務器上配置epel-release源清空緩存,而後安裝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*
全部MySQL服務器上安裝mmm
yum -y install mysql-mmm*
全部服務器上對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 root@192.168.144.151:/etc/mysql-mmm/
root@192.168.144.151's password:
mmm_common.conf 100% 836 267.1KB/s 00:00
[root@master1 ~]# scp /etc/mysql-mmm/mmm_common.conf root@192.168.144.168:/etc/mysql-mmm/
root@192.168.144.168's password:
mmm_common.conf 100% 836 863.2KB/s 00:00
[root@master1 ~]# scp /etc/mysql-mmm/mmm_common.conf root@192.168.144.145:/etc/mysql-mmm/
root@192.168.144.145's password:
mmm_common.conf 100% 836 904.7KB/s 00:00
[root@master1 ~]# scp /etc/mysql-mmm/mmm_common.conf root@192.168.144.164:/etc/mysql-mmm/
在monitor服務器上配置
[root@monitor ~]# vim /etc/mysql-mmm/mmm_mon.conf
<host default>
monitor_user mmm_monitor
monitor_password 123456 ##修改monitor的密碼
</host>
在全部數據庫上爲mmm_agent代理受權,爲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)
修改全部數據庫的mmm_agent.conf
[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 //加入開機自啓動
在monitor上配置
[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)
測試漂移地址
[root@master1 ~]# systemctl stop mariadb.service //模擬中止master1服務器
monitor服務器上查看
[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 //能夠切換虛擬地址
在monitor上安裝MySQL做爲測試機用虛擬ip登陸數據庫
[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)]>