主机名映射
[root@localhost ~]# cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.200.20 mysql-db01192.168.200.28 mysql-db02192.168.200.29 mysql-db03
关闭selinux和iptables
[root@mysql-db01 ~]# setenforce 0[root@mysql-db01 ~]# service iptables stopiptables: Setting chains to policy ACCEPT: filter [ OK ]iptables: Flushing firewall rules: [ OK ]iptables: Unloading modules: [ OK ]
安装(3台都装)
[root@mysql-db01 ~]# yum -y install ncurses-devel[root@mysql-db01 ~]# yum -y install libaio[root@mysql-db01 ~]# tar xf mysql-5.6.17-linux-glibc2.5-x86_64.tar.gz -C /usr/local/[root@mysql-db01 ~]# ln -s /usr/local/mysql-5.6.17-linux-glibc2.5-x86_64 /usr/local/mysql[root@mysql-db01 ~]# useradd mysql -s /sbin/nologin -M[root@mysql-db01 ~]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/[root@mysql-db01 ~]# /bin/cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf[root@mysql-db01 ~]# /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld[root@mysql-db01 ~]# ln -s /usr/local/mysql/bin/* /usr/local/bin/[root@mysql-db01 ~]# which mysqladmin/usr/local/bin/mysqladmin
加入开机自启动并启动mysql
[root@mysql-db01 ~]# chkconfig mysqld on[root@mysql-db01 ~]# chkconfig mysqld --listmysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off[root@mysql-db01 ~]# /etc/init.d/mysqld startStarting MySQL. SUCCESS![root@mysql-db01 ~]# netstat -antup | grep mysqltcp 0 0 :::3306 :::* LISTEN 1258/mysqld
配置密码
[root@mysql-db01 ~]# mysqladmin -uroot password '666666'
主库操作(mysql-db01)
修改配置文件
#修改主库配置文件/etc/my.cnf[root@mysql-db01 mysql]# cat /etc/my.cnf[client]socket = /usr/local/mysql/data/mysql.sock[mysqld]lower_case_table_names = 1default-storage-engine = InnoDBport = 3306datadir = /usr/local/mysql/datacharacter-set-server = utf8socket = /usr/local/mysql/data/mysql.socklog_bin = mysql-bin #开启binlog日志server_id = 1 #设置server_idinnodb_buffer_pool_size = 200Mslave-parallel-workers = 8thread_cache_size = 600back_log = 600slave_net_timeout = 60max_binlog_size = 512Mkey_buffer_size = 8Mquery_cache_size = 64Mjoin_buffer_size = 2Msort_buffer_size = 2Mquery_cache_type = 1thread_stack = 192K#重启动MySQL服务[root@mysql-db01 mysql]# /etc/init.d/mysqld restartShutting down MySQL.. SUCCESS!Starting MySQL. SUCCESS!
创建主从复制用户
mysql> grant replication slave on *.* to rep@'192.168.200.%' identified by '666666';Query OK, 0 rows affected (0.01 sec)mysql> select user,host from mysql.user;+------+---------------+| user | host |+------+---------------+| root | 127.0.0.1 || rep | 192.168.200.% || root | ::1 || | localhost || root | localhost || | mysql-db01 || root | mysql-db01 |+------+---------------+7 rows in set (0.00 sec)mysql> show grants for rep@'192.168.200.%';+----------------------------------------------------------------------------------------------------------------------------+| Grants for rep@192.168.200.% |+----------------------------------------------------------------------------------------------------------------------------+| GRANT REPLICATION SLAVE ON *.* TO 'rep'@'192.168.200.%' IDENTIFIED BY PASSWORD '*B2B366CA5C4697F31D4C55D61F0B17E70E5664EC' |+----------------------------------------------------------------------------------------------------------------------------+1 row in set (0.00 sec)
从库操作(mysql-db02和mysql-db03)
修改配置文件
#修改mysql-db02配置文件(和mysql-db01配置文件一致)#只需要修改server-id = 5选项[root@mysql-db02 ~]# cat /etc/my.cnf[client]socket = /usr/local/mysql/data/mysql.sock[mysqld]lower_case_table_names = 1default-storage-engine = InnoDBport = 3306datadir = /usr/local/mysql/datacharacter-set-server = utf8socket = /usr/local/mysql/data/mysql.socklog_bin = mysql-bin #从binlog也要打开server_id = 5 #仅需修改此项innodb_buffer_pool_size = 200Mslave-parallel-workers = 8thread_cache_size = 600back_log = 600slave_net_timeout = 60max_binlog_size = 512Mkey_buffer_size = 8Mquery_cache_size = 64Mjoin_buffer_size = 2Msort_buffer_size = 2Mquery_cache_type = 1thread_stack = 192K[root@mysql-db02 ~]# /etc/init.d/mysqld restart #重启mysqlShutting down MySQL.. SUCCESS!Starting MySQL. SUCCESS!#修改mysql-db03配置文件(和mysql-db01配置文件一致)#只需要修改server-id = 10选项[root@mysql-db03 ~]# cat /etc/my.cnf[client]socket = /usr/local/mysql/data/mysql.sock[mysqld]lower_case_table_names = 1default-storage-engine = InnoDBport = 3306datadir = /usr/local/mysql/datacharacter-set-server = utf8socket = /usr/local/mysql/data/mysql.socklog_bin = mysql-bin #从binlog也要打开server_id = 10 #只需修改此项innodb_buffer_pool_size = 200Mslave-parallel-workers = 8thread_cache_size = 600back_log = 600slave_net_timeout = 60max_binlog_size = 512Mkey_buffer_size = 8Mquery_cache_size = 64Mjoin_buffer_size = 2Msort_buffer_size = 2Mquery_cache_type = 1thread_stack = 192K[root@mysql-db03 ~]# /etc/init.d/mysqld restart #重启mysqlShutting down MySQL.. SUCCESS!Starting MySQL. SUCCESS!
开启GTID
mysql> show global variables like '%gtid%'; #查看GTID是否开启+--------------------------+-------+| Variable_name | Value |+--------------------------+-------+| enforce_gtid_consistency | OFF || gtid_executed | || gtid_mode | OFF || gtid_owned | || gtid_purged | |+--------------------------+-------+5 rows in set (0.07 sec) #没开启
编辑mysql配置文件(主库从库都需要修改) 
再次查看GTID状态
[root@mysql-db01 ~]# mysql -uroot -p666666Warning: Using a password on the command line interface can be insecure.Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 1Server version: 5.6.17-log MySQL Community Server (GPL)Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show global variables like '%gtid%';+--------------------------+-------+| Variable_name | Value |+--------------------------+-------+| enforce_gtid_consistency | ON | # 执行GTID一致| gtid_executed | || gtid_mode | ON | # 开启GTID模块| gtid_owned | || gtid_purged | |+--------------------------+-------+5 rows in set (0.00 sec)
开启从库的主从复制功能(mysql-db02,mysql-db03)
mysql> start slave; #开启主从同步功能Query OK, 0 rows affected, 1 warning (0.01 sec)mysql> show slave status\G*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.0.51Master_User: repMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000003Read_Master_Log_Pos: 151Relay_Log_File: mysql-db02-relay-bin.000002Relay_Log_Pos: 361Relay_Master_Log_File: mysql-bin.000003Slave_IO_Running: Yes #此项yes代表成功Slave_SQL_Running: Yes #此项yes代表成功Replicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:#....以下省略若干行....
因为MHA需要relaylog日志,但是mysql会自动删除relaylog日志,所以需要关闭自动删除
#登陆从库[root@mysql-db02 ~]# mysql -uroot -p123123#临时禁用自动删除relay log功能mysql> set global relay_log_purge = 0;Query OK, 0 rows affected (0.00 sec)#设置只读mysql> set global read_only=1;Query OK, 0 rows affected (0.00 sec)
编辑配置文件/etc/my.cof 
修改配置文件必须重启服务
/etc/init.d/mysqld restart
部署MHA
环境准备(所有节点mysql-01,mysql-02,mysql-03)
#光盘安装依赖包[root@mysql-db01 ~]# yum -y install perl-DBD-MySQL#安装mha4mysql-node-0.56-0.el6.noarch.rpm[root@mysql-db01 rpm]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpmPreparing... ########################################### [100%]1:mha4mysql-node ########################################### [100%][root@mysql-db01 rpm]# mysql -uroot -p666666Warning: Using a password on the command line interface can be insecure.Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 3Server version: 5.6.17-log MySQL Community Server (GPL)Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> grant all privileges on *.* to mha@'192.168.200.%' identified by '666666';Query OK, 0 rows affected (0.12 sec)mysql> select user,host from mysql.user where user='mha';+------+---------------+| user | host |+------+---------------+| mha | 192.168.200.% | #主库上创建从库自动复制+------+---------------+1 row in set (0.10 sec)# 3台MsQL都需要安装mha4mysql-node-0.56-0.el6.noarch.rpm
部署管理节点(mha-manager)
在mysql-db03上部署管理节点
#使用阿里云源+epel源wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repowget -O /etc/yum.repos.d/epel-6.repo http://mirrors.aliyun.com/repo/epel-6.repo#安装manager依赖包(需要公网源)[root@mysql-db03 ~]# yum -y install perl-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes#安装manager包[root@mysql-db03 rpm]# rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpmPreparing... ########################################### [100%]1:mha4mysql-manager ########################################### [100%]
编辑配置文件
#创建配置文件目录[root@mysql-db03 ~]# mkdir -p /etc/mha#创建日志目录[root@mysql-db03 ~]# mkdir -p /var/log/mha/mha1#创建配置文件(默认没有)[root@mysql-db03 ~]# cd /etc/mha/[root@mysql-db03 mha]# ls[root@mysql-db03 mha]# vim /etc/mha/mha1.cnf[root@mysql-db03 mha]# cat /etc/mha/mha1.cnf[server default]manager_log=/var/log/mha/mha1/manager #manager管理日志存放路径manager_workdir=/var/log/mha/mha1 #manager管理日志的目录路径master_binlog_dir=/usr/local/mysql/data #binlog日志的存放路径user=mha #管理账户password=123123 #管理账户密码ping_interval=2 #存活检查的间隔时间repl_user=rep #主从复制的授权账户repl_password=123123 #主从复制的授权账户密码ssh_user=root #用于ssh连接的账户[server1]hostname=192.168.0.51port=3306[server2]#candidate_master=1 #此条暂时注释掉(后面解释)#check_repl_delay=0 #此条暂时注释掉(后面解释)hostname=192.168.0.52port=3306[server3]hostname=192.168.0.53port=3306
配置ssh信任(所有节点mysql-db01,mysql-db02,mysql-db03)
#创建秘钥对,三个都创建,因为要互相免密码登录[root@mysql-db03 ~]# ssh-keygen -t dsa -P "" -f ~/.ssh/id_dsa#发送mysql-db03公钥,包括自己[root@mysql-db03 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.200.20[root@mysql-db03 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.200.28[root@mysql-db03 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.200.29#发送mysql-db02公钥,包括自己[root@mysql-db02 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.200.20[root@mysql-db02 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.200.28[root@mysql-db02 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.200.29#发送mysql-db01公钥,包括自己[root@mysql-db01 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.200.20[root@mysql-db01 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.200.28[root@mysql-db01 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.200.29
启动测试
ssh检查检测
[root@mysql-db03 mha]# masterha_check_ssh --conf=/etc/mha/mha1.cnf #ssh检查命令Sat Nov 24 06:21:45 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.Sat Nov 24 06:21:45 2018 - [info] Reading application default configuration from /etc/mha/mha1.cnf..Sat Nov 24 06:21:45 2018 - [info] Reading server configuration from /etc/mha/mha1.cnf..Sat Nov 24 06:21:45 2018 - [info] Starting SSH connection tests..Sat Nov 24 06:21:46 2018 - [debug]Sat Nov 24 06:21:45 2018 - [debug] Connecting via SSH from root@192.168.200.20(192.168.200.20:22) to root@192.168.200.28(192.168.200.28:22)..Sat Nov 24 06:21:45 2018 - [debug] ok.Sat Nov 24 06:21:45 2018 - [debug] Connecting via SSH from root@192.168.200.20(192.168.200.20:22) to root@192.168.200.29(192.168.200.29:22)..Sat Nov 24 06:21:45 2018 - [debug] ok.Sat Nov 24 06:21:47 2018 - [debug]Sat Nov 24 06:21:46 2018 - [debug] Connecting via SSH from root@192.168.200.29(192.168.200.29:22) to root@192.168.200.20(192.168.200.20:22)..Sat Nov 24 06:21:46 2018 - [debug] ok.Sat Nov 24 06:21:46 2018 - [debug] Connecting via SSH from root@192.168.200.29(192.168.200.29:22) to root@192.168.200.28(192.168.200.28:22)..Sat Nov 24 06:21:47 2018 - [debug] ok.Sat Nov 24 06:21:47 2018 - [debug]Sat Nov 24 06:21:45 2018 - [debug] Connecting via SSH from root@192.168.200.28(192.168.200.28:22) to root@192.168.200.20(192.168.200.20:22)..Sat Nov 24 06:21:46 2018 - [debug] ok.Sat Nov 24 06:21:46 2018 - [debug] Connecting via SSH from root@192.168.200.28(192.168.200.28:22) to root@192.168.200.29(192.168.200.29:22)..Sat Nov 24 06:21:46 2018 - [debug] ok.Sat Nov 24 06:21:47 2018 - [info] All SSH connection tests passed successfully. #出现这个就表示成功
主从复制检查
(1)错误的主从复制检测 [root@mysql-db03 ~]# masterha_check_repl --conf=/etc/mha/mha1.cnf
如果出现下面的情况 
因此在mysql-db02和mysql-db03上添加主从复制的用户即可。
grant replication slave on *.* to rep@'192.168.200.%' identified by '666666';
再次检查

启动MHA
#启动[root@mysql-db03 ~]# nohup masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/mha1/manager.log 2>&1 & #启动后按几次回车,不退出就是没问题。[root@mysql-db03 ~]# ps -ef | grep perl | grep -v greproot 5585 2925 0 09:23 pts/1 00:00:00 perl /usr/bin/masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover
(1)登陆mysql-db02(192.168.200.28)查看信息状态
#登陆数据库mysql-db02(192.168.200.28)[root@mysql-db03 ~]# mysql -uroot -p666666mysql> show slave status\G*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.200.20 #这是主库IP地址Master_User: repMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000003Read_Master_Log_Pos: 1656Relay_Log_File: mysql-db02-relay-bin.000004Relay_Log_Pos: 1796Relay_Master_Log_File: mysql-bin.000003Slave_IO_Running: YesSlave_SQL_Running: Yes..以下省略若干内容..
(2)停掉mysql-db01(192.168.200.20)上的MySQL服务
[root@localhost rpm]# /etc/init.d/mysqld stopShutting down MySQL...... SUCCESS!
(3)查看mysql-db03上的MySQL从库同步状态
[root@localhost mha]# mysql -uroot -p666666 -e 'show slave status\G'Warning: Using a password on the command line interface can be insecure.*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.200.28 #现在的主库IPMaster_User: repMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000003 #binlog日志Read_Master_Log_Pos: 662 #binlog日志位置Relay_Log_File: localhost-relay-bin.000002Relay_Log_Pos: 665Relay_Master_Log_File: mysql-bin.000003Slave_IO_Running: YesSlave_SQL_Running: Yes..以下省略若干内容..
(4)查看mysql-db02上的MySQL,主库同步状态。 
5)查看mysql-db03上的mha进程状态
[root@mysql-db03 ~]# ps -ef | grep perl | grep -v grep #查询发现mha进程已经没了[root@mysql-db03 ~]#
(6)查看mha配置文件信息 
进行mha的故障还原测试
(1)将故障宕机的mysql-db01的MySQL服务启动并授权进行从同步
[root@mysql-db01 ~]# /etc/init.d/mysqld startStarting MySQL. SUCCESS![root@mysql-db01 ~]# mysql -uroot -p666666mysql> CHANGE MASTER TO MASTER_HOST='192.168.200.28', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='rep', MASTER_PASSWORD='666666';
(2)将mha配置文件里缺失的部分补全
[root@localhost mha]# cat /etc/mha/mha1.cnf[server default]manager_log=/var/log/mha/mha1/managermanager_workdir=/var/log/mha/mha1master_binlog_dir=/usr/local/mysql/datapassword=666666ping_interval=2repl_password=666666repl_user=repssh_user=rootuser=mha[server1]hostname=192.168.200.20port=3306[server2]hostname=192.168.200.28port=3306[server3]hostname=192.168.200.29port=3306
(3)启动mha进程
[root@mysql-db03 ~]# nohup masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/mha1/manager.log 2>&1 &[root@localhost mha]# ps -ef |grep perlroot 4541 934 0 23:39 pts/0 00:00:00 perl /usr/bin/masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover
(4)停掉mysql-db02上的MySQL服务
[root@localhost ~]# /etc/init.d/mysqld stopShutting down MySQL...... SUCCESS!
(5)查看mysql-db03上的主从同步状态:
(6)启动mysql-db02上的MySQL服务
[root@mysql-db02 ~]# /etc/init.d/mysqld startStarting MySQL. SUCCESS![root@mysql-db02 ~]# mysql -uroot -p666666mysql> CHANGE MASTER TO MASTER_HOST='192.168.200.20', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='rep', MASTER_PASSWORD='666666';mysql> start slave;mysql> show slave status\G #查看从同步状态
(7)再次补全mha配置文件后,启动mha进程
[root@localhost mha]# cat /etc/mha/mha1.cnf[server default]manager_log=/var/log/mha/mha1/managermanager_workdir=/var/log/mha/mha1master_binlog_dir=/usr/local/mysql/datapassword=666666ping_interval=2repl_password=666666repl_user=repssh_user=rootuser=mha[server1]hostname=192.168.200.20port=3306[server2]hostname=192.168.200.28port=3306[server3]hostname=192.168.200.29port=3306[root@mysql-db03 ~]# nohup masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/mha1/manager.log 2>&1 &[root@localhost mha]# ps -ef | grep perl | grep -v greproot 5027 934 0 Nov24 pts/0 00:00:00 perl /usr/bin/masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover
浙公网安备 33010602011771号