MySql8.x 修改密码

Posted YoungerChina

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySql8.x 修改密码相关的知识,希望对你有一定的参考价值。

1、常用方案(不适合8.x)

1.1 修改配置

修改配置文件/etc/my.cnf

增加如下内容:

# 无密码登录
skip-grant-tables

1.2 重启mysql服务

systemctl restart mysqld

1.3 修改mysql密码

在输入密码时,直接回车。

# mysql -u root -p
Enter password:   <直接回车>
Welcome to the MySQL monitor.  Commands end with ; or \\g.
Your MySQL connection id is 7
Server version: 8.0.31 MySQL Community Server - GPL

Copyright (c) 2000, 2022, 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> 

修改update user set password=PASSWORD('123456') where user='root'

mysql> use mysql
mysql> update user set password=PASSWORD('123456') where user='root';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('123456') where user='root'' at line 1
mysql>

        出现了语法错误之后,查看user表中字段。发现表中没有password 字段,所以update user set password 会出错,查找资料发现mysql 8.0.2之后数据库,密码规则发生变化,改变了user表。

2、解决方案(适合8.x)

        恢复配置文件/etc/my.cnf默认配置,然后重启mysqld服务

(1)通过/var/log/mysqld.log查询密码

        在mysqld.log中查询以下记录:

        A temporary password is generated for root@localhost:XXXXXXXX

2022-10-11T07:22:24.592233Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: T7w4:nawmL(m

(2)登录mysql

(3)修改mysql密码

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Mysql#135';
Query OK, 0 rows affected (0.02 sec)

成功

以上是关于MySql8.x 修改密码的主要内容,如果未能解决你的问题,请参考以下文章

MySql8.x 修改密码

mysql8.x开启远程登录

mysql 8.x 压缩包zip安装

Mycat 整合 MySQL 8.x 踩坑实践

Mysql第二期 MySQL环境搭建

Mysql第二期 MySQL环境搭建