mysql登錄或者初始化時忘記密碼

初始化報錯:
[root@controller my.cnf.d]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we’ll need the current
password for the root user. If you’ve just installed MariaDB, and
you haven’t set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: NO)
登錄失敗:
[root@controller my.cnf.d]# mysql -uroot
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)

可以通過重置mysql root密碼解決

1、修改 /etc/mysql/my.cnf文件,找到mysqld關鍵字,在mysqld下面添加skip-grant-tables,保存退出
2、重啓 systemctl restart mysql
3、進行無密碼登錄 mysql -uroot
4、修改root密碼
update user set password=PASSWORD(“password”) where user=“root”;
exit
5、vi /etc/mysql/my.cnf,把剛纔添加的 skip-grant-tables 刪掉,保存退出
6、再次重啓 systemctl restart mysql

這樣就完成了mysql root密碼修改。
情況一:初始化時一般密碼爲空,可以在初始化時設置root密碼,但是某種原因可能你初始化時mysql已經有初始密碼了,但是你有不知道這個密碼,無法進行初始化。
情況二:就是簡單的忘記了root密碼。
這兩種情況都可以按以上方法修改root密碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章