Ubuntu下mysql忘记root密码

公司mysql之前是一位离职员工在维护的。走了以后才让我维护,root密码不知道,吓死宝宝了。

[root ~]# mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password:NO)
  • 首先,停止mysql服务
[root ~]# service mysql stop 
mysql stop/waiting
  • 启动mysql安全模式
[root ~]# mysqld_safe --skip-grant-tables &
  • 这时用root登录mysql就不需要密码了
[root ~]# mysql -u root
mysql>
  • 更改用户名,密码
mysql> use mysql;
Database changed
mysql> select * from  user;
Empty set (0.00 sec)
mysql> truncate table user;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> grant all privileges on *.* to root@localhost identified by 'YourNewPassword' with grant option;
Query OK, 0 rows affected (0.01 sec)
  • 查看密码
 mysql> select host, user from user; +-----------+------+
| host | user | +-----------+------+
| localhost | root | +-----------+------+
1 row in set (0.00 sec)
  • 退出安全模式并杀掉进程,启动mysql服务
mysql> quit;
[root ~]# kill -KILL [PID of mysqld_safe]
[root ~]# kill -KILL [PID of mysqld]
[root ~]# service mysql start

这个时候就可以登录了

[root ~]# mysql -u root -pYourNewPassword 
 mysql> 

参考:
Access denied for user ‘root@localhost’ (using password:NO)

你可能感兴趣的:(mysql)