配置binlog-server
修改mha配置文件
[root@mysql-db03 ~]# vim /etc/mha/app1.cnf
[binlog1]
no_master=1
hostname=10.0.0.53
master_binlog_dir=/data/mysql/binlog/
备份binlog
#创建备份binlog目录
[root@mysql-db03 ~]# mkdir -p /data/mysql/binlog/
#进入该目录
[root@mysql-db03 ~]# cd /data/mysql/binlog/
#备份binlog
[root@mysql-db03 binlog]# mysqlbinlog -R --host=10.0.0.55 --user=mha --password=123 --raw --stop-never mysql-bin.000001 &
#启动mha
[root@mysql-db03 binlog]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /etc/mha/manager.log 2>&1 &
测试binlog备份
#查看binlog目录中的binlog
[root@mysql-db03 binlog]# ll
total 44
-rw-r--r-- 1 root root 285 Mar 8 03:11 mysql-bin.000001
#登录主库
[root@mysql-db01 ~]# mysql -uroot -p123
#刷新binlog
mysql> flush logs;
#再次查看binlog目录
[root@mysql-db03 binlog]# ll
total 48
-rw-r--r-- 1 root root 285 Mar 8 03:11 mysql-bin.000001
-rw-r--r-- 1 root root 143 Mar 8 04:00 mysql-bin.000002
MySQL中间件Atlas
Atlas简介
Atlas是由 Qihoo 360公司Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目。它在MySQL官方推出的MySQL-Proxy 0.8.2版本的基础上,修改了大量bug,添加了很多功能特性。
Atlas主要功能
- 1.读写分离
- 2.从库负载均衡
- 3.IP过滤
- 4.自动分表
- 5.DBA可平滑上下线DB
- 6.自动摘除宕机的DB
Atlas相对于官方MySQL-Proxy的优势
- 1.将主流程中所有Lua代码用C重写,Lua仅用于管理接口
- 2.重写网络模型、线程模型
- 3.实现了真正意义上的连接池
- 4.优化了锁机制,性能提高数十倍
安装Atlas
同学们有福了,安装Atlas真的是炒鸡简单,官方提供的Atlas有两种:
1)Atlas (普通) : Atlas-2.2.1.el6.x86_64.rpm
2)Atlas (分表) : Atlas-sharding_1.0.1-el6.x86_64.rpm
这里我们只需要下载普通的即可。
#在主库安装,进入安装包目录
[root@mysql-db01 ~]# cd /home/oldboy/tools/
#下载Atlas
[root@mysql-db01 tools]#
wget httpss://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm
#安装
[root@mysql-db01 tools]# rpm -ivh Atlas-2.2.1.el6.x86_64.rpm
Preparing... ########################################### [100%]
1:Atlas ########################################### [100%]
编辑配置文件
#进入Atlas工具目录
[root@db04 ~]# cd /usr/local/mysql-proxy/
total 0
drwxr-xr-x 2 root root 75 Nov 21 18:43 bin
drwxr-xr-x 2 root root 22 Nov 21 18:43 conf
drwxr-xr-x 3 root root 331 Nov 21 18:43 lib
drwxr-xr-x 2 root root 6 Dec 17 2014 log
[root@mysql-db01 ~]# cd /usr/local/mysql-proxy/bin/
#生成密码
[root@mysql-db01 bin]# ./encrypt oldboy123
#修改Atlas配置文件
[root@mysql-db01 ~]# vim /usr/local/mysql-proxy/conf/test.cnf
#Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔
proxy-backend-addresses = 10.0.0.51:3306
#Atlas后端连接的MySQL从库的IP和端口
proxy-read-only-backend-addresses = 10.0.0.52:3306,10.0.0.53:3306
#用户名与其对应的加密过的MySQL密码
pwds = root:1N/CNLSgqXuTZ6zxvGQr9A==
#SQL日志的开关
sql-log = ON
#Atlas监听的工作接口IP和端口
proxy-address = 0.0.0.0:3307
#默认字符集,设置该项后客户端不再需要执行SET NAMES语句
charset = utf8
#管理接口的用户名
admin-username = user
#管理接口的密码
admin-password = pwd
#Atlas监听的管理接口IP和端口
admin-address = 0.0.0.0:2345
#client-ips = 127.0.0.1, 192.168.1
启动Atlas
[root@mysql-db01 ~]# /usr/local/mysql-proxy/bin/mysql-proxyd test start
OK: MySQL-Proxy of test is started
Atlas管理操作
#用atlas管理用户登录
[root@mysql-db01 ~]# mysql -uuser -ppwd -h127.0.0.1 -P2345
#查看可用命令帮助
mysql> select * from help;
#查看后端代理的库
mysql> SELECT * FROM backends;
+-------------+----------------+-------+------+
| backend_ndx | address | state | type |
+-------------+----------------+-------+------+
| 1 | 10.0.0.51:3307 | up | rw |
| 2 | 10.0.0.53:3307 | up | ro |
| 3 | 10.0.0.52:3307 | up | ro |
+-------------+----------------+-------+------+
#平滑摘除mysql
mysql> REMOVE BACKEND 2;
Empty set (0.00 sec)
#检查是否摘除成功
mysql> SELECT * FROM backends;
+-------------+----------------+-------+------+
| backend_ndx | address | state | type |
+-------------+----------------+-------+------+
| 1 | 10.0.0.51:3307 | up | rw |
| 2 | 10.0.0.52:3307 | up | ro |
+-------------+----------------+-------+------+
#保存到配置文件中
mysql> SAVE CONFIG;
Empty set (0.06 sec)
Atlas管理接口
[root@db04 conf]# mysql -uuser -ppwd -h127.0.0.1 -P2345
mysql> SELECT * FROM help;
+----------------------------+---------------------------------------------------------+
| 命令 | 描述 |
---------------------------------------------------------------------------------------
| SELECT * FROM help | 查看帮助 |
| SELECT * FROM backends | 查看后端的服务器状态 |
| SET OFFLINE $backend_id | 平滑下线 例如:set offline 2; |
| SET ONLINE $backend_id | 平滑上线 例如:set online 2; |
| ADD MASTER $backend | 添加后端主库:add master 10.0.0.55:3306; |
| ADD SLAVE $backend | 添加后端从库:add slave 10.0.0.56:3306 |
| REMOVE BACKEND $backend_id | 删除后端节点: remove backend 1; |
| SELECT * FROM clients | 查看允许连接的客户端IP |
| ADD CLIENT $client | 添加客户端IP:add client 10.0.0.51 |
| REMOVE CLIENT $client | 删除客户端IP:REMOVE CLIENT 10.0.0.51; |
| SELECT * FROM pwds | 查看后端数据库的用户名和密码 |
| ADD PWD $pwd | 添加用户,自动加密例如:add pwd root:123 |
| ADD ENPWD $pwd | 添加用户,需要手写加密后的密码:add enpwd ljk:3yb5jEku5h4=;|
| REMOVE PWD $pwd | 删除没有用的用户:remove pwd ljk; |
| SAVE CONFIG | 保存到配置文件 |
| SELECT VERSION | 查看版本 |
+----------------------------+---------------------------------------------------------+
使用
mysql> select * from backends;
+-------------+----------------+-------+------+
| backend_ndx | address | state | type |
+-------------+----------------+-------+------+
| 1 | 10.0.0.55:3306 | up | rw |
| 2 | 10.0.0.53:3306 | up | ro |
| 3 | 10.0.0.51:3306 | up | ro |
+-------------+----------------+-------+------+
3 rows in set (0.00 sec)
mysql> add master 10.0.0.54:3306;
Empty set (0.00 sec)
mysql> select * from backends;
+-------------+----------------+-------+------+
| backend_ndx | address | state | type |
+-------------+----------------+-------+------+
| 1 | 10.0.0.55:3306 | up | rw |
| 2 | 10.0.0.54:3306 | up | rw |
| 3 | 10.0.0.51:3306 | up | ro |
| 4 | 10.0.0.53:3306 | up | ro |
+-------------+----------------+-------+------+
4 rows in set (0.00 sec)
mysql> add slave 10.0.0.52:3306;
Empty set (0.00 sec)
mysql> select * from backends;
+-------------+----------------+-------+------+
| backend_ndx | address | state | type |
+-------------+----------------+-------+------+
| 1 | 10.0.0.55:3306 | up | rw |
| 2 | 10.0.0.54:3306 | up | rw |
| 3 | 10.0.0.51:3306 | up | ro |
| 4 | 10.0.0.53:3306 | up | ro |
| 5 | 10.0.0.52:3306 | up | ro |
+-------------+----------------+-------+------+
5 rows in set (0.00 sec)
脚本
[root@db03 ~]# vim atlas.sh
#!/bin/bash
down_master=`sed -rn 's#^Master (.*)(.*down!$#1#gp' /etc/mha/manager.log`
new_master=`sed -rn 's#^Selected (.*)(.*master.$#1#gp' /etc/mha/manager.log`
new_master_id=`mysql -uuser -ppwd -h127.0.0.1 -P2345 -e 'select * from backends'|grep $new_master|awk '{print $1}'`
#删除被提升为主库的从库
mysql -uuser -ppwd -h127.0.0.1 -P2345 -e 'remove backend $new_master_id;save config;'
#将down掉的master变成从库
mysql -uuser -ppwd -h127.0.0.1 -P2345 -e 'add slave ${down_master}:3306;save config;'
/usr/local/mysql-proxy/bin/mysql-proxyd test restart
配置文件
[root@mysql-db01 ~]# vim /usr/local/mysql-proxy/conf/test.cnf