mysql开放打开远程连接的两种方法

方法一:
更改mysql数据库user表的host列,把localhost改为%

[root@localhost ~]# mysql -u root -p123***
mysql> use mysql;
mysql> select Host,User from user;
mysql> update user set host=‘%’ where user=‘root’;
mysql> flush privileges;

 

方法二(推荐):
添加一个新的授权

首先链接本机的mysql(用ssh登录终端,输入如下命令):

mysql -uroot -p
输入密码登陆进去后,输入如下的语句,执行。

grant all privileges on *.* to ‘root’@’%’ identified by ‘******’ with grant option;

FLUSH PRIVILEGES;

————————————————————————————————————————————————

报错解决方法:

一、报错
MySQL执行命令:

UPDATE user SET host = ‘%’ WHERE user = ‘root’;

报错:

ERROR 1062 (23000): Duplicate entry ‘%-root’ for key ‘user.PRIMARY’

mysql> UPDATE user SET host = ‘%’ WHERE user = ‘root’;
ERROR 1062 (23000): Duplicate entry ‘%-root’ for key ‘user.PRIMARY’
二、原因
查看所有的用户

select host, user from user;

发现,有两个root用户,我们将host 为host的那条数据删除。

 

mysql> DELETE FROM user WHERE user = ‘root’ AND host = ‘host’;
Query OK, 1 row affected (0.01 sec)
再次进行更新

mysql> update user set host=’%’ where user=’root’;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
再次查看用户数据

mysql> select host, user from user;
+———–+——————+
| host | user |
+———–+——————+
| % | root |
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
+———–+——————+
4 rows in set (0.00 sec)
此时HOST已经变成了%

最后使用更改生效

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

来源地址:mysql开放打开远程连接的两种方法

转载声明:本站文章若无特别说明,皆为原创,转载请注明来源:www.88531.cn资享网,谢谢!^^

© 版权声明
THE END
喜欢就支持一下吧
点赞30 分享