怎样重置ubuntu mysql8密码

密码很难记住,所以如果您忘记了 MySQL root 密码,幸运的是,有一种方法可以更改它。这篇文章是为您而写的,在这篇文章结束时,您将成功更改 MySQL 的密码。

本博客演示了如何在 Ubuntu 上重置使用包管理器安装的 MySQL 和 MariaDB 数据库的root密码。更改 root 密码的过程会有所不同,具体取决于您是否安装了 MySQL 以及其他供应商的发行版或软件包附带的默认 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服务器

要更改MySQL root密码,首先需要关闭MySQL服务器,运行以下命令:

sudo systemctl stop mysql.service

检查MySQL 服务器的**状态以进行验证。**运行以下命令:

bash 复制代码
sudo systemctl status mysql.service

3.跳过授权表和网络

要在不授予表和网络检查的情况下启动 MySQL 服务器,请设置MySQL 在启动时使用的环境 变量 **MYSQLD_OPTS 。**运行以下命令:

bash 复制代码
sudo systemctl set-environment MYSQLD_OPTS="--skip-networking --skip-grant-tables"

好了,环境变量设置好了,我们就可以不用密码登录MySQL shell了。

4.启动MySQL服务

设置环境变量MYSQLD_OPTS 后,启动MySQL 服务。运行以下命令:

bash 复制代码
sudo systemctl start mysql.service

确认MySQL 服务的状态(如果正在运行):

5. 登录MySQL Shell

此时我们就可以不用密码访问MySQL数据库了:

bash 复制代码
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密码

现在,首先**刷新权限。**运行以下命令:

bash 复制代码
mysql> flush privileges;
bash 复制代码
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进程并重新启动MySQL服务

首先,在重新启动 MySQL 服务器之前杀死 MySQL 的所有进程。

并重新启动 MySQL 服务器,运行以下命令:

bash 复制代码
sudo killall -u mysql

8.使用新密码登录MySQL

如果一切顺利,您现在应该能够使用 root 密码登录 MySQL 数据库:

bash 复制代码
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> 
相关推荐
likfishdn14 分钟前
Linux的文件与目录管理
linux·运维·服务器
chian-ocean34 分钟前
Linux 文件缓冲区:高效数据访问的幕后推手
linux·运维·服务器
加油=^_^=35 分钟前
【Linux】进程优先级 | 进程调度(三)
linux·运维·服务器
若云止水1 小时前
Ubuntu 下 nginx-1.24.0 源码分析 - ngx_init_cycle 函数 - 详解(4)
数据库·nginx·ubuntu
吃汤圆的抹香鲸1 小时前
GoLand 安装包 绿色版 Win,Mac,Linux 包含IntelliJ全家桶 专为Go语言设计的集成开发环境(IDE)
linux·windows·macos·go·intellij-idea·go1.19
追寻光1 小时前
Cannot find a valid baseurl for repo: base/7/x86_64
linux
元气满满的热码式1 小时前
Docker数据卷操作实战
linux·运维·docker·云原生·容器
陆沙1 小时前
deepseek-r1-centos-本地服务器配置方法
linux·服务器·centos
北京耐用通信1 小时前
航空装配自动化神器Ethercat转profient网关搭配机器人精准控制
运维·机器人·自动化
吃汤圆的抹香鲸2 小时前
Rider 安装包 绿色版 Win/Mac/Linux 适合.NET和游戏开发者使用 2025全栈开发终极指南:从零配置到企业级实战
linux·运维·windows·sql·游戏·macos·.net