MySQL+Amoeba+MySQLMMM高可用群集

一、MySQL-MMM(Master-Master MySQL)

MMM概述

  • 双主故障切换和日常管理的脚本程序
  • 由多个mysql主服务器和多个mysql从服务器组成
  • 虽然叫做双主复制,但是业务上同一时刻只允许对一个主进行写入,另一台备选主上提供部分读服务,
  • MMM使用Perl语言开发,主要用来监控和管理MySQL Master-Master(双主)复制
  • 其内部附加的工具脚本也可以实现多个slave的read负载均衡。

注:

  • 同一时刻只允许一个主进行写入,额外主提供部分读的服务

  • 不适用于要求数据一致性很高的场合(可替换产品为:Heartbeat+DRBD+MySQL高可用方案)

MMM组成

  • mmm_mond:监控进程,负责所有的监控、决定和处理所有节点
  • mmm_agentd:运行在每个MySQL数据库的代理进程,完成监控本地状态并于监控端通信
  • mmm_control:一个脚本,提供mmm_mond进程的命令

二、案例

实验环境:
六台Centos6,两台主mysql(master01、master02),两台从mysql(slave01、slave02),一台监控(mmm_mond),一台读写调度器(amoeba)

先部署主主(master01与master02)复制

Master01

1.准备工作

vim /etc/sysconfig/network-scripts/ifcfg-eth0
    DEVICE=eth0
    TYPE=Ethernet
    ONBOOT=yes
    NM_CONTROLLED=no
    BOOTPROTO=static
    IPADDR=192.168.1.10
    NETMASK=255.255.255.0
vim /etc/sysconfig/network-scripts/ifcfg-eth1
    DEVICE=eth1
    TYPE=Ethernet
    ONBOOT=yes
    NM_CONTROLLED=no
    BOOTPROTO=dhcp
vim /etc/hosts
    192.168.1.10    db1
    192.168.1.20    db2
    192.168.1.30    db3
    192.168.1.40    db4
vim /etc/sysconfig/network
    HOSTNAME=db1
reboot

2.YUM源配置并安装MMM

rm -rf /etc/yum.repos.d/*
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
yum -y install epel-release
yum -y install mysql-mmm* mysql mysql-server mysql-devel

3.MySQL配置

/etc/init.d/mysqld start && chkconfig --level 35 mysqld on
mysqladmin -uroot password "123"
cp /usr/share/doc/mysql-server-5.1.73/my-medium.cnf /etc/my.cnf
vim /etc/my.cnf
    [mysqld]
    50 log-slave-updates
/etc/init.d/mysqld restart

4.授权并主主同步

mysql -u root -p
mysql> grant replication slave on *.* to 'slave'@'192.168.1.%' identified by '123';
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      647 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql> change master to master_host='192.168.1.20',master_user='slave',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=481;
//所跟IP、log、pos等信息都为第二台主的信息
mysql> start slave;
mysql> show slave status\G;
//查看同步状态,I/0和SQL线程状态为yes则正确

Master02

1.准备工作

vim /etc/sysconfig/network-scripts/ifcfg-eth0
    DEVICE=eth0
    TYPE=Ethernet
    ONBOOT=yes
    NM_CONTROLLED=no
    BOOTPROTO=static
    IPADDR=192.168.1.20
    NETMASK=255.255.255.0
vim /etc/sysconfig/network-scripts/ifcfg-eth1
    DEVICE=eth1
    TYPE=Ethernet
    ONBOOT=yes
    NM_CONTROLLED=no
    BOOTPROTO=dhcp
vim /etc/hosts
    192.168.1.10    db1
    192.168.1.20    db2
    192.168.1.30    db3
    192.168.1.40    db4
vim /etc/sysconfig/network
HOSTNAME=db2
reboot

2.YUM源配置并安装MMM

rm -rf /etc/yum.repos.d/*
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
yum -y install epel-release
yum -y install mysql-mmm* mysql mysql-server mysql-devel

3.MySQL配置

/etc/init.d/mysqld start && chkconfig --level 35 mysqld on
mysqladmin -uroot password "123"
cp /usr/share/doc/mysql-server-5.1.73/my-medium.cnf /etc/my.cnf
vim /etc/my.cnf
    [mysqld]
    50 log-slave-updates
    58 server-id = 2
/etc/init.d/mysqld restart

4.授权并主主同步

mysql -u root -p
mysql> grant replication slave on *.* to 'slave'@'192.168.1.%' identified by '123';
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      481 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql> change master to master_host='192.168.1.10',master_user='slave',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=647;
//所跟IP、log、pos等信息都为第一台主的信息
mysql> start slave;
mysql> show slave status\G;

再部署两对主从复制(m1与s1,m2与s2)

Slave01

1.准备工作

vim /etc/sysconfig/network-scripts/ifcfg-eth0
    DEVICE=eth0
    TYPE=Ethernet
    ONBOOT=yes
    NM_CONTROLLED=no
    BOOTPROTO=static
    IPADDR=192.168.1.30
    NETMASK=255.255.255.0
vim /etc/sysconfig/network-scripts/ifcfg-eth1
    DEVICE=eth1
    TYPE=Ethernet
    ONBOOT=yes
    NM_CONTROLLED=no
    BOOTPROTO=dhcp
vim /etc/hosts
    192.168.1.10    db1
    192.168.1.20    db2
    192.168.1.30    db3
    192.168.1.40    db4
vim /etc/sysconfig/network
    HOSTNAME=db3
reboot

2.YUM源配置并安装MMM

rm -rf /etc/yum.repos.d/*
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
yum -y install epel-release
yum -y install mysql-mmm* mysql mysql-server mysql-devel

3.MySQL配置

/etc/init.d/mysqld start && chkconfig --level 35 mysqld on
mysqladmin -uroot password "123"
cp /usr/share/doc/mysql-server-5.1.73/my-medium.cnf /etc/my.cnf
vim /etc/my.cnf
    [mysqld]
    50 relay-log=relay-log-bin
    51 relay-log-index=slave-relay-bin.index
    59 server-id = 3
/etc/init.d/mysqld restart

4.授权并主从同步(m1,s1)

mysql -u root -p
mysql> change master to master_host='192.168.1.10',master_user='slave',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=647;
//所跟IP、log、pos等信息都为第一台主的信息
mysql> start slave;
mysql> show slave status\G;

Slave02

1.准备工作

vim /etc/sysconfig/network-scripts/ifcfg-eth0
    DEVICE=eth0
    TYPE=Ethernet
    ONBOOT=yes
    NM_CONTROLLED=no
    BOOTPROTO=static
    IPADDR=192.168.1.40
    NETMASK=255.255.255.0
vim /etc/sysconfig/network-scripts/ifcfg-eth1
    DEVICE=eth1
    TYPE=Ethernet
    ONBOOT=yes
    NM_CONTROLLED=no
    BOOTPROTO=dhcp
vim /etc/hosts
    192.168.1.10    db1
    192.168.1.20    db2
    192.168.1.30    db3
    192.168.1.40    db4

2.YUM源配置并安装MMM

rm -rf /etc/yum.repos.d/*
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
yum -y install epel-release
yum -y install mysql-mmm* mysql mysql-server mysql-devel

3.MySQL配置

/etc/init.d/mysqld start && chkconfig --level 35 mysqld on
mysqladmin -uroot password "123"
cp /usr/share/doc/mysql-server-5.1.73/my-medium.cnf /etc/my.cnf
vim /etc/my.cnf
    [mysqld]
    50 relay-log=relay-log-bin
    51 relay-log-index=slave-relay-bin.index
    59 server-id = 4
/etc/init.d/mysqld restart

4.授权并主从同步(m2,s2)

mysql -u root -p
mysql> change master to master_host='192.168.1.20',master_user='slave',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=647;
//所跟IP、log、pos等信息都为第二台主的信息
mysql> start slave;
mysql> show slave status\G;

配置高可用MySQL-MMM

Master01

1.新建授权用户

mysql> grant super,replication client,process on *.* to 'mmm_agent'@'192.168.1.%' identified by '123';//主服务器授权,从服务器自动同步
mysql> grant replication client on *.* to 'mmm_monitor'@'192.168.1.%' identified by '123';           //主服务器授权,从服务器自动同步
mysql> grant all on *.* to 'test'@'192.168.1.%' identified by '123';                    //新建测试用户

2.配置MMM_COMMON

vim /etc/mysql-mmm/mmm_common.conf
    active_master_role      writer
    
            cluster_interface       eth0                    //集群IP承载的接口
            pid_path                /var/run/mysql-mmm/mmm_agentd.pid       //PID文件位置(存放MMM的进程号)
            bin_path                /usr/libexec/mysql-mmm/         //运行命令位置
            replication_user        slave                   //需使用主从同步时授权用户
            replication_password    123
            agent_user              mmm_agent                   //代理连接
            agent_password          123
    
    
            ip      192.168.1.10
            mode    master
            peer    db2                     //当db1主机不能使用,自动切换到db2
    
    
            ip      192.168.1.20
            mode    master                  //当前服务器作为主服务器(写)
            peer    db1                     //当db2主机不能使用,自动切换到db1
    
    
            ip      192.168.1.30
            mode    slave
    
    
            ip      192.168.1.40
            mode    slave                   //当前主机作为从服务器(读)
    
    
            hosts   db1, db2
            ips     192.168.1.250               //写服务器VIP
            mode    exclusive                   //只有一个host可以writer
    
    
            hosts   db3, db4
            ips     192.168.1.251, 192.168.1.252        //读服务器VIP
            mode    balanced                    //多个host可以reader
    

3.配置MMM_AGENT

vim /etc/mysql-mmm/mmm_agent.conf
include mmm_common.conf
this db1

4.拷贝文件给其余主机

scp /etc/mysql-mmm/mmm_common.conf [email protected]:/etc/mysql-mmm/
scp /etc/mysql-mmm/mmm_common.conf [email protected]:/etc/mysql-mmm/
scp /etc/mysql-mmm/mmm_common.conf [email protected]:/etc/mysql-mmm/
/etc/init.d/mysql-mmm-agent restart && chkconfig --level 35 mysql-mmm-agent on

Master02

配置MMM_AGENT

vim /etc/mysql-mmm/mmm_agent.conf
this db2
/etc/init.d/mysql-mmm-agent restart && chkconfig --level 35 mysql-mmm-agent on

Slave01

配置MMM_AGENT

vim /etc/mysql-mmm/mmm_agent.conf
this db3
/etc/init.d/mysql-mmm-agent restart && chkconfig --level 35 mysql-mmm-agent on

Slave02

配置MMM_AGENT

vim /etc/mysql-mmm/mmm_agent.conf
this db4
/etc/init.d/mysql-mmm-agent restart && chkconfig --level 35 mysql-mmm-agent on

监控配置(mmm_mon)

1.准备工作

vim /etc/sysconfig/network-scripts/ifcfg-eth0
    DEVICE=eth0
    TYPE=Ethernet
    ONBOOT=yes
    NM_CONTROLLED=no
    BOOTPROTO=static
    IPADDR=192.168.1.50
    NETMASK=255.255.255.0
vim /etc/sysconfig/network-scripts/ifcfg-eth1
    DEVICE=eth1
    TYPE=Ethernet
    ONBOOT=yes
    NM_CONTROLLED=no
    BOOTPROTO=dhcp
vim /etc/hosts
    192.168.1.10    db1
    192.168.1.20    db2
    192.168.1.30    db3
    192.168.1.40    db4
reboot

2.YUM源配置并安装MMM

rm -rf /etc/yum.repos.d/*
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
yum -y install epel-release
yum -y install mysql-mmm* mysql

3.配置MMM_COMMON

scp 192.168.1.10:/etc/mysql-mmm/mmm_common.conf /etc/mysql-mmm/

4.配置MMM_MON

vim /etc/mysql-mmm/mmm_mon.conf
    include mmm_common.conf
    
            ip                  127.0.0.1
            ping_ips            192.168.1.10,192.168.1.20,192.168.1.30,192.168.1.40
    
    
            monitor_user        mmm_monitor
            monitor_password    123
    
    debug 0

5.启动服务并验证

/etc/init.d/mysql-mmm-monitor restart && chkconfig --level 35 mysql-mmm-monitor on  //监控端启动
mmm_control show                                    //查看节点状态
mysql -u test -p -h 192.168.1.250

报错解决方案:

[root@localhost ~]# mysql -u test -p -h 192.168.1.250
Enter password:
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.254' (113)

主服务器:

grant super,replication client,process on *.* to 'mmm_agent'@'db1' identified by '123';
grant super,replication client,process on *.* to 'mmm_agent'@'db2' identified by '123';
grant super,replication client,process on *.* to 'mmm_agent'@'db3' identified by '123';
grant super,replication client,process on *.* to 'mmm_agent'@'db4' identified by '123';

日志查看:

tail -f /var/log/mysql-mmm/mmm_agentd.log                       //MySQL端的Agent日志
tail -f /var/log/mysql-mmm/mmm_mond.log                     //监控机端的Monitor日志

部署读写调度器Amoeba

1.环境准备

vim /etc/sysconfig/network-scripts/ifcfg-eth0
    DEVICE=eth0
    TYPE=Ethernet
    ONBOOT=yes
    NM_CONTROLLED=no
    BOOTPROTO=static
    IPADDR=192.168.1.254
    NETMASK=255.255.255.0

2.安装jdk与amoeba

yum -y erase java-*
chmod +x jdk-6u14-linux-x64.bin
./jdk-6u14-linux-x64.bin
mv jdk1.6.0_14/ /usr/local/jdk1.6
vim /etc/profile
export JAVA_HOME=/usr/local/jdk1.6
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/lib:$JAVA_HOME/jre/bin/:$PATH:$HOME/bin
export AMOEBA_HOME=/usr/local/amoeba
export PATH=$PATH:$AMOEBA_HOME/bin
source /etc/profile && java -version
mkdir /usr/local/amoeba
tar zxvf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/
chmod -R 755 /usr/local/amoeba/

3.在主Mysql数据库新建授权用户

mysql -u root -p
    mysql> grant all on *.* to haha@'192.168.1.%' identified by '123';

4.编辑amoeba配置文件

vim /usr/local/amoeba/conf/amoeba.xml
 30                hehe            //设置连接Amoeba用户
 31
 32                123         //设置连接Amoeba用户
115                 slaves
116
117                 master        注意删除的注释
118                 slaves         //定义读服务器池
vim /usr/local/amoeba/conf/dbServers.xml
 25                         
 26                         haha               //设置连接Mysql的用户
 27
 28                         123                //设置连接mysql的密码
注意删除的注释
 43         
 44                 
 45                         
 46                         192.168.1.254         //定义写服务器IP
 47                 
 48         
 49         
 50                 
 51                         
 52                         192.168.1.30          //定义读服务器IP
 53                 
 54         
 55         
 56                 
 57                         
 58                         192.168.1.40          //定义读服务器IP
 59                 
 60
 61         
 62         
 68                         slave1,slave2     //定义输入slaves读服务器池的主机
 69                 
amoeba start &
netstat -utpln | grep 8066

5.连接测试

client:mysql -u hehe -p -h 192.168.1.254 -P 8066

转载于:https://blog.51cto.com/13770206/2160237

你可能感兴趣的:(MySQL+Amoeba+MySQLMMM高可用群集)