天天看點

mysql學習之-密碼管理(預設密碼,修改密碼,解決忘記密碼)

1. mysql安裝後預設沒有密碼,初始化安裝後預設密碼登入,需要馬上修改root密碼。

[root@mysql ~]# cat /root/.mysql_secret    --檢視root賬号密碼

# the random password set for the root user at tue dec 13 18:59:06 2016 (local time): 7mhtyx3azel9ouf8

[root@mysql ~]# mysql -uroot -p    -- 登入mysql

enter password:               --(此處複制粘貼 7mhtyx3azel9ouf8)

welcome to the mysql monitor.  commands end with ; or \g.

your mysql connection id is 2

server version: 5.6.25

copyright (c) 2000, 2015, oracle and/or its affiliates. all rights reserved.

oracle is a registered trademark of oracle corporation and/or its

affiliates. other names may be trademarks of their respective

owners.

type 'help;' or '\h' for help. type '\c' to clear the current input statement.

說明:  出現mysql>   表示一切順利成功。

2.  修改密碼

mysql> set password = password('123456');   --設定密碼為123456

[root@mysql etc]# mysql -uroot -p123456     --修改密碼後,登入

warning: using a password on the command line interface can be insecure.

your mysql connection id is 39

server version: 5.6.25 mysql community server (gpl)

mysql>     --登入成功

3. 忘記mysql資料庫root的密碼後,如何修改密碼。

[root@mysql etc]# service mysql stop   --停止mysql程序

shutting down mysql.... success! 

[root@mysql etc]# mysqld_safe --skip-grant-tables&  --啟動mysql時不啟動grant-tables,授權表。執行後出現下面畫面,另起一個新會話。

[1] 3574

[root@mysql etc]# 161215 06:13:53 mysqld_safe logging to '/var/lib/mysql/mysql.err'.

161215 06:13:53 mysqld_safe starting mysqld daemon with databases from /var/lib/mysql

[root@mysql ~]# ps -ef|grep mysql   --檢視mysql程序是否起來

root       3467   3440  0 05:29 pts/2    00:00:00 mysql -uandy -px xx

root       3574   2845  0 06:13 pts/0    00:00:00 /bin/sh /usr/bin/mysqld_safe --skip-grant-tables

mysql      3677   3574  2 06:13 pts/0    00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-

mysql> update user set password=password('cccccc') where user='root';   --更改root密碼

query ok, 4 rows affected (0.04 sec)

rows matched: 4  changed: 4  warnings: 0

mysql> flush privileges;    --權限更新

query ok, 0 rows affected (0.00 sec)

[root@mysql ~]# mysql -uroot -p123456    --使用舊密碼登入,發現不行,說明密碼已經更新

error 1045 (28000): access denied for user 'root'@'localhost' (using password: yes)

[root@mysql ~]# mysql -uroot -pcccccc   --新密碼登入成功

mysql>