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

Cri*_*n M 3 mysql raspbian raspberry-pi3

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

这是我的/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
Run Code Online (Sandbox Code Playgroud)

我已经试过了:

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
Run Code Online (Sandbox Code Playgroud)

在中my.cnf,注释了绑定地址,我可以localhost使用SQL Workbench从进行访问,但不能从远程主机进行访问。

在没有注释绑定地址的情况下,我无法从进行访问localhost,但是可以使用SQL Workbench从远程主机进行访问,例如:

mysql -u root
mysql: unknown variable 'bind-address=0.0.0.0'
Run Code Online (Sandbox Code Playgroud)

这是我的用户表:

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

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

Ber*_*fen 7

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

[mysqld]
bind-address = 0.0.0.0
Run Code Online (Sandbox Code Playgroud)

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