如何配置MySQL进行本地和远程访问?

时间:2018-02-16 14:10:51

标签: mysql mariadb raspberry-pi3 raspbian

我在Raspberry Pi上使用MySQL服务器版本: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

我试过这个:

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中,使用bind-address注释,我可以从localhost访问,但不能使用SQL Workbench从远程主机访问。使用bind-address取消注释时,我无法从localhost访问,但我可以使用SQL Workbench从远程主机访问,例如:

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

这是我的用户表:

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

如何配置服务器以允许来自任何IP的root用户的本地和远程访问?

3 个答案:

答案 0 :(得分:4)

bind-address = 0.0.0.0 上添加一行,与[mysqld]一样:

[mysqld]
bind-address = 0.0.0.0

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

答案 1 :(得分:2)

根据this link,似乎mysql客户端无法识别绑定地址。

为了在本地连接,我必须使用这一行:

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

答案 2 :(得分:0)

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

之后不起作用
systemctl restart mysqld

最后使用bind-address起作用:

# 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