6. 应用透明---VIP
vip : 10.0.0.55/24
6.1 vip 故障转移脚本
上传mha_script.tar文件到/usr/local/bin 解压
6.2 修改权限
[root@db03 bin]# chmod +x /usr/local/bin/*
=============================================
插曲:将脚本修改成这个版本得因为脚本里有中文#
[root@db03 bin]yum install -y dos2unix #
[root@db03 bin]dos2unix * #
=============================================
6.3 修改内容
[root@db03 bin]# cp master_ip_failover master_ip_failover.bak
my $vip = '10.0.0.55/24';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
my $ssh_Bcast_arp= "/sbin/arping -I eth0 -c 3 -A 10.0.0.55";
6.4 修改Manager 配置文件
vim /etc/mha/app1.cnf
master_ip_failover_script=/usr/local/bin/master_ip_failover
6.5 重启MHA
[root@db03 bin]# masterha_stop --conf=/etc/mha/app1.cnf
[root@db03 bin]# 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.6 手工在主库添加VIP
[root@db02 ~]# ifconfig eth0:1 10.0.0.55/24
6.7 效果测试
使用navicat 连接测试MHA vip功能。
7. 故障提醒功能
7.1 准备脚本
[root@db03 bin]# cp send_report send_report.bak1
my $smtp='smtp.qq.com'; # smtp服务器
my $mail_from='[email protected]'; # 发件箱
my $mail_user='22654481'; # 用户名 QQ号
my $mail_pass='gemghsvgkeyzcagh'; # 授权码
my $mail_to=['[email protected]']; # 收件箱
#my $mail_to=['[email protected]','[email protected]'];
7.2 修改配置文件
vim /etc/mha/app1.cnf
# 添加一行:
report_script=/usr/local/bin/send_report
7.3 重启MHA
[root@db03 bin]# masterha_stop --conf=/etc/mha/app1.cnf
[root@db03 bin]# 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 &
7.4 模拟主库宕机
7.4.1 确认主库
[root@db03 bin]# masterha_check_status --conf=/etc/mha/app1.cnf
app1 (pid:27096) is running(0:PING_OK), master:10.0.0.52
7.4.2 宕主库
[root@db02 ~]# /etc/init.d/mysqld stop
Shutting down MySQL............ SUCCESS!
7.4.3 观察 vip 漂移
7.4.4 观察 邮件
7.5 修复MHA 架构1主2从
略
8. 日志补偿的冗余方案--binlog_server
8.1 创建必要目录(db03)
mkdir -p /data/binlog_server/
chown -R mysql.mysql /data/*
cd /data/binlog_server/
[root@db03 ~]# mysql -e "show slave status \G"|grep "Master_Log"
Master_Log_File: mysql-bin.000008
Read_Master_Log_Pos: 194
Relay_Master_Log_File: mysql-bin.000008
Exec_Master_Log_Pos: 194
[root@db03 ~]#
[root@db03 binlog_server]# masterha_check_status --conf=/etc/mha/app1.cnf
app1 (pid:7057) is running(0:PING_OK), master:10.0.0.52
mysqlbinlog -R --host=10.0.0.52 --user=mha --password=mha --raw --stop-never mysql-bin.000004 &
注意:
拉取日志的起点,需要按照目前从库的已经获取到的二进制日志点为起点
8.2 配置文件设置
vim /etc/mha/app1.cnf
[binlog1]
no_master=1
hostname=10.0.0.53
master_binlog_dir=/data/binlog_server/
8.3 重启MHA
[root@db03 bin]# masterha_stop --conf=/etc/mha/app1.cnf
[root@db03 bin]# 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 &
8.4 故障演练及修复
额外修复binlog server
[root@db03 binlog_server]# cd /data/binlog_server/
[root@db03 binlog_server]#
[root@db03 binlog_server]# ll
total 4
-rw-r----- 1 root root 485 Jul 15 12:09 mysql-bin.000004
[root@db03 binlog_server]# rm -rf *
[root@db03 binlog_server]# mysql -e "show slave status \G"|grep "Master_Log"
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 234
Relay_Master_Log_File: mysql-bin.000004
Exec_Master_Log_Pos: 234
[root@db03 binlog_server]# mysqlbinlog -R --host=10.0.0.51 --user=mha --password=mha --raw --stop-never mysql-bin.000004 &
[root@db03 binlog_server]# 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 &
[2] 8375
[root@db03 binlog_server]# masterha_check_status --conf=/etc/mha/app1.cnf
app1 (pid:8375) is running(0:PING_OK), master:10.0.0.51
9. MHA的维护操作 - 在线切换功能
9.1 只切换角色
masterha_master_switch --conf=/etc/mha/app1.cnf --master_state=alive --new_master_host=10.0.0.52 --orig_master_is_new_slave --running_updates_limit=10000
注意:
master_ip_online_change_script is not defined. If you do not disable writes on the current master manually, applications keep writing on the current master. Is it ok to proceed? (yes/NO): yes
1. 此种方法 切换,要注意将原主库,FTWRL,否则会造成主从不一致。
2. 手工切换vip
3. 重新拉去新主库的binlog
4. 发邮件功能
9.2 master_ip_online_change_script功能实现
功能: 在线切换时,自动锁原主库,VIP自动切换
9.2.1 准备切换脚本
vim /usr/local/bin/master_ip_online_change
my $vip = "10.0.0.55/24";
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key $vip down";
my $ssh_Bcast_arp= "/sbin/arping -I ens33 -c 3 -A 10.0.0.55";
9.2.2 修改MHA配置文件
vim /etc/mha/app1.cnf
master_ip_online_change_script=/usr/local/bin/master_ip_online_change
9.2.3 停 MHA
[root@db03 bin]# masterha_stop --conf=/etc/mha/app1.cnf
9.2.4 检查repl
[root@db03 bin]# masterha_check_repl --conf=/etc/mha/app1.cnf
9.2.5 在线切换
masterha_master_switch --conf=/etc/mha/app1.cnf --master_state=alive --new_master_host=10.0.0.51 --orig_master_is_new_slave --running_updates_limit=10000
9.2.6 重构binlogserver
[root@db03 bin]# ps -ef |grep mysqlbinlog
root 28144 16272 0 17:50 pts/1 00:00:00 mysqlbinlog -R --host=10.0.0.52 --user=mha --password=x x --raw --stop-never mysql-bin.000005
root 28529 16272 0 18:03 pts/1 00:00:00 grep --color=auto mysqlbinlog
[root@db03 bin]# kill -9 28144
[root@db03 bin]# cd /data/binlog_server/
[root@db03 binlog_server]# ll
total 4
-rw-r----- 1 root root 194 Apr 1 17:50 mysql-bin.000005
[root@db03 binlog_server]# rm -rf *
[root@db03 binlog_server]# mysqlbinlog -R --host=10.0.0.51 --user=mha --password=mha --raw --stop-never mysql-bin.000009 &
[1] 28534
9.2.7 启动MHA
[root@db03 bin]# 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 &
[root@db03 binlog_server]# masterha_check_status --conf=/etc/mha/app1.cnf
app1 (pid:28535) is running(0:PING_OK), master:10.0.0.51
================================
Atlas 读写分离技术
1. 介绍
Atlas是由 Qihoo 360, Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目。
它是在mysql-proxy 0.8.2版本的基础上,对其进行了优化,增加了一些新的功能特性。
360内部使用Atlas运行的mysql业务,每天承载的读写请求数达几十亿条。
下载地址
https://github.com/Qihoo360/Atlas/releases
2.安装配置
yum install -y Atlas*
cd /usr/local/mysql-proxy/conf
mv test.cnf test.cnf.bak
vi test.cnf
[mysql-proxy]
admin-username = user
admin-password = pwd
proxy-backend-addresses = 10.0.0.55:3306
proxy-read-only-backend-addresses = 10.0.0.52:3306,10.0.0.53:3306
pwds = repl:3yb5jEku5h4=,mha:O2jBXONX098=
daemon = true
keepalive = true
event-threads = 8
log-level = message
log-path = /usr/local/mysql-proxy/log
sql-log=ON
proxy-address = 0.0.0.0:33060
admin-address = 0.0.0.0:2345
charset=utf8
启动atlas
/usr/local/mysql-proxy/bin/mysql-proxyd test start
ps -ef |grep proxy
3. Atlas功能测试
测试读操作:
mysql -umha -pmha -h 10.0.0.53 -P 33060
db03 [(none)]>select @@server_id;
测试写操作:
mysql> begin;select @@server_id;commit;
注意:
DDL建议不要再Atlas触发,最好是到主库触发(Online DDL或者PT-OSC)。
DML建议begin; DML; commit;
4. Atlas 的管理操作
[root@db03 conf]# mysql -uuser -ppwd -h 10.0.0.53 -P2345
db03 [(none)]>select * from help;
4.1 查看所有节点
db03 [(none)]>SELECT * FROM backends;
+-------------+----------------+-------+------+
| backend_ndx | address | state | type |
+-------------+----------------+-------+------+
| 1 | 10.0.0.55:3306 | up | rw |
| 2 | 10.0.0.52:3306 | up | ro |
| 3 | 10.0.0.53:3306 | up | ro |
+-------------+----------------+-------+------+
3 rows in set (0.00 sec)
4.2 节点的上线和下线
db03 [(none)]>SET OFFLINE 1;
+-------------+----------------+---------+------+
| backend_ndx | address | state | type |
+-------------+----------------+---------+------+
| 1 | 10.0.0.55:3306 | offline | rw |
+-------------+----------------+---------+------+
1 row in set (0.01 sec)
db03 [(none)]>SELECT * FROM backends;
+-------------+----------------+---------+------+
| backend_ndx | address | state | type |
+-------------+----------------+---------+------+
| 1 | 10.0.0.55:3306 | offline | rw |
| 2 | 10.0.0.52:3306 | up | ro |
| 3 | 10.0.0.53:3306 | up | ro |
+-------------+----------------+---------+------+
db03 [(none)]>SET ONLINE 1;
+-------------+----------------+---------+------+
| backend_ndx | address | state | type |
+-------------+----------------+---------+------+
| 1 | 10.0.0.55:3306 | unknown | rw |
+-------------+----------------+---------+------+
4.3 删除和添加节点
db03 [(none)]>REMOVE BACKEND 3;
db03 [(none)]>ADD SLAVE 10.0.0.53:3306;
4.4 用户管理
db01 [(none)]>grant all on *.* to oldguo@'10.0.0.%' identified by '123';
db03 [(none)]>SELECT * FROM pwds;
db03 [(none)]>add pwd oldguo:123;
4.5 持久化配置文件
db03 [(none)]>save config;