1、忘記了mysql密碼,可以先通過設置my.cnf文件爲任意登錄,一般是在/etc目錄下,可以用locate命令查找一下
[root@VM_184_46_centos etc]# locate my.cnf
/etc/my.cnf
/etc/my.cnf.d
[root@VM_184_46_centos etc]#
2、使用 vim /etc/my.cnf命令編輯該文件(建議先備份),在[mysqld]下面加上 skip-grant-tables ,
3、保存後執行service mysql restart 重啓mysql
4、接下來就可以用命令登錄mysql了,登錄後修改mysql密碼
# mysql -uroot -p
mysql> use mysql;
mysql> update user set password=password('123') where user='root';
mysql> flush privileges;
5、Ctrl+C退出mysql
6、 vim /etc/my.cnf 刪除剛剛的 skip-grant-tables
7、重啓mysql,完成。