1. 程式人生 > 實用技巧 >MySQL-Atlas--讀寫分離架構

MySQL-Atlas--讀寫分離架構

一、Atlas簡介

Atlas是由 Qihoo 360公司Web平臺部基礎架構團隊開發維護的一個基於MySQL協議的資料中間層專案。它在MySQL官方推出的MySQL-Proxy 0.8.2版本的基礎上,修改了大量bug,添加了很多功能特性。目前該專案在360公司內部得到了廣泛應用,很多MySQL業務已經接入了Atlas平臺,每天承載的讀寫請求數達幾十億條。同時,有超過50家公司在生產環境中部署了Atlas,超過800人已加入了我們的開發者交流群,並且這些數字還在不斷增加。

mysql-proxy是一個輕量級的二進位制應用程式,位於一個或多個MySQL客戶端與伺服器之間。客戶端使用通常的憑據連線到代理,而不是連線到伺服器。代理充當客戶端和伺服器之間的中間人。
在其基本形式中,代理只是重定向器。它從客戶端獲取一個空儲存桶(一個查詢),將其帶到伺服器,用資料填充儲存桶,然後將其傳遞迴客戶端。

主要功能:

  • 讀寫分離
  • 從庫負載均衡
  • IP過濾
  • 自動分表
  • DBA可平滑上下線DB
  • 自動摘除宕機的DB


二、安裝配置

2.1 atlas下載

  • 下載地址:https://github.com/Qihoo360/Atlas/releases
  • 本次使用版本:Atlas-2.2.1.el6.x86_64.rpm
    注意:
  • 1、Atlas只能安裝執行在64位的系統上
  • 2、Centos 5.X安裝 Atlas-XX.el5.x86_64.rpm,Centos6.X安裝Atlas-XX.el6.x86_64.rpm。
  • 3、後端mysql版本應大於5.1,建議使用Mysql 5.6以上
  • 4、8.0以上版本建議使用MySQL官方MySQL-Router

2.2 atlas安裝及配置

yum localinstall -y Atlas-2.2.1.el6.x86_64.rpm 
cd /usr/local/mysql-proxy/conf/
mv test.cnf test.cnf.default

vi test.cnf
[mysql-proxy]
admin-username = user
admin-password = pwd
proxy-backend-addresses = 192.168.80.54:3307
proxy-read-only-backend-addresses = 192.168.80.54:3308,192.168.80.54:3309
pwds = root:/iZxz+0GRoA=
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=utf8mb4

2.3 atlas啟動

/usr/local/mysql-proxy/bin/mysql-proxyd test start
ps -ef | grep mysql-proxy

2.4 atlas讀寫功能測試

登陸:
mysql -uroot -p123456 -h192.168.80.54 -P33060

測試讀操作:
select @@server_id;

測試寫操作:
begin;select @@server_id;commit;

2.5 新增一個使用者操作

建立一個dev使用者(select  update  insert delete)密碼123456,要通過80網段登入
1. 在主庫中,先要建立使用者
mysql -uroot -p -h192.168.80.54 -P3307
grant select, update, insert, delete on *.* to dev@'192.168.80.%' identified by '123456';

2. 在atlas中新增生產使用者
/usr/local/mysql-proxy/bin/encrypt  123456      ---->製作加密密碼

vi test.cnf
pwds = root:/iZxz+0GRoA=,dev:/iZxz+0GRoA=       ---->新增使用者密碼

/usr/local/mysql-proxy/bin/mysql-proxyd test restart    ---->重啟atlas

mysql -udev -p123456  -h192.168.80.54 -P33060       ---->連線

三、Atlas基本管理

3.1 連線管理介面

[root@db03 ~]# mysql -uuser -ppwd -h127.0.0.1 -P2345
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.99-agent-admin

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

3.2 列印幫助

mysql> select * from help;
+----------------------------+---------------------------------------------------------+
| command                    | description                                             |
+----------------------------+---------------------------------------------------------+
| SELECT * FROM help         | shows this help                                         |
| SELECT * FROM backends     | lists the backends and their state                      |
| SET OFFLINE $backend_id    | offline backend server, $backend_id is backend_ndx's id |
| SET ONLINE $backend_id     | online backend server, ...                              |
| ADD MASTER $backend        | example: "add master 127.0.0.1:3306", ...               |
| ADD SLAVE $backend         | example: "add slave 127.0.0.1:3306", ...                |
| REMOVE BACKEND $backend_id | example: "remove backend 1", ...                        |
| SELECT * FROM clients      | lists the clients                                       |
| ADD CLIENT $client         | example: "add client 192.168.1.2", ...                  |
| REMOVE CLIENT $client      | example: "remove client 192.168.1.2", ...               |
| SELECT * FROM pwds         | lists the pwds                                          |
| ADD PWD $pwd               | example: "add pwd user:raw_password", ...               |
| ADD ENPWD $pwd             | example: "add enpwd user:encrypted_password", ...       |
| REMOVE PWD $pwd            | example: "remove pwd user", ...                         |
| SAVE CONFIG                | save the backends to config file                        |
| SELECT VERSION             | display the version of Atlas                            |
+----------------------------+---------------------------------------------------------+
16 rows in set (0.00 sec)

3.3 查詢後端mysql節點狀態

mysql> select * from backends;
+-------------+--------------------+-------+------+
| backend_ndx | address            | state | type |
+-------------+--------------------+-------+------+
|           1 | 192.168.80.54:3307 | up    | rw   |
|           2 | 192.168.80.54:3308 | up    | ro   |
|           3 | 192.168.80.54:3309 | up    | ro   |
+-------------+--------------------+-------+------+
4 rows in set (0.00 sec)

3.4 動態刪除節點

mysql> remove backend 3;
Empty set (0.00 sec)

3.5 動態新增節點

mysql> add slave 192.168.80.54:3309;
Empty set (0.00 sec)

3.6 儲存修改到配置檔案

mysql> save config;
Empty set (0.01 sec)

四、其它讀寫分離建議

MySQL-Router    ---> MySQL官方
ProxySQL         --->Percona公司開發
Maxscale         ---> MariaDB開發