1、.文件版本: V1.0 文件编号: R2 识别含有最新更新的 slave;3 应用差异的中继日志(relay log)到其他的 slave;4 应用从 master 保存的二进制日志事件(binlog events);5 提升一个 slave 为新的 master;6 使其他的 slave 连接新的 master 进行复制;MHA 软件包说明MHA 软件由两部分组成,Manager 工具包和 Node 工具包,具体的说明如下。 Manager 工具包主要包括以下几个工具:1 masterha_check_ssh 检查 MHA 的 SSH 配置状况2 masterha_check_repl 检查
2、 MySQL 复制状况3 masterha_manger 启动 MHA4 masterha_check_status 检测当前 MHA 运行状态.5 masterha_master_monitor 检测 master 是否宕机6 masterha_master_switch 控制故障转移(自动或者手动)7 masterha_conf_host 添加或删除配置的 server 信息 Node 工具包这些工具通常由 MHA Manager 的脚本触发,无需人为操作)主要包括以下几个工具:save_binary_logs 保存和复制 master 的二进制日志apply_diff_relay_log
3、s 识别差异的中继日志事件并将其差异的事件应用于其他的 slavefilter_mysqlbinlog 去除不必要的 ROLLBACK 事件(MHA 已不再使用这个工具)purge_relay_logs 清除中继日志(不会阻塞 SQL 线程)注意:为了尽可能的减少主库硬件损坏宕机造成的数据丢失,因此在配置 MHA 的同时建议配置成 MySQL 5.5 的半同步复制。关于半同步复制原理各位自己进行查阅。(不是必须)实验环境使用电信云主机。详细情况:1、操作系统版本Ubuntu 12.04.5 LTS n l 2、操作系统位数x86_64, 64 位操作系统3、操作系统内核Linux master
4、sql 3.2.0-23-generic #36-Ubuntu SMP Tue Apr 10 20:39:51 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux4、云主机情况角色 Ip 地址 主机名 Server_id 类型Monitor host 192.168.62.37 manager 37 监控复制组Master 192.168.62.42 mastersql 42 写入Candicate master 192.168.62.36 slavesql2 36 读Slave 192.168.62.41 slavesql1 41 读其中 master 对外提供写
5、服务,备选 master(实际的 slave,主机名 slavesql2)提供读服务,slave 也提供相关的读服务,一旦 master 宕机,将会把备选 master 提升为新的 master,slave 指向新的 master。上面和下面所有的命令最好都使用 root 用户执行,我曾经使用非 root 用户,最后发现很烦,另 ubuntu 默认 root 是不可以 ssh 登陆.的,要先:passwd root 给 root 添加密码,这样 root 就可以 ssh 登陆了。建立 ssh 无密码登录环境 manager 公约操作rootmanager: ssh-keygenGenerati
6、ng public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa):Enter passphrase (empty for no passphrase):Enter same passphrase again:Your identification has been saved in /root/.ssh/id_rsa.Your public key has been saved in /root/.ssh/id_rsa.pub.The key fingerprint is:20:a3:4
7、a:e2:1f:c4:4c:9a:90:8f:00:c4:26:36:aa:81 rootiZ28i9mza1uZThe keys randomart image is:+- RSA 2048-+|+. |o* |X o . |Eo*. o . |+=o+ S |= | . | . . | . |+-+然后在/root/.ssh/id_rsa.pub.下面会新建两个文件 id_rsa(私钥)和 id_rsa.pub(私钥)。拷贝公约到其他三台机器:rootmanager: ssh-copy-id -i .ssh/id_rsa.pub root192.168.62.37#为什么要在本机也要设置呢
8、,因为 manager 节点安装在这上面,如不设置在下面 ssh 检查时会通不过。rootmanager: ssh-copy-id -i .ssh/id_rsa.pub root192.168.62.37rootmanager: ssh-copy-id -i .ssh/id_rsa.pub root192.168.62.36rootmanager: ssh-copy-id -i .ssh/id_rsa.pub root192.168.62.41过程示意图(因其过程都一样,故只示范 192.168.62.37) 主 mysql 公约操作ssh-keygen -t rsa.ssh-copy-id
9、-i .ssh/id_rsa.pub root192.168.62.37ssh-copy-id -i .ssh/id_rsa.pub root192.168.62.41ssh-copy-id -i .ssh/id_rsa.pub root192.168.62.42 从 mysql1 公约操作ssh-keygen -t rsassh-copy-id -i .ssh/id_rsa.pub root192.168.62.42ssh-copy-id -i .ssh/id_rsa.pub root192.168.62.36ssh-copy-id -i .ssh/id_rsa.pub root192.16
10、8.62.37 从 mysql2 公约操作从 mysql2 也就是主 mysql 宕机以后要充当主 mysql 的备用主 mysql。ssh-keygen -t rsassh-copy-id -i .ssh/id_rsa.pub root192.168.62.37ssh-copy-id -i .ssh/id_rsa.pub root192.168.62.41ssh-copy-id -i .ssh/id_rsa.pub root192.168.62.42 主机名rootmanager:# cat /etc/hostnamemanager在不重启的情况下在手动执行一次。rootlocalhost:
11、# hostname managerrootmanager:# bash我只列出 manager 机器操作步骤,其他机器一样操作设置 mastersql,slavesql1,slavesql2 修改 hostsrootmanager:# cat /etc/hosthost.conf hostname hosts hosts.allow hosts.denyrootslavesql1:# cat /etc/hosts127.0.0.1 localhost192.168.56.98 ubuntu1# The following lines are desirable for IPv6 capabl
12、e hosts:1 ip6-localhost ip6-loopback.fe00:0 ip6-localnetff00:0 ip6-mcastprefixff02:1 ip6-allnodesff02:2 ip6-allrouters192.168.62.42 mastersql192.168.62.41 slavesql1192.168.62.36 slavesql2192.168.62.37 manager添加到 hosts 文件中,对应各自的 ip 和主机名我只列出了 manager 的主机 hosts 文件,其他三台机器相同操作修改 hosts。 测试 ssh 登录rootmanag
13、er:# ssh mastersqlWelcome to Ubuntu 12.04.5 LTS (GNU/Linux 3.2.0-23-generic x86_64)* Documentation: https:/ information as of Wed Aug 10 10:38:20 CST 2016System load: 0.27 Processes: 83Usage of /: 4.2% of 98.85GB Users logged in: 1Memory usage: 4% IP address for eth0: 192.168.62.42Swap usage: 0% IP
14、address for eth0:0: 192.168.62.200Graph this data and manage this system at https:/ packages can be updated.6 updates are security updates.New release 14.04.4 LTS available.Run do-release-upgrade to upgrade to it.Last login: Wed Aug 10 10:22:36 2016 from 192.168.62.33rootmastersql:#说明已经无密码登陆了。rootma
15、nager:# ssh slavesql1rootmanager:# ssh slavesql2我只测试了 manger 无密码登陆到其他三台机器。其他类似。.安装 MySQL5.5安装 mysql 和配置主从关系 在线安装 mysql5.5rootmastersql:# apt-get install -y mysql-server The following NEW packages will be installed:libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient16libnet-daemon-perl
16、 libplrpc-perl mysql-client-5.5 mysql-client-core-5.5mysql-common mysql-server mysql-server-5.5 mysql-server-core-5.50 upgraded, 12 newly installed, 0 to remove and 1 not upgraded.Need to get 23.8 MB of archives.After this operation, 61.2 MB of additional disk space will be used.Do you want to conti
17、nue Y/n? 此时按下 Y【mysql root 密码设为: root】 编辑 mysql 配置文件rootmastersql:# cat /etc/mysql/f|grep bind-addressbind-address = 0.0.0.0 #这样就可以 root 远程连接 mysql 启动 mysql 和查询启动状态 启动 mysqlrootmastersql:# /etc/init.d/mysql start 查看 mysql 的启动情况rootmastersql:# ps aux|grep -v grep | grep mysqlmysql 29437 0.0 0.6 63444
18、8 53868 ? Ssl Aug09 0:13 /usr/sbin/mysqld可以看出 mysql 进程已经启动,且进 pid 号为 29347。接着查看 mysql 的端口号。 查看 mysql 的端口号rootmastersql:# netstat -lnp|grep mysqltcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 29437/mysqldunix 2 ACC STREAM LISTENING 2662765 29437/mysqld /var/run/mysqld/mysqld.sock.从上面的列表可以看出 mysql 的默认端口号已经启动。
19、登陆 mysqlrootmastersql:# mysql -uroot -prootWelcome to the MySQL monitor. Commands end with ; or g.Your MySQL connection id is 39Server version: 5.5.49-0ubuntu0.12.04.1-log (Ubuntu)Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle C
20、orporation 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 databases;+-+| Database |+-+| information_schema | hive | mysql | networkd | performance_schema |+-+5 rows in set (0.01 sec)以上单台 m
21、ysql 测试 OK。此时我只在 mastersql 主机上在线安装了 mysql5.5,其他两个主机 mysqlsql1 和 mysqlsql2 相同 操作安装mysql5.5。Manager 主机不需要安装 mysql。 数据库一致性在主数据库里导出所有的数据库。然后导入到两个从数据库,保证做主从之前数据一致。主数据库之前一直在使用,两个从数据库都是新安装的。在导出数据库之前一定要锁表,或者保证数据库没有使用。rootmastersql:mysqldump -uroot -proot -all-databases -lock-tables=false - /root/all.sql把从主数
22、据中的数据拷贝到两到两台从 mysql,且将该 all.sql 导入到这两个从 mysql 数据库。rootslavesql1: mysql -uroot -proot install plugin rpl_semi_sync_master soname semisync_master.so;mysql set global rpl_semi_sync_master_enabled=1;mysql set global rpl_semi_sync_master_timeout=1000;mysql show global status like rpl%;为了让 mysql 在重启时自动加载该
23、功能,在/etc/mysql/f 加入:rpl_semi_sync_master_enabled=1rpl_semi_sync_master_timeout=1000 备选 master(slavesql2)上:mysql install plugin rpl_semi_sync_master soname semisync_master.so;mysql set global rpl_semi_sync_master_enabled=1;mysql set global rpl_semi_sync_master_timeout=1000;mysql install plugin rpl_se
24、mi_sync_slave soname semisync_slave.so;mysql set global rpl_semi_sync_slave_enabled=1;在/etc/mysql/f 中加入:rpl_semi_sync_master_enabled=1 rpl_semi_sync_master_timeout=1000 rpl_semi_sync_slave_enabled=1 slavesql1 上:mysql install plugin rpl_semi_sync_slave soname semisync_slave.so;mysql set global rpl_se
25、mi_sync_slave_enabled=1;在/etc/mysql/f 中加入:rpl_semi_sync_slave_enabled=1在备用节点和从节点的/etc/mysql/f 中加入选项:read_only=1relay_log_purge=0 配置 mysql 主从 在 master 上rootmastersql:# cat /etc/mysql/f|grep log_bin#log_bin = /var/log/mysql/mysql-bin.loglog_bin = realcloud修改 mysql 的主的 log_bin 日志名字,用于主宕机以后从备主机接管后从同步的 l
26、og_bin。名字不通用于区别。此时,mysql 的 logbin 的位置路径是路径:/var/lib/mysql,而不再是/var/log/mysql。.mysql grant replication slave on *.* to realcloud192.168.62.% identified by realcloud;mysql show master status;+-+-+-+-+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+-+-+-+-+| realcloud.000010 | 107 | | |+-+-+-+-+1
27、 row in set (0.00 sec)记录下 “File”和“Position” 即当前主库使用的二进制日志名称和位置。 在备选 master(mysqlsql2 )和 slave(mysqlsql1)上mysql slave stop;mysql change master to master_host=“192.168.62.42“,master_user=“realcloud“,master_password=“realcloud“,master_log_file=“realcloud.000007“,master_log_pos=107;master_log_file 和 mas
28、ter_log_pos 是上面记下的东西。 在备选 master(slavesql2)上,授权其网段内其他机器登陆的账号和密码mysql grant replication slave on *.* to repl192.168.62.% identified by repl; 然后在备选 master(slavesql2)和 slave(slavesql1)上mysqlslave start;mysql show slave statusG | egrep Slave_IO|Slave_SQLSlave_IO_State: Waiting for master to send eventSl
29、ave_IO_Running: YesSlave_SQL_Running: Yes# 如果 Slave_IO_Running: Yes 和 Slave_SQL_Running: Yes 则说明主从配置成功# 还可以到 master 上执行 Mysqlshow global status like “rpl%“; mysql show global status like “rpl%“;+-+-+| Variable_name | Value |+-+-+| Rpl_semi_sync_master_clients | 2 | Rpl_semi_sync_master_net_avg_wait_
30、time | 0 | Rpl_semi_sync_master_net_wait_time | 0 | Rpl_semi_sync_master_net_waits | 0 | Rpl_semi_sync_master_no_times | 0 | Rpl_semi_sync_master_no_tx | 0 | Rpl_semi_sync_master_status | ON | Rpl_semi_sync_master_timefunc_failures | 0 | Rpl_semi_sync_master_tx_avg_wait_time | 0 | Rpl_semi_sync_mast
31、er_tx_wait_time | 0 | Rpl_semi_sync_master_tx_waits | 0 | Rpl_semi_sync_master_wait_pos_backtraverse | 0 | Rpl_semi_sync_master_wait_sessions | 0 |.| Rpl_semi_sync_master_yes_tx | 0 | Rpl_semi_sync_slave_status | OFF | Rpl_status | AUTH_MASTER |+-+-+如果 Rpl_semi_sync_master_clients 是 2.说明半同步复制正常在做主从的
32、时候,我设置了两组账号和密码,分别如下,列其作用1accout:root password:root 用于登陆远程 mysql 账号和密码2accout:realcloud password:realcloud 用于主从复制的时候,主开发给从的账号和密码3 accout:repl password:replManager 管理时候使用的账号和密码 测试 mysql 主从 主 mysql 创建数据库rootmastersql:# mysql -uroot -proot -e “create database ReadlCloudDatabase;“rootmastersql:# mysql -u
33、root -proot -e “show databases;“+-+| Database |+-+| information_schema | ReadlCloudDatabase | hive | mysql | networkd | performance_schema |+-+主 mysql 创建一个新的数据库为 ReadlCloudDatabase。 被主 mysql 查看同步情况rootslavesql2:# mysql -uroot -proot -e “show databases;“+-+| Database |+-+| information_schema | ReadlC
34、loudDatabase | hive | mysql | networkd | performance_schema |+-+. 从 mysql 查看同步情况rootslavesql1:# mysql -uroot -proot -e “show databases;“+-+| Database |+-+| information_schema | ReadlCloudDatabase | hive | mysql | networkd | performance_schema |+-+结合之前的 status 状态和 mysql 主从同步操作查看可见测试 mysql 主从成功。安装 MHA
35、部署 MHA 安装 MHA Node 先在 4 台机器上安装 MHA Node:rootmanager:# apt-get install libdbd-mysql-perlrootmanager:# dpkg -i mha4mysql-node_0.54-0_all.debrootmanager:# ll /usr/local/bintotal 40-r-xr-xr-x 1 root root 15498 Aug 10 14:05 apply_diff_relay_logs-r-xr-xr-x 1 root root 4807 Aug 10 14:05 filter_mysqlbinlog-
36、r-xr-xr-x 1 root root 7401 Aug 10 14:05 purge_relay_logs-r-xr-xr-x 1 root root 7263 Aug 10 14:05 save_binary_logs其他三台机器按照此方式一样安装。 安装 MHA manager 在 manager 上安装 MHA Manager:rootmanager:# apt-get install libdbd-mysql-perl.rootmanager:# apt-get install libconfig-tiny-perlrootmanager:# apt-get install li
37、blog-dispatch-perlrootmanager:# apt-get install libparallel-forkmanager-perlrootmanager:# mha4mysql-manager-0.56.tar.gzrootmanager:# cd mha4mysql-manager-0.56rootmanager:/mha4mysql-manager-0.56# perl Makefilerootmanager:/mha4mysql-manager-0.56# make use warnings FATAL = all;use Getopt:Long;my ($comm
38、and, $ssh_user, $orig_master_host, $orig_master_ip,$orig_master_port, $new_master_host, $new_master_ip, $new_master_port);my $vip = 192.168.62.200; # Virtual IPmy $key = “1“;my $ssh_start_vip = “/sbin/ifconfig eth0:$key $vip“;my $ssh_stop_vip = “/sbin/ifconfig eth0:$key down“;GetOptions(command=s =
39、$command,ssh_user=s = $ssh_user,orig_master_host=s = $orig_master_host,orig_master_ip=s = $orig_master_ip,orig_master_port=i = $orig_master_port,new_master_host=s = $new_master_host,new_master_ip=s = $new_master_ip,new_master_port=i = $new_master_port,);exit sub main print “nnIN SCRIPT TEST=$ssh_sto
40、p_vip=$ssh_start_vip=nn“;if ( $command eq “stop“ | $command eq “stopssh“ ) # $orig_master_host, $orig_master_ip, $orig_master_port are passed.# If you manage master ip address at global catalog database,# invalidate orig_master_ip here.my $exit_code = 1;eval print “Disabling the VIP on old master: $
41、orig_master_host n“;$exit_code = 0;if ($) .warn “Got Error: $n“;exit $exit_code;exit $exit_code;elsif ( $command eq “start“ ) # all arguments are passed.# If you manage master ip address at global catalog database,# activate new_master_ip here.# You can also grant write access (create user, set read
42、_only=0, etc) here.my $exit_code = 10;eval print “Enabling the VIP - $vip on the new master - $new_master_host n“;$exit_code = 0;if ($) warn $;exit $exit_code;exit $exit_code;elsif ( $command eq “status“ ) print “Checking the Status of the script OK n“;ssh $ssh_usercluster1 “ $ssh_start_vip “;exit 0
43、;else exit 1;# A simple system call that enable the VIP on the new mastersub start_vip() ssh $ssh_user$new_master_host “ $ssh_start_vip “;# A simple system call that disable the VIP on the old_mastersub stop_vip() ssh $ssh_user$orig_master_host “ $ssh_stop_vip “;sub usage print“Usage: master_ip_fail
44、over -command=start|stop|stopssh|status -orig_master_host=host -orig_master_ip=ip -orig_master_port=port -new_master_host=host -new_master_ip=ip -new_master_port=portn“;将此文档复制两次到/usr/local/bin, 分别命名为 master_ip_failover 和 master_ip_online_change_script然后将 manager 主机中的/etc/f 中下面两行注释去掉(使用该脚本),注意添加可执行权限。master_ip_failover_script=/usr/local/bin/master_ip_failovermaster_ip_online_change_script=/usr/local/bin/master_ip_online_change_script.测试 MHA 停止主 mysql在主 mysql 的主机上行停止 mysql 服务。rootmastersql:# /etc/init.d/mysql stop 查看从 mysql 情况rootslavesql2:# mysql -ur