Atlas實作讀寫分離
環境介紹:
試驗機器4台:
192.168.0.41 Atlas
192.168.0.42 Master
192.168.0.43 Slave-1
192.168.0.44 Slave-2
4台機器均為RHEL 6.4 x86_64系統
[root@Nginx ~]# uname -a
Linux Nginx 2.6.32-358.el6.x86_64 #1 SMP Tue Jan 29 11:47:41 EST 2013 x86_64 x86_64 x86_64 GNU/Linux
MySQL版本為5.5.21
防火牆及SElinux全部關閉;無VIP;
試驗機的主從已搭建完畢,資料一緻;
安裝情況:
安裝包使用的是RPM包
Atlas-2.2.el6.x86_64.rpm
使用RPM -hiv Atlas-2.2.el6.x86_64.rpm
安裝成功
配置檔案:
[mysql-proxy]
#帶#号的為非必需的配置項目
#管理接口的使用者名
admin-username=admin
#管理接口的密碼
admin-password=123
#Atlas後端連接配接的MySQL主庫的IP和端口,可設定多項,用逗号分隔
proxy-backend-addresses=192.168.0.42:3306
#Atlas後端連接配接的MySQL從庫的IP和端口,@後面的數字代表權重,用來作負載均衡,若省略則預設為1,可設定多項,用逗号分隔
proxy-read-only-backend-addresses=192.168.0.43:3306,192.168.0.44:3306
#使用者名與其對應的加密過的MySQL密碼,密碼使用PREFIX/bin目錄下的加密程式encrypt加密,下行的user1和user2為示例,将其替換為你的MySQL的使用者名和加密密碼!
pwds=atlas:3yb5jEku5h4=
#設定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=4
#日志級别,分為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=ON
#慢日志輸出設定。當設定了該參數時,則日志隻輸出執行時間超過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,0.192.168.0
#Atlas前面挂接的LVS的實體網卡的IP(注意不是虛IP),若有LVS且設定了client-ips則此項必須設定,否則可以不設定
#lvs-ips = 192.168.1.1
========================華麗的分割線============================
根據上面的配置檔案,得到賬戶資訊如下:
Atlas管理賬号 :admin
Atlas管理賬号密碼:123
Atlas管理賬号端口:2345
Atlas工作賬号:atlas
Atlas工作賬号密碼:atlas:3yb5jEku5h4= (123,用encrypt編譯過的)
Atlas工作賬号端口:1234
在主庫及從庫機器進行賬号授權:
mysql>GRANT all privileges ON *.* TO 'atlas'@'192.168.0.%' IDENTIFIED BY '123';
3台機器全部如此授權,在Atlas伺服器上遠端登陸3台機器成功.
在Atlas伺服器驗證Atlas管理賬号及工作賬号:
[root@Nginx bin]# /usr/local/mysql-proxy/bin/mysql-proxyd test start
OK: MySQL-Proxy of test is started
[root@Nginx bin]# ps -ef | grep mysql | grep -v grep
root 6638 5610 0 02:00 pts/2 00:00:00 mysql -h127.0.0.1 -P1234 -uatlas -px x
root 6667 1919 0 02:01 pts/1 00:00:00 mysql -h127.0.0.1 -P2345 -uadmin -px x
root 6695 1 0 02:06 ? 00:00:00 /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/test.cnf
root 6696 6695 0 02:06 ? 00:00:00 /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/test.cnf
[root@Nginx ~]# mysql -h127.0.0.1 -P2345 -uadmin -p123
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, 2011, 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 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", ... |
| ADD CLIENT $client | example: "add client 192.168.1.2", ... |
| REMOVE CLIENT $client | example: "remove client 192.168.1.2", ... |
| SAVE CONFIG | save the backends to config file |
10 rows in set (0.00 sec)
mysql> select * from backends;
+-------------+-------------------+-------+------+
| backend_ndx | address | state | type |
| 1 | 192.168.0.42:3306 | down | rw |
| 2 | 192.168.0.44:3306 | down | ro |
| 3 | 192.168.0.43:3306 | down | ro |
3 rows in set (0.00 sec)
噩夢就此開始
登陸atlas工作賬号:
[root@Nginx ~]# mysql -h127.0.0.1 -P1234 -uatlas -p123
Server version: 5.0.81-log
mysql> show databases;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 2
Current database: *** NONE ***
ERROR 2013 (HY000): Lost connection to MySQL server during query
mysql>
無法連接配接到伺服器
在管理賬号中,将所有機器上線
mysql> SET ONLINE 1;
+-------------+-------------------+---------+------+
| backend_ndx | address | state | type |
| 1 | 192.168.0.42:3306 | unknown | rw |
1 row in set (0.00 sec)
再檢視backends狀态:
依然如此
參照網上的方法,在3台主從機上的my.cnf中加入以下參數:
max_connect_errors=100000
max_allowed_packet=16777216 (16M)
依然不行
咨詢開發人員,得知:
此選項,如非必須,不要打開
故障排除後,進行讀寫分離測試
在atlas用戶端,建立資料庫
mysql> create database atlas;
Query OK, 1 row affected (10.02 sec)
mysql> create table atlas1
-> (
-> name varchar(15) not null,
-> sex enum('M','F') not null,
-> age int(2) not null,
-> primary key('name')
-> );
mysql> desc atlas.atlas1;
+-------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
| name | varchar(15) | NO | PRI | NULL | |
| sex | enum('M','F') | NO | | NULL | |
| age | int(2) | NO | | NULL | |
3 rows in set (0.01 sec)
插入資料:
mysql> insert into atlas.atlas1
-> VALUES
-> ('Zhangxuan','M','30'),
-> ('Yangyanjie','M','28'),
-> ('Liuchang','F','22');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
在主庫上進行一緻性檢查:
[root@MA ~]# pt-table-checksum --user=root --host=192.168.0.42 --nocheck-replication-filters --nocheck-binlog-format
TS ERRORS DIFFS ROWS CHUNKS SKIPPED TIME TABLE
10-28T04:36:06 0 0 3 1 0 0.028 atlas.atlas1
10-28T04:36:06 0 0 51 1 0 0.027 mhatest.mha
10-28T04:36:06 0 0 3012 1 0 0.282 mhatest.num1
10-28T04:36:06 0 0 5 1 0 0.029 mhatest.test
成功,然後在從庫1(192.168.0.43)和從庫2(192.168.0.43)上各增加一條資料
從庫1(192.168.0.43):
mysql> INSERT INTO atlas.atlas1 VALUES ('Yangzhenyu','M','30');
Query OK, 1 row affected (0.01 sec)
mysql> select * from atlas1;
+--------+---+--+
| name | sex | age |
| Liuchang | F | 22 |
| Yangyanjie | M | 28 |
| Yangzhenyu | M | 30 |
| Zhangxuan | M | 30 |
+--------+---+---+
4 rows in set (0.00 sec)
從庫2(192.168.0.44)
mysql> INSERT INTO atlas.atlas1 VALUES ('Chenfei','M','14');
mysql> select * from atlas.atlas1;
| Chenfei | M | 14 |
此時,主庫(192.168.0.42)上:
開始驗證讀寫分離:
在atlas伺服器上執行:
mysql> select * from atlas.atlas1 where name='Yangzhenyu';
Empty set (0.00 sec) /*第一次執行居然無傳回值*/
+------------+-----+-----+
1 row in set (0.00 sec) /*第二次正确傳回*/
查詢日志:
[10/28/2014 03:10:07] C:127.0.0.1:39798 S:192.168.0.43:3306 OK 0.719 "select * from atlas.atlas1 where name='Yangzhenyu'"
[10/28/2014 03:10:10] C:127.0.0.1:39798 S:192.168.0.44:3306 OK 0.921 "select * from atlas.atlas1 where name='Yangzhenyu'"
第一次無傳回值原因:兩個read-only庫資料不一緻導緻,将兩個從庫資料一緻後,查詢
傳回值正常:
1 row in set (0.00 sec) /*第一次查詢*/
1 row in set (0.00 sec) /*第二次查詢*/
讀寫分離成功
時間有限,下一步準備用sysbench進行壓力測試
本文轉自 liang3391 51CTO部落格,原文連結:http://blog.51cto.com/liang3391/1569074