Mysql訪問中間件--Atlas初探

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

Atlas主要有如下功能git

  1. 讀寫分離
  2. 從庫負載均衡
  3. IP過濾
  4. 自動分表(目前只支持在同一個庫中進行分表)
  5. DBA可平滑上下線DB
  6. 自動摘除宕機的DB

Atlas相對於官方MySQL-Proxy有如下優點github

  1. 將主流程中全部Lua代碼用C重寫,Lua僅用於管理接口
  2. 重寫網絡模型、線程模型
  3. 實現了真正意義上的鏈接池
  4. 優化了鎖機制,性能提升數十倍

下面就一步一步帶你們去安裝和使用Atlas數據庫中間件安裝Atlas-sharding1.0.1-el6.x8664.rpm包你們能夠從++https://github.com/Qihoo360/Atlas/releases++網站下載到最新的rpm包,推薦使用rpm包安裝加密應用訪問mysql數據庫訪問密碼
安裝好Atlas的rpm包以後,進入到/usr/local/mysql-proxy/bin目錄,使用下面命令對密碼進行加密sql

./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數據庫運維與架構設計)

喜歡的同窗能夠關注個人公衆號(db_arch)(Mysql數據庫運維與架構設計)

相關文章
相關標籤/搜索