MySQL 修改root用戶密碼


三種修改root用戶密碼的方式:

方法一: 使用set password命令

首先登錄MySQL,使用mysql自帶的客戶端連接mysql,命令如:mysql -uroot -p

會提示你輸入當前root密碼,默認為空,直接回車就可以了.

格式: mysql> set password for 用戶名@localhost=password('新密碼');

例子: mysql> set password for root@localhost=password('666');

這里password('666')中的password會自動進行加密.

 

 

方法二:使用mysqladmin 執行管理操作的客戶端程序

格式: C:\Users\Administrator>mysqladmin -u用戶名 -p舊密碼 password 新密碼

例子: C:\Users\Administrator>mysqladmin -uroot -p666 password 新密碼(也可以不輸入,直接回車,提示的時候再輸入,這樣會安全些,別人無法通過dos下的doskey/history查看到你的新密碼)

如下面的例子中的Warning: Using a password on the command line interface can be insecure. 就是安全提示

 

C:\Users\Administrator>mysqladmin -uroot -p666 password
Warning: Using a password on the command line interface can be insecure.
New password: ***
Confirm new password: ***

C:\Users\Administrator>doskey/history (查看歷史記錄命令)
mysql -uroot -p
mysqladmin -uroot -p666 password
doskey/history

 

方法三: update直接修改mysql庫中的user表

mysql> update user set password=password('888') where user='root';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 3  Changed: 2  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

 

----------------------------------------------------------------忘記root密碼,怎么辦?----------------------------------------------------------------

這里需要重啟mysqld服務端

C:\Users\Administrator>net stop mysqld
mysqld 服務正在停止.
mysqld 服務已成功停止。

 

C:\Users\Administrator>mysqld --skip-grant-tables  (跳過用戶認證權限表)
2019-06-18 10:54:24 0 [Warning] TIMESTAMP with implicit DEFAULT value is depreca
ted. Please use --explicit_defaults_for_timestamp server option (see documentati
on for more details).
2019-06-18 10:54:24 0 [Note] --secure-file-priv is set to NULL. Operations relat
ed to importing and exporting data are disabled
2019-06-18 10:54:24 0 [Note] mysqld (mysqld 5.6.44) starting as process 2004 ...

重新打開一個dos窗口

C:\Users\Administrator>mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.44 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| oldboydb           |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.01 sec)

mysql> use mysql;
Database changed
mysql> update user set password=password('666') where user='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 3  Changed: 0  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> quit;
Bye

C:\Users\Administrator>tasklist | findstr mysql
mysqld.exe                    2004 Console                    1    454,756 K

C:\Users\Administrator>taskkill /F /PID 2004
成功: 已終止 PID 為 2004 的進程。

C:\Users\Administrator>net start mysqld
mysqld 服務正在啟動 .
mysqld 服務已經啟動成功。








 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM