? ?密碼很難記住,所以如果您忘記了 MySQL root 密碼,幸運的是,有一種方法可以更改它。這篇文章是為您而寫的,在這篇文章結(jié)束時,您將成功更改 MySQL 的密碼。
本博客演示了如何在 Ubuntu 上重置使用包管理器安裝的 MySQL 和 MariaDB 數(shù)據(jù)庫的root密碼。更改 root 密碼的過程會有所不同,具體取決于您是否安裝了 MySQL 以及其他供應(yīng)商的發(fā)行版或軟件包附帶的默認(rèn) systemd 配置。
1.檢查MySQL版本
首先,檢查您的 MySQL 版本,因為這篇文章包含在版本 8 或更高版本上更改 root 密碼的解決方案。如果你的MySQL版本低于8,那么答案就會不同。運行以下命令:
mysql --version
? ? ? MySQL版本輸出:mysql ?Ver 8.0.36-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))
2.停止MySQL服務(wù)器
? 要更改MySQL?root密碼,首先需要關(guān)閉MySQL服務(wù)器,運行以下命令:
sudo systemctl stop mysql.service
?檢查MySQL 服務(wù)器的狀態(tài)以進(jìn)行驗證。運行以下命令:
sudo systemctl status mysql.service
3.跳過授權(quán)表和網(wǎng)絡(luò)
? ? 要在不授予表和網(wǎng)絡(luò)檢查的情況下啟動 MySQL 服務(wù)器,請設(shè)置MySQL 在啟動時使用的環(huán)境? ? ? 變量?MYSQLD_OPTS 。運行以下命令:
sudo systemctl set-environment MYSQLD_OPTS="--skip-networking --skip-grant-tables"
好了,環(huán)境變量設(shè)置好了,我們就可以不用密碼登錄MySQL shell了。
4.啟動MySQL服務(wù)
? 設(shè)置環(huán)境變量MYSQLD_OPTS后,啟動MySQL 服務(wù)。運行以下命令:
sudo systemctl start mysql.service
確認(rèn)MySQL 服務(wù)的狀態(tài)(如果正在運行):
5. 登錄MySQL Shell
此時我們就可以不用密碼訪問MySQL數(shù)據(jù)庫了:
wenhua@my-dream:~$ sudo mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.36-0ubuntu0.20.04.1 (Ubuntu)
Copyright (c) 2000, 2024, Oracle and/or its affiliates.
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>
6.重置root密碼
現(xiàn)在,首先刷新權(quán)限。運行以下命令:
mysql> flush privileges;
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '(YOUR NEW PASSWORD)';;
Query OK, 0 rows affected (0.01 sec)
mysql> quit;
Bye
((YOUR NEW PASSWORD)替換成
新密碼后退出
7.殺死所有MySQL進(jìn)程并重新啟動MySQL服務(wù)
首先,在重新啟動 MySQL 服務(wù)器之前殺死 MySQL 的所有進(jìn)程。
并重新啟動 MySQL 服務(wù)器,運行以下命令:文章來源:http://www.zghlxwxcb.cn/news/detail-833299.html
sudo killall -u mysql
8.使用新密碼登錄MySQL
? 如果一切順利,您現(xiàn)在應(yīng)該能夠使用 root 密碼登錄 MySQL 數(shù)據(jù)庫:文章來源地址http://www.zghlxwxcb.cn/news/detail-833299.html
wenhua@my-dream:~$ sudo mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.36-0ubuntu0.20.04.1 (Ubuntu)
Copyright (c) 2000, 2024, Oracle and/or its affiliates.
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>
到了這里,關(guān)于怎樣重置ubuntu mysql8密碼的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!