案例搭建 Mysql-mmm+mysql5.6双主高可用集群

 

案例环境:

系统

IP地址

主机名

所需软件

Centos 7.4 1708 64bit

192.168.100.101

master1

mysql-5.6.36.tar.gz

mysql-mmm

mysql-mmm-agent

mysql-tools

Centos 7.4 1708 64bit

192.168.100.102

master2

mysql-5.6.36.tar.gz

mysql-mmm

mysql-mmm-agent

mysql-tools

Centos 7.4 1708 64bit

192.168.100.103

slave1

mysql-5.6.36.tar.gz

mysql-mmm

mysql-mmm-agent

mysql-tools

Centos 7.4 1708 64bit

192.168.100.104

slave2

mysql-5.6.36.tar.gz

mysql-mmm

mysql-mmm-agent

mysql-tools

Centos 7.4 1708 64bit

192.168.100.105

monitor

mysql-5.6.36.tar.gz

mysql-mmm

mysql-mmm-monitor

mysql-tools

Centos 7.4 1708 64bit

192.168.100.106

client

mysql

 

 

案例步骤:

  • 部署master1节点的ntp服务以及域名解析;
  • 配置master2、slave1、slave2、monitor节点同步ntp时间及域名解析(在此只列举master2单台主机配置);
  • 分别在master1、master2、slave1、slave2节点上安装mysql服务(在此只列举master1单台主机配置);
  • 配置master1实现双主复制;
  • 配置master2实现双主复制;
  • 配置slave1和slave2实现主从复制(在此只列举slave1单台主机配置);
  • 分别在master1、master2、slave1、slave2四个节点安装mysql-mmm软件程序并且配置agent指定本机的节点名称(在此只列举master1单台主机配置);
  • 在master1节点上授权monitor节点连接数据库集群;
  • 安装monitor节点上的mysql-mmm软件程序;
  • 配置 monitor节点上的mysql-mmm的配置文件并且复制到各个mysql节点;
  • 启动master1、master2、slave1、slave2四个节点上的mysql-mmm服务(在此只列举master1单台主机配置);
  • 启动monitor节点上的mysql-mmm服务并查看集群状态;
  • 客户端验证测试读写数据;
  • 将master1主节点的mysql服务停止,测试集群状况;

 

 

 

  • 部署master1节点的ntp服务以及域名解析;

[root@master1 ~]# cat <>/etc/hosts

192.168.100.101 master1

192.168.100.102 master2

192.168.100.103 slave1

192.168.100.104 slave2

192.168.100.105 monitor

END

[root@master1 ~]# yum -y install ntp

[root@master1 ~]# sed -i '/^server/s/^/#/g' /etc/ntp.conf

[root@master1 ~]# cat <>/etc/ntp.conf

server 127.127.1.0

fudge 127.127.1.0 stratum 8

END

[root@master1 ~]# systemctl start ntpd

[root@master1 ~]# systemctl enable ntpd

Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

 

  • 配置master2、slave1、slave2、monitor节点同步ntp时间及域名解析(在此只列举master2单台主机配置);

[root@master2 ~]# cat <>/etc/hosts

192.168.100.101 master1

192.168.100.102 master2

192.168.100.103 slave1

192.168.100.104 slave2

192.168.100.105 monitor

END

[root@master2 ~]# yum -y install ntpdate

[root@master2 ~]# /usr/sbin/ntpdate 192.168.100.101

ech 9 Aug 18:04:38 ntpdate[1106]: adjust time server 192.168.100.101 offset 0.299673 sec

[root@master2 ~]# echo "/usr/sbin/ntpdate 192.168.100.101">>/etc/rc.local

[root@master2 ~]# chmod +x /etc/rc.local

 

  • 分别在master1、master2、slave1、slave2节点上安装mysql服务(在此只列举master1单台主机配置);

[root@master1 ~]# yum -y install ncurses cmake

[root@master1 ~]# mount /dev/cdrom /mnt/

mount: /dev/sr0 写保护,将以只读方式挂载

[root@master1 ~]# rpm -ivh /mnt/Packages/ncurses-devel-5.9-13.20130511.el7.x86_64.rpm --nodeps

[root@master1 ~]# ls

mysql-5.6.36.tar.gz

[root@master1 ~]# tar zxvf mysql-5.6.36.tar.gz -C /usr/src/

[root@master1 ~]# cd /usr/src/mysql-5.6.36/

[root@master2 mysql-5.6.36]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DENABLE_DOWNLOADS=1

[root@master1 mysql-5.6.36]# make

[root@master1 mysql-5.6.36]# make install

[root@master1 mysql-5.6.36]# cd

[root@master1 ~]# cp /usr/src/mysql-5.6.36/support-files/mysql.server /etc/init.d/

[root@master1 ~]# chmod +x /etc/init.d/mysql.server

[root@master1 ~]# cat <>/usr/lib/systemd/system/mysqld.service

[Unit]

Description=mysqldapi

After=network.target

 

[Service]

Type=forking

PIDFile=/usr/local/mysql/logs/mysqld.pid

ExecStart=/etc/init.d/mysql.server start

ExecReload=/etc/init.d/mysql.server restart

ExecStop=/etc/init.d/mysql.server stop

PrivateTmp=Flase

 

[Install]

WantedBy=multi-user.target

END

[root@master1 ~]# echo "export PATH=$PATH:/usr/local/mysql/bin/" >>/etc/profile

[root@master1 ~]# source /etc/profile

[root@master1 ~]# groupadd mysql

[root@master1 ~]# useradd -g mysql mysql

[root@master1 ~]# cat </etc/my.cnf

[mysqld]

basedir = /usr/local/mysql

datadir = /usr/local/mysql/data

port = 3306

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

character_set_server=utf8

init_connect='SET NAMES utf8'

log-error=/usr/local/mysql/logs/mysqld.log

pid-file=/usr/local/mysql/logs/mysqld.pid

skip-name-resolve

END

[root@master1 ~]# mkdir /usr/local/mysql/logs

[root@master1 ~]# chown mysql:mysql /usr/local/mysql/ -R

[root@master1 ~]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data ##初始化mysql

[root@master1 ~]# systemctl start mysqld

[root@master1 ~]# systemctl enable mysqld

Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.

[root@master1 ~]# netstat -utpln |grep 3306

tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      31481/mysqld

[root@master1 ~]# mysqladmin -uroot password 123123

Warning: Using a password on the command line interface can be insecure.

[root@master1 ~]# mysql -uroot -p123123

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| test               |

+--------------------+

mysql> exit

 

  • 配置master1实现双主复制;

[root@master1 ~]# cat <>/etc/my.cnf

server-id=1

log-bin=mysql-bin

log-slave-updates

sync_binlog=1

auto_increment_increment=2

auto_increment_offset=1

relay-log=relay1-log-bin

relay-log-index=slave-relay1-bin.index

END

注解:

sync_binlog=1 ##主机每次提交事务的时候把二进制日志的内容同步到磁盘上,所以即使服务器崩溃,也会把时间写入到日志中;

auto_increment_increment=2 ##以下两参数用于主主复制中,用于错开增值,防止键值冲突

[root@master1 ~]# systemctl restart mysqld

[root@master1 ~]# mysql -uroot -p123123

mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000001 |      120 |              |                  |                   |

+------------------+----------+--------------+------------------+-------------------+

mysql> grant replication slave on *.* to 'master'@'192.168.100.%' identified by '123123';

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

mysql> exit

[root@master1 ~]# mysql -uroot -p123123

mysql> change master to master_host='192.168.100.102',master_user='master',master_password='123123',master_log_file='mysql-bin.000001',master_log_pos=120;

Query OK, 0 rows affected, 2 warnings (0.01 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.102

                  Master_User: master

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000001

          Read_Master_Log_Pos: 421

               Relay_Log_File: relay1-log-bin.000002

                Relay_Log_Pos: 584

        Relay_Master_Log_File: mysql-bin.000001

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

 

  • 配置master2实现双主复制;

[root@master2 ~]# cat <>/etc/my.cnf

server-id=2

log-bin=mysql-bin

log-slave-updates

sync_binlog=1

auto_increment_increment=2

auto_increment_offset=1

relay-log=relay2-log-bin

relay-log-index=slave-relay2-bin.index

END

[root@master2 ~]# systemctl restart mysqld

[root@master2 ~]# mysql -uroot -p123123

mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000001 |      120 |              |                  |                   |

+------------------+----------+--------------+------------------+-------------------+

mysql> grant replication slave on *.* to 'master'@'192.168.100.%' identified by '123123';

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

mysql> exit

[root@master2 ~]# mysql -uroot -p123123

mysql> change master to master_host='192.168.100.101',master_user='master',master_password='123123',master_log_file='mysql-bin.000001',master_log_pos=120;

Query OK, 0 rows affected, 2 warnings (0.01 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.102

                  Master_User: master

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000001

          Read_Master_Log_Pos: 421

               Relay_Log_File: relay1-log-bin.000002

                Relay_Log_Pos: 584

        Relay_Master_Log_File: mysql-bin.000001

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

 

  • 配置slave1和slave2实现主从复制(在此只列举slave1单台主机配置);

[root@slave1 ~]# cat <>/etc/my.cnf

server-id=3

relay-log=relay3-log-bin

relay-log-index=slave-relay3-bin.index

END

[root@slave1 ~]# systemctl restart mysqld

[root@slave1 ~]# mysql -uroot -p123123

mysql> change master to master_host='192.168.100.101',master_user='master',master_password='123123',master_log_file='mysql-bin.000001',master_log_pos=120;

Query OK, 0 rows affected, 2 warnings (0.02 sec)

mysql> start slave;

Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G;

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.100.101

                  Master_User: master

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000002

          Read_Master_Log_Pos: 421

               Relay_Log_File: relay3-log-bin.000003

                Relay_Log_Pos: 584

        Relay_Master_Log_File: mysql-bin.000002

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

 

[root@master1 ~]# mysql -uroot -p123123

mysql> create database linuxfan;

Query OK, 1 row affected (0.00 sec)

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| linuxfan           |

| mysql              |

| performance_schema |

| test               |

+--------------------+

mysql> exit

 

[root@master2 ~]# mysql -uroot -p123123

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| linuxfan           |

| mysql              |

| performance_schema |

| test               |

+--------------------+

mysql> exit

 

[root@slave1 ~]# mysql -uroot -p123123

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| linuxfan           |

| mysql              |

| performance_schema |

| test               |

+--------------------+

mysql> exit

 

  • 分别在master1、master2、slave1、slave2四个节点安装mysql-mmm软件程序并且配置agent指定本机的节点名称(在此只列举master1单台主机配置);

[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

[root@master1 ~]# yum -y install mysql-mmm mysql-mmm-agent mysql-mmm-tools

[root@master1 ~]# vi /etc/mysql-mmm/mmm_agent.conf

include mmm_common.conf

 

# The 'this' variable refers to this server.  Proper operation requires

# that 'this' server (db1 by default), as well as all other servers, have the

# proper IP addresses set in mmm_common.conf.

this db1

:wq

 

  • 在master1节点上授权monitor节点连接数据库集群;

[root@master1 ~]# mysql -uroot -p123123

mysql> grant replication client on *.* to 'mmm_monitor'@'192.168.100.%' identified by 'monitor';

Query OK, 0 rows affected (0.00 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)

mysql> exit

权限注解:

replication client:权限用于执行show master status等命令。这些命令是用来查看复制状态的;

replication slave:是用于连接主库从库进行读取二进制文件进而实现复制的;

super:杀死mysql中连接的进程,设置全局变量,重置主从配置的权限;
process:具有查看当前运行的sql的权限 ,以及explain执行计划;

 

  • 安装monitor节点上的mysql-mmm软件程序;

[root@monitor ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

[root@monitor ~]# yum -y install epel-release

[root@monitor ~]# yum -y install mysql-mmm mysql-mmm-tools mysql-mmm-monitor

 

  • 配置 monitor节点上的mysql-mmm的配置文件并且复制到各个mysql节点;

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

active_master_role      writer ##指定活跃角色为写角色

    cluster_interface       eth0 ##承载的网卡

    pid_path                /var/run/mysql-mmm/mmm_agentd.pid

    bin_path                /usr/libexec/mysql-mmm/ ##可执行文件路径

    replication_user        master ##mysql集群主从复制账户

    replication_password    123123

    agent_user              mmm_agent ##monitor连接集群的账户

    agent_password          agent

    ip      192.168.100.101

    mode    master

    peer    db2 ##指定对立的节点名

    ip      192.168.100.102

    mode    master

    peer    db1

    ip      192.168.100.103

    mode    slave

    ip      192.168.100.104

    mode    slave

    hosts   db1, db2

    ips     192.168.100.250

    mode    exclusive ##同一时间存在单个主

    hosts   db3, db4

    ips     192.168.100.251,192.168.100.252

    mode    balanced ##轮询

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

include mmm_common.conf

    ip                  127.0.0.1

    pid_path            /var/run/mysql-mmm/mmm_mond.pid

    bin_path            /usr/libexec/mysql-mmm    

    status_path         /var/lib/mysql-mmm/mmm_mond.status    

ping_ips            192.168.100.101, 192.168.100.102, 192.168.100.103, 192.168.100.104     ##指定监听的所有节点ip

auto_set_online     60 ##判定其online的时间,超过60s认为其down

   

    monitor_user        mmm_monitor ##monitor的工作用户

    monitor_password    monitor

debug 0

[root@monitor ~]# for i in 101 102 103 104;do scp /etc/mysql-mmm/mmm_common.conf [email protected].$i:/etc/mysql-mmm/; done ##将配置文件复制到mysql节点

 

  • 启动master1、master2、slave1、slave2四个节点上的mysql-mmm服务(在此只列举master1单台主机配置);

[root@master1 ~]# mkdir /var/run/mysql-mmm

[root@master1 ~]# vi /usr/lib/systemd/system/mysql-mmm-agent.service

   7 PIDFile=/var/run/mysql-mmm/mmm_agentd.pid

[root@master1 ~]# systemctl daemon-reload

[root@master1 ~]# systemctl start mysql-mmm-agent

[root@master1 ~]# netstat -utpln |grep mmm

tcp        0      0 192.168.100.101:9989    0.0.0.0:*               LISTEN      32670/mmm_agentd

 

  • 启动monitor节点上的mysql-mmm服务并查看集群状态;

[root@monitor ~]# mkdir /var/run/mysql-mmm

[root@monitor ~]# vi /usr/lib/systemd/system/mysql-mmm-monitor.service

   7 PIDFile=/var/run/mysql-mmm/mmm_mond.pid

[root@monitor ~]# systemctl daemon-reload

[root@monitor ~]# systemctl start mysql-mmm-monitor

[root@monitor ~]# netstat -utpln |grep mmm

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

[root@monitor ~]# mmm_control show

# Warning: agent on host db4 is not reachable

  db1(192.168.100.101) master/ONLINE. Roles: writer(192.168.100.250)

  db2(192.168.100.102) master/ONLINE. Roles:

  db3(192.168.100.103) slave/ONLINE. Roles: reader(192.168.100.251) reader(192.168.100.252)

  db4(192.168.100.104) slave/ONLINE. Roles:

 

  • 客户端验证测试读写数据;

[root@master1 ~]# mysql -uroot -p123123

mysql> grant all on *.* to 'client'@'192.168.100.%' identified by '123123';

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

mysql> exit

 

[root@client ~]# yum -y install mysql

[root@client ~]# mysql -uclient -p123123 -h192.168.100.250

MySQL [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| linuxfan           |

| mysql              |

| performance_schema |

| test               |

+--------------------+

5 rows in set (0.00 sec)

MySQL [(none)]> exit

 

[root@client ~]# mysql -uclient -p123123 -h192.168.100.251

MySQL [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| linuxfan           |

| mysql              |

| performance_schema |

| test               |

+--------------------+

MySQL [(none)]> exit

 

  • 将master1主节点的mysql服务停止,测试集群状况;

[root@master1 ~]# systemctl stop mysqld

 

[root@monitor ~]# mmm_control show

# Warning: agent on host db4 is not reachable

  db1(192.168.100.101) master/HARD_OFFLINE. Roles:

  db2(192.168.100.102) master/ONLINE. Roles: writer(192.168.100.250)

  db3(192.168.100.103) slave/ONLINE. Roles: reader(192.168.100.251)

  db4(192.168.100.104) slave/ONLINE. Roles:

注解:

ONLINE. Roles表示在线节点;

HARD_OFFLINE表示ping不通并且(或者)mysql连接中断,会导致hard_offline状态;

admin_offline是手动下线的状态;

 

[root@monitor ~]# tail /var/log/mysql-mmm/mmm_mond.log

2018/08/10 07:44:37 FATAL State of host 'db1' changed from AWAITING_RECOVERY to ONLINE because of auto_set_online(60 seconds). It was in state AWAITING_RECOVERY for 61 seconds

2018/08/10 07:44:37 FATAL Agent on host 'db1' is reachable again

2018/08/10 07:50:28 FATAL Agent on host 'db1' is reachable again

2018/08/10 08:04:00 FATAL State of host 'db1' changed from ONLINE to HARD_OFFLINE (ping: OK, mysql: not OK)

 

[root@client ~]# mysql -uroot -p123123 192.168.100.250

MySQL [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| linuxfan           |

| mysql              |

| performance_schema |

| test               |

+--------------------+

MySQL [(none)]> exit

你可能感兴趣的:(mysql)