赞
踩
环境准备:需要四台服务器 centos7.9 数据库版本5.7.20
master01 | 192.168.52.5 | 主节点 |
master02 | 192.168.52.6 | 备主节点 |
slave01 | 192.168.52.7 | 从节点 |
manager | 192.168.52.10 | 管理节点 |
注意:所有服务器都需要安装mysql服务,manager管理节点不用启动服务。
2.先在master01上装mysql数据库,配置ip地址,设置主机名 安装epel源,安装Mha依赖 配置hosts文件。克隆其他三台
#我这里数据库是二进制方式安装!
#配置ip地址,设置主机名
- vim /etc/sysconfig/network-scripts/ifcfg-ens33
- TYPE=Ethernet
- BOOTPROTO=static
- IPADDR=192.168.52.6
- GATEWAY=192.168.52.2
- DNS1=114.114.114.114
- hostnamectl set-hostname master01-5
#关闭防火墙和selinux
- systemctl stop firewalld
- systemctl disabled firewalld
- vim /etc/selinux/config
- SELINUX=disabled
#安装epel源
- wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/
- yum makecache
#安装Mha依赖
yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Config-IniFiles ncftp perl-Params-Validate perl-CPAN perl-Test-Mock-LWP.noarch perl-LWP-Authen-Negotiate.noarch perl-devel perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker
#配置host文件解析
- cat /etc/hosts
- 192.168.52.5 master01-5
- 192.168.52.6 master02-6
- 192.168.52.7 slave01-7
- 192.168.52.10 manager01-10
注意:设置主机名一定要和hosts文件中的主机名一致!
#停止mysql服务,删除auto.cnf防止uuid冲突
- /etc/init.d/mysql.server stop
- rm -f /usr/local/mysql/data/auto.cnf
#关机克隆其他三台
systemctl poweroff
3.配置ssh免密登录,安装插件,配置半同步 所有的主机都要配置(master01,master02,slave01,manager不用安装插件)
#配置ssh免密登录
- ssh-keygen -t rsa
- for i in {master01-5,master02-6,slave01-7,manager01-10};do ssh-copy-id $i;done
#查看插件的位置,我这里在/usr/local/mysql/lib/plugin/
show variables like '%plugin%';
#查看是否支持动态载入
show variables like '%dynam%';
#安装插件
INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
#配置半同步
#查看半同步配置默认是关闭
show variables like '%semi%';
#在配置文件里,配置半同步
master01配置:
- vim /etc/my.cnf
- [mysqld]
- basedir=/usr/local/mysql
- datadir=/usr/local/mysql/data
- pid-file=/usr/local/mysql/data/mysqld.pid
- log-error=/usr/local/mysql/data/mysql.err
- socket=/tmp/mysql.sock
- log_bin=mysql-bin #开启二进制日志
- server_id=1 #设置server_id,不能重复
- relay_log=relay-bin #开启中继日志
- relay_log_index=relay_slave_bin.index #中继日志索引文件位置
- relay_log_purge=0 #关闭清除中继日志
- rpl_semi_sync_master_enabled=1 #开启半同步
- rpl_semi_sync_slave_enabled=1
- rpl_semi_sync_master_timeout=1000 #设置超时时间
- binlog_format=mixed #二进制类型设置为混合类型
master02配置:
vim /etc/my.cnf [mysqld] basedir=/usr/local/mysql datadir=/usr/local/mysql/data pid-file=/usr/local/mysql/data/mysqld.pid log-error=/usr/local/mysql/data/mysql.err socket=/tmp/mysql.sock log_bin=mysql-bin server_id=2 relay_log=relay-bin relay_log_index=relay_slave_bin.index relay_log_purge=0 rpl_semi_sync_master_enabled=1 rpl_semi_sync_slave_enabled=1 rpl_semi_sync_master_timeout=1000 binlog_format=mixed
slave01配置:
- vim /etc/my.cnf
- [mysqld]
- basedir=/usr/local/mysql
- datadir=/usr/local/mysql/data
- pid-file=/usr/local/mysql/data/mysqld.pid
- log-error=/usr/local/mysql/data/mysql.err
- socket=/tmp/mysql.sock
- log_bin=mysql-bin
- server_id=3
- relay_log=relay-bin
- relay_log_index=relay_slave_bin.index
- rpl_semi_sync_slave_enabled=1
- binlog_format=mixed
- read_only=1
配置完成重启服务:
/etc/init.d/mysql restart
#查看半同步状态
show status like ‘%rpl_semi_sync%’;
4.创建用户,做主从复制,安装部署Mha
master01,master02(创建rep和manager用户)slave只需要创建manager用户
#创建用户
- grant replication client ,replication slave on *.* to rep@'192.168.52.%' identified by
- '123';
- grant all on *.* to manager@'192.168.52.%' identified by '123';
#配置主从复制 master02和slave01相同操作
#查看二进制日志状态
show master status;
#master02从master01。
- change master to
- master_host='192.168.52.5',
- master_user='rep',
- master_password='123',
- master_log_file='mysql-bin.000001',
- master_log_pos=761;
#启动查看状态
start slave;
show slave status\G
#安装Mha(master01,master02,slave01,manager)
操作管理节点需要两个都安装, 在3台数据库节点只要安装MHA的node节点:
上传mha4mysql-node和manager包
下载地址:Releases · yoshinorim/mha4mysql-manager · GitHub
Releases · yoshinorim/mha4mysql-node · GitHub
- tar zxf mha4mysql-node-0.58.tar.gz
- cd mha4mysql-node-0.58
- perl Makefile.PL
- make && make install
- manager节点上操作:
- tar zxf mha4mysql-manager-0.58.tar.gz
- cd mha4mysql-manager-0.58/
- perl Makefile.PL
- make && make install
- mkdir /etc/masterha
- mkdir -p /masterha/app
- mkdir /scripts
- cp samples/conf/* /etc/masterha/
- cp samples/scripts/* /scripts/
#配置manager文件
- cat app1.cnf
- [server default]
- manager_workdir=/masterha/app1
- manager_log=/masterha/app1/manager.log
- user=manager
- password=123
- ssh_user=root
- repl_user=rep
- repl_password=123
- ping_interval=1
-
- [server1]
- hostname=192.168.52.5
- port=3306
- master_binlog_dir=/usr/local/mysql/data
- candidate_master=1
-
- [server2]
- hostname=192.168.52.6
- port=3306
- master_binlog_dir=/usr/local/mysql/data
- candidate_master=1
-
- [server3]
- hostname=192.168.52.7
- port=3306
- master_binlog_dir=/usr/local/mysql/data
- no_master=1
-
- #清空masterha_default.cnf文件
- >/etc/masterha/masterha_default.cnf
- #SSH有效验证
- masterha_check_ssh --global_conf=/etc/masterha/masterha_default.cnf --conf=/etc/masterha/app1.cnf
- masterha_check_repl --conf=/etc/masterha/app1.cnf
- 在(master01,master02,slave01,manager)上做软链接
- ln -s /usr/local/mysql/bin/* /usr/local/bin/
- #启动Mha服务
- nohup masterha_manager --global_conf=/etc/masterha/masterha_default.cnf --conf=/etc/masterha/app1.cnf &>/tmp/mha_manager.log &
- #查看状态
- masterha_check_status --conf=/etc/masterha/app1.cnf
- 模拟故障
- 如果Mha启动失败,查看tailf /masterha/app1/manager.log日志
- 解决问题后删除app1.failover.complete 文件
- rm -f /masterha/app1/app1.failover.complete
- 重新启动Mha服务
- nohup masterha_manager --global_conf=/etc/masterha/masterha_default.cnf --conf=/etc/masterha/app1.cnf &>/tmp/mha_manager.log &
到这里Mha服务已经部署完成了!!!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。