目的
目的是在Ubuntu 18.04 Bionic Beaver Linux上重置丟失的root用戶MySQL密碼
操作係統和軟件版本
- 操作係統:-Ubuntu 18.04仿生海狸
- 軟件:-MySQL版本14.14或更高版本
要求
以root或通過特權訪問Ubuntu係統sudo
命令是必需的。
困難
簡單
約定
使用說明
通過使用mysql_secure_installation重置
重置MySQL數據庫根密碼的最簡單方法是執行mysql_secure_installation
程序,並在提示您輸入新的MySQL根密碼時:
$ sudo mysql_secure_installation
....
Please set the password for root here.
New password:
Re-enter new password:
通過使用skip-grant-tables重置
如果由於某種原因上述方法失敗,請按照以下步驟使用--skip-grant-tables
重置MySQL root密碼。
讓我們從停止當前正在運行的MySQL數據庫開始:
$ sudo service mysql stop
接下來,創建一個/var/run/mysqld
MySQL進程用來存儲和訪問套接字文件的目錄:
$ sudo mkdir -p /var/run/mysqld
$ sudo chown mysql:mysql /var/run/mysqld
準備就緒後,使用以下命令手動啟動MySQLlinux命令和選項:
$ sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &
$ [1] 2708
確認該進程正在按預期運行:
$ jobs
[1]+ Running sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &
在這一階段,我們可以不用密碼訪問MySQL數據庫:
$ mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20-1ubuntu1 (Ubuntu)
Copyright (c) 2000, 2017, 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>
使用MySQL會話優先刷新權限:
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
接下來,重置根密碼。下列命令將MySQL根密碼重置為linuxconfig.org
:
mysql> USE mysql;
Database changed
mysql> UPDATE user SET authentication_string=PASSWORD("linuxconfig.org") WHERE User='root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1
mysql> UPDATE user SET plugin="mysql_native_password" WHERE User='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
退出MySQL會話:
mysql> quit
Bye
優雅地終止電流mysqld
處理:
$ sudo pkill mysqld
linuxconfig@ubuntu:~$ jobs
[1]+ Done sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking
最後,啟動MYSQL數據庫:
$ sudo service mysql start
如果一切順利,您現在應該可以使用root密碼登錄到MySQL數據庫:
$ mysql -u root --password=linuxconfig.org
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 4
Server version: 5.7.20-1ubuntu1 (Ubuntu)
Copyright (c) 2000, 2017, 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>