MySQL + Atlas 部署读写分离
原文地址MySQL + Atlas --- 部署讀寫分離
序章
Atlas是360團隊弄出來的一套基于MySQL-Proxy基礎之上的代理,修改了MySQL-Proxy的一些BUG,并且優(yōu)化了很多東西。而且安裝方便。
Atlas官方鏈接:?https://github.com/Qihoo360/Atlas/blob/master/README_ZH.md
Atlas下載鏈接:?https://github.com/Qihoo360/Atlas/releases
?
環(huán)境:
| 系統(tǒng) | IP | 配置 |
| CentOS 7.0 | 192.168.1.40 | Atlas代理服務 |
| CentOS 7.0 | 192.168.1.50 | 主MySQL數(shù)據(jù)庫 |
| CentOS 7.0 | 192.168.1.41 | 從MySQL數(shù)據(jù)庫 |
?
?
1. 數(shù)據(jù)庫的配置
需要進入50與41數(shù)據(jù)庫中配置用戶名與密碼,用戶必須是遠程可以訪問的用戶,配置方法如下:
?
首先進入到50的MySQL數(shù)據(jù)庫中,創(chuàng)建用戶“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數(shù)據(jù)庫,才能修改host權限的信息
?
# 進入數(shù)據(jù)庫 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表,看看修改成功了沒有??梢钥吹?#xff0c;buck的用戶,host已經(jīng)修改成百分號了。
?
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)?
?
更新數(shù)據(jù)庫信息,如果沒更新數(shù)據(jù)庫的信息,修改不會立即生效,那就需要重啟數(shù)據(jù)庫了。這邊直接更新數(shù)據(jù)庫的信息,可以避免重啟。
mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)?
主從MySQL都需要創(chuàng)建一個數(shù)據(jù)庫,我這創(chuàng)建的數(shù)據(jù)庫是“test”,為了方便測試讀寫分離
mysql> create database test; Query OK, 1 row affected (0.00 sec)?
【注解:50數(shù)據(jù)庫與41的數(shù)據(jù)庫同樣配置】
?
?
2. 主從數(shù)據(jù)庫連接
配置主從服務器需要編寫MySQL的配置文件,詳情配置步驟如下:
主服務器 ( 192.168.1.50 ),使用vim進行配置
?
[mysqld] datadir=/data/mysql socket=/var/lib/mysql/mysql.sock user=mysql #主從復制配置 innodb_flush_log_at_trx_commit=1 sync_binlog=1 #需要備份的數(shù)據(jù)庫 binlog-do-db=test #不需要備份的數(shù)據(jù)庫 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,表示備份全部數(shù)據(jù)庫。】
?
重啟mysqld服務
[root@localhost bin]# /etc/init.d/mysqld restart?
進入數(shù)據(jù)庫,配置主從復制的權限
mysql> grant replication slave on *.* to 'buck'@'127.0.0.1' identified by 'hello'; Query OK, 0 rows affected (0.00 sec)?
鎖定數(shù)據(jù)庫
mysql> flush tables with read lock; Query OK, 0 rows affected (0.00 sec)?
查看主數(shù)據(jù)庫信息,記住下面的“File”與“Position”的信息,它們是用來配置從數(shù)據(jù)庫的關鍵信息??梢钥吹较旅嫱降臄?shù)據(jù)庫的“test”數(shù)據(jù)庫,主從數(shù)據(jù)庫如果數(shù)據(jù)不一樣,首先需要手動去同步一下數(shù)據(jù),我在Windows環(huán)境下只用Navicat復制的數(shù)據(jù),這里就不演示了。
?
mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 589 | test | mysql | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)?
?
從服務器 ( 192.168.1.41 ),也需要使用vim進行配置,只需要在[mysqld]下面加入server-id=2就可以,全部配置如下:
?
[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysqlserver-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?
?
進入數(shù)據(jù)庫,配置從數(shù)據(jù)庫的信息,這里輸入剛才記錄下來的“File”與“Position”的信息,并且在從服務器上執(zhí)行:
| 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.000001' | 日志文件的名稱,需要與主服務器對應 |
| master_log_pos=589 | 日志位置,需要與主服務器對應 |
| master_connect_retry=20 | 重連次數(shù) |
?
mysql> change master to master_host='192.168.1.50',-> master_user='buck',-> master_password='hello',-> master_port=3306,-> master_log_file='mysql-bin.000001',-> master_log_pos=589,-> master_connect_retry=20; Query OK, 0 rows affected (0.01 sec)?
啟動進程
mysql> start slave; Query OK, 0 rows affected (0.00 sec)?
檢查主從復制狀態(tài),要看到下列標紅的信息中,兩個都是Yes,才說明主從連接正確,如果有一個是No,需要重新確定剛才記錄的日志信息,停掉“stop slave”重新進行配置主從連接。
?
mysql> show slave status \G; *************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.246.134Master_User: buckMaster_Port: 3306Connect_Retry: 10Master_Log_File: mysql-bin.000001Read_Master_Log_Pos: 589Relay_Log_File: mysqld-relay-bin.000001Relay_Log_Pos: 251Relay_Master_Log_File: mysql-bin.000001Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 17620976Relay_Log_Space: 407Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: 1 row in set (0.00 sec)ERROR: No query specified?
?可以進入主數(shù)據(jù)庫,運行以下命令,測試主從服務的實際效果。
mysql> use test;
mysql> create table wl(id int(4),sex varchar(255),birthday date);
進入從庫
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| wl |
+----------------+
1 row in set (0.00 sec)
mysql>
?
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 ~]# mkdir /demo/atlas [root@localhost ~]# cd /demo/atlas [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目錄下放的都是可執(zhí)行文件
1. “encrypt”是用來生成MySQL密碼加密的,在配置的時候會用到
2. “mysql-proxy”是MySQL自己的讀寫分離代理
3. “mysql-proxyd”是360弄出來的,后面有個“d”,服務的啟動、重啟、停止。都是用他來執(zhí)行的
?
conf目錄下放的是配置文件
1. “test.cnf”只有一個文件,用來配置代理的,可以使用vim來編輯
?
lib目錄下放的是一些包,以及Atlas的依賴
log目錄下放的是日志,如報錯等錯誤信息的記錄
?
進入bin目錄,使用encrypt來對數(shù)據(jù)庫的密碼進行加密,我的MySQL數(shù)據(jù)的用戶名是buck,密碼是hello,我需要對密碼進行加密
[root@localhost bin]# ./encrypt 123456 /iZxz+0GRoA=?
配置Atlas,使用vim進行編輯
[root@localhost conf]# cd /usr/local/mysql-proxy/conf/ [root@localhost conf]# vim test.cnf?
進入后,可以在Atlas進行配置,360寫的中文注釋都很詳細,根據(jù)注釋來配置信息,其中比較重要,需要說明的配置如下:
這是用來登錄到Atlas的管理員的賬號與密碼,與之對應的是“#Atlas監(jiān)聽的管理接口IP和端口”,也就是說需要設置管理員登錄的端口,才能進入管理員界面,默認端口是2345,也可以指定IP登錄,指定IP后,其他的IP無法訪問管理員的命令界面。方便測試,我這里沒有指定IP和端口登錄。
#管理接口的用戶名 admin-username = user#管理接口的密碼 admin-password = pwd?
這是用來配置主數(shù)據(jù)的地址與從數(shù)據(jù)庫的地址,這里配置的主數(shù)據(jù)庫是135,從數(shù)據(jù)庫是134
#Atlas后端連接的MySQL主庫的IP和端口,可設置多項,用逗號分隔 proxy-backend-addresses = 192.168.1.50:3306#Atlas后端連接的MySQL從庫的IP和端口,@后面的數(shù)字代表權重,用來作負載均衡,若省略則默認為1,可設置多項,用逗號分隔 proxy-read-only-backend-addresses = 192.168.1.41:3306@1?
這個是用來配置MySQL的賬戶與密碼的,我的MySQL的用戶是buck,密碼是hello,剛剛使用Atlas提供的工具生成了對應的加密密碼
#用戶名與其對應的加密過的MySQL密碼,密碼使用PREFIX/bin目錄下的加密程序encrypt加密,下行的user1和user2為示例,將其替換為你的MySQL的用戶名和加密密碼! pwds = buck:/iZxz+0GRoA=?
這是設置工作接口與管理接口的,如果ip設置的”0.0.0.0”就是說任意IP都可以訪問這個接口,當然也可以指定IP和端口,方便測試我這邊沒有指定,工作接口的用戶名密碼與MySQL的賬戶對應的,管理員的用戶密碼與上面配置的管理員的用戶密碼對應。
#Atlas監(jiān)聽的工作接口IP和端口 proxy-address = 0.0.0.0:1234#Atlas監(jiān)聽的管理接口IP和端口 admin-address = 0.0.0.0:2345?
?
啟動Atlas
[root@localhost bin]# pwd
/usr/local/mysql-proxy/bin
[root@localhost bin]# ./mysql-proxyd test start
OK: MySQL-Proxy of test is started?
測試一下Atlas服務器的MySQL狀態(tài),要確認它是關閉狀態(tài),并且使用mysql命令,進不去數(shù)據(jù)庫,前提是Atlas服務器也需要裝mysql,直接運行 ?yum -y install 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)?
確認系統(tǒng)中自帶的MySQL進不去了,使用如下命令,進入Atlas的管理模式“mysql -h127.0.0.1?-P2345 -uuser -ppwd?”,能進去說明Atlas正常運行著呢,因為它會把自己當成一個MySQL數(shù)據(jù)庫,所以在不需要數(shù)據(jù)庫環(huán)境的情況下,也可以進入到MySQL數(shù)據(jù)庫模式。
?
[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-adminCopyright (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-logCopyright (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>?
你可以讓數(shù)據(jù)庫某一臺down掉,來測試監(jiān)控的可用性。從這里可以看出我們的監(jiān)控是好使的,啦啦啦啦!!
?
?
如果工作接口可以進入了,就可以在Windows平臺下,使用Navicat來連接數(shù)據(jù)庫,填寫對應的host,Port,用戶名,密碼就可以
?
?
4. 讀寫分離測試
這里測試讀寫分離需要使用到Jmeter了,它是Java寫第一套開源的壓力測試工具,因為這個比較方便。他有專門測試MySQL的模塊,需要使用MySQL的JDBC驅(qū)動jar包,配置很簡單,東西很好很強大很好用。
Jmeter下載地址:http://jmeter.apache.org/download_jmeter.cgi
MySQL的JDBC? :http://dev.mysql.com/downloads/connector/j/
?
下載下來后,分別都解壓開來,打開Jmeter ( 在bin路面下的jmeter.bat ) ,在測試計劃中,導致JDBC的jar包
配置JDBC的驅(qū)動
分別做查詢與插入語句
?
配置好了以后,就先運行查詢操作,然后分別監(jiān)控主數(shù)據(jù)庫與從數(shù)據(jù)庫所在機器的流量,來確定是否讀寫,使用“sar -n DEV 1 10000”命令來監(jiān)控讀寫
先來測試寫,目前數(shù)據(jù)庫里面一條信息都沒有,開啟配置好了的Jmeter,進行寫入數(shù)據(jù)測試
?
主數(shù)據(jù)庫 ( 192.168.1.50 )
從數(shù)據(jù)庫 ( 192.168.1.41 )
可以看到測試插入數(shù)據(jù)的操作時,主數(shù)據(jù)庫的網(wǎng)卡流量很大,從數(shù)據(jù)庫的流量很小,是應為主數(shù)據(jù)是主要負責寫入的,而從數(shù)據(jù)庫主要是負責同步的。
?
查看數(shù)據(jù)庫,發(fā)現(xiàn)已經(jīng)插入了6W多條數(shù)據(jù)了。
?
進行讀取數(shù)據(jù)的測試,只需要執(zhí)行查詢就好,執(zhí)行“select *from sbtest;”來查詢數(shù)據(jù)表
主數(shù)據(jù)庫 ( 192.168.1.50 )
從數(shù)據(jù)庫 ( 192.168.1.41 )
可以看到135(50)數(shù)據(jù)庫的流量非常大,134(41)沒有什么流量,這下就可以確定了數(shù)據(jù)是從數(shù)據(jù)庫讀取的。已經(jīng)實現(xiàn)了讀寫分離。
總結(jié)
以上是生活随笔為你收集整理的MySQL + Atlas 部署读写分离的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: webpack-plugin-webpa
- 下一篇: Apache防盗链