目录

1、程序报错:(不能远程连接数据库)

2、测试是否能ping到远程机器

3、登录数据库

4、仍无法连接到数据库,可能不能访问端口号,再次测试(端口telnet 不通)

5、MySQL远程登录连接成功

6、如果上面方法还是没有解决。直接kill进程,重启。


1、程序报错:(不能远程连接数据库)

┌──(root💀kali2022)-[~]
└─# mysql -u root -p 'root' -h 192.168.172.130
ERROR 2002 (HY000): Can't connect to server on '192.168.172.130' (115)

2、测试是否能ping到远程机器

ping 192.168.172.130  (可以ping通,说明网络是连通的)

3、登录数据库

──(root💀kali2022)-[~]
└─# mysql -u root -p                                                                       1 ⨯
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 10.6.8-MariaDB-1 Debian buildd-unstable

Copyright (c) 2000, 2018, 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                                  |
+-------------+-----------+-------------------------------------------+
| mariadb.sys | localhost |                                           |
| root        | localhost | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| mysql       | localhost | invalid                                   |
+-------------+-----------+-------------------------------------------+
3 rows in set (0.022 sec)

添加%

MariaDB [(none)]> show grants\G
*************************** 1. row ***************************                                                                                                                                                                               
Grants for root@localhost: GRANT ALL PRIVILEGES ON *.* TO `root`@`localhost` IDENTIFIED BY PASSWORD '*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B' WITH GRANT OPTION                                                                            
*************************** 2. row ***************************                                                                                                                                                                               
Grants for root@localhost: GRANT PROXY ON ''@'%' TO 'root'@'localhost' WITH GRANT OPTION                                                                                                                                                     
2 rows in set (0.000 sec)                                                                                                                                                                                                                    
                                                                                                                                                                                                                                             
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'  IDENTIFIED BY PASSWORD '*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B';                                                                                                     
Query OK, 0 rows affected (0.001 sec)  

MariaDB [(none)]> select user,host,password from mysql.user;
+-------------+-----------+-------------------------------------------+
| User        | Host      | Password                                  |
+-------------+-----------+-------------------------------------------+
| mariadb.sys | localhost |                                           |
| root        | localhost | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| mysql       | localhost | invalid                                   |
| root        | %         | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+-------------+-----------+-------------------------------------------+
4 rows in set (0.001 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.001 sec)   

再次远程访问:

┌──(root💀kali2022)-[~]
└─# mysql -u root -p 'root' -h '192.168.172.130'
Enter password: 
ERROR 2002 (HY000): Can't connect to server on '192.168.172.130' (115)

4、仍无法连接到数据库,可能不能访问端口号,再次测试(端口telnet 不通)

┌──(root💀kali2022)-[~]
└─# telnet 192.168.172.130 3306                                                                     127 ⨯
Trying 192.168.172.130...
telnet: Unable to connect to remote host: Connection refused

1)可能是防火墙未关,关闭防火墙,再次访问(失败后尝试第2条)

┌──(root💀kali2022)-[~]
└─# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
┌──(root💀kali2022)-[~]
└─#  service firewalld stop
Redirecting to /bin/systemctl stop firewalld.service

2)未访问成功,确认mysql是否开启对外端口,用下面任意一条命令查看端口号是否被占用

┌──(root💀kali2022)-[~]
└─# netstat -napt                                                                                     1 ⨯
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address     Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:3306    0.0.0.0:*               LISTEN      2453/mariadbd                                                                                                             
┌──(root💀kali2022)-[~]
└─# netstat -lnpt | grep 3306
tcp      0      0 127.0.0.1:3306      0.0.0.0:*           LISTEN      2453/mariadbd  

发现3306对应的地址还是127.0.0.1,说明该端口对本地可见,对外不可见

##开启对外端口,编辑50-server.cnf 注释掉捆绑的地址
┌──(root💀kali2022)-[/etc/mysql/mariadb.conf.d]
└─# vim 50-server.cnf 

将下面这行注释掉
#bind-address           = 127.0.0.1

┌──(root💀kali2022)-[/etc/init.d]
└─# netstat -lnpt | grep 3306                                                               1 ⨯
tcp        0      0 127.0.0.1:3306       0.0.0.0:*           LISTEN      2732/mariadbd       
                                                                                                
┌──(root💀kali2022)-[/etc/init.d]
└─# kill -9 2732
                                                                                                
┌──(root💀kali2022)-[/etc/init.d]
└─# systemctl restart mariadb.service  #重启服务
                                                                                                
┌──(root💀kali2022)-[/etc/init.d]     #再次查看端口,运行正常
└─# netstat -lnpt | grep 3306      
tcp     0   0 0.0.0.0:3306        0.0.0.0:*           LISTEN      3241/mariadbd       
tcp6    0   0 :::3306             :::*                LISTEN      3241/mariadbd       

5、MySQL远程登录连接成功

┌──(root💀kali2022)-[~]
└─# telnet 192.168.172.130 3306                                                            1 ⨯
Trying 192.168.172.130...
Connected to 192.168.172.130.
Escape character is '^]'.
Z
5.5.5-10.6.8-MariaDB-1+s'`3z/y��!8G;2(q%^-c+Kmysql_native_passwordConnection closed by foreign host.
                                                                                               
┌──(root💀kali2022)-[~]
└─# mysql -u root -h '192.168.172.130' -p                                                  1 ⨯

Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 10.6.8-MariaDB-1 Debian buildd-unstable

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

6、如果上面方法还是没有解决。直接kill进程,重启。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐