Atlas是由 Qihoo 360公司Web平臺部基礎架構團隊開發維護的一個基于MySQL協議的數據中間層項目。它在MySQL官方推出的MySQL-Proxy 0.8.2版本的基礎上,修改了大量bug,添加了很多功能特性,目前該項目在360公司內部得到了廣泛應用。
下面是Atlas的架構圖

Atlas的架構圖
Atlas主要有以下功能
- 讀寫分離
- 從庫負載均衡
- IP過濾
- 自動分表(目前只支持在同一個庫中進行分表)
- DBA可平滑上下線DB
- 自動摘除宕機的DB
Atlas相對于官方MySQL-Proxy有以下優勢
- 將主流程中所有Lua代碼用C重寫,Lua僅用于管理接口
- 重寫網絡模型、線程模型
- 實現了真正意義上的連接池
- 優化了鎖機制,性能提高數十倍
下面就一步一步帶大家去安裝和使用Atlas數據庫中間件
安裝Atlas-sharding_1.0.1-el6.x86_64.rpm包
大家可以從++https://github.com/Qihoo360/Atlas/releases++網站下載到最新的rpm包,推薦使用rpm包安裝
加密應用訪問mysql數據庫訪問密碼
安裝好Atlas的rpm包之后,進入到/usr/local/mysql-proxy/bin目錄,使用下面命令對密碼進行加密
./encrypt tony ANDKNNypf4k= <--這個就是加密后的密碼
配置Atlas配置文件(/usr/local/mysql-proxy/conf/opentest.cnf)
[mysql-proxy] #帶#號的為非必需的配置項目 #管理接口的用戶名 admin-username = user #管理接口的密碼 admin-password = pwd12345 #Atlas后端連接的MySQL主庫的IP和端口,可設置多項,用逗號分隔 proxy-backend-addresses = 10.10.57.206:3306 #Atlas后端連接的MySQL從庫的IP和端口,@后面的數字代表權重,用來作負載均衡,若省略則默認為1,可設置多項,用逗號分隔 proxy-read-only-backend-addresses = 10.10.57.207:3306@1,10.10.57.208:3306@1 #用戶名與其對應的加密過的MySQL密碼,密碼使用PREFIX/bin目錄下的加密程序encrypt加密,下行的user1和user2為示例,將其替換為你的MySQL的用戶名和加密密碼! pwds = tony:ANDKNNypf4k= #設置Atlas的運行方式,設為true時為守護進程方式,設為false時為前臺方式,一般開發調試時設為false,線上運行時設為true,true后面不能有空格。 daemon = true #設置Atlas的運行方式,設為true時Atlas會啟動兩個進程,一個為monitor,一個為worker,monitor在worker意外退出后會自動將其重啟,設為false時只有worker,沒有monitor,一般開發調試時設為false,線上運行時設為true,true后面不能有空格。 keepalive = true #工作線程數,對Atlas的性能有很大影響,推薦設置成系統的CPU核數的2至4倍 event-threads = 2 #日志級別,分為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 = REALTIME #實例名稱,用于同一臺機器上多個Atlas實例間的區分 instance = opentest #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
啟動Atlas服務
/usr/local/mysql-proxy/bin/mysql-proxyd opentest start
檢查Atlas服務狀態
ps -ef||grep -i mysql /usr/local/mysql-proxy/bin/mysql-proxyd opentest status
連接Atlas管理
mysql -h10.10.57.205 -P2345 -uuser -ppwd12345
連接好之后,可以使用select * from help;查看可以查看Atlas的管理命令
例如:查看mysql庫的讀寫分離信息
mysql> SELECT * FROM backends; +----------+-------------------+-------+------+-------------+ | group_id | address | state | type | backend_ndx | +----------+-------------------+-------+------+-------------+ | -1 | 10.10.57.206:3306 | up | rw | 1 | | -1 | 10.10.57.207:3306 | up | ro | 2 | | -1 | 10.10.57.208:3306 | up | ro | 3 | +----------+-------------------+-------+------+-------------+ 3 rows in set (0.00 sec)
測試應用連接Atlas服務
mysql -h10.10.57.205 -P1234 -utony -ptony 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 11 Server version: 5.0.81-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, 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. mysql> mysql> select * from test.t_test; +----+-------+ | id | name | +----+-------+ | 1 | test1 | | 2 | test2 | +----+-------+ 2 rows in set (0.00 sec)
通過查看sql運行日志,可以看到應用已經實現了讀寫分離和負載均衡
[08/30/2018 16:48:18] C:10.10.57.208:56858 S:10.10.57.206:3306 OK 13.602 "insert into t_test values('1','test1')" [08/30/2018 16:48:35] C:10.10.57.208:56858 S:10.10.57.206:3306 OK 12.519 "insert into t_test values('2','test2')" [08/30/2018 16:48:47] C:10.10.57.208:56858 S:10.10.57.208:3306 OK 0.414 "select * from t_test" [08/30/2018 16:48:47] C:10.10.57.208:56858 S:10.10.57.207:3306 OK 0.456 "select * from t_test" [08/30/2018 16:48:48] C:10.10.57.208:56858 S:10.10.57.208:3306 OK 0.413 "select * from t_test"
喜歡的同學可以關注我的公眾號(db_arch)(Mysql數據庫運維與架構設計)