目的
目的是在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>