MMM高可用集羣案例拓撲圖:
環境準備:
主服務器1:192.168.18.146 db1 vip:192.168.18.250
主服務器2:192.168.18.147 db2
從服務器1:192.168.18.128 db3 vip:192.168.18.251
從服務器2:192.168.18.148 db4 vip:192.168.18.252
監控服務器:192.168.18.145
Master1:
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# yum install epel-release -y
[root@localhost ~]# yum clean all && yum makecache
[root@localhost ~]# vim /etc/my.cnf
//在行首按9dd刪除9行
[mysqld]
log_error=/var/lib/mysql/mysql.err
log=/var/lib/mysql/mysql_log.log
log_slow_queries=/var/lib/mysql_slow_queris.log
binlog-ignore-db=mysql,information_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=1
log_slave_updates=true
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
//輸入:wq保存退出
[root@localhost ~]# systemctl start mariadb.service
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# netstat -ntap | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2989/mysqld
[root@localhost ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
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.18.%' identified by '123456';
Query OK, 0 rows affected (0.02 sec)
MariaDB [(none)]> change master to master_host='192.168.18.147',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=245;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.18.147
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000001
Read_Master_Log_Pos: 575
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 694
Relay_Master_Log_File: mysql_bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
......以下省略多行
//以上兩個狀態顯示爲YSE就是正常
MariaDB [(none)]> create database school;
Query OK, 1 row affected (0.00 sec)
//創建school
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
//因爲從master2中刪除了數據庫中的school,所以master1中同步刪除
MariaDB [(none)]> create database myschool;
Query OK, 1 row affected (0.01 sec)
//兩臺從服務器上開啓同步過後,在master1上創建myschool
Master2:
[root@localhost ~]# yum install epel-release -y
[root@localhost ~]# yum clean all && yum makecache
[root@localhost ~]# vim /etc/my.cnf
//在行首按9dd刪除9行
[mysqld]
log_error=/var/lib/mysql/mysql.err
log=/var/lib/mysql/mysql_log.log
log_slow_queries=/var/lib/mysql_slow_queris.log
binlog-ignore-db=mysql,information_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=2
log_slave_updates=true
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
//輸入:wq保存退出
[root@localhost ~]# systemctl start mariadb.service
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# netstat -ntap | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2989/mysqld
[root@localhost ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
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.18.%' identified by '123456';
Query OK, 0 rows affected (0.02 sec)
MariaDB [(none)]> change master to master_host='192.168.18.146',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=245;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.18.146
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000001
Read_Master_Log_Pos: 575
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 694
Relay_Master_Log_File: mysql_bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
......以下省略多行
//以上兩個狀態顯示爲YSE就是正常
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| test |
+--------------------+
5 rows in set (0.00 sec)
//此時可以看到在master1中建的school
MariaDB [(none)]> drop database school;
Query OK, 0 rows affected (0.01 sec)
//刪除數據中的school
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| myschool |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.01 sec)
//此時可以同步到master1上的更新數據
Slave1:
[root@localhost ~]# yum install epel-release -y
[root@localhost ~]# yum clean all && yum makecache
[root@localhost ~]# vim /etc/my.cnf
//在行首按9dd刪除9行
[mysqld]
log_error=/var/lib/mysql/mysql.err
log=/var/lib/mysql/mysql_log.log
log_slow_queries=/var/lib/mysql_slow_queris.log
binlog-ignore-db=mysql,information_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=3
log_slave_updates=true
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
//輸入:wq保存退出
[root@localhost ~]# systemctl start mariadb.service
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# netstat -ntap | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2989/mysqld
[root@localhost ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> change master to master_host='192.168.18.146',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=245;
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| myschool |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.01 sec)
//此時可以同步到master1上的更新數據
Slave2:
[root@localhost ~]# yum install epel-release -y
[root@localhost ~]# yum clean all && yum makecache
[root@localhost ~]# vim /etc/my.cnf
//在行首按9dd刪除9行
[mysqld]
log_error=/var/lib/mysql/mysql.err
log=/var/lib/mysql/mysql_log.log
log_slow_queries=/var/lib/mysql_slow_queris.log
binlog-ignore-db=mysql,information_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=4
log_slave_updates=true
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
//輸入:wq保存退出
[root@localhost ~]# systemctl start mariadb.service
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# netstat -ntap | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2989/mysqld
[root@localhost ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> change master to master_host='192.168.18.146',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=245;
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| myschool |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.01 sec)
//此時可以同步到master1上的更新數據
安裝高羣集可用軟件:
[root@localhost ~]# yum install mysql-mmm* -y
//五臺服務器都需要安裝,包括監控服務器
//此處監控服務器區別於其他服務器,需要先:yum install epel-release -y
//在master1中修改配置文件:
[root@localhost ~]# cd /etc/mysql-mmm/
[root@localhost mysql-mmm]# ls
mmm_agent.conf mmm_common.conf mmm_mon.conf mmm_mon_log.conf mmm_tools.conf
[root@localhost mysql-mmm]# vim mmm_common.conf
active_master_role writer
<host default>
cluster_interface ens33
pid_path /run/mysql-mmm-agent.pid
bin_path /usr/libexec/mysql-mmm/
replication_user replication
replication_password 123456
agent_user mmm_agent
agent_password 123456
</host>
<host db1>
ip 192.168.18.146 //master1的IP地址
mode master
peer db2
</host>
<host db2>
ip 192.168.18.147 //master2的IP地址
mode master
peer db1
</host>
<host db3>
ip 192.168.18.128 //slave1的IP地址
mode slave
</host>
<host db4>
ip 192.168.18.148 //slave2的IP地址
mode slave
</host>
<role writer>
hosts db1, db2
ips 192.168.18.250
mode exclusive
</role>
<role reader>
hosts db3, db4
ips 192.168.18.251, 192.168.18.252
mode balanced
</role>
~
//修改完成後輸入:wq保存退出
//同步此配置文件到另外四臺服務器上,需要輸yes確定,並輸入對應賬戶的密碼
[root@localhost mysql-mmm]# scp mmm_common.conf [email protected]:/etc/mysql-mmm/
Are you sure you want to continue connecting (yes/no)? yes
[email protected]'s password:
mmm_common.conf 100% 837 622.3KB/s 00:00
[root@localhost mysql-mmm]# scp mmm_common.conf [email protected]:/etc/mysql-mmm/
Are you sure you want to continue connecting (yes/no)? yes
[email protected]'s password:
mmm_common.conf 100% 837 941.1KB/s 00:00
[root@localhost mysql-mmm]# scp mmm_common.conf [email protected]:/etc/mysql-mmm/
Are you sure you want to continue connecting (yes/no)? yes
[email protected]'s password:
mmm_common.conf 100% 837 805.6KB/s 00:00
[root@localhost mysql-mmm]# scp mmm_common.conf [email protected]:/etc/mysql-mmm/
Are you sure you want to continue connecting (yes/no)? yes
[email protected]'s password:
mmm_common.conf 100% 837 775.5KB/s 00:00
Monitor監控服務器上的操作:
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# yum install mysql-mmm* -y
[root@localhost mysql-mmm]# yum clean all && yum makecache
[root@localhost ~]# yum install mariadb mariadb-server -y
每臺數據庫服務器中進行授權代理:
[root@localhost mysql-mmm]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> grant super, replication client, process on *.* to 'mmm_agent'@'192.168.18.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> grant replication client on *.* to 'mmm_monitor'@'192.168.18.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> flush privileges; //刷新數據
Query OK, 0 rows affected (0.00 sec)
每臺服務器上設定代理名稱的操作:
兩臺主服務器上:
[root@localhost mysql-mmm]# vim mmm_agent.conf
//master1中,this is db1
//master2中,this is db2
兩臺從服務器上:
[root@localhost ~]# cd /etc/mysql-mmm/
[root@localhost mysql-mmm]# vim mmm_agent.conf
//slave1中,this is db3
//slave2中,this is db4
修改完成後輸入:wq保存退出
所有主從服務器上開啓代理功能:
[root@localhost mysql-mmm]# systemctl start mysql-mmm-agent.service
[root@localhost mysql-mmm]# systemctl enable mysql-mmm-agent.service
開啓監控服務:
[root@localhost mysql-mmm]# systemctl start mysql-mmm-monitor.service
驗證地址是否漂移:
[root@localhost mysql-mmm]# mmm_control show
db1(192.168.18.146) master/ONLINE. Roles: writer(192.168.18.250)
db2(192.168.18.147) master/ONLINE. Roles:
db3(192.168.18.128) slave/ONLINE. Roles: reader(192.168.18.251)
db4(192.168.18.148) slave/ONLINE. Roles: reader(192.168.18.252)
//利用命令調整虛擬IP切換至master2:
[root@localhost mysql-mmm]# mmm_control move_role writer db2
OK: Role 'writer' has been moved from 'db1' to 'db2'. Now you can wait some time and check new roles info!
[root@localhost mysql-mmm]# mmm_control show
db1(192.168.18.146) master/ONLINE. Roles:
db2(192.168.18.147) master/ONLINE. Roles: writer(192.168.18.250)
db3(192.168.18.128) slave/ONLINE. Roles: reader(192.168.18.251)
db4(192.168.18.148) slave/ONLINE. Roles: reader(192.168.18.252)
//檢測所有狀態是否都正常:
[root@localhost mysql-mmm]# mmm_control checks all
db4 ping [last change: 2019/11/25 15:25:54] OK
db4 mysql [last change: 2019/11/25 15:25:54] OK
db4 rep_threads [last change: 2019/11/25 15:25:54] OK
db4 rep_backlog [last change: 2019/11/25 15:25:54] OK: Backlog is null
db2 ping [last change: 2019/11/25 15:25:54] OK
db2 mysql [last change: 2019/11/25 15:25:54] OK
db2 rep_threads [last change: 2019/11/25 15:25:54] OK
db2 rep_backlog [last change: 2019/11/25 15:25:54] OK: Backlog is null
db3 ping [last change: 2019/11/25 15:25:54] OK
db3 mysql [last change: 2019/11/25 15:25:54] OK
db3 rep_threads [last change: 2019/11/25 15:25:54] OK
db3 rep_backlog [last change: 2019/11/25 15:25:54] OK: Backlog is null
db1 ping [last change: 2019/11/25 15:25:54] OK
db1 mysql [last change: 2019/11/25 15:25:54] OK
db1 rep_threads [last change: 2019/11/25 15:25:54] OK
db1 rep_backlog [last change: 2019/11/25 15:25:54] OK: Backlog is null
以上就是所有的羣集功能都完成了!
故障測試:
在監控服務器中把地址切回db1:
[root@localhost mysql-mmm]# 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@localhost mysql-mmm]# mmm_control show
db1(192.168.18.146) master/ONLINE. Roles: writer(192.168.18.250)
db2(192.168.18.147) master/ONLINE. Roles:
db3(192.168.18.128) slave/ONLINE. Roles: reader(192.168.18.251)
db4(192.168.18.148) slave/ONLINE. Roles: reader(192.168.18.252)
在master1中關閉服務:
[root@localhost mysql-mmm]# systemctl stop mariadb.service
時再回到監控服務器中看是否會自動進行地址漂移:
[root@localhost mysql-mmm]# mmm_control show
db1(192.168.18.146) master/HARD_OFFLINE. Roles:
db2(192.168.18.147) master/ONLINE. Roles: writer(192.168.18.250)
db3(192.168.18.128) slave/ONLINE. Roles: reader(192.168.18.251)
db4(192.168.18.148) slave/ONLINE. Roles: reader(192.168.18.252)
//此時因爲master1宕機,所以地址自動漂移到master2下
如果此時master1再次上線,不會搶佔vip虛擬地址!
如果我們把slave1給宕機,再回到監控服務器,此時兩個虛擬IP地址後會給到slave2從服務器:
[root@localhost mysql-mmm]# mmm_control show
db1(192.168.18.146) master/ONLINE. Roles:
db2(192.168.18.147) master/ONLINE. Roles: writer(192.168.18.250)
db3(192.168.18.128) slave/HARD_OFFLINE. Roles:
db4(192.168.18.148) slave/ONLINE. Roles: reader(192.168.18.251), reader(192.168.18.252)
//兩個虛擬IP都會給到slave2(db4)
如果此時恢復slave1上線,那麼192.168.18.251這個虛擬地址又重新回到slave1上!
在Master1上爲監控服務器地址授權登錄:
[root@localhost mysql-mmm]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 796
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> grant all on *.* to 'zzz'@'192.168.18.145' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
在監控服務器上驗證:
[root@localhost mysql-mmm]# mysql -u zzz -p -h 192.168.18.250
Enter password: //輸入zzz用戶的密碼
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2376
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database KGC; //創建KGC的數據庫
Query OK, 1 row affected (0.01 sec)
**再回到master1上驗證是否可以同步:**
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| KGC |
| myschool |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.01 sec)