1. 命令行輸入以下命令,可以無密碼啓動服務。
mysqld --console --skip-grant-tables --shared-memory
效果圖如下:
2. 服務啓動後,以空密碼登入系統。
mysql.exe -u root
3. 執行 sql 命令設置 root 的用戶密碼。
UPDATE mysql.user SET authentication_string='123456' WHERE user='root' and host='localhost';
4. 切換到 MySQL 的安裝目錄 bin 目錄下執行命令:
mysql -u root -p
輸入密碼,回車。
可以用 命令查看一下默認安裝的數據庫:
show databases;
查詢 mysql 庫的所有表:
use mysql;
show tables;
看到默認初始化了mysql數據庫,其中user表裏面存儲MySQL用戶信息。我們可以看一下默認MySQL用戶:
select user,host,authentication_string from mysql.user;
管理員 root 的 host 是 localhost,代表僅限 localhost 登錄訪問。如果要允許開放其他 ip 登錄,則需要添加新的 host。允許所有ip訪問,可以直接修改成“%”。
但是執行 –skip-grant-tables 後,再創建用戶時會提示錯誤:The MySQL server is running with the –skip-grant-tables option so it cannot execute this statement ,因爲此時數據庫是隻讀的權限,所以需要重新加載一遍權限表即:flush privileges;