Mysql-proxy代理內網數據庫


Mysql-proxy

參考:https://segmentfault.com/q/1010000000394160

情景分析:首先您需要正在使用UCloud雲主機(uhoust)以及雲數據庫(udb)。

鑒於udb無外網ip不能直接訪問,可利用mysql-proxy將udb跳轉到雲主機的某一端口上進行訪問,方便客戶操作。

1. 安裝mysql-proxy

[root@localhost ~]# yum install mysql-proxy

查看版本:

[root@localhost ~]# mysql-proxy -V
mysql-proxy 0.8.5
  chassis: 0.8.5
  glib2: 2.28.8
  libevent: 1.4.13-stable
  LUA: Lua 5.1.4
    package.path: /usr/lib64/mysql-proxy/lua/?.lua
    package.cpath: /usr/lib64/mysql-proxy/lua/?.so
-- modules
  proxy: 0.8.5

2. 配置mysql-proxy

配置mysql-proxy

大家也可以mysql-proxy -help-all查看它的幫助選項

此服務默認缺省端口為4040,也就是通過訪問4040就能訪問目標3306

但是新版mysql-proxy已經將4040改為了3307,默認3307。

可以直接命令開啟服務,不過推薦使用配置文件,步驟如下:

[root@localhost ~]# vim /etc/mysql-proxy.cnf
[mysql-proxy]
daemon = true
pid-file = /var/run/mysql-proxy.pid
log-file = /var/log/mysql-proxy.log
log-level = debug
max-open-files = 1024
plugins = admin,proxy
user = mysql-proxy
#
#Proxy Configuration
proxy-address = 0.0.0.0:3307 # 這里我們注釋這一行,代理端口就默認改為了4040,也可以不改,后面連接端口就改為3307就可以了。
proxy-backend-addresses = 10.41.1.2  # 內網的數據庫地址,默認3306端口
#proxy-read-only-backend-addresses =
#proxy-lua-script =
#proxy-skip-profiling = true
#
# Admin Configuration
# admin-address = 0.0.0.0:4041 # 監聽端口是4041
admin-lua-script = /usr/lib64/mysql-proxy/lua/admin.lua
admin-username = root  # 數據庫用戶名
admin-password = rootdbpassword  # 數據庫密碼

寫好配置文件保存后需要改變權限,以上配置是由mysql-proxy自動生成的,所以權限已經是660了,可以查看一下:

[root@localhost ~]# ll /etc/mysql-proxy.cnf 
-rw-rw---- 1 root root 519 Aug 10  2016 /etc/mysql-proxy.cnf

如果不是,用以下命令修改。

[root@localhost ~]# chmod 0660 /etc/mysql-proxy.cnf

3. 啟動進程

[root@localhost ~]# mysql-proxy --defaults-file=/etc/mysql-proxy.cnf

查看是否啟動成功:

[root@localhost ~]# netstat -anplut|grep mysql-proxy
tcp        0      0 0.0.0.0:4041                0.0.0.0:*                   LISTEN      28110/mysql-proxy   
tcp        0      0 0.0.0.0:3307                0.0.0.0:*                   LISTEN      28110/mysql-proxy 

若關閉kill命令把程序終止

4. 打開防火牆端口

需要在UCloud管理控制台中 打開雲主機3307端口。

5. 測試

在外網環境,另一台主機:

[root@localhost ~]# mysql -u root -p -P3307 -h host

host為uhoust外網ip

6、錯誤分析

錯誤1、

[root@localhost ~]# mysql-proxy --defaults-file=/etc/mysql-proxy.cnf
2018-12-28 20:49:35: (critical) Key file contains key 'daemon' which has value that cannot be interpreted.
2018-12-28 20:49:35: (message) Initiating shutdown, requested from mysql-proxy-cli.c:367
2018-12-28 20:49:35: (message) shutting down normally, exit code is: 1

配置不對,可能用了網上的配置比如:

[mysql-proxy]
admin-username=root    #admin用戶名
admin-password=123fsck    #admin密碼
admin-lua-script=/usr/lib64/mysql-proxy/lua/admin.lua    #lua位置,參見上面的版本信息
daemon=true        # mysql-proxy以守護進程方式運行
keepalive=true        #保持連接啟動進程會有2個, 一號進程用來監視二號進程
proxy-backend-addresses=10.6.X.XX  #目標地址,udb內網地址,默認端口3306
log-file=/var/log/mysql-proxy.log
log-level=debug

用這段配置就會報上述錯誤,版本不一樣。新版mysql-proxy會自動生成/etc/mysql-proxy.cnf配置,我們修改相應字段就好了。如上述步驟2。

錯誤2、

[root@localhost ~]# mysql -hxx.xx.xx.xx -P4041 -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11690111
Server version: 5.6.20-ucloudrel1-log Source distribution
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> show databases;
ERROR 1105 (07000): use 'SELECT * FROM help' to see the supported commands
mysql> use mysql;
ERROR 1105 (07000): [admin] we only handle text-based queries (COM_QUERY)
mysql> select * from mysql.user;
ERROR 1105 (07000): use 'SELECT * FROM help' to see the supported commands
mysql> select * from help
    -> ;
+------------------------+------------------------------------+
| command                | description                        |
+------------------------+------------------------------------+
| SELECT * FROM help     | shows this help                    |
| SELECT * FROM backends | lists the backends and their state |
+------------------------+------------------------------------+
2 rows in set (0.14 sec)
mysql> select * from backends
    -> ;
+-------------+-------------------+---------+------+------+-------------------+
| backend_ndx | address           | state   | type | uuid | connected_clients |
+-------------+-------------------+---------+------+------+-------------------+
|           1 | 10.41.1.2:3306 | unknown | rw   | NULL |                 0 |
+-------------+-------------------+---------+------+------+-------------------+
1 row in set (0.18 sec)


這是因為一開始我們用了這樣的配置:

[root@localhost ~]# vim /etc/mysql-proxy.cnf
[mysql-proxy]
daemon = true
pid-file = /var/run/mysql-proxy.pid
log-file = /var/log/mysql-proxy.log
log-level = debug
max-open-files = 1024
plugins = admin,proxy
user = mysql-proxy
#
#Proxy Configuration
proxy-address = 0.0.0.0:3307
proxy-backend-addresses = 10.41.1.2
#proxy-read-only-backend-addresses =
#proxy-lua-script =
#proxy-skip-profiling = true
#
# Admin Configuration
# admin-address = 0.0.0.0:4041
admin-lua-script = /usr/lib64/mysql-proxy/lua/admin.lua
admin-username = root
admin-password = rootdbpassword

以上兩點不一樣,此時我們查看mysql-proxy進程,會發現

[root@localhost ~]# netstat -anplut|grep mysql-proxy
tcp        0      0 0.0.0.0:4041                0.0.0.0:*                   LISTEN      28110/mysql-proxy   
tcp        0      0 0.0.0.0:3307                0.0.0.0:*                   LISTEN      28110/mysql-proxy 

這里,這個配置是新版的mysql-proxy配置,如上述所示,此時mysql-proxy的代理端口是3307,監聽端口是4041,但是你的進程連接的卻是這個監聽端口,所以會報上述錯誤。此時,我們只需要將連接端口改為3307就可以了。(老版mysql-proxy的端口是4040.)

[root@localhost ~]# mysql -hxx.xx.xx.xx -P4041 -uroot -p

改為===》

[root@localhost ~]# mysql -hxx.xx.xx.xx -P3307 -uroot -p

錯誤就解決了。


免責聲明!

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



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