在服务器上装完mysql8.0.15后,想通过本地的 sqlyog 工具连接,用户root,初始密码都正确

但是在本地依然报错:

error no. 1045 access denied for user ‘root‘@’*****‘ (using password:YES)

注:初始密码的方法: https://blog.csdn.net/liliuqing/article/details/88721881

原来: MySql -Server 出于安全方面考虑默认只允许本机(localhost, 127.0.0.1)来连接访问.

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

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

1
mysql -u root -p

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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





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,出现错误,如下:

1
2
3
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’;

结果如下:

1
2
3
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,刷新数据库。否则会保留在缓存中。结果如下:

1
2
3
mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

4.继续输入命令:

use mysql

select host,user from user;

结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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;

结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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,远程登录开启成功。