mysql8.0.15用户root登录开启远程访问权限

接上篇,在服务器上装完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窗口,输入命令:

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,远程登录开启成功。

  • 39
    点赞
  • 89
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值