无法连接AWS上的远程MySQL服务器,但SSH隧道可以使用。

3
我将尝试设置MySQL,以便用户“imbnpandmkexby”可以从任何远程IP地址或本地连接到数据库“de0rllo43ct314”。
以下是我已经采取的步骤:
1)在我的MySQL配置中,我已经注释掉了bind-address行,验证文件中没有skip-networking,并重新启动了MySQL。
#/etc/mysql/my.cnf:
#bind-address    = 127.0.0.1

2)我在所需的数据库“de0rllo43ct314”上为用户“imbnpandmkexby”添加了远程权限(使用“%”符号):

[ remote ] > mysql -u root -p
[ mysql  ] > GRANT ALL PRIVILEGES ON de0rllo43ct314.* TO 'imbnpandmkexby'@'localhost' IDENTIFIED BY 'passwordhere' WITH GRANT OPTION;
[ mysql  ] > GRANT ALL PRIVILEGES ON de0rllo43ct314.* TO 'imbnpandmkexby'@'127.0.0.1' IDENTIFIED BY 'passwordhere' WITH GRANT OPTION;
[ mysql  ] > GRANT ALL PRIVILEGES ON de0rllo43ct314.* TO 'imbnpandmkexby'@'%' IDENTIFIED BY 'passwordhere' WITH GRANT OPTION;
[ mysql  ] > FLUSH PRIVILEGES;
[ mysql  ] > select * from mysql.user\G

这将输出:
*************************** 6. row ***************************
                  Host: localhost
                  User: imbnpandmkexby
              Password: *0000000000000000000000
...
*************************** 7. row ***************************
                  Host: 127.0.0.1
                  User: imbnpandmkexby
              Password: 
...
*************************** 8. row ***************************
                  Host: %
                  User: imbnpandmkexby
              Password: 
...

3) 现在,我可以使用Sequel Pro连接SSH隧道。用户似乎拥有所有正确的权限。

enter image description here

4) 接下来,我打开了一个防火墙端口,并验证MySQL正在监听该端口:

[ remote ] > sudo iptables -I INPUT 10 -p tcp --dport 3306 -j ACCEPT
[ remote ] > sudo iptables -L

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             state     RELATED,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp     dpt:4505
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp     dpt:4506
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:https
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http-alt
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:zabbix-agent
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:zabbix-trapper
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:mysql
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
ACCEPT     icmp --  anywhere             anywhere             icmp echo-request
LOG        all  --  anywhere             anywhere             limit: avg 5/min burst 5 LOG level debug prefix "iptables denied: "
REJECT     all  --  anywhere             anywhere             reject-with icmp-port-unreachable

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  anywhere             anywhere             reject-with icmp-port-unreachable

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere    

既然这个盒子托管在亚马逊EC2上,我也在其安全组中打开了3306端口:

enter image description here

5) 我可以通过telnet进入该端口:

Trying 00.00.00.000...
Connected to ec2-00.00.00.000.us-west-2.compute.amazonaws.com.
Escape character is '^]'.

=========== 我卡住了:============

显示00.00.00.000而不是实际IP地址

当我尝试从我的本地计算机连接数据库时,它无法工作:

[ local ] > mysql -u imbnpandmkexby -h 00.00.00.000 -p
[ local ] > Enter password: 
[ local ] > ERROR 2003 (HY000): Can't connect to MySQL server on '00.00.00.000' (61)

我能连接到dreamhost服务器上的数据库,所以看起来不是我的问题所在造成的阻塞。
[ local ] > mysql -u dreamhost_user -h mysql.dreamhostdomain.com -p
[ local ] > Enter password: 
[ local ] > Welcome to the MySQL monitor.  Commands end with ; or \g.

我是否缺少一层权限?


这不是授权问题,而是涉及到EC2或您所在位置的防火墙。如果您在办公室,一些公司会限制对某些端口的出站访问。 - datasage
@datasage,我更新了我的描述,以展示我可以从本地机器连接到其他MySQL数据库。 - ninapavlich
2个回答

2

好的,终于搞清楚了!我遇到了两个问题:

1)我的SQL规则在iptables中被REJECT规则覆盖了:

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:4505
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:4506
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:https
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http-alt
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:zabbix-agent
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:zabbix-trapper
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
ACCEPT     icmp --  anywhere             anywhere             icmp echo-request
LOG        all  --  anywhere             anywhere             limit: avg 5/min burst 5 LOG level debug prefix "iptables denied: "
REJECT     all  --  anywhere             anywhere             reject-with icmp-port-unreachable
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:mysql

我所做的是删除最后一条规则,然后将其重新添加到第10个位置:

[ remote ] > iptables -vnL --line-numbers ##Prints rules along with line numbers
[ remote ] > iptables -D INPUT 14
[ remote ] > sudo iptables -I INPUT 10 -p tcp --dport 3306 -j ACCEPT

我知道这是向正确方向迈出的一步,因为现在我可以通过telnet连接到这个盒子(“telnet 00.00.00.000 3306”)。

2)我的第二个问题是,我的MySQL用户只在“localhost”用户上设置了密码,而没有在具有访问“127.0.0.1”或“%”权限的用户上设置。结果发现每个用户 - 主机组合都需要一个密码。现在当我在MySQL控制台中运行“select * from mysql.user\G”时,我得到:

*************************** 6. row ***************************
                  Host: localhost
                  User: imbnpandmkexby
              Password: *0000000000000000000000
...
*************************** 7. row ***************************
                  Host: 127.0.0.1
                  User: imbnpandmkexby
              Password: *0000000000000000000000
...
*************************** 8. row ***************************
                  Host: %
                  User: imbnpandmkexby
              Password: *0000000000000000000000
...

0

在 MySQL 服务器上运行 tcpdump,以确保 tcp/3306 真的到达了该服务器,或查看它被阻塞在哪里。

如果连接到远程 tcp/3306 卡住并超时,那么它被黑洞或防火墙拒绝了。如果立即返回无法连接,则很可能已经全部到达服务器,但被拒绝(并返回了 tcp 响应)。


嗨@nandoP,我不熟悉tcpdump,所以我在谷歌上搜索了一个示例命令。这是您建议的吗?还是有更好的使用tcpdump的方法?命令>>>“tcpdump -i eth0 -s 0 -l -w - dst port 3306”返回了>>>**“listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes”** - ninapavlich
这就是意图。如果服务器正在接收流量,数据包将开始淹没您的控制台。尝试运行“tcpdump -i eth0”,看看我的意思(按Ctrl-C停止)。此测试显示tcp / 3306流量未到达您的AWS实例。在办公室周界设备(防火墙)上进行相同的测试,并查看流量是否通过IPSec隧道传输到Amazon。您连接的机器网络是否处于IPSec第2阶段配置中? - nandoP

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接