欢迎光临!
若无相欠,怎会相见

MySQL(MariaDB):解决 “Host ‘*’ is not allowed to connect to this MariaDB server”

序言

在 NAS 上安装了 Synology 官方的 MariaDB 套件, 方便存储一些数据。

但是遇到了无法连接的情况, 这里记录一下, 方便以后使用。

正文部分来自于 https://blog.csdn.net/qq_45085295/article/details/123442231, 做了部分修改。

正文

报的错误如下:

Host '192.168.31.x' is not allowed to connect to this MariaDB server.

就是内网的某个机器不被允许连接数据库服务器。

简单的权限问题,原因:

MySQL没有设置远程(或局域网)的访问权限

root@DS423plus:~# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 28
Server version: 10.3.32-MariaDB Source distribution

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)]> use mysql;
Database changed
MariaDB [mysql]> select user, host from user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1       |
| root | localhost |
+------+-----------+
3 rows in set (0.002 sec)

MariaDB [mysql]> grant all on *.* to 'root'@'%' identified by "<你的密码>" with grant option;
Query OK, 0 rows affected (0.026 sec)
MariaDB [mysql]> select user, host from user;                                               +------+-----------+
| user | host      |
+------+-----------+
| root | %         |
| root | 127.0.0.1 |
| root | ::1       |
| root | localhost |
+------+-----------+
4 rows in set (0.000 sec)

OK, 这就完了。 再次使用数据库工具就能正常连接了。

可以先查询当前 MySQL 的用户都有什么, 顺手把没用的清一清。 如果和图中一样有一条 host=% 的用户信息, 那么它其实就是一个远程访问这个 MySQL 的账号。 就不用再设置了。

如果没有或者是忘记了密码, 就执行下条命令:

MariaDB [(none)]> grant all privileges on *.* to '用户名'@'%' identified by ‘密码’ with grant option;

注释:

  1. 第一个 * , 表示被授权访问的库
  2. 第二个 *, 表示库下的所有表
  3. ‘用户名’@’%’ 用户名 表示授权用户, % 表示任意的 ip 地址
  4. 【identified by ‘密码’】 访问 MySQL 的密码。 如果想要设置免密码访问, 这条可以去掉

整句命令的意思就是, 允许在任何 IP 地址上用这个用户名和密码来访问这个 MySQL。

MariaDB [(none)]> flush privileges;

更新服务, 或者直接重启mariadb server 也行

如果还是连不上mysql,就关一下服务器的防火墙

systemctl stop firewalld.service

如果还是连不上,查看一下3306端口是否已经开放。

结语

以上经本人实测可以解决问题。

如有错误, 敬请指出, 感谢指正!  — 2025-01-04  21:55:10

赞(0) 打赏
转载请注明:飘零博客 » MySQL(MariaDB):解决 “Host ‘*’ is not allowed to connect to this MariaDB server”
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

欢迎光临