我知道这命令:

GRANT ALL PRIVILEGES
ON database.*
TO 'user'@'yourremotehost'
IDENTIFIED BY 'newpassword';

但它只允许我授予一个特定的IP地址来访问远程MySQL数据库。如果我想让任何远程主机都能访问这个MySQL数据库呢?我怎么做呢?基本上,我将这个数据库公开,这样每个人都可以访问它。


当前回答

GRANT ALL PRIVILEGES ON *.* TO 'user'@'ipadress'

其他回答

你需要修改mysql的配置文件:

开始编辑mysql配置文件

vim /etc/mysql/my.cnf

add:

bind-address = 0.0.0.0

假设以上步骤已经完成,MySql端口3306可以远程访问;不要忘记在mysql配置文件中绑定公网ip地址。

例如在我的ubuntu服务器上:

#nano /etc/mysql/my.cnf

在文件中,搜索[mysqld]区域块并添加新的绑定地址,在本例中为192.168.0.116。它看起来是这样的

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

bind-address        = 127.0.0.1    
bind-address        = 192.168.0.116

.....    
......

如果你愿意,你可以删除localhost(127.0.0.1)绑定,但是你必须明确地给出一个IP地址来访问本地机器上的服务器。

最后一步是重新启动MySql服务器 在ubuntu ()

stop mysql

start mysql

或# / etc / init。D /mysql重启其他系统

现在MySQL数据库可以通过以下方式远程访问:

mysql -u username -h 192.168.0.116 -p

比如我的CentOS

sudo gedit /etc/mysql/my.cnf

注释掉下面的行

#bind-address = 127.0.0.1

然后

Sudo服务mysqld重启

使用该命令:

GRANT ALL ON yourdatabasename.* TO root@'%' IDENTIFIED BY 'yourRootPassword';

然后:

FLUSH PRIVILEGES; 

然后注释掉文件"/etc/mysql/mysql.conf.d/mysql .cnf"(这是必须的!)

bind-address = 127.0.0.1 

对我有用!

需要更改配置文件才能通过本地主机启用连接。

要通过远程ip连接,请以“root”用户登录并在mysql中运行以下查询。

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';

GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' WITH GRANT OPTION;

CREATE USER 'username'@'%' IDENTIFIED BY 'password';

GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' WITH GRANT OPTION;

FLUSH PRIVILEGES;

这将创建一个在本地主机和远程ip上都可以访问的新用户。

还要注释位于/etc/mysql/my.cnf中的my.cnf文件中的下面一行

bind-address = 127.0.0.1

重新启动mysql

sudo service mysql restart

现在你应该可以远程连接到你的mysql了。