1. 程式人生 > 其它 >MySQL中介軟體Atlas

MySQL中介軟體Atlas

目錄

MySQL中介軟體Atlas

Mysql 的 proxy 中介軟體有比較多的工具,例如,mysql-proxy(官方提供), atlas , cobar, mycat, tddl, tinnydbrouter等等。而Atlas是由 Qihoo 360公司Web平臺部基礎架構團隊開發維護的一個基於MySQL協議的資料中間層專案。它在MySQL官方推出的MySQL-Proxy 0.8.2版本的基礎上,修改了大量bug,添加了很多功能特性。目前該專案在360公司內部得到了廣泛應用,很多MySQL業務已經接入了Atlas平臺,每天承載的讀寫請求數達幾十億條。

同時,有超過50家公司在生產環境中部署了Atlas,超過800人已加入了我們的開發者交流群,並且這些數字還在不斷增加。而且安裝方便。配置的註釋寫的蠻詳細的,都是中文。

Atlas官方連結: https://github.com/Qihoo360/Atlas/blob/master/README_ZH.md

Atlas下載連結: https://github.com/Qihoo360/Atlas/releases

主要功能

Atlas主要功能(代理)

​ 1.讀寫分離

​ 2.從庫負載均衡

​ 3.IP過濾

​ 4.自動分表

​ 5.DBA可平滑上下線DB(不影響使用者的體驗,把你的資料庫下線)

​ 6.自動摘除宕機的DB

Atlas相對於官方MySQL-Proxy的優勢

​ 1.將主流程中所有Lua程式碼用C重寫,Lua僅用於管理介面

​ 2.重寫網路模型、執行緒模型

​ 3.實現了真正意義上的連線池

​ 4.優化了鎖機制,效能提高數十倍

使用場景

Atlas是一個位於前端應用與後端MySQL資料庫之間的中介軟體,在後端DB看來,Atlas相當於連線它的客戶端,在前端應用看來,Atlas相當於一個DB。Atlas作為服務端與應用程式通訊,它實現了MySQL的客戶端和服務端協議,同時作為客戶端與MySQL通訊。它對應用程式遮蔽了DB的細節,同時為了降低MySQL負擔,它還維護了連線池。

Atlas使得應用程式設計師無需再關心讀寫分離、分表等與MySQL相關的細節,可以專注於編寫業務邏輯,同時使得DBA的運維工作對前端應用透明,上下線DB前端應用無感知。

企業讀寫分離及分庫分表其他方案瞭解

  • Mysql-proxy(oracle)

  • Mysql-router(oracle)

  • Atlas (Qihoo 360)

  • Atlas-sharding (Qihoo 360)

  • Cobar(是阿里巴巴(B2B)部門開發)

  • Mycat(基於阿里開源的Cobar產品而研發)

  • TDDL Smart Client的方式(淘寶)

  • Oceanus(58同城資料庫中介軟體)

  • OneProxy(原支付寶首席架構師樓方鑫開發 )

  • vitess(谷歌開發的資料庫中介軟體)

  • Heisenberg(百度)

  • TSharding(蘑菇街白輝)

  • Xx-dbproxy(金山的Kingshard、噹噹網的sharding-jdbc )

  • amoeba

安裝Atlas

# 雖然包時el6的,但是centos7也能用
wget https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm
 
rpm -ivh Atlas-2.2.1.el6.x86_64.rpm

mysql庫建立賬號

grant all on *.* to 'root'@'%' identified by '123456';
flush privileges;

grant all on *.* to 'shanhe'@'%' identified by '123456';
flush privileges;

配置

[root@manager ~]# cd /usr/local/mysql-proxy/conf
[root@manager mysql-proxy]# vim test.cnf
[mysql-proxy]

#帶#號的為非必需的配置專案

#管理介面的使用者名稱
admin-username = shanhe

#管理介面的密碼
admin-password = 123456

#Atlas後端連線的MySQL主庫的IP和埠,可設定多項,用逗號分隔
proxy-backend-addresses = 192.168.0.10:3306 

#Atlas後端連線的MySQL從庫的IP和埠,@後面的數字代表權重,用來作負載均衡,若省略則預設為1,可設定多項,用逗號分隔
proxy-read-only-backend-addresses = 192.168.0.14:3306,192.168.0.6:3306

#使用者名稱與其對應的加密過的MySQL密碼,密碼使用PREFIX/bin目錄下的加密程式encrypt加密,下行的user1和user2為示例,將其替換為你的MySQL的使用者名稱和加密密碼!
pwds = root:/iZxz+0GRoA=, shanhe:/iZxz+0GRoA=

#設定Atlas的執行方式,設為true時為守護程序方式,設為false時為前臺方式,一般開發除錯時設為false,線上執行時設為true,true後面不能有空格。
daemon = true

#設定Atlas的執行方式,設為true時Atlas會啟動兩個程序,一個為monitor,一個為worker,monitor在worker意外退出後會自動將其重啟,設為false時只有worker,沒有monitor,一般開發除錯時設為false,線上執行時設為true,true後面不能有空格。
keepalive = true

#工作執行緒數,對Atlas的效能有很大影響,可根據情況適當設定
event-threads = 8

#日誌級別,分為message、warning、critical、error、debug五個級別
log-level = message

#日誌存放的路徑
log-path = /usr/local/mysql-proxy/log

#SQL日誌的開關,可設定為OFF、ON、REALTIME,OFF代表不記錄SQL日誌,ON代表記錄SQL日誌,REALTIME代表記錄SQL日誌且實時寫入磁碟,預設為OFF
sql-log = OFF

#慢日誌輸出設定。當設定了該引數時,則日誌只輸出執行時間超過sql-log-slow(單位:ms)的日誌記錄。不設定該引數則輸出全部日誌。
#sql-log-slow = 10

#例項名稱,用於同一臺機器上多個Atlas例項間的區分
#instance = test

#Atlas監聽的工作介面IP和埠
proxy-address = 0.0.0.0:1234

#Atlas監聽的管理介面IP和埠
admin-address = 0.0.0.0:2345

#分表設定,此例中person為庫名,mt為表名,id為分表字段,3為子表數量,可設定多項,以逗號分隔,若不分表則不需要設定該項
#tables = person.mt.id.3

#預設字符集,設定該項後客戶端不再需要執行SET NAMES語句
charset = utf8

#允許連線Atlas的客戶端的IP,可以是精確IP,也可以是IP段,以逗號分隔,若不設定該項則允許所有IP連線,否則只允許列表中的IP連線
#client-ips = 127.0.0.1, 192.168.1

#Atlas前面掛接的LVS的物理網絡卡的IP(注意不是虛IP),若有LVS且設定了client-ips則此項必須設定,否則可以不設定
#lvs-ips = 192.168.1.1

啟動服務

#1、啟動,配置檔名為test.conf對應此處的test
[root@VM-0-11-centos conf]# /usr/local/mysql-proxy/bin/mysql-proxyd test start
OK: MySQL-Proxy of test is started
 
#2、驗證啟動(沒起來他也顯示OK)
[root@VM-0-11-centos conf]# ps -ef|grep [m]ysql-proxy
[root@VM-0-11-centos conf]# netstat -lntup|grep [m]ysql-proxy
 
#3、檢視日誌定位問題
tail -f /usr/local/mysql-proxy/log/test.log

Atlas使用

[root@VM-0-10-centos ~]# mysql -ushanhe -p123456 -h192.168.0.11 -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, 2021, Oracle and/or its affiliates.

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.

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)

mysql> SELECT * FROM backends;
+-------------+-------------------+-------+------+
| backend_ndx | address           | state | type |
+-------------+-------------------+-------+------+
|           1 | 192.168.0.10:3306 | up    | rw   |
|           2 | 192.168.0.14:3306 | up    | ro   |
|           3 | 192.168.0.6:3306  | up    | ro   |
+-------------+-------------------+-------+------+
3 rows in set (0.00 sec)