
安装 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';
|
声明:1、本博客不从事任何主机及服务器租赁业务,不参与任何交易,也绝非中介。博客内容仅记录博主个人感兴趣的服务器测评结果及一些服务器相关的优惠活动,信息均摘自网络或来自服务商主动提供;所以对本博客提及的内容不作直接、间接、法定、约定的保证,博客内容也不具备任何参考价值及引导作用,访问者需自行甄别。2、访问本博客请务必遵守有关互联网的相关法律、规定与规则;不能利用本博客所提及的内容从事任何违法、违规操作;否则造成的一切后果由访问者自行承担。3、未成年人及不能独立承担法律责任的个人及群体请勿访问本博客。4、一旦您访问本博客,即表示您已经知晓并接受了以上声明通告。







