//開啟MySQL
C:\WINDOWS\system32>net start mysql
MySQL 服務正在啟動 ...
MySQL 服務已經啟動成功。
//登陸報錯
C:\WINDOWS\system32>mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
//關閉MySQL
C:\WINDOWS\system32>net stop mysql
MySQL 服務正在停止.
MySQL 服務已成功停止。
//無密碼啟動MySQL服務
C:\WINDOWS\system32>mysqld --console --skip-grant-tables --shared-memory
2018-11-22T14:06:27.964267Z 0 [System] [MY-010116] [Server]
D:\mysql-8.0.13-winx64\bin\mysqld.exe (mysqld 8.0.13) starting as process 2972
2018-11-22T14:06:30.981556Z 0 [Warning] [MY-010068] [Server]
CA certificate ca.pem is self signed.
2018-11-22T14:06:31.006330Z 0 [System] [MY-010931] [Server]
D:\mysql-8.0.13-winx64\bin\mysqld.exe: ready for connections.
Version: '8.0.13' socket: '' port: 0 MySQL Community Server - GPL.
2018-11-22T14:06:31.209604Z 0 [Warning] [MY-011311] [Server]
Plugin mysqlx reported: 'All I/O interfaces are disabled, X Protocol won't be accessible'
再開一個cmd視窗B:
//無密碼【登陸】(密碼處直接enter)
C:\WINDOWS\system32>mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.13 MySQL Community Server - GPL
Copyright (c) 2000, 2018, 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> UPDATE mysql.user SET authentication_string='' WHERE user='root' and host='localhost';
Query OK, 1 row affected (0.08 sec)
Rows matched: 1 Changed: 1 Warnings: 0
//重新整理
mysql> flush privileges;
Query OK, 0 rows affected (0.06 sec)
//查詢
mysql> select host,user,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host | user | plugin | authentication_string |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root | caching_sha2_password | |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
4 rows in set (0.00 sec)
//---------------------------------------------設定加密的密碼---------------------------------------------
//以caching_sha2_password加密密碼並設定
mysql> ALTER user 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'admin';
Query OK, 0 rows affected (0.17 sec)
//重新整理
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
//查詢
mysql> select host,user,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host | user | plugin | authentication_string |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root | caching_sha2_password | $A$005$r/r8"We_EpPb9584lw2cALUsOsvkB/hHg1qUqocVxDMMkFQ8RyQcXASZoff5 |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
4 rows in set (0.00 sec)
關閉A/B cmd視窗,再開一個cmd 視窗C
//登陸成功
C:\WINDOWS\system32>mysql -uroot -padmin
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.13 MySQL Community Server - GPL
Copyright (c) 2000, 2018, 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> ALTER user 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'admin';
Query OK, 0 rows affected (0.12 sec)
//重新整理
mysql> flush privileges;
Query OK, 0 rows affected (0.03 sec)
//查詢
mysql> select host,user,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host | user | plugin | authentication_string |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root | mysql_native_password | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
4 rows in set (0.00 sec)
mysql>
其他常用cmd命令
//---------------------------------------常用語句------------------------------------------------------------
//修改加密規則
mysql> UPDATE mysql.user SET plugin='mysql_native_password' WHERE user='root';
//查詢
mysql> select host,user,plugin,authentication_string from mysql.user;
//對密碼進行加密
mysql> update mysql.user set password=password('admin') where user='root' and host='localhost';
//重新整理
flush privileges;
//退出
mysql> exit
注意:
mysql> use mysql;
mysql> update user set password=password('123') where user='root' and host='localhost';
//等價於
mysql> update mysql.user set password=password('123') where user='root' and host='localhost';