- 如图所示。其中
192.168.4.10、192.168.4.11
作为MySQL双主服务器,192.168.4.12、192.168.4.13
作为主服务器的从服务器。
- 一 、安装MySQL数据库
- 1.1步骤一:部署数据库主机|
- 1.2 卸载系统自带的mariadb-server、mariadb软件包(如果有的话),清理/etc/my.cnf配置文件,此配置文件由RHEL自带的mariadb-libs库提供
root@monitor ~]# yum -y remove mariadb-server mariadb
[root@monitor ~]# rpm -qf /etc/my.cnf
[root@monitor ~]# mv /etc/my.cnf /etc/my.cnf.old
- 1.3 在bundle的整合包中,并不是所有的rpm包都会用到,将一些重复的删除。
安装mysql时可能会缺少某些依赖包,需提前单独安装
[root@monitor ~]# cd pan/`
[root@monitor pan]# tar xvf mysql-5.7.17.tar
[root@monitor pan]# yum -y install perl-Data-Dumper perl-JSON perl-Time-HiRes #安装依赖包
[root@monitor pan]# rpm -Uvh mysql-community-*.rpm #升级安装
[root@monitor pan]# systemctl restart mysqld
[root@monitor pan]# systemctl enable mysqld
[root@monitor pan]# netstat -antpu | grep mysql #查看端口
- 1.4 连接MySQL服务器、修改密码永久配置文件
[root@monitor pan]# vim /etc/my.cnf
[root@monitor pan]# systemctl restart mysqld
[root@monitor pan]# grep "y password" /var/log/mysqld.log
[root@monitor pan]# mysqladmin -u root -p'ny26skpNZ#j ' password '123456'
- 1.5 数据库授权部分为了方便试验我们直接允许所有地址访问了,真实环境需谨慎
mysql> grant replication slave on *.* to slaveuser@"%" identified by "123456"; #主从同步授权
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication client on *.* to monitor@"%" identified by "123456"; #MMM所需架构用户授权
Query OK, 0 rows affected (0.06 sec)
mysql> grant replication client,process,super on *.* to agent@"%" identified by "123456"; #MMM所需架构用户授权
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on *.* to root@"%" identified by "123456"; #测试用户授权
Query OK, 0 rows affected (0.00 sec)
- 二、部署双主多从结构
- 2.1 开启主数据库binlog日志、设置server_id
- 2.2 master1主机操作
[root@master1 ~]# vim /etc/my.cnf
[root@master1 ~]# systenctl restart mysqld`
- 2.3 master2主机操作
[root@master2 ~]# vim /etc/my.cnf
[root@master2 ~]# systenctl restart mysqld
** - 2.4 slave1设置:**
[root@slave1 ~]# vim /etc/my.cnf`
[root@slave1~]# systenctl restart mysqld`
- 2.5 slave2设置:
[root@slave2 ~]# vim /etc/my.cnf
[root@slave2~]# systenctl restart mysqld
- 2.6 配置主从从从关系,配置master2、slave1、slave2成为master1的从服务器,
- 查看`master1服务器binlong日志使用节点信息:
[root@master1 ~]# mysql -uroot -p123456
- 2.7 设置master2为master1从:
[root@master2~]# mysql -uroot -p123456
- 2.8 设置slave1为master1从:
[root@slave1**~]# mysql -uroot -p123456
- 2.9 设置slave2为master1从:
[root@slave2~]# mysql -uroot -p123456
- 2.10 配置主主从从关系,将master1配置为master2的从,查看master2的binlog使用信息:
[root@master2**~]# mysql -uroot -p123456
-2.11 设置master1成为master2的从:
[root@master1**~]# mysql -uroot -p123456`
- 三 、MySQL-MMM架构部署
如图所示。其中192.168.4.10、192.168.4.11作为MySQL双主服务器,192.168.4.12、192.168.4.13作为主服务器的从服务器,192.168.4.100作为MySQL-MMM架构中管理监控服务器,实现监控MySQL主从服务器的工作状态及决定故障节点的移除或恢复工作,架构搭建完成后使用客户机192.168.4.120进行访问,客户机需要安装MySQL-client软件包。
- 3.1 步骤一:安装`MySQL-MMM
- monitor监视主机需要提前安装
[root@monitor ~]yum -y install mariadb-server
- 3.11安装MySQL-MMM依赖包关系(MySQL集群内5台服务器
master1,master2,slave1,slave2,monitor
)均需安装
[root@master1 ~]# yum -y install gcc* perl*
- 3.12安装安装Log-Log4perl 类
[root@master1 mysql-mmm]# cd /root/pan/ #切换到文件目录
[root@master1 mysql-mmm]# unzip mysql-mmm.zip #解压ZIP包
[root@master1 mysql-mmm]# cd mysql-mmm
[root@master1 mysql-mmm]# rpm -ivh perl-Log-Log4perl-1.26-1.el6.rf.noarch.rpm
- 3.13安装Proc-Daemon类
[root@master1 mysql-mmm]# tar -zxvf Algorithm-Diff-1.1902.tar.gz
[root@master1 mysql-mmm]# cd Algorithm-Diff-1.1902/
[root@master1 Algorithm-Diff-1.1902]# perl Makefile.PL && make && make install
- 3.14安装Proc-Daemon类
[root@master1 Algorithm-Diff-1.1902]# cd /root/pan/mysql-mmm/
[root@master1 mysql-mmm]# tar -zxvf Proc-Daemon-0.03.tar.gz
[root@master1 mysql-mmm]# cd Proc-Daemon-0.03/
[root@master1 Proc-Daemon-0.03]# perl Makefile.PL && make && make install
- 3.15 安装Net-ARP虚拟IP分配工具:
[root@master1 Proc-Daemon-0.03]# cd /root/pan/mysql-mmm/
[root@master1 mysql-mmm]#gunzip Net-ARP-1.0.8.tgz
[root@master1 mysql-mmm]# tar xvf Net-ARP-1.0.8.tar
[root@master1 mysql-mmm]# cd Net-ARP-1.0.8/
[root@master1 Net-ARP-1.0.8]# perl Makefile.PL && make && make install
- 3.16 安装Mysql-MMM软件包:
[root@master1 Net-ARP-1.0.8]# cd /root/pan/mysql-mmm/
[root@master1 mysql-mmm] # tar xvf mysql-mmm-2.2.1.tar.gz
[root@master1 mysql-mmm] cd mysql-mmm-2.2.1/
[root@master1 mysql-mmm-2.2.1]# make && make install
- 3.2 步骤二:修改配置文件
- 3.21 修改公共配置文件
- 本案例中MySQL集群的5台服务器(master1、master2、slave1、slave2、monitor)都需要配置,可以先配好一台后使用scp复制。
[root@master1 ~]# vim /etc/mysql-mmm/mmm_common.conf
active_master_role writer
<host default>
cluster_interface eth0 #设置主从同步的用户
pid_path /var/run/mmm_agentd.pid
bin_path /usr/lib/mysql-mmm/
replication_user slaveuser #设置主从同步的用户
replication_password 123456 #设置主从同步用户密码
agent_user agent #mmm-agent控制数据库用户
agent_password 123456 #mmm-agent控制数据库用户密码
</host>
<host master1> #设置第一个主服务器
ip 192.168.4.10 #master1 IP 地址
mode master
peer 123456 #指定另外一台主服务器
</host>
<host master2> #指定另外一台主服务器
ip 192.168.4.11
mode master
peer 123456
</host>
<host slave1> #设置第一台从服务器
ip 192.168.4.12 slave1 IP 地址
mode slave #本段落配置的是slave服务器
</host>
<host slave2>
ip 192.168.4.13
mode slave
</host>
<role writer> #设置写入服务器工作模式
hosts master1,master2 #提供写的主服务器
ips 192.168.4.200 #设置VIP地址
mode exclusive #排他模式
</role>
<role reader> #设置读取服务器工作模式
hosts slave1,slave2 #提供读的服务器信息
ips 192.168.4.201,192.168.4.202 #多个虚拟IP
mode balanced #均衡模式
</role>
[root@master1 ~]#
- 3.22 修改管理主机配置文件(monitor主机配置)
[root@monitor ~]# vim /etc/mysql-mmm/mmm_mon.conf
include mmm_common.conf
<monitor>
ip 192.168.4.100 #设置管理主机IP地址
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.4.10,192.168.4.11,192.168.4.12,192.168.4.13
#设置被监控数据库
</monitor>
<host default>
monitor_user monitor #监控数据库MySQL用户
monitor_password 123456 #监控数据库MySQL用户密码
</host>
debug 0
[root@monitor ~]#
-
3.23 修改客户端配置文件
-
3.231master1配置
[root@master1 ~]# vim /etc/mysql-mmm/mmm_agent.conf
include mmm_common.conf
this master1
- 3.232 master2配置
[root@master2 ~]# vim /etc/mysql-mmm/mmm_agent.conf
include mmm_common.conf
this master2
- 3.233 slave1配置
[root@slave1 ~]# vim /etc/mysql-mmm/mmm_agent.conf
include mmm_common.conf
this slave1
- 3.234 slave2配置
[root@slave2 ~]# vim /etc/mysql-mmm/mmm_agent.conf
include mmm_common.conf
this slave2
-
3.3 启动MMM集群架构
-
master1操作:
[root@master1 ~]# /etc/init.d/mysql-mmm-agent start
Daemon bin: '/usr/sbin/mmm_agentd'
Daemon pid: '/var/run/mmm_agentd.pid'
Starting MMM Agent daemon... Ok
- master2操作:
[root@master2 ~]# /etc/init.d/mysql-mmm-agent start
Daemon bin: '/usr/sbin/mmm_agentd'
Daemon pid: '/var/run/mmm_agentd.pid'
Starting MMM Agent daemon... Ok
- slave1操作:
[root@master2 ~]# /etc/init.d/mysql-mmm-agent start
Daemon bin: '/usr/sbin/mmm_agentd'
Daemon pid: '/var/run/mmm_agentd.pid'
Starting MMM Agent daemon... Ok
- slave2操作:
[root@slave2 ~]# /etc/init.d/mysql-mmm-agent start
Daemon bin: '/usr/sbin/mmm_agentd'
Daemon pid: '/var/run/mmm_agentd.pid'
Starting MMM Agent daemon... Ok
- monitor主机操作:
[root@monitor ~]# /etc/init.d/mysql-mmm-monitor start
Daemon bin: '/usr/sbin/mmm_mond'
Daemon pid: '/var/run/mmm_mond.pid'
Starting MMM Monitor daemon: Ok
-
3.4设置集群中服务器为online状态
-
控制命令只能在管理端monitor服务器上执行。
-
3.41查看当前集群中各服务器状态:
[root@monitor ~]# mmm_control show
master1(192.168.4.10) master/AWAITING_RECOVERY. Roles:
master2(192.168.4.11) master/AWAITING_RECOVERY. Roles:
slave1(192.168.4.12) slave/AWAITING_RECOVERY. Roles:
slave2(192.168.4.13) slave/AWAITING_RECOVERY. Roles:
- 3.42设置4台数据库主机状态为online:
[root@monitor ~]# mmm_control set_online master1
OK: State of 'master1' changed to ONLINE. Now you can wait some time and check its new roles!
[root@monitor ~]# mmm_control set_online master2
OK: State of 'master2' changed to ONLINE. Now you can wait some time and check its new roles!
[root@monitor ~]# mmm_control set_online slave1
OK: State of 'slave1' changed to ONLINE. Now you can wait some time and check its new roles!
[root@monitor ~]# mmm_control set_online slave2
OK: State of 'slave2' changed to ONLINE. Now you can wait some time and check its new roles!
[root@monitor ~]#
- 3.43再次查看当前集群中各服务器状态:
[root@monitor ~]# mmm_control show
master1(192.168.4.10) master/ONLINE. Roles: writer(192.168.4.200)
master2(192.168.4.11) master/ONLINE. Roles:
slave1(192.168.4.12) slave/ONLINE. Roles: reader(192.168.4.201)
slave2(192.168.4.13) slave/ONLINE. Roles: reader(192.168.4.202)
[root@monitor ~]#
-
3.5测试MySQL-MMM架构
-
3.51客户机安装MySQL-client软件包
[root@client ~]# tar xvf MySQL-5.6.15-1.el6.x86_64.rpm-bundle.tar
.. ..
[root@client ~]# rpm -ivh MySQL-client-5.6.15-1.el6.x86_64.rpm
.. ..
- 3.52MySQL-MMM虚拟IP访问测试
[root@client ~]# mysql -h192.168.4.200 -uroot -ppwd123 -e "show databases"
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| tarena |
| test |
+--------------------+
[root@client ~]#
[root@client ~]# mysql -h192.168.4.201 -uroot -ppwd123 -e "show databases"
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| tarena |
| test |
+--------------------+
[root@client ~]#
[root@client ~]# mysql -h192.168.4.202 -uroot -ppwd123 -e "show databases"
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| tarena |
| test |
+--------------------+
[root@client ~]#
- 3.53主数据库宕机测试
[root@master1 ~]# service mysql stop #停止master1上服务
Shutting down MySQL.... [确定]
[root@master1 ~]#
[root@monitor ~]# mmm_control show #查看集群内服务器状态
- 3.54通过输出信息可以看到虚拟IP从master1切换到master2:
master1(192.168.4.10) master/HARD_OFFLINE. Roles:
master2(192.168.4.11) master/ONLINE. Roles: writer(192.168.4.200)
slave1(192.168.4.12) slave/ONLINE. Roles: reader(192.168.4.201)
slave2(192.168.4.13) slave/ONLINE. Roles: reader(192.168.4.202)
[root@monitor ~]#
[root@client ~]# mysql -h192.168.4.200 -uroot -ppwd123 -e "show databases" #访问虚拟IP测试
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| tarena |
| test |
+--------------------+
[root@client ~]#
来源:CSDN
作者:Pan Fei Long
链接:https://blog.csdn.net/qq_37684859/article/details/93906315