mysql8.0 root修改密码并开启远程访问权限

进入mysql命令行后

1、修改密码

mysql> alter user 'root'@'localhost' identified with mysql_native_password by '123456';
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'

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

2、修改远程访问

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)

执行命令

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)

最后远程登录测试一下。

posted @ 2022-06-15 10:13  tt1234  阅读(4740)  评论(0编辑  收藏  举报