配置 MySQL 以进行本地和远程访问

Configure MySQL for local and remote access

我正在使用 MySQL 服务器版本:10.1.23-MariaDB-9+deb9u1 Raspbian 9.0 Raspberry Pi。

这是我的 /etc/mysql/my.cnf:

# The MariaDB configuration file
#
# The MariaDB/MySQL tools read configuration files in the following order:
# 1. "/etc/mysql/mariadb.cnf" (this file) to set global defaults,
# 2. "/etc/mysql/conf.d/*.cnf" to set global options.
# 3. "/etc/mysql/mariadb.conf.d/*.cnf" to set MariaDB-only options.
# 4. "~/.my.cnf" to set user-specific options.
#
# If the same option is defined multiple times, the last one will apply.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.

#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]

# Import all .cnf files from configuration directory
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mariadb.conf.d/

#bind-address = 0.0.0.0

我试过这个:

sudo mysql_secure_installation
Change root password: y
Password
Retyped password
Remove anonymous users: y
Disallow root login remotely: n
Remove test database: y
Reload priviledges: y

CREATE USER 'root'@'%.%.%.%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%.%.%.%' WITH GRANT OPTION;
FLUSH PRIVILEDGES;
service mysql restart

my.cnf 中,注释了绑定地址后,我可以从 localhost 访问,但不能使用 SQL Workbench.

从远程主机访问

取消注释绑定地址后,我无法从 localhost 访问,但我可以使用 SQL Workbench 从远程主机访问,例如:

mysql -u root
mysql: unknown variable 'bind-address=0.0.0.0'

这是我的用户 table:

MariaDB [(none)]> select user, host, password from mysql.user;
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *054D119DEAD56E226D8356557796BFA72E71BA40 |
| root | %.%.%.%   | *054D119DEAD56E226D8356557796BFA72E71BA40 |
| root | %         | *054D119DEAD56E226D8356557796BFA72E71BA40 |
+------+-----------+-------------------------------------------+

如何配置服务器以允许根从任何 IP 进行本地和远程访问?

根据 this link 看来 mysql 客户端无法识别绑定地址。

为了在本地连接,我必须使用这条线:

mysql --no-defaults -u[username] -p[password] [database]

使用 [mysqld] 在 bind-address = 0.0.0.0 上添加一行,例如:

[mysqld]
bind-address = 0.0.0.0

在你的情况下,服务器和本地客户端都读取绑定地址,客户端想要连接到 IP 0.0.0.0

以防万一 - 我首先将绑定地址放在两行之间,但在

之后它不起作用
systemctl restart mysqld

在末尾加上绑定地址就可以了:

# The MariaDB configuration file 
#
# The MariaDB/MySQL tools read configuration files in the following order:
# 1. "/etc/mysql/mariadb.cnf" (this file) to set global defaults,
# 2. "/etc/mysql/conf.d/*.cnf" to set global options.
# 3. "/etc/mysql/mariadb.conf.d/*.cnf" to set MariaDB-only options.
# 4. "~/.my.cnf" to set user-specific options.
#
# If the same option is defined multiple times, the last one will apply.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.

#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]

# Import all .cnf files from configuration directory
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mariadb.conf.d/

[mysqld]
bind-address = 0.0.0.0