1、安装完MySQL5.7之后,使用mysql -u root -p登录MySQL时报错:

[root@localhost bin]# mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

2、百度搜了很多解决方法,都是使用安全模式启动数据:

service mysqld stop                                              #首先关闭mysql服务
mysqld_safe --user=root --skip-grant-tables --skip-networking &  #然后使用mysqld_safe跳过权限方式启动
mysql                                                            #然后直接登录,跳过密码验证
update mysql.user set password=password('123456') where user='root';    #将密码设置为123456

3、但是在安装目录/usr/bin/下根本找不到mysqld_safe这个文件,所以这个方法无效。

4、找了大半天,终于找到一个介绍,skip-grant-tables这个是配置参数,可以放在mysql配置文件里面修改,然后把这个配置参数放到/etc/my.cnf文件里面。

5、添加进去后重启mysql服务,就可以正常登进去mysql数据库了;

 

6、但是设置密码的时候,输入update mysql.user set password=password('123456') where user='root';发现password字段不存在,突然间心态大崩。

7、最后执行select * from mysql.user;命令查看找到user表authentication_string这个列的值与password的只相似,于是将这个字段的值备份一下。

8、执行update mysql.user set authentication_string=password('123456') where user='root';

9、然后退出mysql,执行systemctl restart mysqld.service重启数据库,再次登录,终于解决问题!

Logo

更多推荐