国内外VPS
主机测评与优惠

MySQL 8.0如何重置密码

MySQL 8.0如何重置密码

安装 MySQL 8.0 后,默认的用户 root 是没有密码的,需要修改默认的初始化密码。假如忘记了 root 用户的密码,也是需要重置密码的。

首先安装 MySQL 8.0 后,启动 MySQL 服务。我这里是在 MAC 上安装使用的。

 

1
2
brew install mysql
brew services start mysql

因为默认是没有密码的,所以先直接进入 MySQL 服务中。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
➜  ~ mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 13
Server version: 8.0.12 Homebrew
Copyright (c) 2000, 2018, 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 时候启动了授权表,也就是没有加上 skip-grant-tables 启动参数。使用这种方法设置密码:

 

1
ALTER USER ‘root'@'localhost' IDENTIFIED WITH caching_sha2_password BY ‘123456';

如果你用数据库可视化软件链接时,出现下面的报错:

 

1
Unable to load authentication plugin ‘caching_sha2_password’.

这是因为 MySQL 8.0 使用的新的身份验证机制 caching_sha2_password,如果不想用这个,可以用之前的旧的验证方式。

 

1
ALTER USER ‘root'@'localhost' IDENTIFIED WITH mysql_native_password BY ‘123456';

或者修改默认的配置也可以。

 

1
2
[mysqld]
default_authentication_plugin=mysql_native_password

方法二

假如启动 MySQL 时候没有启动了授权表,也就是加上 skip-grant-tables 启动参数。
这种时候一般是忘记了 root 用户的密码,这里的 root 用户是 MySQL 的用户,不要和 Linux 上的用户搞混了。
使用这种方法设置密码:

首先关闭并启动 MySQL,并进入 MySQL 命令行。

 

1
2
3
brew services stop mysql
mysqld –skip-grant-tables  –skip-networking
mysql

把密码设置为空。

 

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

然后关闭 MySQL 并正常启动,不需要 skip-grant-tables 启动参数了。

 

1
mysql -uroot

重复方法一的操作设置密码。

 

1
ALTER USER ‘root'@'localhost' IDENTIFIED WITH caching_sha2_password BY ‘123456';

赞(0)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《MySQL 8.0如何重置密码》
文章链接:https://www.liuzhanwu.com/2289.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

登录

找回密码

注册