1. 程式人生 > 資料庫 >MySQL Atlas 讀寫分離

MySQL Atlas 讀寫分離

image

image

1. Atlas介紹

 Atlas是由 Qihoo 360, Web平臺部基礎架構團隊開發維護的一個基於MySQL協議的資料中間層專案。
它是在mysql-proxy 0.8.2版本的基礎上,對其進行了優化,增加了一些新的功能特性。
360內部使用Atlas執行的mysql業務,每天承載的讀寫請求數達幾十億條。
下載地址
https://github.com/Qihoo360/Atlas/releases
注意:
1、Atlas只能安裝執行在64位的系統上
2、Centos 5.X安裝 Atlas-XX.el5.x86_64.rpm,Centos 6.X安裝Atlas-XX.el6.x86_64.rpm。
3、後端mysql版本應大於5.1,建議使用Mysql 5.6以上

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;

4 生產使用者要求

開發人員申請一個應用使用者 app(  select  update  insert)  密碼123456,要通過10網段登入
1. 在主庫中,建立使用者
grant select ,update,insert on *.* to app@'10.0.0.%' identified by '123456';
# 所有節點檢視
db01 [(none)]>select user,host from mysql.user;
2. 在atlas中新增生產使用者
/usr/local/mysql-proxy/bin/encrypt  123456      ---->製作加密密碼
3. 改配置檔案
vim test.cnf
pwds = repl:3yb5jEku5h4=,mha:O2jBXONX098=,app:/iZxz+0GRoA=
/usr/local/mysql-proxy/bin/mysql-proxyd test restart
[root@db03 conf]# mysql -uapp -p123456  -h 10.0.0.53 -P 33060

5 Atlas基本管理

連線管理介面
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.55:3306 | up    | rw   |
|           2 | 10.0.0.51:3306 | up    | ro   |
|           3 | 10.0.0.53:3306 | up    | ro   |
+-------------+----------------+-------+------+
3 rows in set (0.00 sec)
動態新增刪除節點
REMOVE BACKEND 3;
動態新增節點
ADD SLAVE 10.0.0.53:3306;
儲存配置到配置檔案
SAVE CONFIG;
6. 自動分表
介紹
使用Atlas的分表功能時,首先需要在配置檔案test.cnf設定tables引數。
tables引數設定格式:資料庫名.表名.分表字段.子表數量,
比如:
你的資料庫名叫school,表名叫stu,分表字段叫id,總共分為2張表,那麼就寫為school.stu.id.2,如果還有其他的分表,以逗號分隔即可。
7. 關於讀寫分離建議

MySQL-Router    ---> MySQL官方
ProxySQL         --->Percona
Maxscale         ---> MariaDB