優勢:高可用性,擴展性好,出現故障自動切換,對於主主同步,在同一時間只提供一臺數據庫寫操做,保證的數據的一致性。 缺點:Monitor節點是單點,能夠結合Keepalived實現高可用。
MMM(Master-Master replication managerfor Mysql,Mysql主主複製管理器)是一套靈活的腳本程序,基於perl實現,用來對mysql replication進行監控和故障遷移,並能管理mysql Master-Master複製的配置(同一時間只有一個節點是可寫的)。mysql
mmm_mond:監控進程,負責全部的監控工做,決定和處理全部節點角色活動。此腳本須要在監管機上運行。sql
mmm_agentd:運行在每一個mysql服務器上的代理進程,完成監控的探針工做和執行簡單的遠端服務設置。此腳本須要在被監管機上運行。數據庫
mmm_control:一個簡單的腳本,提供管理mmm_mond進程的命令。vim
mysql-mmm的監管端會提供多個虛擬IP(VIP),包括一個可寫VIP,多個可讀VIP,經過監管的管理,這些IP會綁定在可用mysql之上,當某一臺mysql宕機時,監管會將VIP遷移至其餘mysql。緩存
在整個監管過程當中,須要在mysql中添加相關受權用戶,以便讓mysql能夠支持監理機的維護。受權的用戶包括一個mmm_monitor用戶和一個mmm_agent用戶,若是想使用mmm的備份工具則還要添加一個mmm_tools用戶。服務器
master1服務器 192.168.13.167 master2服務器 192.168.13.151 slave1服務器 192.168.13.168 slave2服務器 192.168.13.145 monitor服務器 192.168.13.164
[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 ##開啓數據庫
[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 ##重啓數據庫
[root@master1 ~]# scp /etc/my.cnf root@192.168.13.151:/etc/ (server-id=22) [root@master1 ~]# scp /etc/my.cnf root@192.168.13.168:/etc/ (server-id=33) [root@master1 ~]# scp /etc/my.cnf root@192.168.13.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.13.%' identified by '123456'; ##受權給13段的網段複製的權限用戶名replication密碼123456 Query OK, 0 rows affected (0.00 sec) //master2服務器// MariaDB [(none)]> change master to master_host='192.168.13.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.13.%' 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.13.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
MariaDB [(none)]> change master to master_host='192.168.13.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) //從服務器上查看數據庫// MariaDB [(none)]> show databases; ##實現主主,主從同步 +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | school | | test | +--------------------+ 5 rows in set (0.00 sec)
##monitor服務器上 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 [root@master1 ~]# yum -y install mysql-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.13.167 ##master1地址 mode master peer db2 </host> <host db2> ip 192.168.13.151 ##master2地址 mode master peer db1 </host> <host db3> ip 192.168.13.168 ##slave1地址 mode slave </host> <host db4> ip 192.168.13.145 ##slave2地址 mode slave </host> <role writer> hosts db1, db2 ##寫服務器虛擬ip ips 192.168.13.250 mode exclusive </role> <role reader> hosts db3, db4 ##讀服務器虛擬ip ips 192.168.13.251, 192.168.13.252 mode balanced </role> ##複製到其餘的服務器中 [root@master1 ~]# scp /etc/mysql-mmm/mmm_common.conf root@192.168.13.151:/etc/mysql-mmm/ root@192.168.13.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.13.168:/etc/mysql-mmm/ root@192.168.13.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.13.145:/etc/mysql-mmm/ root@192.168.13.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.13.164:/etc/mysql-mmm/
[root@monitor ~]# vim /etc/mysql-mmm/mmm_mon.conf <host default> monitor_user mmm_monitor monitor_password 123456 ##修改monitor的密碼 </host>
MariaDB [(none)]> grant super, replication client, process on *.* to 'mmm_agent'@'192.168.13.%' identified by '123456'; ##受權代理 Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> grant replication client on *.* to 'mmm_monitor'@'192.168.13.%' identified by '123456'; ##受權監控 Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> flush privileges; ##刷新權限 Query OK, 0 rows affected (0.00 sec)
[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 ##加入開機自啓動
[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.13.167,192.168.13.151,192.168.13.168,192.168.13. 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.13.167) master/ONLINE. Roles: writer(192.168.13.250) db2(192.168.13.151) master/ONLINE. Roles: db3(192.168.13.168) slave/ONLINE. Roles: reader(192.168.13.252) db4(192.168.13.145) slave/ONLINE. Roles: reader(192.168.13.251)
[root@master1 ~]# systemctl stop mariadb.service ##模擬中止master1服務器 //monitor服務器上查看// [root@monitor ~]# mmm_control show db1(192.168.13.167) master/HARD_OFFLINE. Roles: db2(192.168.13.151) master/ONLINE. Roles: writer(192.168.13.250) db3(192.168.13.168) slave/ONLINE. Roles: reader(192.168.13.252) db4(192.168.13.145) slave/ONLINE. Roles: reader(192.168.13.251) (重啓master1數據庫服務,虛擬地址不會被搶佔到master1) mmm_control checks all //須要各類OK mmm_control move_role writer db1 ##能夠切換虛擬地址
[root@monitor ~]# yum install mysql -y //master1服務器上受權monitor地址訪問// MariaDB [(none)]> grant all on *.* to 'testdba'@'192.168.13.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.13.250 ##使用虛擬地址便可登陸數據庫 Enter password: MariaDB [(none)]>