当前位置:Gxlcms > 数据库问题 > mysql-mmm主主复制

mysql-mmm主主复制

时间:2021-07-01 10:21:17 帮助过:7人阅读



MMM(Master-Master replication manager for MySQL)是一套支持双主故障切换和双主日常管理的脚本程序,主要管理双主复制,而实际上在应用中只有一个主负责写的操作,另一台待机冗余,或者负责读的一部分操作。还可以结合主从复制,分离读写请求。

mmm分为agent端和monitor端,agent端部署在数据库节点上,monitor部署在监控管理端。monitor在整个数据库集群中是唯一存在的单点故障的点,因为monitor端只复制监控和管理agent节点,任务量非常的轻,一般不会出现什么故障的,而且monitor端在为agent数据库服务器分配完vip地址后,即使停止了monitor服务,也不会影响业务的,只需要及时的修复即可。实在不放心可以为monitor部署keepalived.





环境


主机名:系统:IP地址:安装软件:数据库角色
m1centos6.5192.168.100.150mysql mysql-server mysql-mmm*master
m2centos6.5192.168.100.151mysql mysql-server mysql-mmm*master
m3centos6.5192.168.100.152mysql mysql-server mysql-mmm*slave
m4centos6.5192.168.100.153mysql mysql-server mysql-mmm*slave
monitorcentos6.5192.168.100.154mysql   mysql-mmm*monitor监控



数据库角色,和对应vip:

主机:vip角色
m1192.168.100.250
write负责写的操作
m2write平时不工作,待机冗余
m3192.168.100.201read读的操作
m4192.168.100.202read读的操作






修改主机名:依次修改m1 m2 m3 m4 monitor



分别在m1 - m4 安装mysql服务:

  1. [root@m1 ~]# yum -y install mysql mysql-server mysql-devel
  1. [root@m2 ~]# yum -y install mysql mysql-server mysql-devel
  1. [root@m3 ~]# yum -y install mysql mysql-server mysql-devel
  1. [root@m4 ~]# yum -y install mysql mysql-server mysql-devel


修改m1的mysql主配置文件:

  1. [root@m1 ~]# vi /etc/my.cnf 
  2.     
  3. [mysqld]
  4. datadir=/var/lib/mysql
  5. socket=/var/lib/mysql/mysql.sock
  6. log-error=/var/lib/mysql/mysql.err
  7. slow_query_log_file=/var/lib/mysql/slow_query_log.log
  8. user=mysql
  9. character-set-server=utf8
  10. log-bin=mysql-bin
  11. server-id=150
  12. binlog-ignore-db=mysql,information_schema
  13. log-slave-updates
  14. sync_binlog=1
  15. auto_increment_increment=2
  16. auto_increment_offset=1
  17. [client]
  18. default_character_set=utf8
  19. [mysqld_safe]
  20. log-error=/var/log/mysqld.log
  21. pid-file=/var/run/mysqld/mysqld.pid

启动mysql服务,查看是否启动

  1. [root@m1 ~]# /etc/init.d/mysqld start
  2. [root@m1 ~]# netstat -utpln |grep 3306
  3. tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      1359/mysqld

     

将mysql的主配置文件同步到m2、m3、m4服务器上

  1. [root@m1 ~]# for i in 1 2 3;do scp /etc/my.cnf root@192.168.100.15$i:/etc/;done
  2. The authenticity of host ‘192.168.100.151 (192.168.100.151)‘ can‘t be established.
  3. RSA key fingerprint is 6b:3d:8b:50:dc:45:ea:58:38:6e:5d:0d:9b:8f:04:f2.
  4. Are you sure you want to continue connecting (yes/no)? yes
  5. Warning: Permanently added ‘192.168.100.151‘ (RSA) to the list of known hosts.
  6. root@192.168.100.151‘s password: 
  7. my.cnf                                            100%  465     0.5KB/s   00:00    
  8. The authenticity of host ‘192.168.100.152 (192.168.100.152)‘ can‘t be established.
  9. RSA key fingerprint is 6b:3d:8b:50:dc:45:ea:58:38:6e:5d:0d:9b:8f:04:f2.
  10. Are you sure you want to continue connecting (yes/no)? yes
  11. Warning: Permanently added ‘192.168.100.152‘ (RSA) to the list of known hosts.
  12. root@192.168.100.152‘s password: 
  13. my.cnf                                            100%  465     0.5KB/s   00:00    
  14. The authenticity of host ‘192.168.100.153 (192.168.100.153)‘ can‘t be established.
  15. RSA key fingerprint is 6b:3d:8b:50:dc:45:ea:58:38:6e:5d:0d:9b:8f:04:f2.
  16. Are you sure you want to continue connecting (yes/no)? yes
  17. Warning: Permanently added ‘192.168.100.153‘ (RSA) to the list of known hosts.
  18. root@192.168.100.153‘s password: 
  19. my.cnf                                            100%  465     0.5KB/s   00:00

  

登陆数据库查看该数据库的bin-log文件名和偏移量:

    记下File Position的信息,等会要在m1-m2-m3数据库上用到。

  1. [root@m1 ~]# mysqladmin -uroot password 123123
  2. [root@m1 ~]# mysql -uroot -p123123
  3. mysql> show master status;
  4. +------------------+----------+--------------+--------------------------+
  5. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
  6. +------------------+----------+--------------+--------------------------+
  7. | mysql-bin.000003 |      249 |              | mysql,information_schema |
  8. +------------------+----------+--------------+--------------------------+
  9. 1 row in set (0.00 sec)


授权允许复制

  1. mysql> grant replication slave on *.* to ‘replication‘@‘192.168.100.%‘ identified by ‘123123‘;
  2. Query OK, 0 rows affected (0.02 sec)
  3.     ##授权replication用户在192.168.100.0这个网段有对数据库复制的权限
  4. mysql> flush privileges;    ##刷新权限
  5. Query OK, 0 rows affected (0.00 sec)
  6. mysql> quit
  7. Bye


更改m2数据库配置文件

    这是m1服务器同步过来的配置文件,只需将server-id改了就可以了,server-id在mysql集群中是唯一的标识符,在这里以ip地址结尾定义了

  1. [root@m2 ~]# sed -i ‘/server-id/s/150/151/g‘ /etc/my.cnf 
  2. [root@m2 ~]# grep id /etc/my.cnf 
  3. server-id=151
  4. pid-file=/var/run/mysqld/mysqld.pid

启动登入数据库

  1. [root@m2 ~]# /etc/init.d/mysqld start
  2. [root@m2 ~]# mysqladmin -uroot password 123123
  3. [root@m2 ~]# mysql -uroot -p123123

在m2上查看bin-log文件和偏移量记录下来,并授权用户复制权限

  1. mysql> show master status;
  2. +------------------+----------+--------------+--------------------------+
  3. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
  4. +------------------+----------+--------------+--------------------------+
  5. | mysql-bin.000003 |      249 |              | mysql,information_schema |
  6. +------------------+----------+--------------+--------------------------+
  7. 1 row in set (0.00 sec)
  8. mysql> grant replication slave on *.* to ‘replication‘@‘192.168.100.%‘ identified by ‘123123‘;
  9. Query OK, 0 rows affected (0.01 sec)
  10. mysql> flush privileges;
  11. Query OK, 0 rows affected (0.05 sec)


在m1上添加另一个主m2 同步数据

  1. [root@m1 ~]# mysql -uroot -p123123
  2. mysql> change master to
  3.     -> master_host=‘192.168.100.151‘,   ##m2的ip
  4.     -> master_user=‘replication‘,  ##m2上面授权的用户
  5.     -> master_password=‘123123‘,    ##m2上授权用户的密码
  6.     -> master_log_file=‘mysql-bin.000003‘,  ##m2的bin-log文件(刚刚在m2上查到的)
  7.     -> master_log_pos=249;    ##日志文件的偏移量
  8. Query OK, 0 rows affected (0.08 sec)

指定完了以后启动同步

  1. mysql> start slave;
  2. Query OK, 0 rows affected (0.00 sec)

查看同步状态信息:

    这两项都为yes算是成功了

            Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

  1. mysql> show slave status\G;
  2. *************************** 1. row ***************************
  3.                Slave_IO_State: Waiting for master to send event
  4.                   Master_Host: 192.168.100.151
  5.                   Master_User: replication
  6.                   Master_Port: 3306
  7.                 Connect_Retry: 60
  8.               Master_Log_File: mysql-bin.000003
  9.           Read_Master_Log_Pos: 495
  10.                Relay_Log_File: mysqld-relay-bin.000002
  11.                 Relay_Log_Pos: 497
  12.         Relay_Master_Log_File: mysql-bin.000003
  13.              Slave_IO_Running: Yes
  14.             Slave_SQL_Running: Yes
  15.               Replicate_Do_DB: 
  16.           Replicate_Ignore_DB: 
  17.            Replicate_Do_Table: 
  18.        Replicate_Ignore_Table: 
  19.       Replicate_Wild_Do_Table: 
  20.   Replicate_Wild_Ignore_Table: 
  21.                    Last_Errno: 0
  22.                    Last_Error: 
  23.                  Skip_Counter: 0
  24.           Exec_Master_Log_Pos: 495
  25.               Relay_Log_Space: 653
  26.               Until_Condition: None
  27.                Until_Log_File: 
  28.                 Until_Log_Pos: 0
  29.            Master_SSL_Allowed: No
  30.            Master_SSL_CA_File: 
  31.            Master_SSL_CA_Path: 
  32.               Master_SSL_Cert: 
  33.             Master_SSL_Cipher: 
  34.                Master_SSL_Key: 
  35.         Seconds_Behind_Master: 0
  36. Master_SSL_Verify_Server_Cert: No
  37.                 Last_IO_Errno: 0
  38.                 Last_IO_Error: 
  39.                Last_SQL_Errno: 0
  40.                Last_SQL_Error: 
  41. 1 row in set (0.00 sec)
  42. ERROR: 
  43. No query specified



在m2上添加m2的另一个主m1:

  1. mysql> change master to
  2.     -> master_host=‘192.168.100.150‘,
  3.     -> master_user=‘replication‘,
  4.     -> master_password=‘123123‘,
  5.     -> master_log_file=‘mysql-bin.000003‘,
  6.     -> master_log_pos=249;
  7. Query OK, 0 rows affected (0.10 sec)
  8. mysql> start slave;
  9. Query OK, 0 rows affected (0.00 sec)
  10. mysql> show slave status\G;
  11. *************************** 1. row ***************************
  12.                Slave_IO_State: Waiting for master to send event
  13.                   Master_Host: 192.168.100.150
  14.                   Master_User: replication
  15.                   Master_Port: 3306
  16.                 Connect_Retry: 60
  17.               Master_Log_File: mysql-bin.000003
  18.           Read_Master_Log_Pos: 741
  19.                Relay_Log_File: mysqld-relay-bin.000002
  20.                 Relay_Log_Pos: 497
  21.         Relay_Master_Log_File: mysql-bin.000003
  22.              Slave_IO_Running: Yes
  23.             Slave_SQL_Running: Yes
  24.               Replicate_Do_DB: 
  25.           Replicate_Ignore_DB: 
  26.            Replicate_Do_Table: 
  27.        Replicate_Ignore_Table: 
  28.       Replicate_Wild_Do_Table: 
  29.   Replicate_Wild_Ignore_Table: 
  30.                    Last_Errno: 0
  31.                    Last_Error: 
  32.                  Skip_Counter: 0
  33.           Exec_Master_Log_Pos: 741
  34.               Relay_Log_Space: 653
  35.               Until_Condition: None
  36.                Until_Log_File: 
  37.                 Until_Log_Pos: 0
  38.            Master_SSL_Allowed: No
  39.            Master_SSL_CA_File: 
  40.            Master_SSL_CA_Path: 
  41.               Master_SSL_Cert: 
  42.             Master_SSL_Cipher: 
  43.                Master_SSL_Key: 
  44.         Seconds_Behind_Master: 0
  45. Master_SSL_Verify_Server_Cert: No
  46.                 Last_IO_Errno: 0
  47.                 Last_IO_Error: 
  48.                Last_SQL_Errno: 0
  49.                Last_SQL_Error: 
  50. 1 row in set (0.00 sec)
  51. ERROR: 
  52. No query specified



验证:

在m1和m2上各创建个库验证是否同步:

    m1上创建

  1. mysql> create database m1_test;
  2. Query OK, 1 row affected (0.01 sec)
  3. mysql> show databases;
  4. +--------------------+
  5. | Database           |
  6. +--------------------+
  7. | information_schema |
  8. | m1_test            |
  9. | mysql              |
  10. | test               |
  11. |          |
  12. +--------------------+
  13. 5 rows in set (0.00 sec)

m2上查看

  1. mysql> show databases;
  2. +--------------------+
  3. | Database           |
  4. +--------------------+
  5. | information_schema |
  6. | m1_test            |
  7. | mysql              |
  8. | test               |
  9. +--------------------+
  10. 4 rows in set (0.07 sec)

m2上新建库

  1. mysql> create database test_m2;
  2. Query OK, 1 row affected (0.04 sec)

m1上查看

  1. mysql> show databases;
  2. +--------------------+
  3. | Database           |
  4. +--------------------+
  5. | information_schema |
  6. | m1_test            |
  7. | mysql              |
  8. | test               |
  9. | test_m2            |
  10. +--------------------+
  11. 5 rows in set (0.00 sec)

正常同步了。



在m3-m4从数据库上指定主的数据库

  1. [root@m3 ~]# sed -i ‘/server-id/s/150/152/g‘ /etc/my.cnf
  2. [root@m3 ~]# grep id /etc/my.cnf 
  3. server-id=152
  4. pid-file=/var/run/mysqld/mysqld.pid
  5. [root@m3 ~]# /etc/init.d/mysqld start
  1. [root@m3 ~]# mysqladmin -uroot password 123123
  2. [root@m3 ~]# mysql -uroot -p123123
  1. mysql> change master to
  2.     -> master_host=‘192.168.100.150‘,
  3.     -> master_user=‘replication‘,
  4.     -> master_password=‘123123‘,
  5.     -> master_log_file=‘mysql-bin.000003‘,
  6.     -> master_log_pos=249;
  7. Query OK, 0 rows affected (0.08 sec)
  1. mysql> start slave;
  2. Query OK, 0 rows affected (0.00 sec)
  1. mysql> show slave status\G;
  2. *************************** 1. row ***************************
  3.                Slave_IO_State: Waiting for master to send event
  4.                   Master_Host: 192.168.100.150
  5.                   Master_User: replication
  6.                   Master_Port: 3306
  7.                 Connect_Retry: 60
  8.               Master_Log_File: mysql-bin.000003
  9.           Read_Master_Log_Pos: 929
  10.                Relay_Log_File: mysqld-relay-bin.000002
  11.                 Relay_Log_Pos: 931
  12.         Relay_Master_Log_File: mysql-bin.000003
  13.              Slave_IO_Running: Yes
  14.             Slave_SQL_Running: Yes
  15.               Replicate_Do_DB: 
  16.           Replicate_Ignore_DB: 
  17.            Replicate_Do_Table: 
  18.        Replicate_Ignore_Table: 
  19.       Replicate_Wild_Do_Table: 
  20.   Replicate_Wild_Ignore_Table: 
  21.                    Last_Errno: 0
  22.                    Last_Error: 
  23.                  Skip_Counter: 0
  24.           Exec_Master_Log_Pos: 929
  25.               Relay_Log_Space: 1087
  26.               Until_Condition: None
  27.                Until_Log_File: 
  28.                 Until_Log_Pos: 0
  29.            Master_SSL_Allowed: No
  30.            Master_SSL_CA_File: 
  31.            Master_SSL_CA_Path: 
  32.               Master_SSL_Cert: 
  33.             Master_SSL_Cipher: 
  34.                Master_SSL_Key: 
  35.         Seconds_Behind_Master: 0
  36. Master_SSL_Verify_Server_Cert: No
  37.                 Last_IO_Errno: 0
  38.                 Last_IO_Error: 
  39.                Last_SQL_Errno: 0
  40.                Last_SQL_Error: 
  41. 1 row in set (0.04 sec)
  42. ERROR: 
  43. No query specified
  1. mysql> show databases
  2.     -> ;
  3. +--------------------+
  4. | Database           |
  5. +--------------------+
  6. | information_schema |
  7. | m1_test            |
  8. | mysql              |
  9. | test               |
  10. | test_m2            |
  11. +--------------------+
  12. 5 rows in set (0.08 sec)
  13. mysql>




  1. [root@m4 ~]# sed -i ‘/server-id/s/150/153/g‘ /etc/my.cnf
  2. [root@m4 ~]# grep id /etc/my.cnf 
  3. server-id=153
  4. pid-file=/var/run/mysqld/mysqld.pid
  5. [root@m4 ~]# /etc/init.d/mysqld start
  6. [root@m4 ~]# mysqladmin -uroot password 123123
  7. [root@m4 ~]# mysql -uroot -p123123
  1. mysql> change master to
  2.     -> master_host=‘192.168.100.150‘,
  3.     -> master_user=‘replication‘,
  4.     -> master_password=‘123123‘,
  5.     -> master_log_file=‘mysql-bin.000003‘,
  6.     -> master_log_pos=249;
  7. Query OK, 0 rows affected (0.10 sec)
  1. mysql> start slave;
  2. Query OK, 0 rows affected (0.00 sec)
  1. mysql> show slave status\G;
  2. *************************** 1. row ***************************
  3.                Slave_IO_State: Waiting for master to send event
  4.                   Master_Host: 192.168.100.150
  5.                   Master_User: replication
  6.                   Master_Port: 3306
  7.                 Connect_Retry: 60
  8.               Master_Log_File: mysql-bin.000003
  9.           Read_Master_Log_Pos: 929
  10.                Relay_Log_File: mysqld-relay-bin.000002
  11.                 Relay_Log_Pos: 931
  12.         Relay_Master_Log_File: mysql-bin.000003
  13.              Slave_IO_Running: Yes
  14.             Slave_SQL_Running: Yes
  15.               Replicate_Do_DB: 
  16.           Replicate_Ignore_DB: 
  17.            Replicate_Do_Table: 
  18.        Replicate_Ignore_Table: 
  19.       Replicate_Wild_Do_Table: 
  20.   Replicate_Wild_Ignore_Table: 
  21.                    Last_Errno: 0
  22.                    Last_Error: 
  23.                  Skip_Counter: 0
  24.           Exec_Master_Log_Pos: 929
  25.               Relay_Log_Space: 1087
  26.               Until_Condition: None
  27.                Until_Log_File: 
  28.                 Until_Log_Pos: 0
  29.            Master_SSL_Allowed: No
  30.            Master_SSL_CA_File: 
  31.            Master_SSL_CA_Path: 
  32.               Master_SSL_Cert: 
  33.             Master_SSL_Cipher: 
  34.                Master_SSL_Key: 
  35.         Seconds_Behind_Master: 0
  36. Master_SSL_Verify_Server_Cert: No
  37.                 Last_IO_Errno: 0
  38.                 Last_IO_Error: 
  39.                Last_SQL_Errno: 0
  40.                Last_SQL_Error: 
  41. 1 row in set (0.00 sec)
  42. ERROR: 
  43. No query specified
  1. mysql> show databases;
  2. +--------------------+
  3. | Database           |
  4. +--------------------+
  5. | information_schema |
  6. | m1_test            |
  7. | mysql              |
  8. | test               |
  9. | test_m2            |
  10. +--------------------+
  11. 5 rows in set (0.00 sec)





安装mysql-mmm:

    下载epel扩展yum源:  在所用服务器上执行

  1. wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

     安装mmm软件:   在所有服务器上执行

  1. yum -y install mysql-mmm*


授权: 在m1上授权,其他主机会自动同步权限

  1. [root@m1 ~]# mysql -uroot -p123123
  2. mysql> grant replication client on *.* to ‘mmm_monitor‘@‘192.168.100.%‘ identified by ‘monitor‘;
  3. Query OK, 0 rows affected (0.02 sec)
  4. mysql> grant super,replication client,process on *.* to ‘mmm_agent‘@‘192.168.100.&‘ identified by ‘agent‘;
  5. Query OK, 0 rows affected (0.00 sec)
  6. mysql> flush privileges;
  7. Query OK, 0 rows affected (0.00 sec)

修改配置文件: mmm_common.conf(所有节点的通用配置文件)



[root@monitor ~]# vi /etc/mysql-mmm/mmm_common.conf 

  1. active_master_role      writer
  2. <host default>
  3.     cluster_interface       eth0
  4.     pid_path                /var/run/mysql-mmm/mmm_agentd.pid
  5.     bin_path                /usr/libexec/mysql-mmm/
  6.     replication_user        replication   ##复制同步的用户名
  7.     replication_password    123123    ##同步的密码
  8.     agent_user              mmm_agent   ##代理的用户名
  9.     agent_password          agent   ##代理密码
  10. </host>  
  11. <host db1>
  12.     ip      192.168.100.150
  13.     mode    master
  14.     peer    db2
  15. </host>
  16. <host db2>
  17.     ip      192.168.100.151
  18.     mode    master
  19.     peer    db1
  20. </host>
  21. <host db3>
  22.     ip      192.168.100.152
  23.     mode    slave
  24. </host>
  25. <host db4>
  26.     ip      192.168.100.153
  27.     mode    slave
  28. </host>
  29. <role writer>
  30.     hosts   db1, db2
  31.     ips     192.168.100.250    ##写的vip
  32.     mode    exclusive      ##独占模式
  33. </role>
  34. <role reader>
  35.     hosts   db3, db4
  36.     ips     192.168.100.201, 192.168.100.202   ##读的vip
  37.     mode    balanced    ##平衡模式
  38. </role>

同步配置文件到m1 m2 m3 m4 上:

  1. [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
  2. The authenticity of host ‘192.168.100.150 (192.168.100.150)‘ can‘t be established.
  3. RSA key fingerprint is 6b:3d:8b:50:dc:45:ea:58:38:6e:5d:0d:9b:8f:04:f2.
  4. Are you sure you want to continue connecting (yes/no)? yes
  5. Warning: Permanently added ‘192.168.100.150‘ (RSA) to the list of known hosts.
  6. root@192.168.100.150‘s password: 
  7. mmm_common.conf                                           100%  851     0.8KB/s   00:00    
  8. The authenticity of host ‘192.168.100.151 (192.168.100.151)‘ can‘t be established.
  9. RSA key fingerprint is 6b:3d:8b:50:dc:45:ea:58:38:6e:5d:0d:9b:8f:04:f2.
  10. Are you sure you want to continue connecting (yes/no)? yes
  11. Warning: Permanently added ‘192.168.100.151‘ (RSA) to the list of known hosts.
  12. root@192.168.100.151‘s password: 
  13. mmm_common.conf                                           100%  851     0.8KB/s   00:00    
  14. The authenticity of host ‘192.168.100.152 (192.168.100.152)‘ can‘t be established.
  15. RSA key fingerprint is 6b:3d:8b:50:dc:45:ea:58:38:6e:5d:0d:9b:8f:04:f2.
  16. Are you sure you want to continue connecting (yes/no)? yes
  17. Warning: Permanently added ‘192.168.100.152‘ (RSA) to the list of known hosts.
  18. root@192.168.100.152‘s password: 
  19. mmm_common.conf                                           100%  851     0.8KB/s   00:00    
  20. The authenticity of host ‘192.168.100.153 (192.168.100.153)‘ can‘t be established.
  21. RSA key fingerprint is 6b:3d:8b:50:dc:45:ea:58:38:6e:5d:0d:9b:8f:04:f2.
  22. Are you sure you want to continue connecting (yes/no)? yes
  23. Warning: Permanently added ‘192.168.100.153‘ (RSA) to the list of known hosts.
  24. root@192.168.100.153‘s password: 
  25. mmm_common.conf                                           100%  851     0.8KB/s   00:00

修改数据库mysql-m1mysql-m4

  1. [root@m1 ~]# vi /etc/mysql-mmm/mmm_agent.conf 
  2. [root@m1 ~]# cat /etc/mysql-mmm/mmm_agent.conf
  3. include mmm_common.conf
  4. this db1
  1. [root@m2 ~]# vi /etc/mysql-mmm/mmm_agent.conf 
  2. [root@m2 ~]# cat /etc/mysql-mmm/mmm_agent.conf
  3. include mmm_common.conf
  4. this db2
  1. [root@m3 ~]# vi /etc/mysql-mmm/mmm_agent.conf 
  2. [root@m3 ~]# cat /etc/mysql-mmm/mmm_agent.conf
  3. include mmm_common.conf
  4. this db3
  1. [root@m4 ~]# vi /etc/mysql-mmm/mmm_agent.conf 
  2. [root@m4 ~]# cat /etc/mysql-mmm/mmm_agent.conf
  3. include mmm_common.conf
  4. this db4



  1. [root@monitor ~]# vi /etc/mysql-mmm/mmm_mon.conf 
  2. [root@monitor ~]# cat /etc/mysql-mmm/mmm_mon.conf
  3. include mmm_common.conf
  4. <monitor>
  5.     ip                  127.0.0.1
  6.     pid_path            /var/run/mysql-mmm/mmm_mond.pid
  7.     bin_path            /usr/libexec/mysql-mmm
  8.     status_path         /var/lib/mysql-mmm/mmm_mond.status
  9.     ping_ips            192.168.100.150, 192.168.100.151, 192.168.100.152, 192.168.100.153
  10.         ##监测每个节点数据库:修改为每个服务器的真实ip地址
  11.     auto_set_online     60     ##自动上线时间,
  12. </monitor>
  13. <host default>
  14.     monitor_user        mmm_monitor     ##监控服务的用户名
  15.     monitor_password    monitor     ##监控服务的密码,这两项是在m1上授权的
  16. </host>
  17. debug 0



启动服务:m1-m4的mmm-agent服务; 监控端的mmm-monitor服务

  1. [root@m1 ~]# /etc/init.d/mysql-mmm-agent start
  2. Starting MMM Agent Daemon:                                 [确定]
  3. [root@m1 ~]#
  1. [root@m2 ~]# 
  2. [root@m2 ~]# /etc/init.d/mysql-mmm-agent start
  3. Starting MMM Agent Daemon:                                 [确定]
  1. [root@m3 ~]# /etc/init.d/mysql-mmm-agent start
  2. Starting MMM Agent Daemon:                                 [确定]
  1. [root@m4 ~]# /etc/init.d/mysql-mmm-agent start
  2. Starting MMM Agent Daemon:                                 [确定]


  1. [root@monitor ~]# /etc/init.d/mysql-mmm-monitor start
  2. Starting MMM Monitor Daemon:                               [确定]


查看各代理数据库状态

  1. [root@monitor ~]# mmm_control show
  2.   db1(192.168.100.150) master/ONLINE. Roles: writer(192.168.100.250)
  3.   db2(192.168.100.151) master/ONLINE. Roles: 
  4.   db3(192.168.100.152) slave/ONLINE. Roles: reader(192.168.100.201)
  5.   db4(192.168.100.153) slave/ONLINE. Roles: reader(192.168.100.202)


            ##发现写的请求交给db1的vip,读的请求交给db3 db4 vip



使用vip登录数据库:

    先在m1上授权:因为设置了同步,只在一个数据库上授权,其他数据库会同步权限


  1. [root@m1 ~]# mysql -uroot -p123123 -s
  2. mysql> grant all on *.* to ‘root‘@192.168.100.154 identified by ‘123123‘;
  3. mysql> flush privileges;


  1. [root@monitor ~]# mysql -uroot -p123123 -h 192.168.100.250 -s
  2. mysql> show databases;
  3. Database
  4. information_schema
  5. m1_test
  6. mysql
  7. test
  8. test_m2

登陆读的数据库:

  1. [root@monitor ~]# mysql -uroot -p123123 -h 192.168.100.201 -s
  2. mysql> show databases;
  3. Database
  4. information_schema
  5. m1_test
  6. mysql
  7. test
  8. test_m2
  9. mysql>


在生产环境中,只需在应用服务器上,指定写数据的vip地址,和读数据的vip地址池即可。




测试:

    模拟主服务器m1故障,将mysql停止了,再查看状态:

  1. [root@m1 ~]# /etc/init.d/mysqld stop
  2. 停止 mysqld:                                              [确定]
  3. [root@m1 ~]#
  1. [root@monitor ~]# mmm_control show
  2.   db1(192.168.100.150) master/HARD_OFFLINE. Roles:   ##显示为离线状态
  3.   db2(192.168.100.151) master/ONLINE. Roles: writer(192.168.100.250)  ##vip转移到db2
  4.   db3(192.168.100.152) slave/ONLINE. Roles: reader(192.168.100.201)
  5.   db4(192.168.100.153) slave/ONLINE. Roles: reader(192.168.100.202)

    再把m1启动

  1. [root@m1 ~]# /etc/init.d/mysqld start
  2. 正在启动 mysqld:                                          [确定]
  3. [root@m1 ~]#
  1. [root@monitor ~]# mmm_control show
  2.   db1(192.168.100.150) master/AWAITING_RECOVERY. Roles:   ##恢复状态
  3.   db2(192.168.100.151) master/ONLINE. Roles: writer(192.168.100.250)
  4.   db3(192.168.100.152) slave/ONLINE. Roles: reader(192.168.100.201)
  5.   db4(192.168.100.153) slave/ONLINE. Roles: reader(192.168.100.202)
  6. [root@monitor ~]# mmm_control show
  7.   db1(192.168.100.150) master/ONLINE. Roles:   ##在线状态
  8.   db2(192.168.100.151) master/ONLINE. Roles: writer(192.168.100.250)
  9.   db3(192.168.100.152) slave/ONLINE. Roles: reader(192.168.100.201)
  10.   db4(192.168.100.153) slave/ONLINE. Roles: reader(192.168.100.202)

    模拟从数据库m3故障,将数据库mysql停止查看状态,再启动查看状态

  1. [root@m3 ~]# /etc/init.d/mysqld stop
  2. 停止 mysqld:                                              [确定]

        此时会将读数据库db3的vip转移到db4;db4暂时负责读的操作

  1. [root@monitor ~]# mmm_control show
  2.   db1(192.168.100.150) master/ONLINE. Roles: 
  3.   db2(192.168.100.151) master/ONLINE. Roles: writer(192.168.100.250)
  4.   db3(192.168.100.152) slave/HARD_OFFLINE. Roles: 
  5.   db4(192.168.100.153) slave/ONLINE. Roles: reader(192.168.100.201), reader(192.168.100.202)
  1. [root@m3 ~]# /etc/init.d/mysqld start
  2. 正在启动 mysqld:                                          [确定]
  3. [root@m3 ~]#

        在db3数据库恢复正常后,vip会转移回来,从新工作接受读的操作

  1. [root@monitor ~]# mmm_control show
  2.   db1(192.168.100.150) master/ONLINE. Roles: 
  3.   db2(192.168.100.151) master/ONLINE. Roles: writer(192.168.100.250)
  4.   db3(192.168.100.152) slave/ONLINE. Roles: reader(192.168.100.201)
  5.   db4(192.168.100.153) slave/ONLINE. Roles: reader(192.168.100.202)


本文出自 “向往技术的小白” 博客,请务必保留此出处http://lesliecheung.blog.51cto.com/12622169/1958969

mysql-mmm主主复制

标签:mysql-mmm mysql集群

人气教程排行