Loading

本地设备无法访问服务器端的Maraidb的解决实例

问题背景

本人在腾讯云中有一个轻量应用服务器,数据库使用的是10.5.15-MariaDB-0+deb11u1 Debian 11,一直以来都是在服务器中使用数据库,没有尝试过从本地设备连接到服务器的数据库中。

在尝试开放防火墙3306端口和给予链接账户所有的权限之后,本地多次尝试链接仍然失败,错误信息一直为2002 - Can't connect to server on 'xxx' (36)

解决过程

查找MariaDB的官网,找到个这个网页:Configuring MariaDB for Remote Client Access - MariaDB Knowledge Base

文章中说明了一个参数bind-address

bind-address requires a little bit of background information. A given server usually has at least two networking interfaces (although this is not required) and can easily have more. The two most common are a Loopback network device and a physical Network Interface Card (NIC) which allows you to communicate with the network. MariaDB is bound to the loopback interface by default because it makes it impossible to connect to the TCP port on the server from a remote host (the bind-address must refer to a local IP address, or you will receive a fatal error and MariaDB will not start). This of course is not desirable if you want to use the TCP port from a remote host, so you must remove this bind-address directive (MariaDB only supports one bind-address, but binds to 0.0.0.0, or :: (every IP) if the bind-address directive is left out).

也就是说MariaDB默认与回环接口绑定,从而导致远程主机不可能连接到服务器上MariaDB的端口,我们只需要将bind-address字段重新设置成0.0.0.0::就能解决问题了

进入目录/etc/mysql,这里存放着MariaDB的配置文件,我只需要确定哪个配置文件中包含着skip-networking这个字段,执行命令查找包含该字段的文件:

cd /etc/mysql
sudo grep -rn "bind-address"

随后返回:

mariadb.conf.d/60-galera.cnf:17:#bind-address = 0.0.0.0
mariadb.conf.d/50-server.cnf:30:bind-address            = 127.0.0.1

其中,第二行返回的是一个没有注释的字段,进入文件mariadb.conf.d/50-server.cnf,把对应的值改为0.0.0.0

随后再重启MariaDB服务:

sudo systemctl restart mariadb

再次尝试链接就能成功啦。

posted @ 2022-05-07 20:28  Frank_Ou  阅读(267)  评论(0编辑  收藏  举报