【问题标题】:Mysql MariaDB is not working with remote connectionsMysql MariaDB 不适用于远程连接
【发布时间】:2026-01-18 10:20:06
【问题描述】:

我已经摸索了一天,可能阅读了很多文章,以及如何让我的 MariaDB 监听远程连接。不幸出现以下错误。

Host 'xxx.xxx.xxx.xxx' is not allowed to connect to this MariaDB server.

我还阅读了此 * questionanotherQuestion 并成功创建新用户并通过以下 MySQL 查询授予所有权限。

CREATE USER 'ahsensaeed'@'%' IDENTIFIED BY 'my_password';
GRANT ALL PRIVILEGES ON *.* TO 'ahsensaeed'@'%' WITH GRANT OPTION;

以下是我的 ahsensaeed 用户授权。

MariaDB [mysql]> show grants for 'ahsensaeed'@'%';
+--------------------------------------------------------------------------------------------------------------------------------------+
| Grants for ahsensaeed@%                                                                                                              |
+--------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'ahsensaeed'@'%' IDENTIFIED BY PASSWORD '*F794ABE2A12665587C6B6D8B61E2F7E987711AFA' WITH GRANT OPTION |
+--------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

最后,我只是刷新了权限,然后,我转到我的 MariaDB 配置文件并对其进行编辑。下面是我放置 MariaDB conf 文件的路径。

/etc/mysql/mariadb.conf.d/50-server.cnf 

下面显示了我的 MariaDB 文件块。

[mysqld]

#
# * Basic Settings
#
user        = mysql
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
port        = 3306
basedir     = /usr
datadir     = /var/lib/mysql
tmpdir      = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 0.0.0.0
.....
.....

然后我只需通过 /etc/init.d/mysql restart 重新启动 mysql 服务。我还在客户端上为 mysql 打开了 3306 端口。

当上述解决方案不起作用时,我还在 /etc/mysql/conf.d/mysql.cnf 文件中添加 bind-address = 0.0.0.0,但仍然失败并出现错误。

以下显示了我如何从我的服务器请求 MariaDB 数据库。

-> mysql -uahsensaeed -p -h hostIp

然后我得到以下错误。

ERROR 1130 (HY000): Host 'hostIp' is not allowed to connect to this MariaDB server

编辑添加了主机和用户数据。

MariaDB [(none)]> select User,Host from mysql.user;
+------------+-----------+
| User       | Host      |
+------------+-----------+
| ahsensaeed | %         |
| root       | localhost |
+------------+-----------+
2 rows in set (0.00 sec)

任何帮助将不胜感激。

【问题讨论】:

  • 这个“*F794ABE2A12665587C6B6D8B61E2F7E987711AFA”不是来自“my_password”的密码
  • @BerndBuffen 不,这仅适用于 *,实际密码为 qwertyasdf
  • 能否请您发布 SELECT * FROM mysql.user WHERE USER LIKE '%ahsensaeed%'; 的输出
  • @BerndBuffen 添加了输出。
  • 你试试FLUSH PRIVILEGES;

标签: mysql linux mariadb


【解决方案1】:

这对我有用(我使用 Ubuntu 18.04 作为虚拟机,使用带有 Vagrant 的 Windows 10):

sudo mysql -e "CREATE DATABASE {db_name};"
sudo mysql -e "CREATE USER {user_name}@localhost IDENTIFIED BY '{password}';"
sudo mysql -e "UPDATE mysql.user SET Host='%' WHERE User='{user_name}';"
sudo mysql -e "GRANT ALL PRIVILEGES ON {db_name}.* TO '{user_name}'@'%';"
sudo mysql -e "FLUSH PRIVILEGES;"
sudo systemctl restart apache2
sudo systemctl restart mariadb

之后,您应该编辑文件:

/etc/mysql/mariadb.conf.d/50-server.cnf

并将 bind-address 属性设置为您的主机 IP 地址,或者将这一行注释掉。

最后,运行命令:

sudo systemctl restart apache2
sudo systemctl restart mariadb

我希望这能满足您的需求

【讨论】: