那么必须给root修改远程访问权限!步骤如下:


1.在服务器端定位到D:\server\mysql-8.0.15-winx64\bin,打开dos窗口,输入命令:


mysql -u root -p

(注:root表示登录的用户)


2.出现让输入密码,输入密码后回车,登录成功后,出现


E:\opt\mysql\mysql-8.0.15-winx64\bin>mysql -uroot -p

Enter password: ************

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 11

Server version: 8.0.15

 

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

3.继续输入:use mysql,出现错误,如下:


mysql> use mysql

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

那么需要更改用户密码,输入命令:


alter user 'root'@'localhost' identified with mysql_native_password by 'amp';


结果如下:


mysql> alter user 'root'@'localhost' identified with mysql_native_password by 'amp';

Query OK, 0 rows affected (0.01 sec)

注:在mysql8版本中更改用户密码需要加入with mysql_native_password,并且要加入;所以一下两种写法都是不对的。

alter user 'root'@'localhost' identified by 'amp';

alter user 'root'@'localhost' identified with mysql_native_password by 'amp'


最后要输入flush privileges,刷新数据库。否则会保留在缓存中。结果如下:


mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

4.继续输入命令:


use mysql


select host,user from user;


结果如下:


mysql> use mysql

Database changed

mysql> select host,user form user;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that

corresponds to your MySQL server version for the right syntax to use near 'user'

 at line 1

mysql> select host,user from user;

+-----------+------------------+

| host      | user             |

+-----------+------------------+

| localhost | mysql.infoschema |

| localhost | mysql.session    |

| localhost | mysql.sys        |

| localhost | root             |

+-----------+------------------+

4 rows in set (0.00 sec)

果然,发现root用户的访问权限是localhost,需要修改host为%,输入命令:


update user set host='%' where user='root';


flush privileges;


结果如下:


mysql> update user set host='%' where user='root';

Query OK, 1 row affected (0.01 sec)

Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

 

再次查询修改后结果,OK。结果如下:

 

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)

解决OK,远程登录开启成功。

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

版权声明:本文为CSDN博主「藤叶香来」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/liliuqing/article/details/88723409