Linux下mysql5.7.18登录报错“Access denied for user 'root'@'localhost' (using password: YES”)

问题描述:在Linux系统下安装mysql时报错:“Access denied for user ‘root’@‘localhost’ (using password: YES”)

解决方案 1:适用于安装mysql后初次以root用户登录mysql报错
1、请使用如下命令打开文件/etc/mysql/debin.cnf:
gedit /etc/mysql/debian.cnf
在打开的文件中,可以看到user = debian-sys-maint这个语句设置了用户名,password = Z7W76fH2arjbWdN6 这个语句就是设置了密码是Z7W76fH2arjbWdN6。(你的电脑user和password可能不同)
2、下面使用debian-sys-maint这个用户名和密码登录MySQL
3、进入mysql环境后,使用下面语句创建root用户名和密码:
mysql>set password for ‘root’@‘localhost’ = password(‘123456’);
这条SQL语句,就设置了可以登录MySQL的用户名root(注意,这个root用户名不是Ubuntu系统的root用户),它的登录密码是123456(你可以修改成你的密码)。
参考资料:http://dblab.xmu.edu.cn/blog/2140-2/

解决方法2:
重置root用户密码。先使用/etc/mysql/debian.cnf文件中[client]节提供的用户名和密码登录Mysql,然后修改root用户的密码。具体操作步骤如下:
1)打开debian.cnf文件,查看[client]节提供的用户名和密码:
sudo gedit /etc/mysql/debian.cnf #记录下打开文件的user和password
2)以上个步骤中打开文件的[client]节提供的用户名和密码登录MySQL: mysql -r <你的user> -p
Enter password: <你的password>
3)查询mysql中有哪些用户:
mysql> use mysql;
mysql> select User from user; #查询用户命令,如下图所示:
±----------+
| User |
±----------+
| ******* |
| mysql.sys |
| root |
±----------+
3 rows in set (0.00 sec)

4)mysql> update mysql.user set authentication_string=‘你的密码’ where user=‘你要修改密码的用户名’; 修改成功后有如下提示:
Query OK, 2 rows affected, 1 warning (0.04 sec)
Rows matched: 2 Changed: 2 Warnings: 1

5)mysql> flush privileges; #立即生效
Query OK, 0 rows affected (0.04 sec)

mysql> quit
Bye
6)#mysql -u *** -p #以修改密码后的用户登录


猜你喜欢

转载自blog.csdn.net/u011208984/article/details/81570163