因为项目需要,在阿里云申请了一个服务器
通过远程连接,安装mysql-创建数据库-创建用户-设定用户可以被在任意主机上进行连接-使用navicat新建连接-测试-连接失败;
查找原因:
-
阿里云防火墙没有开启3306端口
image.png
打开控制台-安全-防火墙-开启-重启服务器-测试连接-失败
-
ubuntu18.04 未开启3306端口对外部访问的过滤导致外部访问无法进入,通过netstat命令,检查3306端口,果然只有一个127.0.0.1:3306的监听端口
ub64@ub64-1804-1:~$ netstat -ntpl
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
检查是否配置mysqld.cnf文件,bind-address的设置值问题
vim /etc/mysql/mysql.conf.d/mysqld.cnf
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
把bind-address修改成0.0.0.0,无限制,重启服务器,重新检查netstat,3306端口的访问已经有所有来源地址的监听了。
ub64@ub64-1804-1:~$ netstat -ntpl
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
重启数据库,继续测试,还是连接失败;
- 防火墙问题,检查防火墙通过规则,果然grep一下没有见到3306的端口记录。
ub64@ub64-1804-1:~$ sudo iptables -L -n | grep 3306
ub64@ub64-1804-1:~$
···
iptables新增一条3306的端口允许通过规则,在重新检查一下iptables,这下有了。
···
ub64@ub64-1804-1:~$ sudo iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
ub64@ub64-1804-1:~$ sudo iptables -L -n | grep 3306
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:3306
ub64@ub64-1804-1:~$
重新测试程序,程序访问成功。mysql的访问,不仅要设置user表用户的访问控制权限,还要设置mysqld.cnf的bind-address,同时系统防火墙规则也要配置好3306的端口通过权限。这3个地方的控制缺一不可
(补充:表用户访问控制权限更改----改表法:
登录MySQL:mysql -uroot -p ,输入密码,登陆进去
选择mysql表,将host只能本地访问的 localhost 改为 %
mysql>use mysql;
mysql>update user set host="%" where user="chase";
mysql> select host,user from user;
+-----------+------------------+
| host | user |
+-----------+------------------+
| % | chase |
| localhost | debian-sys-maint |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+------------------+
+ rows in set (0.00 sec)
mysql>flush privileges; //刷新权限
mysql>exit;
)
网友评论