主服務器1:192.168.18.146 db1 vip:192.168.18.250java
主服務器2:192.168.18.147 db2 mysql
從服務器1:192.168.18.128 db3 vip:192.168.18.251sql
從服務器2:192.168.18.148 db4 vip:192.168.18.252數據庫
監控服務器:192.168.18.145vim
[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
[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上的更新數據
[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上的更新數據
[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 root@192.168.18.147:/etc/mysql-mmm/ Are you sure you want to continue connecting (yes/no)? yes root@192.168.18.147's password: mmm_common.conf 100% 837 622.3KB/s 00:00 [root@localhost mysql-mmm]# scp mmm_common.conf root@192.168.18.128:/etc/mysql-mmm/ Are you sure you want to continue connecting (yes/no)? yes root@192.168.18.128's password: mmm_common.conf 100% 837 941.1KB/s 00:00 [root@localhost mysql-mmm]# scp mmm_common.conf root@192.168.18.148:/etc/mysql-mmm/ Are you sure you want to continue connecting (yes/no)? yes root@192.168.18.148's password: mmm_common.conf 100% 837 805.6KB/s 00:00 [root@localhost mysql-mmm]# scp mmm_common.conf root@192.168.18.145:/etc/mysql-mmm/ Are you sure you want to continue connecting (yes/no)? yes root@192.168.18.145's password: mmm_common.conf 100% 837 775.5KB/s 00:00
[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
以上就是全部的羣集功能都完成了!服務器
[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)
[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下
[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)
[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)