简体   繁体   English

远程Mysql删除所有连接并仅允许localhost和IP地址

[英]Remote Mysql drop all connections and allow only localhost and IP address

I have enabled remote MySQL by editing 我通过编辑启用了远程MySQL

 /etc/mysql/mysql.conf.d/mysqld.cnf 
    bind-address = 0.0.0.0

Now i can access MySQL via any remote ip. 现在我可以通过任何远程ip访问MySQL。

What i want is to disable all connections to my MySQL with IP tables and enable only access from localhost and one IP adress. 我想要的是禁用与IP表的MySQL连接,并只允许从localhost和一个IP地址访问。 I did the following 我做了以下

/sbin/iptables -A INPUT -p tcp -d 127.0.0.1 --dport 3306 -j ACCEPT
/sbin/iptables -A INPUT -p tcp -d 16x.xxx.xx.xx --dport 3306 -j ACCEPT
/sbin/iptables -A INPUT -p tcp --dport 3306 -j DROP

The following code will drop all connections and won't accept localhost or remote ip. 以下代码将删除所有连接,并且不接受localhost或远程ip。 I'm using Ubuntu 16.10 . 我正在使用Ubuntu 16.10。


EDIT: I've also tried a chain 编辑:我也试过一个链

iptables -N mysql 
iptables -A mysql --src 127.0.0.1 -j ACCEPT
iptables -A mysql --src 14x.xxx.xx.xx -j ACCEPT
iptables -A mysql -j DROP 
iptables -I INPUT -m tcp -p tcp --dport 3306 -j mysql   

After DROP line in every possible way port is blocked 在每种可能的方式DROP行后,端口被阻塞

The order of the rules is not correct. 规则的顺序不正确。

Line 线

/sbin/iptables -A INPUT -p tcp --dport 3306 -j DROP

should come last. 应该到最后。

The rule 规则

2 ACCEPT tcp -- 14x.xxx.xxx.xx 14x.xxx.xxx.xx tcp dpt:3306

doesn't look right. 看起来不对劲。 It looks like source and destination IP addresses are the same. 看起来源和目标IP地址是相同的。 You have to whitelist the IP address of the server which you are connecting from. 您必须将要连接的服务器的IP地址列入白名单。

In order to identify IP address add the following iptables rule before the drop rule 为了识别IP地址,在删除规则之前添加以下iptables规则

iptables -I INPUT -m tcp -p tcp --dport 3306  -m limit --limit 5/min -j LOG --log-prefix "Mysql access log: " 

The log goes in dmesg and syslog ( /var/log/syslog on Ubuntu) and looks like this 日志进入dmesg和syslog(Ubuntu上的/ var / log / syslog),看起来像这样

Nov 28 08:55:57 myServer kernel: Mysql access log: IN=eth0 OUT= MAC=00:19:99:ce:15:cb:b0:c6:9a:67:d6:81:08:00 SRC=1.2.3.4 DST=5.6.7.8 LEN=60 TOS=0x10  PREC=0x00 TTL=56 ID=63880 DF PROTO=TCP SPT=40807 DPT=3306 WINDOW=14600 RES=0x00 SYN URGP=0 

In my example source ip address which should be whitelisted is 1.2.3.4 在我的示例中,应该列入白名单的源IP地址是1.2.3.4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM