1、程序报错:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link
2、尝试远程连接数据库:
[root@inode1 ~]# mysql -h192.168.1.165 -uroot -p123456
ERROR 2003 (HY000): Can’t connect to MySQL server on ‘192.168.1.165’ (113)
3、问题找到了,不能连接远程数据库。
4、测试是否能ping到远程机器
[root@inode1 ~]# ping 192.168.1.165
PING 192.168.1.165 (192.168.1.165) 56(84) bytes of data.
64 bytes from 192.168.1.165: icmp_seq=1 ttl=128 time=0.776 ms
64 bytes from 192.168.1.165: icmp_seq=2 ttl=128 time=1.26 ms
64 bytes from 192.168.1.165: icmp_seq=3 ttl=128 time=1.19 ms
^C
— 192.168.1.165 ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2008ms
rtt min/avg/max/mdev = 0.776/1.078/1.268/0.215 ms
说明网络是连通的,再测试以下端口号
5、登录到数据库端
[root@dbServer ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 5.5.52-MariaDB MariaDB Server
Copyright © 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
MariaDB [(none)]> select user,host,password from mysql.user;
±-----±----------±------------------------------------------+
| user | host | password |
±-----±----------±------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | 127.0.0.1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | ::1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
±-----±----------±------------------------------------------+
添加%
MariaDB [(none)]> grant all privileges on . to ‘root’@’%’ identified by ‘123456’;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql 8 创建用户 授权方式
1、先创建权限记录:create user ‘root’@’%’ identified by ‘root123’;
2、授权:grant all privileges on . to ‘root’@’%’ with grant option;
3. flush privileges;
再次远程访问:
[root@hadron ~]# mysql -h192.168.1.165 -uroot -p123456
ERROR 2003 (HY000): Can’t connect to MySQL server on ‘192.168.1.165’ (113)
6、还是无法连接到数据库,可能不能访问端口号,再次测试
[root@hadron ~]# telnet 192.168.1.165 3306
Trying 192.168.1.165…
telnet: connect to address 192.168.1.165: No route to host
找到问题所在了,防火墙屏蔽了这个端口号。
7、直接关闭防火墙即可。
[root@dbServer ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
[root@dbServer ~]# service firewalld stop
Redirecting to /bin/systemctl stop firewalld.service
或
关闭防火墙
service 方式
开启: service iptables start
关闭: service iptables stop
再次远程访问
[root@hadron ~]# telnet 192.168.1.165 3306
Trying 192.168.1.165…
telnet: connect to address 192.168.1.165: No route to host
[root@hadron ~]# mysql -h192.168.1.165 -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 5.5.52-MariaDB MariaDB Server
Copyright © 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
MariaDB [(none)]>
成功了。
8.window添加规则
https://blog.csdn.net/Zzc_IT_log/article/details/80499884