MySQL登录时出现 Access denied for user 'root'@&am

时间:2022-02-10 21:21:38

【环境:Win10通过Hyper创建Ubuntu虚机,docker里运行mysql5.7】

 

症状:

-----------------------------------

远程上去,

1. docker exec -it mysql /bin/bash

2. mysql -u root -p 输入密码,可以进入mysql库。

排除了防火墙、端口等方面的问题,那很可能就是mysql远程授权方面的问题了。

 

解决办法:

-----------------------------------

1. 按照上面步骤进去mysql;
2. 执行授权命令
mysql> grant all privileges on *.* to [email protected]‘%‘ identified by ‘123456‘;
Query OK, 0 rows affected (0.00 sec)
3. 退出再试:  mysql> quit
4、再试登录:    mysql -u root -h 192.168.22.151 -p
  Enter password: 
 结果显示:Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 3
表示成功


如何给用户授权。
 
mysql> grant 权限1,权限2, ... 权限n on 数据库名称.表名称 to 用户名@用户地址 identified by ‘连接口令‘;

权限1,权限2,... 权限n 代表 select、insert、update、delete、create、drop、index、alter、grant、references、reload、shutdown、process、file 等14个权限。
当权限1,权限2,... 权限n 被 all privileges 或者 all 代替时,表示赋予用户全部权限。
当 数据库名称.表名称 被 *.* 代替时,表示赋予用户操作服务器上所有数据库所有表的权限。
用户地址可以是localhost,也可以是IP地址、机器名和域名。也可以用 ‘%‘ 表示从任何地址连接。
‘连接口令‘ 不能为空,否则创建失败。

举几个例子:
mysql> grant select,insert,update,delete,create,drop on db01.employee to [email protected] identified by ‘123456′;
给来自192.168.22.151的用户vaiky分配可对数据库db01的employee表进行select,insert,update,delete,create,drop等操作的权限,并设定口令为123456。

mysql> grant all privileges on db01.* to [email protected] identified by ‘123456′;
给来自192.168.22.151的用户vaiky分配可对数据库db01所有表进行所有操作的权限,并设定口令为123456。

mysql> grant all privileges on *.* to [email protected] identified by ‘123456′;
给来自192.168.22.151的用户vaiky分配可对所有数据库的所有表进行所有操作的权限,并设定口令为123456。

mysql> grant all privileges on *.* to [email protected] identified by ‘123456′;
给本机用户vaiky分配可对所有数据库的所有表进行所有操作的权限,并设定口令为123456。