對MySQL的某些操做不是很熟練,下面記錄一下我找到的比較全面的MySQL修改密碼過程.mysql
a、能夠在建立用戶的時候指定密碼,以及直接使用grant建立用戶的時候指定密碼。 對於已經存在的用戶直接使用grant方式也能夠修改密碼 以下: --演示版本 root@localhost[(none)]> show variables like 'version%'; +-------------------------+------------------------------+ | Variable_name | Value | +-------------------------+------------------------------+ | version | 5.5.37 | | version_comment | MySQL Community Server (GPL) | | version_compile_machine | x86_64 | | version_compile_os | Linux | +-------------------------+------------------------------+ --下面咱們使用grant方式建立一個新賬戶fred,並設定密碼 root@localhost[(none)]> grant usage on *.* to 'fred'@'localhost' identified by 'fred'; Query OK, 0 rows affected (0.00 sec) --查看剛剛建立的帳戶 root@localhost[(none)]> select host,user,password from mysql.user where user='fred'; +-----------+------+-------------------------------------------+ | host | user | password | +-----------+------+-------------------------------------------+ | localhost | fred | *6C69D17939B2C1D04E17A96F9B29B284832979B7 | +-----------+------+-------------------------------------------+ --下面能夠成功登錄mysql SZDB:~ # mysql -ufred -pfred fred@localhost[(none)]> b、使用set password方式來修改帳戶密碼 --下面咱們使用set password方式來設定密碼 root@localhost[(none)]> set password for 'fred'@'localhost'=password('passwd'); Query OK, 0 rows affected (0.00 sec) root@localhost[(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec) --再次登錄時,以前的密碼已經失效,沒法登錄 SZDB:~ # mysql -ufred -pfred ERROR 1045 (28000): Access denied for user 'fred'@'localhost' (using password: YES) --下面使用新密碼登錄成功 SZDB:~ # mysql -ufred -ppasswd fred@localhost[(none)]> --檢索數據庫是否存在jack用戶,以下密碼爲null root@localhost[(none)]> select host,user,password from mysql.user where user='jack'; +-----------+------+----------+ | host | user | password | +-----------+------+----------+ | localhost | jack | | +-----------+------+----------+ c、加密方式更新系統表user的password列 --咱們嘗試直接更新密碼列(不使用加密函數方式) root@localhost[(none)]> update mysql.user set password='jack' where user='jack'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 --因爲直接使用明文,所以系統表user列password顯示爲明文 root@localhost[(none)]> select host,user,password from mysql.user where user='jack'; +-----------+------+----------+ | host | user | password | +-----------+------+----------+ | localhost | jack | jack | +-----------+------+----------+ --Author : Leshami --Blog :http://blog.csdn.net/leshami root@localhost[(none)]> flush privileges; Query OK, 0 rows affected (0.02 sec) --此時沒法登錄 SZDB:~ # mysql -ujack -pjack -h localhost ERROR 1045 (28000): Access denied for user 'jack'@'localhost' (using password: YES) --下面咱們經過set方式來修改jack的密碼,提示找不到jack用戶 root@localhost[(none)]> set password for 'jack'@'localhost'=password('jack'); ERROR 1133 (42000): Can't find any matching row in the user table --咱們切換到mysql數據庫下嘗試, root@localhost[(none)]> use mysql root@localhost[mysql]> set password for 'jack'@'localhost'=password('passwd'); --在mysql數據庫下依舊沒法更新用戶jack的密碼 ERROR 1133 (42000): Can't find any matching row in the user table --下面咱們嘗試用password函數方式來更新password列 root@localhost[mysql]> update user set password=password('passwd') where user='jack'; --此方式更新成功 Query OK, 1 row affected (0.04 sec) Rows matched: 1 Changed: 1 Warnings: 0 root@localhost[mysql]> select host,user,password from user where user='jack'; --能夠看到密碼已經變成了密文 +-----------+------+-------------------------------------------+ | host | user | password | +-----------+------+-------------------------------------------+ | localhost | jack | *59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0 | +-----------+------+-------------------------------------------+ root@localhost[mysql]> flush privileges; Query OK, 0 rows affected (0.00 sec) --此時登錄成功 robin@SZDB:~> mysql -ujack -ppasswd jack@localhost[(none)]>
--假定此時咱們的root賬戶忘記或遺失了密碼,以下面的演示,咱們給出的是xxx,不能登錄到mysql(真實的密碼爲mysql) SZDB:~ # mysql -uroot -pmysql root@localhost[(none)]> SZDB:~ # mysql -uroot -pxxx #忘記密碼,此時沒法正常登陸 Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) --首先中止mysql服務器 SZDB:~ # service mysql stop Shutting down MySQL.. done --使用--skip-grant-tables選項跳過受權表驗證, SZDB:~ # mysqld --help --verbose #獲取mysqld幫助信息 --skip-grant-tables Start without grant tables. This gives all users FULL ACCESS to all tables. --使用--skip-grant-tables啓動mysql服務器 SZDB:~ # mysqld --skip-grant-tables --user=mysql & [1] 10209 SZDB:~ # ps -ef | grep mysql mysql 10209 14240 4 13:52 pts/0 00:00:00 mysqld --skip-grant-tables --user=mysql root 10229 14240 0 13:53 pts/0 00:00:00 grep mysql SZDB:~ # mysql root@localhost[(none)]> select user,host,password from mysql.user where user='root'; +-------+-----------+-------------------------------------------+ | user | host | password | +-------+-----------+-------------------------------------------+ | root | % | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA | | root | 127.0.0.1 | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA | +-------+-----------+-------------------------------------------+ --更新mysql帳戶密碼爲NULL或設定爲新密碼,注設定爲空密碼時能夠直接設置,無須使用加密函數,2者等同 root@localhost[(none)]> update mysql.user set password='' where user='root'; Query OK, 2 rows affected (0.00 sec) Rows matched: 2 Changed: 2 Warnings: 0 root@localhost[(none)]> select user,host,password from mysql.user where user='root'; +------+-----------+----------+ | user | host | password | +------+-----------+----------+ | root | % | | | root | 127.0.0.1 | | +------+-----------+----------+ root@localhost[(none)]> exit Bye #再次中止mysql數據庫服務器 SZDB:~ # service mysql stop Shutting down MySQL. done [1]+ Done mysqld --skip-grant-tables --user=mysql SZDB:~ # service mysql start Starting MySQL.. done SZDB:~ # mysql #重啓後再次登錄,再也不須要任何密碼 root@localhost[(none)]>
a、能夠使用set password for 'user_name'@'host_name'=password('new_pwd')方式來修改密碼 #更正@20141031 b、能夠使用update系統表方式,update user set password=password('passwd') where user='user_name' 注: 對於user表password類,若是不用password函數的話,致使更新後沒法登錄。 但若是將帳戶更新爲空密碼,能夠使用加密函數,也能夠不使用,2者等同。 c、也能夠在用戶建立後直接使用grant方式來更新用戶密碼。 d、對應root密碼丟失或須要重置root密碼的情形,須要使用系統選項--skip-grant-tables啓動服務器後進行重置。 e、有關mysql權限及用戶管理,建立用戶時指定密碼,請參考:MySQL 用戶與權限管理