如何在 MySQL 8.0.11 中重置 root 密码?

How to reset the root password in MySQL 8.0.11?

我真的忘记了我的root密码,我需要更改它。我遵循以下步骤:

我们可以在这些网站上找到:https://www.howtoforge.com/setting-changing-resetting-mysql-root-passwords#recover-mysql-root-password

mysql> use mysql;
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD("TOOR");
mysql> flush privileges;
mysql> quit

第一个错误,所以我尝试了:

mysql> use mysql;
mysql> update user set password=PASSWORD("TOOR") where User='root';
mysql> flush privileges;
mysql> quit

总是说同样的错误:

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 '("TOO
R") WHERE User='root'' at line 1

我该如何解决这个问题?

试试这个:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'newPasswd';

正如here所说:

This function was removed in MySQL 8.0.11

1.if 你处于 skip-grant-tables 模式
在 mysqld_safe:

UPDATE mysql.user SET authentication_string=null WHERE User='root';
FLUSH PRIVILEGES;
exit;

然后,在终端中:

mysql -u root

在mysql中:

ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';

2.not skip-grant-tables 模式
就在 mysql:

ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';

使用 SQLYog 可以执行命令

  • 用户创建

    CREATE USER 'tester'@'localhost' IDENTIFIED BY 'Pass123#d'
    
  • 授权

    GRANT ALL PRIVILEGES ON sakila.* TO 'tester'@'localhost'
    
  • 在 MySQL 8.0

    中更改密码
    ALTER USER 'tester'@'localhost' IDENTIFIED BY 'Pass123#d';
    

    或者如果你知道 authentication_string 直接将其设置为更新

    UPDATE mysql.user 
    SET authentication_string='*F9B62579F38BE95639ACB009D79427F2D617158F'  
    WHERE USER='root'
    
  • 在低版本中更改密码mysql

    GRANT USAGE ON *.\* TO 'tester'@'localhost' IDENTIFIED BY 'Pass123#d'
    SET PASSWORD FOR 'tester'@'localhost' = PASSWORD('Pass123#d');
    

第 1 步:在您的根目录中创建一个新文件(例如 C:) 第 2 步:编写此 ALTER USER 'root'@'localhost' IDENTIFIED BY 'abc' 并保存 第 3 步:如果您的 Mysql 服务已经 运行,请停止此操作并打开您的 CMD 第 4 步:转到您的 Mysql 安装目录(例如 C:\Program Files\MySQL\MySQL Server 8.0\bin)并键入此命令

mysql --init-file=C:/init.sql

第 5 步:执行此命令,一切顺利:)

请参阅此视频以获得更多说明:https://www.youtube.com/watch?v=LsdV-7dFOhk

只需使用 sudo

登录 mysql
Sudo mysql

然后

ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';
exit;

测试一下

mysql -u root -p

我使用的是 8.0.23,但遇到了多个问题。以下是问题和解决方案。 我遵循 mysql 文档并能够进行重置,尽管我遇到了问题。我已经提供了我的解决方案和示例重置脚本。

问题

  1. 其他人概述的以下步骤有各种失败
  2. user 运行 守护进程是 mysql,我不能 su 到 mysql id
  3. 获取错误无法创建锁定文件 /var/run/mysqld/mysqlx。sock.lock,mysqld 文件夹正在被 mysql 服务删除。
  4. 必须在 运行 之后杀死 -9 mysql 进程,非常难看

解决方案

  1. 使用 https://dev.mysql.com/doc/refman/8.0/en/resetting-permissions.html
  2. 中的步骤
  3. 在 运行 mysql 命令时使用 --user=mysql
  4. 在运行 comamnds
  5. 之前创建文件夹 /var/run/mysqld 并为 mysql:mysql chown
  6. 启动后使用mysqladmin shutdown 停止sql 进行重置

在脚本中,我会将 root 重置为 New%Password

# create password reset file
cat << EOF >/tmp/deleteme
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'New%Password';
EOF
# create /var/run/mysqld
mkdir /var/run/mysqld
chown mysql:mysql /var/run/mysqld
# start mysqld and run the command
mysqld --init-file=/tmp/deleteme --user=mysql &
# wait for sql to start
while [ ! -f /var/run/mysqld/mysqlx.sock.lock ]; do sleep 1; done
sleep 3
# stop sql
mysqladmin -u root -pNew%Password shutdown
while [ -f /var/run/mysqld/mysqlx.sock.lock ]; do sleep 1; done
#clean up temp file
rm /tmp/deleteme

If you are on Windows you can try following steps

重置 MySQL 8.0 root 密码 Windows

  1. 从服务
  2. 停止MySQL 8.0服务
  3. 转到路径 C:\Program Files\MySQL\MySQL Server 8.0\bin 并打开 cmd
  4. 运行 mysqld --console --skip-grant-tables --shared-memory
  5. 在同一路径中打开新的cmd
  6. 运行 遵循命令
  7. mysql -u root
  8. select authentication_string,host from mysql.user where user='root';
  9. UPDATE mysql.user SET authentication_string='' WHERE user='root';
  10. 现在关闭 cmd
  11. 尝试启动 MySQL 8.0 服务。
  12. 使用 root 用户名和空白密码进行连接。
  13. 从用户管理更改密码。

https://gist.github.com/pishangujeniya/0f839d11a7e692dadc49821c274a2394

找到这个