MySQL + Atlas --- 部署讀寫分離

閱讀目錄

1. 數據庫用戶配置

2. 主從數據庫鏈接

3. Atlas配置

4. 讀寫分離測試

 

序章

Atlas是360團隊弄出來的一套基於MySQL-Proxy基礎之上的代理,修改了MySQL-Proxy的一些BUG,而且優化了不少東西。並且安裝方便。配置的註釋寫的蠻詳細的,都是中文。英文很差的同窗有福了。mysql

Atlas官方連接: https://github.com/Qihoo360/Atlas/blob/master/README_ZH.mdgit

Atlas下載連接: https://github.com/Qihoo360/Atlas/releasesgithub

 

環境:sql

系統數據庫

IPapache

配置vim

CentOS 6.7後端

192.168.246.143服務器

Atlas代理服務負載均衡

CentOS 6.7

192.168.246.134

主MySQL數據庫

CentOS 6.7

192.168.246.135

從MySQL數據庫

 

 

1. 數據庫的配置

須要進入134與135數據庫中配置用戶名與密碼,用戶必須是遠程能夠訪問的用戶,配置方法以下:

 

首先進入到134的MySQL數據庫中,建立用戶「buck」設置密碼爲「hello」下列標紅的是用戶與密碼。

mysql> grant all on *.* to buck@'127.0.0.1' identified by "hello";
Query OK, 0 rows affected (0.00 sec)

 

修改buck的訪問權限,首先得進入mysql數據庫,才能修改host權限的信息

# 進入數據庫
mysql> use mysql
Database changed

# 修改host權限爲"%"
mysql> update user set host = '%' where user = 'buck';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

 

查看一下user表,看看修改爲功了沒有。能夠看到,buck的用戶,host已經修改爲百分號了。

mysql> select user, host from user;
+------+-----------------------+
| user | host                  |
+------+-----------------------+
| buck | %                     |
| root | 127.0.0.1             |
|      | localhost             |
| root | localhost             |
|      | localhost.localdomain |
| root | localhost.localdomain |
+------+-----------------------+
6 rows in set (0.00 sec)

 

更新數據庫信息,若是沒更新數據庫的信息,修改不會當即生效,那就須要重啓數據庫了。這邊直接更新數據庫的信息,能夠避免重啓。

mysql>  flush privileges;
Query OK, 0 rows affected (0.00 sec)

 

主從MySQL都須要建立一個數據庫,我這建立的數據庫是「test」,爲了方便測試讀寫分離

mysql> create database test;
Query OK, 1 row affected (0.00 sec)

 

【主意:135數據庫與134的數據庫一樣配置, 記得要建立一樣的數據庫哦】

 

 

2. 主從數據庫鏈接

配置主從服務器須要編寫MySQL的配置文件,詳情配置步驟以下:

主服務器 ( 192.168.246.134 ),使用vim進行配置

[mysqld]  
datadir=/data/mysql  
socket=/var/lib/mysql/mysql.sock  
user=mysql  
  
#主從複製配置  
innodb_flush_log_at_trx_commit=1  
sync_binlog=1  
#須要備份的數據庫  
binlog-do-db=test
#不須要備份的數據庫  
binlog-ignore-db=mysql  
  
#啓動二進制文件  
log-bin=mysql-bin  
  
#服務器ID  
server-id=1  
  
# Disabling symbolic-links is recommended to prevent assorted security risks  
symbolic-links=0  
  
[mysqld_safe]  
log-error=/var/log/mysqld.log  
pid-file=/var/run/mysqld/mysqld.pid

【主意:若沒有配置binlog-do-db和binlog_ignore_db,表示備份所有數據庫。】

 

重啓mysqld服務

[root@localhost bin]# /etc/init.d/mysqld restart

 

進入數據庫,配置主從複製的權限

mysql> grant replication slave on *.* to 'buck'@'127.0.0.1' identified by 'hello';
Query OK, 0 rows affected (0.00 sec)

 

鎖定數據庫

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)

 

查看主數據庫信息,記住下面的「File」與「Position」的信息,它們是用來配置從數據庫的關鍵信息。能夠看到下面同步的數據庫的「test」數據庫,主從數據庫若是數據不同,首先須要手動去同步一下數據,我在Windows環境下只用Navicat複製的數據,這裏就不演示了。

mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 | 17620976 | test         | mysql            |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

 

從服務器 ( 192.168.246.135 ),也須要使用vim進行配置,只須要在[mysqld]下面加入server-id=2就能夠,所有配置以下:

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql

server-id=2

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

 

進入數據庫,配置從數據庫的信息,這裏輸入剛纔記錄下來的「File」與「Position」的信息,而且在從服務器上執行:

mysql> change master to master_host='192.168.246.134'

主服務器的IP

master_user='buck'

配置主服務器的用戶名

master_password='hello'

對應用戶的密碼

master_port=3306

主服務器的mysql端口

master_log_file='mysql-bin.000002'

日誌文件的名稱,須要與主服務器對應

master_log_pos=17620976

日誌位置,須要與主服務器對應

master_connect_retry=10

重連次數

mysql> change master to master_host='192.168.246.134',
    -> master_user='buck',
    -> master_password='hello',
    -> master_port=3306,
    -> master_log_file='mysql-bin.000002',
    -> master_log_pos=17620976,
    -> master_connect_retry=10;
Query OK, 0 rows affected (0.01 sec)

啓動進程

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

 

檢查主從複製狀態,要看到下列標紅的信息中,兩個都是Yes,才說明主從鏈接正確,若是有一個是No,須要從新肯定剛纔記錄的日誌信息,停掉「stop slave」從新進行配置主從鏈接。

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.246.134
                  Master_User: buck
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 17620976
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 17620976
              Relay_Log_Space: 407
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
1 row in set (0.00 sec)

ERROR: 
No query specified

 

 

 

3. Atlas配置

下載Atlas會有兩個版本,其中有個分表的版本,可是這個須要其餘的依賴,我這邊不須要分表這種需求,因此安裝普通的版本

     Atlas (普通) : Atlas-2.2.1.el6.x86_64.rpm

     Atlas (分表) : Atlas-sharding_1.0.1-el6.x86_64.rpm

   

首先進入Linux的Home目錄下,下載非分表的安裝包

[root@localhost ~]# cd /home/
[root@localhost home]# wget https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm

 

下載好了以後,進行安裝

[root@localhost home]# rpm -ivh Atlas-2.2.1.el6.x86_64.rpm 
Preparing...                ########################################### [100%]
   1:Atlas                  ########################################### [100%]

 

安裝好了,它會默認在」/usr/local/mysql-proxy」下給你生成4個文件夾,以及須要配置的文件,以下:

[root@localhost home]# ll /usr/local/mysql-proxy/
total 16
drwxr-xr-x. 2 root root 4096 Dec 28 10:47 bin
drwxr-xr-x. 2 root root 4096 Dec 28 10:47 conf
drwxr-xr-x. 3 root root 4096 Dec 28 10:47 lib
drwxr-xr-x. 2 root root 4096 Dec 17  2014 log

bin目錄下放的都是可執行文件

1. 「encrypt」是用來生成MySQL密碼加密的,在配置的時候會用到

2. 「mysql-proxy」是MySQL本身的讀寫分離代理

3. 「mysql-proxyd」是360弄出來的,後面有個「d」,服務的啓動、重啓、中止。都是用他來執行的

 

conf目錄下放的是配置文件

1. 「test.cnf」只有一個文件,用來配置代理的,可使用vim來編輯

 

lib目錄下放的是一些包,以及Atlas的依賴

log目錄下放的是日誌,如報錯等錯誤信息的記錄

 

進入bin目錄,使用encrypt來對數據庫的密碼進行加密,個人MySQL數據的用戶名是buck,密碼是hello,我須要對密碼進行加密

[root@localhost bin]# ./encrypt hello
RePBqJ+5gI4=

 

配置Atlas,使用vim進行編輯

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

 

進入後,能夠在Atlas進行配置,360寫的中文註釋都很詳細,根據註釋來配置信息,其中比較重要,須要說明的配置以下:

這是用來登陸到Atlas的管理員的帳號與密碼,與之對應的是「#Atlas監聽的管理接口IP和端口」,也就是說須要設置管理員登陸的端口,才能進入管理員界面,默認端口是2345,也能夠指定IP登陸,指定IP後,其餘的IP沒法訪問管理員的命令界面。方便測試,我這裏沒有指定IP和端口登陸。

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

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

 

這是用來配置主數據的地址與從數據庫的地址,這裏配置的主數據庫是135,從數據庫是134

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

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

 

這個是用來配置MySQL的帳戶與密碼的,個人MySQL的用戶是buck,密碼是hello,剛剛使用Atlas提供的工具生成了對應的加密密碼

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

 

這是設置工做接口與管理接口的,若是ip設置的」0.0.0.0」就是說任意IP均可以訪問這個接口,固然也能夠指定IP和端口,方便測試我這邊沒有指定,工做接口的用戶名密碼與MySQL的帳戶對應的,管理員的用戶密碼與上面配置的管理員的用戶密碼對應。

#Atlas監聽的工做接口IP和端口
proxy-address = 0.0.0.0:1234

#Atlas監聽的管理接口IP和端口
admin-address = 0.0.0.0:2345

 

 

啓動Atlas

[root@localhost bin]# ./mysql-proxyd test start
OK: MySQL-Proxy of test is started

 

測試一下Atlas服務器的MySQL狀態,要確認它是關閉狀態,而且使用mysql命令,進不去數據庫

[root@localhost bin]# /etc/init.d/mysqld status
mysqld is stopped
[root@localhost bin]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

 

確認系統中自帶的MySQL進不去了,使用以下命令,進入Atlas的管理模式「mysql -h127.0.0.1 -P2345 -uuser -ppwd 」,能進去說明Atlas正常運行着呢,由於它會把本身當成一個MySQL數據庫,因此在不須要數據庫環境的狀況下,也能夠進入到MySQL數據庫模式。

[root@localhost bin]# mysql -h127.0.0.1 -P2345 -uuser -ppwd
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, 2013, 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>

 

能夠訪問「help」表,來看MySQL管理員模式都能作些什麼。可使用SQL語句來訪問

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>

 

也可使用工做接口來訪問,使用命令「mysql -h127.0.0.1 -P1234 -ubuck -phello」

[root@localhost bin]# mysql -h127.0.0.1 -P1234 -ubuck -phello
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.81-log

Copyright (c) 2000, 2013, 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>

 

若是工做接口能夠進入了,就能夠在Windows平臺下,使用Navicat來鏈接數據庫,填寫對應的host,Port,用戶名,密碼就能夠

image_thumb

 

 

4. 讀寫分離測試

這裏測試讀寫分離須要使用到Jmeter了,它是Java寫第一套開源的壓力測試工具,由於這個比較方便。他有專門測試MySQL的模塊,須要使用MySQL的JDBC驅動jar包,配置很簡單,東西很好很強大很好用。

Jmeter下載地址:http://jmeter.apache.org/download_jmeter.cgi

MySQL的JDBC  :http://dev.mysql.com/downloads/connector/j/

 

下載下來後,分別都解壓開來,打開Jmeter ( 在bin路面下的jmeter.bat ) ,在測試計劃中,致使JDBC的jar包

image_thumb2

配置JDBC的驅動

image_thumb4

分別作查詢與插入語句

image_thumb3

 

配置好了之後,就先運行查詢操做,而後分別監控主數據庫與從數據庫所在機器的流量,來肯定是否讀寫,使用「sar -n DEV 1 10000」命令來監控讀寫

先來測試寫,目前數據庫裏面一條信息都沒有,開啓配置好了的Jmeter,進行寫入數據測試

image_thumb10

 

主數據庫 ( 192.168.246.134 )

從數據庫 ( 192.168.246.135 )

能夠看到測試插入數據的操做時,主數據庫的網卡流量很大,從數據庫的流量很小,是應爲主數據是主要負責寫入的,而從數據庫主要是負責同步的。

image_thumb9

 

查看數據庫,發現已經插入了6W多條數據了。

image_thumb11

 

進行讀取數據的測試,只須要執行查詢就好,執行「select *from sbtest;」來查詢數據表

主數據庫 ( 192.168.246.134 )

從數據庫 ( 192.168.246.135 )

能夠看到135數據庫的流量很是大,134沒有什麼流量,這下就能夠肯定了數據是從數據庫讀取的。已經實現了讀寫分離。

image_thumb12

相關文章
相關標籤/搜索