在安装之前,我们先来做些规划和准备工作
环境
1 2 3 4 5 6 7
| Centos: 6.5 manager 10.20.64.209 node1 10.20.64.202 node2 10.20.64.203 node3 10.20.64.204 node4 10.20.64.210
|
系统依赖包
1 2 3 4 5 6
| perl -MCPAN -e 'install "YAML"' yum -y install perl-CPAN yum -y install perl-DBD-MySQL perl -MCPAN -e "install Config::Tiny" perl -MCPAN -e "install Log::Dispatch" perl -MCPAN -e "install Parallel::ForkManager"
|
部署位置
{version}参数部分为自动化预留,手动的同学可参考例子
Key |
Value |
node basedir |
/opt/mha-node{version} 例如mha-node056 |
manager basedir |
/opt/mha-manager{version} 例如mha-manager056 |
config dir |
/opt/mha-manager{version}/etc |
manager work dir |
/opt/mha-manager{version}/apps/{app_name} |
manager log dir |
/opt/mha-manager{version}/logs |
script log dir |
/opt/mha-manager{version}/scripts |
remote work dir |
/opt/mha-node{version}/apps/{app_name} |
系统软链
(很多属于个人习惯,可以跳过,很多都是为了自动化做铺垫。如果读者觉得没有必要,可以忽略下文中软链部分:))
Key |
Value |
node basedir link |
/usr/local/mha-node |
manager basedir link |
/usr/local/mha-manager |
我们看了官方文档后知道MHA需要在主机间建立信任关系,而且官方做法是使用root帐号建立的,这样是很不安全的
我们知道mha的配置文件是有ssh_user选项的,如下,这里我创建了mha系统帐号,并在主机间使用mha帐号建立信任关系
1 2 3
| [server default] ssh_user=mha ssh_port=22
|
创建mysql和mha帐号
1 2 3 4 5 6
| useradd mha -u 512 -G mysql -d /home/mha passwd mha 输入密码 说明:为了统一,我们将mha用户uid固定为512,并隶属于mysql组 (由于mha在切换过程中需要访问binlog目录,当然你也可以开放binlog目录的其他用户读权限)
|
建立mha账号信任关系
创建公钥
1 2 3 4 5
| ssh-keygen 同时将自己的公钥也添加到授权文件中 cat /home/mha/.ssh/id_rsa.pub > /home/mha/.ssh/authorized_keys chmod 600 /home/mha/.ssh/authorized_keys
|
节点间信任
1 2 3 4
| 推送本地公钥要N个远程 cat /home/mha/.ssh/id_rsa.pub | ssh {nodeN ip} "cat - >> /home/mha/.ssh/authorized_keys" ssh {nodeN ip} "chmod 600 /home/mha/.ssh/authorized_keys"
|
管理机与节点间信任
1 2 3 4 5
| 将已经生成的node节点公钥拿到本地 mha@manager ssh{nodeN ip} 'cat /home/mha/.ssh/id_rsa.pub' >> /home/mha/.ssh/authorized_keys 也将自己的公钥推送给node节点 cat /home/mha/.ssh/id_rsa.pub | ssh {nodeN ip} "cat - >> /home/mha/.ssh/authorized_keys"
|
sshd配置
1 2 3 4
| 是否允许使用基于 GSSAPI 的用户认证:关闭认证 #GSSAPIAuthentication no service sshd reload
|
创建mha的mysql实例账号
MHA用到以下权限,所以不用赋予ALL(DBA洁癖)
1
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SUPER ON *.* TO 'mha'@'%' IDENTIFIED BY 'xxx';
|
我们可以将编译安装、创建管理目录、配置全局环境变量封装到RPM包中
详细内容如下
编译安装MHA
Node |
node basedir |
node basedir link |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| tar zxvf mha4mysql-node-0.56.tar.gz cd mha4mysql-node-0.56 perl Makefile.PL INSTALL_BASE=/opt/mha-node056 说明:这里为选择了INSTALL_BASE选项,它会把node的lib包集中在base目录/lib中 如果不写,或者使用PREFIX的话,lib会散落在系统lib中,卸载时不利于清理 make && make install chown -R mha:mysql /opt/mha-node056 ln -s /opt/mha-node056 /usr/local/mha-node ln -s /opt/mha-node056/lib/perl5/MHA /usr/local/share/perl5/MHA-Node echo '/usr/local/share/perl5/MHA-Node' >> /etc/ld.so.conf
|
Node安装完成后会有如下脚本
1 2 3 4
| apply_diff_relay_logs filter_mysqlbinlog purge_relay_logs save_binary_logs
|
Node类库
1 2 3 4 5 6 7 8 9
| BinlogHeaderParser.pm BinlogManager.pm BinlogPosFinderElp.pm BinlogPosFinder.pm BinlogPosFinderXid.pm BinlogPosFindManager.pm NodeConst.pm NodeUtil.pm SlaveUtil.pm
|
Manager |
manager basedir |
manager basedir link |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| tar zxvf mha4mysql-manager-0.56.tar.gz cd mha4mysql-manager-0.56 perl Makefile.PL INSTALL_BASE=/opt/mha-manager056 make && make install chown -R mha:mysql /opt/mha-manager056 ln -s /opt/mha-node056 /usr/local/mha-manager ln -s /opt/mha-node056/lib/perl5/MHA /usr/local/share/perl5/MHA-Manager echo '/usr/local/share/perl5/MHA-Manager' >> /etc/ld.so.conf
|
Manager安装完成后会有如下脚本
1 2 3 4 5 6 7 8 9
| masterha_stop masterha_conf_host masterha_check_status masterha_check_repl masterha_secondary_check masterha_master_switch masterha_master_monitor masterha_manager masterha_check_ssh
|
Manager类库
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| Config.pm DBHelper.pm FileStatus.pm HealthCheck.pm ManagerAdmin.pm ManagerAdminWrapper.pm ManagerConst.pm ManagerUtil.pm MasterFailover.pm MasterMonitor.pm MasterRotate.pm ServerManager.pm Server.pm SSHCheck.pm
|
创建管理目录
一定要保证/opt/mha-node056与/opt/mha-manager056目录权限为mha:mysql
1 2 3
| mkdir -p /opt/mha-node056/apps chown -R mha:mysql /opt/mha-node056
|
Manager |
config dir |
manager work dir |
manager log dir |
script log dir |
1 2 3
| mkdir -p /opt/mha-manager056/{etc,apps,logs,scripts} chown -R mha:mysql /opt/mha-manager056
|
配置全局环境变量
MHA-Node
1 2 3 4
| echo "MHA_NODE_PATH=/usr/local/mha/bin" >> /etc/profile echo "export PATH=\$PATH:\$MHA_NODE_PATH" >> /etc/profile source /etc/profile
|
MHA-Manager
1 2 3 4
| echo "MHA_MANAGER_PATH=/usr/local/mha/bin" >> /etc/profile echo "export PATH=\$PATH:\$MHA_MANAGER_PATH" >> /etc/profile source /etc/profile
|
配置文件
manger & node 合并为一个配置文件{app_name}.cnf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| mha$ vi {app_name}.cnf [server default] ssh_user=mha ssh_port=22 user=mha password= port=3306 repl_user=repl repl_password= log_level=info client_bindir=/usr/local/mysql/bin client_libdir=/usr/local/mysql/lib master_binlog_dir=/opt/mysqldata/{port}/binlog remote_workdir=/opt/mha-node{version}/apps/{app_name} manager_workdir=/opt/mha-manager{version}/apps/{app_name} manager_log=/usr/local/mha-manager/logs/{app_name}.log secondary_check_script= masterha_secondary_check -s {node2 ip} -s {node3 ip} ping_interval=5 这里可以使用你自己的脚本,结合dns或者haproxy完成切换的完整工作 [server1] hostname={node1 ip} candidate_master=1 node_label={app_name} master [server2] hostname={node2 ip} candidate_master=1 node_label={app_name} standby [server3] hostname={node3 ip} no_master=1 node_label={app_name} slave
|
用户环境变量
这个属于踩坑后的配置(后续补充上报错内容)
在MHA check的时候会报错,所以需要添加完整的环境变量到到mha用户下
1 2 3 4 5 6 7 8 9 10
| mha$ echo $PATH 将环境变量实际内容贴到environment文件中,否则bash失效 echo "PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/mysql/bin:/home/mha/bin" > /home/mha/.ssh/environment echo "PermitUserEnvironment yes" >> /etc/ssh/sshd_config 是否允许sshd处理 ~/.ssh/environment 以及 ~/.ssh/authorized_keys 中的 environment= 选项:允许 service sshd reload
|
检测MHA
检测SSH通讯
1
| mha$ masterha_check_ssh --conf=/usr/local/mha-manager/etc/{app_name}.conf
|
检测集群同步
1 2 3
| mha$ masterha_check_repl --conf=/usr/local/mha-manager/etc/{app_name}.conf -- /opt/mha-manager056/apps/test masterha_check_repl自动创建
|
启动MHA
1
| mha$ masterha_manager --conf=/usr/local/mha-manager/etc/{app_name}.conf &
|
MHA Manager在每次切换后,将退出进程。修改配置后,再次检测,启动