mysql-proxy代理同一內網的數據庫

淺析:

圖片.png

具體部署:
mysql服務器:

#授權遠程登錄賬戶

 

[root@localhost ~]# mysql -uroot  -p
mysql> GRANT ALL ON *.* to 'test'@'192.168.0.1' IDENTIFIED BY 'lmdb(A7105#B9606@com!';
mysql> flush privileges;
 mysql> select user,host from mysql.user;
+---------------+----------------+
| user          | host           |
+---------------+----------------+
| root          | 127.0.0.1      |
| zabbix        | 127.0.0.1      |
| test          | 192.168.0.1    |
| root          | localhost      |
+---------------+----------------+
11 rows in set (0.00 sec)
mysql> quit

#防火牆開放數據庫的端口,或者關閉防火牆

a。關閉防火牆

 

[root@localhost ~]# systemctl status iptables
 #    systemctl stop iptables
● iptables.service - IPv4 firewall with iptables
   Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled)
   Active: inactive (dead) since Tue 2019-08-13 15:43:07 CST; 1 weeks 1 days ago
 Main PID: 1969 (code=exited, status=0/SUCCESS)

b。防火牆規則

 

[root@localhost ~]# vim /etc/sysconfig/iptables
-A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT
[root@localhost ~]#  ystemctl restart iptables
mysql-proxy服務器

#下載mysql-proxy包

 

wget https://downloads.mysql.com/archives/get/file/mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz

#安裝

 

[root@e test]# tar zxf mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz -C /usr/local/
cd /usr/local/
[root@e local]# ls
 mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit
[root@e local]# mv mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit/   mysql-proxy
[root@e local]# ls
 mysql-proxy
[root@e local]# mkdir /usr/local/mysql-proxy/{conf,log} -p
[root@e mysql-proxy]# tail -3 /etc/profile
#(添加下面三行內容)
LUA_PATH="/usr/local/mysql-proxy/share/doc/mysql-proxy/?.lua"
export LUA_PATH
export PATH=$PATH:/usr/local/mysql-proxy/bin
[root@e mysql-proxy]# source /etc/profile

 

[root@e conf]# vi mysql-proxy.conf
[mysql-proxy]
user=www
daemon=true
keepalive=true
plugins=proxy,admin
###日誌級別
log-level=info
log-file=/usr/local/mysql-proxy/log/mysql-proxy.log
###本機ip地址
proxy-address=14.152.90.6:9196         #本地的ip和代理端口,後期navicat連接使用的地址

##backend主   注意addresses
proxy-backend-addresses=192.168.0.2:3306      #mysql的內網ip地址

##proxy的管理用戶admin的IP和端口
admin-address=192.168.0.1:9197               #mysql-proxy的內網

###下面的三個參數必須設定,否則mysql-proxy服務啓動不了的
admin-username=test1
admin-password=test1
###admin的lua腳本地址;
admin-lua-script=/usr/local/mysql-proxy/lib/mysql-proxy/lua/admin.lua

#啓動

 

[root@e conf]# chmod 660 mysql-proxy.conf
[root@e conf]# /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf
[root@e conf]# netstat -luntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 14.152.90.6:9196        0.0.0.0:*               LISTEN      11502/mysql-proxy
tcp        0      0 192.168.0.1:9197        0.0.0.0:*               LISTEN      11502/mysql-proxy

#登陸mysql-proxy管理賬戶,查看代理管理列表

 

[root@e bin]# mysql -utest1  -ptest1  -h192.168.0.1  --port=9197
Warning: Using a password on the command line interface can be insecure.
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, 2018, 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> SELECT * FROM backends;
+-------------+------------------+---------+------+------+-------------------+
| backend_ndx | address          | state   | type | uuid | connected_clients |
+-------------+------------------+---------+------+------+-------------------+
|           1 | 192.168.0.2:3306 | unknown | rw   | NULL |                 0 |
+-------------+------------------+---------+------+------+-------------------+
1 row in set (0.00 sec)

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.00 sec)

mysql> quit

#遠程連接mysql

 

[root@e bin]# mysql -utest -p'lmdb(A7105#B9606@com!'  -h 192.168.0.2
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 223226
Server version: 5.7.27-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| CodeDB             |
| mysql              |
| mysqldata          |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

mysql> quit
######################成功######################


#防火牆規則(通過navicat,sqlyog等工具連接數據庫的時候,當然需要開放本地的代理端口)

a。關閉防火牆

 

[root@localhost ~]# systemctl status iptables
 # systemctl stop iptables
● iptables.service - IPv4 firewall with iptables
   Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled)
   Active: inactive (dead) since Tue 2019-08-13 15:43:07 CST; 1 weeks 1 days ago
 Main PID: 1969 (code=exited, status=0/SUCCESS)

b。添加防火牆規則

 

[root@localhost ~]# vim /etc/sysconfig/iptables
-A INPUT -p tcp -m tcp --dport 9196 -j ACCEPT
[root@localhost ~]#  ystemctl restart iptables


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章