mysql-mmm主主复制定义与解析-创新互联
本文主要给大家介绍mysql-mmm主主复制定义与解析,希望可以给大家补充和更新些知识,如有其它问题需要了解的可以持续在创新互联行业资讯里面关注我的更新文章的。
成都创新互联公司主营潼南网站建设的网络公司,主营网站建设方案,重庆App定制开发,潼南h5小程序制作搭建,潼南网站营销推广欢迎潼南等地区企业咨询mysql-MMM主主复制
MMM(Master-Master replication manager for MySQL)是一套支持双主故障切换和双主日常管理的脚本程序,主要管理双主复制,而实际上在应用中只有一个主负责写的操作,另一台待机冗余,或者负责读的一部分操作。还可以结合主从复制,分离读写请求。
mmm分为agent端和monitor端,agent端部署在数据库节点上,monitor部署在监控管理端。monitor在整个数据库集群中是唯一存在的单点故障的点,因为monitor端只复制监控和管理agent节点,任务量非常的轻,一般不会出现什么故障的,而且monitor端在为agent数据库云服务器分配完vip地址后,即使停止了monitor服务,也不会影响业务的,只需要及时的修复即可。实在不放心可以为monitor部署keepalived.
环境
主机名: | 系统: | IP地址: | 安装软件: | 数据库角色 |
m1 | centos6.5 | 192.168.100.150 | mysql mysql-server mysql-mmm* | master |
m2 | centos6.5 | 192.168.100.151 | mysql mysql-server mysql-mmm* | master |
m3 | centos6.5 | 192.168.100.152 | mysql mysql-server mysql-mmm* | slave |
m4 | centos6.5 | 192.168.100.153 | mysql mysql-server mysql-mmm* | slave |
monitor | centos6.5 | 192.168.100.154 | mysql mysql-mmm* | monitor监控 |
数据库角色,和对应vip:
主机: | vip | 角色 | |
m1 | 192.168.100.250 | write | 负责写的操作 |
m2 | write | 平时不工作,待机冗余 | |
m3 | 192.168.100.201 | read | 读的操作 |
m4 | 192.168.100.202 | read | 读的操作 |
修改主机名:依次修改m1 m2 m3 m4 monitor
分别在m1 - m4 安装mysql服务:
[root@m1 ~]# yum -y install mysql mysql-server mysql-devel[root@m2 ~]# yum -y install mysql mysql-server mysql-devel[root@m3 ~]# yum -y install mysql mysql-server mysql-devel[root@m4 ~]# yum -y install mysql mysql-server mysql-devel修改m1的mysql主配置文件:
[root@m1 ~]# vi /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock log-error=/var/lib/mysql/mysql.err slow_query_log_file=/var/lib/mysql/slow_query_log.log user=mysql character-set-server=utf8 log-bin=mysql-bin server-id=150 binlog-ignore-db=mysql,information_schema log-slave-updates sync_binlog=1 auto_increment_increment=2 auto_increment_offset=1 [client] default_character_set=utf8 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid启动mysql服务,查看是否启动
[root@m1 ~]# /etc/init.d/mysqld start [root@m1 ~]# netstat -utpln |grep 3306tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1359/mysqld
将mysql的主配置文件同步到m2、m3、m4服务器上
[root@m1 ~]# for i in 1 2 3;do scp /etc/my.cnf root@192.168.100.15$i:/etc/;done The authenticity of host '192.168.100.151 (192.168.100.151)' can't be established. RSA key fingerprint is 6b:3d:8b:50:dc:45:ea:58:38:6e:5d:0d:9b:8f:04:f2. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.100.151' (RSA) to the list of known hosts. root@192.168.100.151's password: my.cnf 100% 465 0.5KB/s 00:00 The authenticity of host '192.168.100.152 (192.168.100.152)' can't be established. RSA key fingerprint is 6b:3d:8b:50:dc:45:ea:58:38:6e:5d:0d:9b:8f:04:f2. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.100.152' (RSA) to the list of known hosts. root@192.168.100.152's password: my.cnf 100% 465 0.5KB/s 00:00 The authenticity of host '192.168.100.153 (192.168.100.153)' can't be established. RSA key fingerprint is 6b:3d:8b:50:dc:45:ea:58:38:6e:5d:0d:9b:8f:04:f2. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.100.153' (RSA) to the list of known hosts. root@192.168.100.153's password: my.cnf 100% 465 0.5KB/s 00:00登陆数据库查看该数据库的bin-log文件名和偏移量:
记下File Position的信息,等会要在m1-m2-m3数据库上用到。
[root@m1 ~]# mysqladmin -uroot password 123123 [root@m1 ~]# mysql -uroot -p123123 mysql> show master status; +------------------+----------+--------------+--------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+--------------------------+ | mysql-bin.000003 | 249 | | mysql,information_schema | +------------------+----------+--------------+--------------------------+ 1 row in set (0.00 sec)授权允许复制
mysql> grant replication slave on *.* to 'replication'@'192.168.100.%' identified by '123123'; Query OK, 0 rows affected (0.02 sec) ##授权replication用户在192.168.100.0这个网段有对数据库复制的权限 mysql> flush privileges; ##刷新权限 Query OK, 0 rows affected (0.00 sec) mysql> quit Bye更改m2数据库配置文件
这是m1服务器同步过来的配置文件,只需将server-id改了就可以了,server-id在mysql集群中是唯一的标识符,在这里以ip地址结尾定义了
[root@m2 ~]# sed -i '/server-id/s/150/151/g' /etc/my.cnf [root@m2 ~]# grep id /etc/my.cnf server-id=151 pid-file=/var/run/mysqld/mysqld.pid启动登入数据库
[root@m2 ~]# /etc/init.d/mysqld start [root@m2 ~]# mysqladmin -uroot password 123123 [root@m2 ~]# mysql -uroot -p123123在m2上查看bin-log文件和偏移量记录下来,并授权用户复制权限
mysql> show master status; +------------------+----------+--------------+--------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+--------------------------+ | mysql-bin.000003 | 249 | | mysql,information_schema | +------------------+----------+--------------+--------------------------+ 1 row in set (0.00 sec) mysql> grant replication slave on *.* to 'replication'@'192.168.100.%' identified by '123123'; Query OK, 0 rows affected (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.05 sec)在m1上添加另一个主m2 同步数据
[root@m1 ~]# mysql -uroot -p123123 mysql> change master to -> master_host='192.168.100.151', ##m2的ip -> master_user='replication', ##m2上面授权的用户 -> master_password='123123', ##m2上授权用户的密码 -> master_log_file='mysql-bin.000003', ##m2的bin-log文件(刚刚在m2上查到的) -> master_log_pos=249; ##日志文件的偏移量 Query OK, 0 rows affected (0.08 sec)指定完了以后启动同步
mysql> start slave; Query OK, 0 rows affected (0.00 sec)查看同步状态信息:
这两项都为yes算是成功了
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
在m2上添加m2的另一个主m1:
mysql> change master to -> master_host='192.168.100.150', -> master_user='replication', -> master_password='123123', -> master_log_file='mysql-bin.000003', -> master_log_pos=249; Query OK, 0 rows affected (0.10 sec) mysql> start slave; Query OK, 0 rows affected (0.00 sec) mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.100.150 Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 741 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 497 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 741 Relay_Log_Space: 653 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: 1 row in set (0.00 sec) ERROR: No query specified验证:
在m1和m2上各创建个库验证是否同步:
m1上创建
mysql> create database m1_test; Query OK, 1 row affected (0.01 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | m1_test | | mysql | | test | | | +--------------------+ 5 rows in set (0.00 sec)m2上查看
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | m1_test | | mysql | | test | +--------------------+ 4 rows in set (0.07 sec)m2上新建库
mysql> create database test_m2; Query OK, 1 row affected (0.04 sec)m1上查看
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | m1_test | | mysql | | test | | test_m2 | +--------------------+ 5 rows in set (0.00 sec)正常同步了。
在m3-m4从数据库上指定主的数据库
[root@m3 ~]# sed -i '/server-id/s/150/152/g' /etc/my.cnf [root@m3 ~]# grep id /etc/my.cnf server-id=152 pid-file=/var/run/mysqld/mysqld.pid [root@m3 ~]# /etc/init.d/mysqld start[root@m3 ~]# mysqladmin -uroot password 123123 [root@m3 ~]# mysql -uroot -p123123mysql> change master to -> master_host='192.168.100.150', -> master_user='replication', -> master_password='123123', -> master_log_file='mysql-bin.000003', -> master_log_pos=249; Query OK, 0 rows affected (0.08 sec)mysql> start slave; Query OK, 0 rows affected (0.00 sec)mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.100.150 Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 929 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 931 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 929 Relay_Log_Space: 1087 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: 1 row in set (0.04 sec) ERROR: No query specifiedmysql> show databases -> ; +--------------------+ | Database | +--------------------+ | information_schema | | m1_test | | mysql | | test | | test_m2 | +--------------------+ 5 rows in set (0.08 sec) mysql>[root@m4 ~]# sed -i '/server-id/s/150/153/g' /etc/my.cnf [root@m4 ~]# grep id /etc/my.cnf server-id=153 pid-file=/var/run/mysqld/mysqld.pid [root@m4 ~]# /etc/init.d/mysqld start [root@m4 ~]# mysqladmin -uroot password 123123 [root@m4 ~]# mysql -uroot -p123123mysql> change master to -> master_host='192.168.100.150', -> master_user='replication', -> master_password='123123', -> master_log_file='mysql-bin.000003', -> master_log_pos=249; Query OK, 0 rows affected (0.10 sec)mysql> start slave; Query OK, 0 rows affected (0.00 sec)mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.100.150 Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 929 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 931 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 929 Relay_Log_Space: 1087 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: 1 row in set (0.00 sec) ERROR: No query specifiedmysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | m1_test | | mysql | | test | | test_m2 | +--------------------+ 5 rows in set (0.00 sec)安装mysql-mmm:
下载epel扩展yum源: 在所用服务器上执行
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
安装mmm软件: 在所有服务器上执行
yum -y install mysql-mmm*授权: 在m1上授权,其他主机会自动同步权限
[root@m1 ~]# mysql -uroot -p123123 mysql> grant replication client on *.* to 'mmm_monitor'@'192.168.100.%' identified by 'monitor'; Query OK, 0 rows affected (0.02 sec) mysql> grant super,replication client,process on *.* to 'mmm_agent'@'192.168.100.&' identified by 'agent'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)修改配置文件: mmm_common.conf(所有节点的通用配置文件)
[root@monitor ~]# vi /etc/mysql-mmm/mmm_common.conf
active_master_role writer同步配置文件到m1 m2 m3 m4 上:
[root@monitor ~]# for i in 150 151 152 153;do scp /etc/mysql-mmm/mmm_common.conf root@192.168.100.$i:/etc/mysql-mmm/;done The authenticity of host '192.168.100.150 (192.168.100.150)' can't be established. RSA key fingerprint is 6b:3d:8b:50:dc:45:ea:58:38:6e:5d:0d:9b:8f:04:f2. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.100.150' (RSA) to the list of known hosts. root@192.168.100.150's password: mmm_common.conf 100% 851 0.8KB/s 00:00 The authenticity of host '192.168.100.151 (192.168.100.151)' can't be established. RSA key fingerprint is 6b:3d:8b:50:dc:45:ea:58:38:6e:5d:0d:9b:8f:04:f2. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.100.151' (RSA) to the list of known hosts. root@192.168.100.151's password: mmm_common.conf 100% 851 0.8KB/s 00:00 The authenticity of host '192.168.100.152 (192.168.100.152)' can't be established. RSA key fingerprint is 6b:3d:8b:50:dc:45:ea:58:38:6e:5d:0d:9b:8f:04:f2. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.100.152' (RSA) to the list of known hosts. root@192.168.100.152's password: mmm_common.conf 100% 851 0.8KB/s 00:00 The authenticity of host '192.168.100.153 (192.168.100.153)' can't be established. RSA key fingerprint is 6b:3d:8b:50:dc:45:ea:58:38:6e:5d:0d:9b:8f:04:f2. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.100.153' (RSA) to the list of known hosts. root@192.168.100.153's password: mmm_common.conf 100% 851 0.8KB/s 00:00修改数据库mysql-m1到mysql-m4:
[root@m1 ~]# vi /etc/mysql-mmm/mmm_agent.conf [root@m1 ~]# cat /etc/mysql-mmm/mmm_agent.conf include mmm_common.conf this db1[root@m2 ~]# vi /etc/mysql-mmm/mmm_agent.conf [root@m2 ~]# cat /etc/mysql-mmm/mmm_agent.conf include mmm_common.conf this db2[root@m3 ~]# vi /etc/mysql-mmm/mmm_agent.conf [root@m3 ~]# cat /etc/mysql-mmm/mmm_agent.conf include mmm_common.conf this db3[root@m4 ~]# vi /etc/mysql-mmm/mmm_agent.conf [root@m4 ~]# cat /etc/mysql-mmm/mmm_agent.conf include mmm_common.conf this db4[root@monitor ~]# vi /etc/mysql-mmm/mmm_mon.conf [root@monitor ~]# cat /etc/mysql-mmm/mmm_mon.conf include mmm_common.conf启动服务:m1-m4的mmm-agent服务; 监控端的mmm-monitor服务
[root@m1 ~]# /etc/init.d/mysql-mmm-agent start Starting MMM Agent Daemon: [确定] [root@m1 ~]#[root@m2 ~]# [root@m2 ~]# /etc/init.d/mysql-mmm-agent start Starting MMM Agent Daemon: [确定][root@m3 ~]# /etc/init.d/mysql-mmm-agent start Starting MMM Agent Daemon: [确定][root@m4 ~]# /etc/init.d/mysql-mmm-agent start Starting MMM Agent Daemon: [确定][root@monitor ~]# /etc/init.d/mysql-mmm-monitor start Starting MMM Monitor Daemon: [确定]查看各代理数据库状态
[root@monitor ~]# mmm_control show db1(192.168.100.150) master/ONLINE. Roles: writer(192.168.100.250) db2(192.168.100.151) master/ONLINE. Roles: db3(192.168.100.152) slave/ONLINE. Roles: reader(192.168.100.201) db4(192.168.100.153) slave/ONLINE. Roles: reader(192.168.100.202)##发现写的请求交给db1的vip,读的请求交给db3 db4 vip
使用vip登录数据库:
先在m1上授权:因为设置了同步,只在一个数据库上授权,其他数据库会同步权限
[root@m1 ~]# mysql -uroot -p123123 -s mysql> grant all on *.* to 'root'@192.168.100.154 identified by '123123'; mysql> flush privileges;[root@monitor ~]# mysql -uroot -p123123 -h 192.168.100.250 -s mysql> show databases; Database information_schema m1_test mysql test test_m2登陆读的数据库:
[root@monitor ~]# mysql -uroot -p123123 -h 192.168.100.201 -s mysql> show databases; Database information_schema m1_test mysql test test_m2 mysql>在生产环境中,只需在应用服务器上,指定写数据的vip地址,和读数据的vip地址池即可。
测试:
模拟主服务器m1故障,将mysql停止了,再查看状态:
[root@m1 ~]# /etc/init.d/mysqld stop 停止 mysqld: [确定] [root@m1 ~]#[root@monitor ~]# mmm_control show db1(192.168.100.150) master/HARD_OFFLINE. Roles: ##显示为离线状态 db2(192.168.100.151) master/ONLINE. Roles: writer(192.168.100.250) ##vip转移到db2 db3(192.168.100.152) slave/ONLINE. Roles: reader(192.168.100.201) db4(192.168.100.153) slave/ONLINE. Roles: reader(192.168.100.202)再把m1启动
[root@m1 ~]# /etc/init.d/mysqld start 正在启动 mysqld: [确定] [root@m1 ~]#[root@monitor ~]# mmm_control show db1(192.168.100.150) master/AWAITING_RECOVERY. Roles: ##恢复状态 db2(192.168.100.151) master/ONLINE. Roles: writer(192.168.100.250) db3(192.168.100.152) slave/ONLINE. Roles: reader(192.168.100.201) db4(192.168.100.153) slave/ONLINE. Roles: reader(192.168.100.202) [root@monitor ~]# mmm_control show db1(192.168.100.150) master/ONLINE. Roles: ##在线状态 db2(192.168.100.151) master/ONLINE. Roles: writer(192.168.100.250) db3(192.168.100.152) slave/ONLINE. Roles: reader(192.168.100.201) db4(192.168.100.153) slave/ONLINE. Roles: reader(192.168.100.202)模拟从数据库m3故障,将数据库mysql停止查看状态,再启动查看状态
[root@m3 ~]# /etc/init.d/mysqld stop 停止 mysqld: [确定]此时会将读数据库db3的vip转移到db4;db4暂时负责读的操作
[root@monitor ~]# mmm_control show db1(192.168.100.150) master/ONLINE. Roles: db2(192.168.100.151) master/ONLINE. Roles: writer(192.168.100.250) db3(192.168.100.152) slave/HARD_OFFLINE. Roles: db4(192.168.100.153) slave/ONLINE. Roles: reader(192.168.100.201), reader(192.168.100.202)[root@m3 ~]# /etc/init.d/mysqld start 正在启动 mysqld: [确定] [root@m3 ~]#在db3数据库恢复正常后,vip会转移回来,从新工作接受读的操作
[root@monitor ~]# mmm_control show db1(192.168.100.150) master/ONLINE. Roles: db2(192.168.100.151) master/ONLINE. Roles: writer(192.168.100.250) db3(192.168.100.152) slave/ONLINE. Roles: reader(192.168.100.201) db4(192.168.100.153) slave/ONLINE. Roles: reader(192.168.100.202)看了以上关于mysql-mmm主主复制定义与解析,希望能给大家在实际运用中带来一定的帮助。本文由于篇幅有限,难免会有不足和需要补充的地方,如有需要更加专业的解答,可在官网联系我们的24小时售前售后,随时帮您解答问题的。
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网页名称:mysql-mmm主主复制定义与解析-创新互联
浏览路径:http://hbruida.cn/article/cddsco.html