mysql-mmm 安装配置(双主)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql-mmm 安装配置(双主)相关的知识,希望对你有一定的参考价值。

原文地址:mysql-mmm 安装配置 作者:chinaunix1116

MMM即Master-Master Replication Managerfor mysql(mysql主主复制管理器)关于mysql主主复制配置的监控、故障转移和管理的一套可伸缩的脚本套件(在任何时候只有一个节点可以被写入),这个套件也能对居于标准的主从配置的任意数量的从服务器进行读负载均衡,所以你可以用它来在一组居于复制的服务器启动虚拟ip,除此之外,它还有实现数据备份、节点之间重新同步功能的脚本。

MySQL本身没有提供replication failover的解决方案,通过MMM方案能实现服务器的故障转移,从而实现mysql的高可用。MMM不仅能提供浮动IP的功能,更牛的是如果当前的主服务器挂掉后,会将你后端的从自动转向新的主服务器进行同步复制,不用手工更改同步资料。

 

MMM流程:

1.准备环境,6台机器以上,拓扑图,规划IP,同步hosts,时间

2.安装包

3.配置双master

4.配置slave

5.安装mysql-mmm

6.配置monitor mmm-common.conf 定义d1-d4.IP.虚拟IP

7.Agent端mmm-agent.conf db1-db4

8.Monitor端 monIP地址. Ping真实IP

9.授权用户添加mmm.agent用户(4条)mmm.mom用户(1条)

10.启动

 

Red Hat Enterprise Linux Server release 6.3

192.168.11.128 desktop128.example.com              monitor

192.168.11.130 desktop130.example.com                master1

192.168.11.131 desktop131.example.com                master2

192.168.11.133 desktop133.example.com                      slave1

192.168.11.134 desktop134.example.com                slave2

虚拟IP:

192.168.11.200 (w)

192.168.11.201 (r)

192.168.11.202 (r)

192.168.11.203 (r)

192.168.11.204 (r)

技术分享

 

host

iptables -F

setenforce 0

 

cat >> /etc/hosts << ENDF

192.168.11.128 desktop128.example.com

192.168.11.133 desktop133.example.com

192.168.11.130 desktop130.example.com

192.168.11.131 desktop131.example.com

192.168.11.134 desktop134.example.com

ENDF

--------------------------------------------------------------

 (监管端需要安装)

yum install libart_lgpl perl-Date-Manip perl-XML-DOM perl-XML-DOM-XPath perl-XML-Parser perl-XML-RegExp rrdtool perl perl-DBD-MySQL perl-Params-Validate perl-MailTools perl-Time-HiRes perl-Class-Singleton -y

 

 (客户端需要安装)

yum install libart_lgpl perl-Date-Manip perl-XML-DOM perl-XML-DOM-XPath perl-XML-Parser perl-XML-RegExp rrdtool perl perl-DBD-MySQL perl-Params-Validate perl-MailTools perl-Time-HiRes mysql mysql-server mysql-connector-odbc  -y

 

(监控、客户也可以全部安装)

yum install libart_lgpl perl-Date-Manip perl-XML-DOM perl-XML-DOM-XPath perl-XML-Parser perl-XML-RegExp rrdtool perl perl-DBD-MySQL perl-Params-Validate perl-MailTools perl-Time-HiRes mysql mysql-server mysql-connector-odbc perl-Class-Singleton -y

 

光盘中没有的包

rpm -ivh perl-Algorithm-Diff-1.1902-9.el6.noarch.rpm perl-Log-Dispatch-2.26-1.el6.rf.noarch.rpm perl-Log-Log4perl-1.26-1.el6.rf.noarch.rpm perl-Net-ARP-1.0.6-2.1.el6.x86_64.rpm perl-Proc-Daemon-0.06-1.el6.noarch.rpm perl-Proc-ProcessTable-0.44-4.el6.x86_64.rpm rrdtool-perl-1.3.8-6.el6.x86_64.rpm perl-IPC-Shareable-0.60-2.el6.rf.noarch.rpm

-----------------------

2台master修改配置文件

master1

在[mysqld]标签下加入

server_id = 1                             

log_bin = mysql

log_slave_updates = 1

auto_increment_increment = 2

auto_increment_offset = 1         //区分2台master

 

master2

在[mysqld]标签下加入

server_id = 2

log_bin = mysql

log_slave_updates = 1

auto_increment_increment = 2

auto_increment_offset = 2

 

------------------------------------------

 

同步两台机器的数据库

[[email protected] ~]# mysqldump --all-databases > /tmp/mysql-all.sql

[[email protected] ~]# scp /tmp/mysql-all.sql [email protected]:/tmp

[[email protected] ~]# mysql < /tmp/mysql-all.sql

-----------------------------------------------

双向master         //都在master1授权,change master分别做

[[email protected] ~]# cat << ENDF | mysql

> GRANT REPLICATION SLAVE ON *.* TO ‘slave‘@‘192.168.11.130‘ IDENTIFIED BY

‘uplooking‘;

> GRANT REPLICATION SLAVE ON *.* TO ‘slave‘@‘192.168.11.131‘ IDENTIFIED BY

‘uplooking‘;

 

[[email protected] mysql]# mysql

mysql> change master to master_host="192.168.11.130",master_user="slave",master_password="uplooking",master_log_file="mysql.000001",master_log_pos=106;

mysql> slave start;

mysql> show slave status\G

 

[[email protected] mysql]# mysql

mysql> change master to master_host="192.168.11.131",master_user="slave",master_password="uplooking",master_log_file="mysql.000002",master_log_pos=506;

mysql> slave start;

mysql> show slave status\G

 

---------------------------------------------------

2台slave

[[email protected] ~]# vi /etc/my.cnf

在[mysqld]标签下加入

server-id = 3

[[email protected] ~]# service mysqld start

 

[[email protected] ~]# vi /etc/my.cnf

在[mysqld]标签下加入

server-id = 4

[[email protected] ~]# service mysqld start

------------------------------

配置两台mysql的slave模式

master1中授权

[[email protected] ~]# cat << ENDF | mysql

> GRANT REPLICATION SLAVE ON *.* TO ‘slave‘@‘192.168.11.133‘ IDENTIFIED BY

‘uplooking‘;

> GRANT REPLICATION SLAVE ON *.* TO ‘slave‘@‘192.168.11.134‘ IDENTIFIED BY

‘uplooking‘;

> FLUSH PRIVILEGES;

------------------------------------------------

133,134同步主分别在slave1 slave2 change master

 

mysql> change master to master_host="192.168.11.130",master_user="slave",master_password="uplooking",master_log_file="mysql.000001",master_log_pos=106;

slave start

show slave status\G

 

----------------------------------

安装配置mysql-mmm

在每台机器上都安装mysql-mmm

tar xf mysql-mmm-2.2.1.tar.gz

cd mysql-mmm-2.2.1

make install

---------------------------------------------

修改5台机器mmm_common.conf配置文件

[[email protected] mysql-mmm-2.2.1]# cat /etc/mysql-mmm/mmm_common.conf

active_master_role       writer

 

 

 

       cluster_interface          eth0

 

       pid_path                      /var/run/mmm_agentd.pid

       bin_path                      /usr/lib/mysql-mmm/

 

    replication_user        slave

    replication_password    uplooking

 

       agent_user                          mmm_agent

       agent_password                  uplooking

 

 

 

       ip                                       192.168.11.130

       mode                                  master

       peer                             db2

 

 

 

       ip                                       192.168.11.131

       mode                                  master

       peer                             db1

 

 

 

       ip                                       192.168.11.133

       mode                                  slave

 

 

 

       ip                                       192.168.11.134

       mode                                  slave

 

 

 

 

       hosts                                  db1, db2                      // 写

       ips                                      193.168.11.200            // 虚拟IP

       mode                                  exclusive

 

 

 

       hosts                                  db1, db2, db3,db4                             // 读

       ips                                      192.168.11.201,192.168.11.202,192.168.11.203,192.168.11.204    //虚拟ip

       mode                                  balanced

 

 

将此文件同步到所有机器上

[[email protected] ~]# for i in 128 131 133 134 ; do scp /etc/mysql-mmm/mmm_common.conf

192.168.11.$i:/etc/mysql-mmm/ ; done

-------------------------------------

修改agent端配置文件

[[email protected] ~]# vi /etc/mysql-mmm/mmm_agent.conf

include mmm_common.conf

this db1

 

[[email protected] ~]# vi /etc/mysql-mmm/mmm_agent.conf

include mmm_common.conf

this db2

 

[[email protected] ~]# vi /etc/mysql-mmm/mmm_agent.conf

include mmm_common.conf

this db3

 

[[email protected] ~]# vi /etc/mysql-mmm/mmm_agent.conf

include mmm_common.conf

this db4

----------------------------------------------

修改monitor端配置文件

[[email protected] ~]# vi /etc/mysql-mmm/mmm_mon.conf

include mmm_common.conf

 

 

       ip                                       192.168.11.128

       pid_path                      /var/run/mmm_mond.pid

       bin_path                      /usr/lib/mysql-mmm/

       status_path                         /var/lib/misc/mmm_mond.status

       ping_ips                      192.168.0.1, 192.168.0.2, 192.168.0.3       // 真实IP

 

 

 

       monitor_user               mmm_monitor

       monitor_password        uplooking

 

 

debug 0

---------------------------------------------------------------

master1中授权mmm_agent

[[email protected] ~]# mysql

mysql> grant super,replication client,process on *.* to [email protected] identified by ‘uplooking‘;

mysql> grant super,replication client,process on *.* to [email protected] identified by ‘uplooking‘;

mysql> grant super,replication client,process on *.* to [email protected] identified by ‘uplooking‘;

mysql> grant super,replication client,process on *.* to [email protected] identified by ‘uplooking‘;

 

master1中授权mmm_monitor

[[email protected] ~]# mysql

mysql> grant replication client on *.* to mmm_m[email protected] identified by ‘uplooking‘;

mysql> flush privileges;

 

起monitor服务

[[email protected] mysql-mmm]# /etc/init.d/mysql-mmm-monitor start

 

4台agent起服务

[[email protected] mysql-mmm]# service mysql-mmm-agent start

 

[[email protected] ~]# netstat -ntlp         //出现以下信息表明监听成功

tcp        0      0 127.0.0.1:9988              0.0.0.0:*                   LISTEN      20608/mmm_mond 

 

[[email protected] ~]# netstat -ntlp  

tcp        0      0 192.168.11.130:9989         0.0.0.0:*                   LISTEN      14782/mmm_agentd   

 

[[email protected] mysql-mmm]# mmm_control show                //找到4台agent,但没有Online

  db1(192.168.11.130) master/AWAITING_RECOVERY. Roles:

  db2(192.168.11.131) master/AWAITING_RECOVERY. Roles:

  db3(192.168.11.133) slave/AWAITING_RECOVERY. Roles:

  db4(192.168.11.134) slave/AWAITING_RECOVERY. Roles:

[[email protected] mysql-mmm]#

mmm_control set_online db1

mmm_control set_online db2

mmm_control set_online db3

mmm_control set_online db4

 

如遇以下报错,重启下mysqld

[[email protected] Desktop]# mmm_control set_online db3

ERROR: Host ‘db3‘ is ‘HARD_OFFLINE‘ at the moment. It can‘t be switched to ONLINE.

 

[[email protected] mysql-mmm]# mmm_control show

  db1(192.168.11.130) master/ONLINE. Roles: reader(192.168.11.202)

  db2(192.168.11.131) master/ONLINE. Roles: reader(192.168.11.201), writer(193.168.11.200)

  db3(192.168.11.133) slave/ONLINE. Roles: reader(192.168.11.203)

  db4(192.168.11.134) slave/ONLINE. Roles: reader(192.168.11.204)

-----------------------------------------------------------------------------

故障模拟

slave1,slave2同步的是master1,把master1挂掉

[[email protected] ~]# iptables -A INPUT -s 192.168.11.128 -j DROP

//添加一条规则,INPUT链 –s 拒绝monitor端网络数据包 –j 直接丢掉

[[email protected] Desktop]# mmm_control show

# Warning: agent on host db1 is not reachable

  db1(192.168.11.130) master/HARD_OFFLINE. Roles:

  db2(192.168.11.131) master/ONLINE. Roles: reader(192.168.11.204), writer(193.168.11.200)

  db3(192.168.11.133) slave/ONLINE. Roles: reader(192.168.11.201), reader(192.168.11.203)

  db4(192.168.11.134) slave/ONLINE. Roles: reader(192.168.11.202)

然后再恢复master1,看看结果

[[email protected] Desktop]#iptables –D INPUT 1

 

[[email protected] Desktop]# mmm_control set_online db1

[[email protected] Desktop]# mmm_control show

  db1(192.168.11.130) master/ONLINE. Roles: reader(192.168.11.201)

  db2(192.168.11.131) master/ONLINE. Roles: reader(192.168.11.204), writer(193.168.11.200)

  db3(192.168.11.133) slave/ONLINE. Roles: reader(192.168.11.203)

  db4(192.168.11.134) slave/ONLINE. Roles: reader(192.168.11.202)

以上是关于mysql-mmm 安装配置(双主)的主要内容,如果未能解决你的问题,请参考以下文章

MySQL-MMM高可用群集

MySQL-MMM高可用群集

mysql mmm高可用架构设计

部署集群基础环境,MySQL-MMM架构部署,MySQL-MMM架构使用

Database基础:部署集群基础环境MySQL-MMM架构部署MySQL-MMM架构使用

Mysql双主配置及安装部署