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


閱讀目錄

1. 數據庫用戶配置

2. 主從數據庫連接

3. Atlas配置

4. 讀寫分離測試

 

序章

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

Atlas官方鏈接: https://github.com/Qihoo360/Atlas/blob/master/README_ZH.md

Atlas下載鏈接: https://github.com/Qihoo360/Atlas/releases

 

環境:

系統

IP

配置

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM