整体环境:
4台centos7.6服务器
mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
mha4mysql-node-0.58-0.el7.centos.noarch.rpm
mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
配置密钥通信(注意四个节点都需要操作,达到互相免密通信):
创建密钥:ssh-keygen -t rsa
复制给其他通信服务器:ssh-copy-id -i ~/.ssh/id_rsa.pub root@x.x.x.x
rpm安装mysql:
检查是否安装mariadb:rpm -qa|grep mariadb
移除mariadb:rpm -e mariadb版本 --nodeps
正式安装:
1.先common:rpm -ivh mysql-community-common-5.7.28-1.el7.x86_64.rpm
2.再libs:rpm -ivh mysql-community-libs-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-compat-5.7.28-1.el7.x86_64.rpm
3.客户端:rpm -ivh mysql-community-client-5.7.28-1.el7.x86_64.rpm
4.服务端:rpm -ivh mysql-community-server-5.7.28-1.el7.x86_64.rpm
5.工具(选择):rpm -ivh mysql-community-devel-5.7.28-1.el7.x86_64.rpm
初始化:
1.mysqld --initialize --user=mysql
2.记录密码:cat /var/log/mysqld.log
3.设置自启动:systemctl start mysqld.service
4.登录:mysql -uroot -p
5.修改密码:set password=password('密码');
注意:防火墙端口需要放开
主库开启binlog功能:
log_bin=mysql-bin #开启binlog
server-id=1 #服务id
sync-binlog=1 #同步
binlog-ignore-db=information_schema #忽略同步的库
binlog-ignore-db=performance_schema
binlog-ignore-db=sys
binlog-do-db= 需要同步的库,不写则全部需要同步
授权:
grant replication slave on . to 'root'@'%' identified by '密码';
grant all privileges on . to 'root'@'%' identified by '密码';
flush privileges ;//刷新权限
修改完重启服务:systemctl restart mysqld;
从库配置:
server-id=2;
relay_log=mysql-relay-bin
read_only=1
重启完登录:
查看slave状态:show slave status;
设置主库:change master to master_host='X.X.X.X',master_port=3306,master_user='root',master_password='XXXX',master_log_file='mysql-bin.00000*',master_log_pos=XXX;(file和pos均对映主库中信息)
开启:start slave;
半同步复制设置:
主库:
登录后安装插件:install plugin rpl_semi_sync_master soname 'semisync_master.so';
查看semi相关参数:show variables like '%semi%';
修改参数:set global rpl_semi_sync_master_enabled=1; //开启
set global rpl_semi_sync_master_timeout=1000; //设置超时时间
从库:
登录后安装插件:install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
查看:show variables like '%semi%';
修改参数:set global rpl_semi_sync_slave_enabled=1;
MHA搭建:
1.master节点与两个slave节点:
安装MHA的Perl依赖包:yum install perl-DBD-MySQL -y
安装node:rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm
(进入mysql授权命令)MHA的管理账号:
grant all privileges on . to 'mhamanger'@'%' identified by 'mhamanger';
flush privileges;
2.MHA节点上:
安装MHA的Perl依赖包:yum install perl-DBD-MySQL -y
yum install -y perl-Config-Tiny
yum install -y perl-Log-Dispatch
yum install -y perl-Parallel-ForkManager
安装manager:rpm -ivh mha4mysql-manager-0.58-0.el7.centos.noarch.rpm --force --nodeps
安装node:rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm
3.MHA节点操作:
配置文件目录:mkdir -p /etc/mha
日志目录:mkdir -p /var/log/mha/app1
创建配置文件:vim /etc/mha/app1.cnf
4.mha配置文件:
vim /etc/mha/app1.cnf
通信检验:masterha_check_ssh --conf=/etc/mha/app1.cnf
注:All SSH connection tests passed successfully. 则表示成功
检查mha配置文件连接配置参数是否正常:masterha_check_repl --conf=/etc/mha/app1.cnf
开启mha manager监控:
nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null> /var/log/mha/app1/manager.log 2>&1 &
--remove_dead_master_conf //该参数代表当发生主从切换后,老的主库的ip将会从配置文件中移除。
--manger_log //日志存放位置
查看manager状态:masterha_check_status --conf=/etc/mha/app1.cnf
关闭监控:masterha_stop -conf=/etc/mha/app1.cnf