DBProxy 入門到精通系列(二):DBProxy快速入門教程


這里主要用來了解有關DBProxy方面的部署及基本的配置,以及模擬架構

1 DBProxy方面的安裝部署

1)基礎環境的部署

# yum install -y Percona-Server-devel-55.x86_64 Percona-Server-client-55.x86_64 Percona-Server-shared-55  jemalloc jemalloc-devel libevent \
libevent-devel openssl openssl-devel lua lua-devel bison flex libtool.x86_64 libffi-devel libffi glib2.x86_64 glib2-devel.x86_64 glib2-fam.x86_64 git

2)由於需要使用到mysql_config,故此,需要下載mysql5.7的repos,然后將對應鏡像源的5.6開啟,5.7禁用,DBProxy只支持5.5 5.6的版本,故此,請勿在5.7上進行

測試

#rpm -ivh https://repo.mysql.com//mysql57-community-release-el7-9.noarch.rpm
#vim /etc/yum.repos.d/mysql-community.repo
[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/
enabled=1   #這里改成1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=0  #這里改成0,禁用
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

3)部署安裝DBProxy

#git clone https://github.com/Meituan-Dianping/DBProxy.git
#cd DBProxy/
#sh autogen.sh
#sh bootstrap.sh
#make && make install
# ll /usr/local/mysql-proxy/
total 0
drwxr-xr-x 2 root root 162 Mar 27 03:46 bin
drwxr-xr-x 4 root root  42 Mar 27 03:46 lib

 

2 架構設計

 DBProxy可以配置一主多從的結構,也就是架構中必須有一台主庫,0或多個從庫,主庫上可以進行讀寫操作,從庫只能進行讀操作,APP應用的流量可以通過DBProxy中設置規則,將流量路由到適合的數據庫中,部署圖如下

DBProxy 10.11.10.140

Master    10.11.10.141

Slave01  10.11.10.142

Slave02  10.11.10.143

主從配置此處略掉,配置DBProxy

[root@gzxsd ~]# mkdir /usr/local/mysql-proxy/conf 
[root@gzxsd ~]# cd /usr/local/mysql-proxy/
[root@gzxsd mysql-proxy]# cp /data/download/DBProxy/script/source.cnf.samples /usr/local/mysql-proxy/conf/source.cnf

 

#配置conf文件

[root@gzxsd ~]# mkdir /var/log/dbproxy_log


 

[mysql-proxy]

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

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

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

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

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

#用戶名與其對應的加密過的MySQL密碼,密碼使用PREFIX/bin目錄下的加密程序encrypt加密,下行的user1和user2為示例,將其替換為你的MySQL的用戶名和加密密碼! #這個需要執行/data/download/DBProxy/script/encrypt guest
#需要在master上做一次grant all privileges on *.* to dbproxy@'10.11.10.140' identidified by 'xxxx';
pwds
=dbproxy:uqmOY9A= #設置dbproxy的運行方式,設為true時為守護進程方式,設為false時為前台方式,一般開發調試時設為false,線上運行時設為true daemon=true #設置dbproxy的運行方式,設為true時dbproxy會啟動兩個進程,一個為monitor,一個為worker,monitor在worker意外退出后會自動將其重啟,設為false時只有worker,沒有monitor,一般開發調試時設為false,線上運行時設為true keepalive=true #工作線程數,對dbproxy的性能有很大影響,可根據情況適當設置 event-threads=8 #日志級別,分為message、warning、critical、error、debug五個級別 log-level=message #日志存放的路徑 log-path=/var/log/dbproxy_log/ #SQL日志的開關,可設置為OFF、ON、REALTIME,OFF代表不記錄SQL日志,ON代表記錄SQL日志,REALTIME代表記錄SQL日志且實時寫入磁盤,默認為OFF sql-log=ON #SELECT無where條件的限制,分為ON,OFF select-where-limit = OFF #實例名稱,用於同一台機器上多個dbproxy實例間的區分 instance=source #dbproxy監聽的工作接口IP和端口 proxy-address=0.0.0.0:3307 #dbproxy監聽的管理接口IP和端口 admin-address=0.0.0.0:3308 #分表設置,此例中person為庫名,mt為表名,id為分表字段,3為子表數量,可設置多項,以逗號分隔,若不分表則不需要設置該項 #tables = person.mt.id.3 #默認字符集,設置該項后客戶端不再需要執行SET NAMES語句 #charset=utf8 #dbproxy前面掛接的LVS的物理網卡的IP(注意不是虛IP),若有LVS且設置了client-ips則此項必須設置,否則可以不設置 #lvs-ips = 192.168.1.1 max-connections=1000 #長時等待閾值,同步等待時間超過該閾值則認為是長時等待 long-wait-time=500 #慢查詢閾值,查詢執行時間超過該閾值則認為是慢查詢 long-query-time=500 #0:不統計,1:僅統計總體的響應時間,其中包括慢查詢,2:進行直方圖統計;默認為1。 query-response-time-stats=2 #SQL日志文件最大大小,單位為字節,默認為1G sql-log-file-size=1073741824 #保留的最大SQL日志文件個數,默認為0,不保留歷史文件,僅保留當前文件 sql-log-file-num=500 #后台MySQL版本號,默認為5.5 mysql-version=5.5 #后台MySQL最大連接數,默認為0,表示不限制 backend-max-thread-running=64 #后台MySQL連接超過backend-max-thread-running后,每次sleep的時間,單位為ms thread-running-sleep-delay=10 #SQL過濾統計緩存的SQL模板數,默認為0 lastest-query-num=100000 #SQL過濾的時間閾值 query-filter-time-threshold=500 #SQL過濾的頻率閾值 query-filter-frequent-threshold=10 #SQL過濾頻率統計時間窗口內的最小執行次數,根據頻率和該參數計算時間窗口 access-num-per-time-window = 5 #手動過濾SQL是否生效,默認為OFF manual-filter-flag=OFF #自動過濾SQL是否生效,默認為OFF auto-filter-flag=OFF

 

 #啟動DBProxy

[root@gzxsd conf]# /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/source.cnf
[root@gzxsd conf]# ss -ant
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:3307 *:*
LISTEN 0 128 *:3308 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
ESTAB 0 52 10.11.10.140:22 10.11.20.31:49783
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*

 

#連接測試

[root@gzxsd ~]# mysql -uguest -p -h10.11.10.140 -P3308
Enter password: 
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, 2016, 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.

[guest@10.11.10.140][(none)]> select * from help;

 

[root@gzxsd ~]# mysql -udbproxy -p -h10.11.10.140 -P3307
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.35-33.0-log MySQL Community Server (GPL)

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

[dbproxy@10.11.10.140][(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.01 sec)

 


免責聲明!

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



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