mysql安裝後,第一次登錄,殊不知道默認密碼,該如何作?(一篇重量級的文章)

修改MySQL的登陸設置: 
# vi /etc/my.cnf 
在[mysqld]的段中加上一句:skip-grant-tables 
例如: 
[mysqld] 
datadir=/var/lib/mysql 
socket=/var/lib/mysql/mysql.sock 
skip-name-resolve 
skip-grant-tables 
保存而且退出vi。 
3.從新啓動mysqld 
# /etc/init.d/mysqld restart 
Stopping MySQL: [ OK ] 
Starting MySQL: [ OK ] 
4.登陸並修改MySQL的root密碼 
# /usr/bin/mysql 
Welcome to the MySQL monitor. Commands end with ; or \g. 
Your MySQL connection id is 3 to server version: 3.23.56 
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer. 
mysql> USE mysql ; 
Reading table information for completion of table and column names 
You can turn off this feature to get a quicker startup with -A 
Database changed 
mysql> UPDATE user SET Password = password ( ‘new-password’ ) WHERE User = ‘root’ ; 
Query OK, 0 rows affected (0.00 sec) 
Rows matched: 2 Changed: 0 Warnings: 0 
# 上面那個過程須要注意一下,若是mysql版本是5.7的話,會提示無password字段。由於在5.7以後,
# 被authentication_string字段替代了,應該寫成
# UPDATE user SET authentication_string= password ( ‘new-password’ ) WHERE User = ‘root’ ;。可是須要還注意
# 一點後面那個password是個命令,不能改,千萬不要寫成了
# UPDATE user SET authentication_string= authentication_string( ‘new-password’ ) WHERE User = ‘root’ ;
mysql> flush privileges ; 
Query OK, 0 rows affected (0.01 sec) 
mysql> quit 
Bye 
5.將MySQL的登陸設置修改回來 
# vi /etc/my.cnf 
將剛纔在[mysqld]的段中加上的skip-grant-tables刪除 
保存而且退出vi。 
6.從新啓動mysqld 
# /etc/init.d/mysqld restart 
Stopping MySQL: [ OK ] 
Starting MySQL: [ OK ]

參考點:有同事告訴我,在安裝後,mysql會隨機生成一個默認密碼。默認密碼能夠在/var/log/mysqld.log文件裏找到。執行cat /var/log/mysqld.log。獲得結果以下圖:mysql

 

補充:在上面的修改默認密碼方法中,其實不行。只能經過命令:mysql -u root -p登陸進去mysql。卻沒有缺陷進入數據庫,好比use recruit(假如存在),會出現須要重置密碼的提示。因此此時還須要從新具體的密碼,經過以下命令來修改:sql

ALTER USER 'root'@'localhost' IDENTIFIED BY 'root_JUST_666';

那麼此時的登陸密碼就是:root_JUST_666。數據庫

相關文章
相關標籤/搜索