公司最近为新的MySQL架构进行调整,要求给出方案,我这边提出使用MHA+Atlas做高可用集群读写分离架构,就多方讨论最终确认方案,进行实施;
一、简单说下MHA的工作原理
1个管理节点可以管理多套mysql架构,可以不装在mysql主机上
通过管理节点,来对其它数据节点上的mysql做监控,会每隔几秒做心跳检测
二、MHA的简单架构图
三、部署MHA
1,环境准备(依赖包、软件包、创建存放目录等)所有节点进行
#安装依赖包 [[email protected] ~]#yum install perl-DBD-MySQL -y #进入安装包存放目录 [[email protected] ~]#cd /home/oldboy/tools/ #上传mha安装包 [[email protected]]# rz -be mha4mysql-manager-0.56-0.el6.noarch.rpm mha4mysql-manager-0.56.tar.gz mha4mysql-node-0.56-0.el6.noarch.rpm mha4mysql-node-0.56.tar.gz #安装node包 [[email protected] tools]# rpm -ivhmha4mysql-node-0.56-0.el6.noarch.rpm Preparing... ########################################### [100%] 1:mha4mysql-node ###########################################[100%] #登录数据库 [[email protected]]# mysql -uroot -poldboy123 #添加mha管理账号 mysql> grant allprivileges on *.* to [email protected]‘10.0.0.%‘ identified by ‘mha‘; #查看是否添加成功 mysql> selectuser,host from mysql.user; #主库上创建,从库会自动复制(在从库上查看)
2,命令软连接 所有节点进行
[[email protected] ~]# ln -s /application/mysql/bin/mysqlbinlog/usr/bin/mysqlbinlog [[email protected] ~]# ln -s /application/mysql/bin/mysql/usr/bin/mysql #如果不创建命令软连接,检测mha复制情况的时候会报错
3,部署管理节点(mha-manager)
1) 在mysql-db03上部署管理节点
#使用epel源 [[email protected] ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo #安装manager依赖包 [[email protected] ~]# yum install -y perl-Config-Tinyepel-release perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes #安装manager包 [[email protected] tools]# rpm -ivhmha4mysql-manager-0.56-0.el6.noarch.rpm Preparing... ###########################################[100%] 1:mha4mysql-manager ########################################### [100%]
2) 编辑配置文件
#创建配置文件目录 [[email protected] ~]# mkdir -p /etc/mha #创建日志目录 [[email protected] ~]# mkdir -p /var/log/mha/app1 #编辑mha配置文件 [[email protected] ~]# vim /etc/mha/app1.cnf [server default] candidate_master=1 check_repl_delay=0 manager_log=/var/log/mha/app1/manager manager_workdir=/var/log/mha/app1 master_binlog_dir=/application/mysql/data master_ip_failover_script=/usr/local/bin/master_ip_failover password=mha ping_interval=2 repl_password=oldboy123 repl_user=rep ssh_user=root user=mha [server1] hostname=10.0.0.51 port=3306 [server2] hostname=10.0.0.52 port=3306 [server3] hostname=10.0.0.53 port=3306
3) 建立ssh-key免秘钥登录 所有节点进行
#创建秘钥对 [[email protected] ~]# ssh-keygen -t dsa -P ‘‘ -f ~/.ssh/id_dsa>/dev/null 2>&1 #发送公钥,包括自己 [[email protected] ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected] [[email protected] ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected] [[email protected] ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected]
4) 启动测试 db03
#测试ssh [[email protected] ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf #看到如下字样,则测试成功 Tue Mar 7 01:03:33 2017 -[info] All SSH connection tests passed successfully. #测试复制 [[email protected] ~]# masterha_check_repl --conf=/etc/mha/app1.cnf #看到如下字样,则测试成功 MySQL Replication Health is OK.
5) 启动MHA db03
[[email protected] ~]# 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 &
6) 切换master测试
登录数据库db02
[[email protected] ~]# mysql -uroot -poldboy123 #检查复制情况 mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.51 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000006 Read_Master_Log_Pos: 191 Relay_Log_File: mysql-db02-relay-bin.000002 Relay_Log_Pos: 361 Relay_Master_Log_File: mysql-bin.000006 Slave_IO_Running: Yes Slave_SQL_Running: Yes
#登录数据库(db03)
[[email protected] ~]# mysql -uroot -poldboy123 #检查复制情况 mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.51 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000006 Read_Master_Log_Pos: 191 Relay_Log_File: mysql-db03-relay-bin.000002 Relay_Log_Pos: 361 Relay_Master_Log_File:mysql-bin.000006 Slave_IO_Running: Yes Slave_SQL_Running: Yes
#停掉主库
[[email protected] ~]# /etc/init.d/mysqld stop Shutting down MySQL..... SUCCESS! #登录数据库(db02) [[email protected] ~]# mysql -uroot -poldboy123 #查看slave状态 mysql> show slave status\G #db02的slave已经为空 Empty set (0.00 sec) #登录数据库(db03) [[email protected] ~]# mysql -uroot -poldboy123 #查看slave状态 mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State:Waiting for master to send event Master_Host: 10.0.0.52 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000006 Read_Master_Log_Pos:191 Relay_Log_File: mysql-db03-relay-bin.000002 Relay_Log_Pos: 361 Relay_Master_Log_File: mysql-bin.000006 Slave_IO_Running: Yes Slave_SQL_Running: Yes
时间: 2024-10-12 23:28:14