Centos7如何修改或重置MySQL8.0的root密码

本文详细介绍了在Centos7系统中重置MySQL8.0 root密码的步骤,包括修改配置文件、免密码登录、清空密码、设置新密码及刷新权限等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Centos7如何修改或重置MySQL8.0的root密码

一、修改配置文件

1、停止mysql服务

service mysqld stop

2、修改配置文件/etc/my.cnf
1)输入命令:vim /etc/my.cnf
2)按I进入编辑模式,在my.cnf文件中添加skip-grant-tables(意思是忽略密码),按esc键,输入:wq保存退出
忽略密码
3、启动/重启Mysql

service mysqld start
或
service mysqld restart

二、免密码登录mysql并设置把密码先设置为空字符串
1、登录:mysql -u root -p
2、按回车直接进入数据库
3、使用数据库:use mysql;
4、查看root用户信息:select host, user, authentication_string, plugin from user;
5、更新root用户信息,把密码设置为空字符串:
update user set authentication_string=’’ where user = ‘root’;
6、刷新权限表:FLUSH PRIVILEGES
7、退出:exit

[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 24
Server version: 8.0.20 MySQL Community Server - GPL
Copyright (c) 2000, 2020, 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> 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> update user set authentication_string='' where  user = 'root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye

三、修改配置表,并重启
1、修改配置文件/etc/my.cnf,把上述步骤添加的skip-grant-tables注释掉
2、重启:service mysqld restart

四、设置root登录mysql的密码
1、登录:mysql -u root -p(这时候还是不用输入密码,因为上面已经把密码设置为空字符串了);
2、修改root用户密码:ALTER USER ‘root’@’%’ IDENTIFIED WITH mysql_native_password BY ‘123456’;
3、刷新权限表:FLUSH PRIVILEGES
3、退出:exit
修改密码
五、用新设置的密码登录mysql
用密码登录mysql

### 如何在 CentOS重置 MySQL 8.0Root 用户密码 #### 停止并重启 MySQL 数据库服务 为了安全地更改 `root` 密码,需先停止当前运行的 MySQL MariaDB 服务,并以跳过权限表的方式启动它。 ```bash sudo systemctl stop mysqld.service sudo mkdir /var/run/mysqld/ sudo chown mysql:mysql /var/run/mysqld/ sudo mysqld_safe --skip-grant-tables & ``` 上述命令会创建必要的目录结构来支持无授权模式下的 MySQL 运行[^1]。 #### 登录到 MySQL 并执行 SQL 查询语句 此时可以无需提供任何凭证就能连接至数据库服务器: ```sql mysql -u root ``` 一旦成功登录,则可继续操作以更新 `root` 账户的信息。首先切换到默认的安全配置文件所在的数据库: ```sql USE mysql; ``` 接着查询现有的认证方式以及散列后的密码字符串以便了解现状: ```sql SELECT Host, User, Authentication_String AS PasswordHash, Plugin FROM users WHERE User = 'root'; ``` 这一步骤有助于确认后续应采取的具体措施[^3]。 对于那些使用了插件验证机制而非传统哈希算法存储密码的情况,建议通过下面的方法将其改为更简单的形式——即移除现有密码后再重新设定新的强密钥组合: ```sql UPDATE user SET authentication_string = '' WHERE User = 'root'; FLUSH PRIVILEGES; ``` 此过程将清除掉旧有的身份验证数据,并立即生效这些变更[^5]。 最后按照标准流程为管理员角色指定一个新的高强度口令: ```sql ALTER USER 'root'@'localhost' IDENTIFIED BY 'YourStrongPassword!'; FLUSH PRIVILEGES; EXIT; ``` 这里替换了 `'YourStrongPassword!'` 成一个复杂度较高的实际值作为最终的目标状态[^2]。 #### 成设置后恢复正常的服务运作 成以上所有步骤之后,记得要正常关闭临时实例并将常规守护进程恢复在线: ```bash sudo pkill -9 mysqld sudo rm -rf /var/run/mysqld/* sudo systemctl start mysqld.service ``` 现在应该能够凭借刚刚定义的新凭据访问管理控制台了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值