1. 程式人生 > >Atlas讀寫分離[高可用]

Atlas讀寫分離[高可用]

tps .com 空格 str net bash slave vim mysqlslap

Atlas下載地址: https://github.com/Qihoo360/Atlas/releases

Atlas是出於360的, 比mysql-proxy更穩定, 部署起來更方便。

環境:

proxy:192.168.99.60

master:192.168.99.61

slave:192.168.99.62

技術分享圖片

1.安裝

由於我使用的是rpm包, 直接安裝

rpm -ivh Atlas-2.2.el6.x86_64.rpm

就是這麽簡單, 安裝完成。

目錄默認在:

/usr/local/mysql-proxy

一覽目錄結構:

total 16
drwxr-xr-x. 2 root root 4096 Dec 12 10:04 bin
drwxr-xr-x. 2 root root 4096 Dec 12 11:04 conf
drwxr-xr-x. 3 root root 4096 Dec 12 10:04 lib
drwxr-xr-x. 2 root root 4096 Dec 12 11:17 log

2.配置文件

cd /usr/local/mysql-proxy/conf

vim test.cnf

很多中文指示, 完美

[mysql-proxy]

#帶#號的為非必需的配置項目

#管理接口的用戶名
admin-username = admin

#管理接口的密碼
admin-password = admin

#Atlas後端連接的MySQL主庫的IP和端口,可設置多項,用逗號分隔
proxy-backend-addresses = 192.168.99.61:3306

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

#用戶名與其對應的加密過的MySQL密碼,密碼使用PREFIX/bin目錄下的加密程序encrypt加密,下行的user1和user2為示例,將其替換為你的MySQL的用戶名和加密密碼!
pwds = admin:la1Ux+Bu4zo=

#設置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 = REALTIME

#慢日誌輸出設置。當設置了該參數時,則日誌只輸出執行時間超過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

3.啟動

cd /usr/local/mysql-proxy/bin

./mysql-proxyd test start

在這個目錄下有mysql-proxy mysql-proxyd, 第一個是mysql團隊的, 第二個是屬於360團隊的, 我們使用的是360的。

端口

netstat -tlnp | grep  mysql-proxy



tcp        0      0 0.0.0.0:1234                0.0.0.0:*                   LISTEN      7270/mysql-proxy    
tcp        0      0 0.0.0.0:2345                0.0.0.0:*                   LISTEN      7270/mysql-proxy    

留意您的配置文件: 1234端口是api接口, 提供能外部進行工作連接的, 2345是屬於內部管理的, 比如查詢負載主機狀態等。

進程

ps aux | grep mysql-proxy



root      1227  0.0  0.0 103244   856 pts/5    S+   16:03   0:00 grep mysql-proxy
root      7269  0.0  0.0  64916  5168 ?        S    11:17   0:00 /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/test.cnf
root      7270  0.5  0.0 196084 40848 ?        Sl   11:17   1:30 /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/test.cnf

4.測試

登錄內部

mysql -u admin -padmin -P 2345 -h 192.168.99.60



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, 2012, 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>

登錄成功,查看讀寫分離狀態

 select * from backends;



+-------------+--------------------+-------+------+
| backend_ndx | address            | state | type |
+-------------+--------------------+-------+------+
|           1 | 192.168.99.61:3306 | up    | rw   |
|           2 | 192.168.99.62:3306 | up    | ro   |
+-------------+--------------------+-------+------+
2 rows in set (0.00 sec)

登錄外部

mysql -u admin -padmin -P 1234 -h 192.168.99.60



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, 2012, 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> show databases;



+--------------------+
| Database           |
+--------------------+
| information_schema |
| cm_prd             |
| jkapp              |
| monitorsys         |
| mysql              |
| mysqlslap          |
| ndbinfo            |
| performance_schema |
| sso                |
| test               |
| zskdb              |
+--------------------+
11 rows in set (0.01 sec)

查詢日誌

/usr/local/mysql-proxy/log

ls

sql_test.log  test.log  test.pid

sql_test.log是運行過的sql記錄, test.log是日誌,test.pid是進程ID

cat test.log

2017-12-12 11:17:59: (message) chassis-unix-daemon.c:138: [angel] we try to keep PID=7270 alive
2017-12-12 11:17:59: (message) mysql-proxy 0.8.2 started - instance: test
2017-12-12 11:17:59: (message) proxy listening on port 0.0.0.0:1234
2017-12-12 11:17:59: (message) added read/write backend: 192.168.75.61:3306
2017-12-12 11:17:59: (message) added read-only backend: 192.168.75.62:3306
2017-12-12 11:17:59: (message) chassis-event-thread.c:235: starting 8 threads
cat sql_test.log


[12/12/2017 16:17:33] C:192.168.99.60:52091 S:192.168.99.62:3306 OK 6.271 "select @@version_comment limit 1"
[12/12/2017 16:17:37] C:192.168.99.60:52091 S:192.168.99.62:3306 OK 2.425 "show databases"

Atlas讀寫分離[高可用]