首页 文章 mysql 8 初次安装设置密码 或者 忘记密码后重置

mysql 8 初次安装设置密码 或者 忘记密码后重置

发布时间:2020-02-05 12:09:01 作者:Soul-Yang 阅读量:1129

首先你确定是忘记了?还是初次安装不知道密码?

初次安装之后,正确打开方式是:走设置流程,方法如下(安装完成并启动mysql之后执行):

1.执行执行以下命令设置密码,可以命令行登录先。

mysql_secure_installation

运行mysql_secure_installation会执行几个设置:

0)设置密码安全级别

a)为root用户设置密码

b)删除匿名账号

c)取消root用户远程登录

d)删除test库和对test库的访问权限

e)刷新授权表使修改生效

2. MySQL8.0登录提示caching_sha2_password问题解决方法

查看身份验证类型

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> SELECT Host, User, plugin from user;
+-----------+------------------+-----------------------+
| Host      | User             | plugin                |
+-----------+------------------+-----------------------+
| %         | root             | caching_sha2_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session    | caching_sha2_password |
| localhost | mysql.sys        | caching_sha2_password |
| localhost | root             | caching_sha2_password |
+-----------+------------------+-----------------------+
5 rows in set (0.00 sec)

root 用户的验证器插件为 caching_sha2_password

mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.01 sec)

使生效

mysql> FLUSH PRIVILEGES;

验证是否生效

mysql> SELECT Host, User, plugin from user;
+-----------+------------------+-----------------------+
| Host      | User             | plugin                |
+-----------+------------------+-----------------------+
| %         | root             | mysql_native_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session    | caching_sha2_password |
| localhost | mysql.sys        | caching_sha2_password |
| localhost | root             | mysql_native_password |
+-----------+------------------+-----------------------+
5 rows in set (0.00 sec)

如果是重置密码(初次安装也可用这种方法改密码),往下看

1.先关闭mysql

linux  :   service stop mysql      
mac  :  brew services stop mysql

2.执行以下内容,使用安全模式运行mysql

mysqld_safe --skip-grant-tables

3. 再打开一个终端,无需密码直接登陆mysql

4.执行以下内容

use mysql;
update user set authentication_string='' where user='root';     //将密码设为空(亲测有效)

5.正常重启mysql,在终端使用空密码登录root  ,然后使用正确修改密码方式修改密码;( 不在安全模式下直接修改密码,因为可能会由权限问题,出现各种语句无法执行


登录后执行改密:
ALTER USER "root"@"localhost" IDENTIFIED  BY "123456";


刷新全新(密码立即生效,无需重启mysql)

FLUSH PRIVILEGES;

延伸(如改完之后shell能登陆,但phpmyadmin无法登陆请往下看 ):

在MySQL 8.0.11中,caching_sha2_password是默认的身份验证插件,而不是以往的mysql_native_password。

有关此更改对服务器操作的影响以及服务器与客户端和连接器的兼容性的信息,请参阅caching_sha2_password作为首选的身份验证插件。(翻译自 https://dev.mysql.com/doc/refman/8.0/en/caching-sha2-pluggable-authentication.html

今天在新服务上配置安装mysql8.0.11时,像往常一样设置mysql密码,设置成功后 在shell下输入mysql -u root -p,再输入密码能正常进入,但在phpmyadmin或直接用 http://php.net/manual/zh/mysqli.real-connect.php上的连接,均提示无法连接 ,具体报错信息为


mysqli_real_connect(): The 
server requested authentication method unknown 
to the client [sha256_password]  

搜了一圈,找到官方文档才发现从8.0.11版本起,不再像mysql5.7及以前版本那样,设置用户密码时默认的验证方式为caching_sha2_password,如果发现升级mysql8.0.11后原有的程序不能连接mysql,可迅速在mysql command line client客户端用下面的命令设置成mysql5.7及以前版本的密码验证方式,同时MYSQL8.0.11下修改密码的方式与原先也不大一样,原先的部分修改密码的命令在mysql8.0.11下不能使用。

  1. > use mysql  
  2. > ALTER USER 
    'root'@
    'localhost' IDENTIFIED 
    WITH mysql_native_password BY 
    '你的密码'; 
  3. > FLUSH PRIVILEGES;      

二, mysql 安装了最新版本8.0.11后创建用户并授权后,授权的用户连接数据库提示

The 
server requested authentication method unknown 
to the client

查阅一些相关的资料后发现是由于新版本的mysql账号密码解锁机制不一致导致的

解决办法:

删除创建的用户和授权,

  1. 找到mysql配置文件并加入

  2. default_authentication_plugin=mysql_native_password

变为原来的验证方式,然后从新创建用户并授权即可

  1. mysql -uroot -p

  2.  输入密码
  3. use mysql;

  4. ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密码';

The caching_sha2_password and sha256_password authentication plugins provide more secure password encryption than the mysql_native_password plugin, and caching_sha2_password provides better performance than sha256_password. Due to these superior security and performance characteristics of caching_sha2_password, it is as of MySQL 8.0 the preferred authentication plugin, and is also the default authentication plugin rather than mysql_native_password.

翻译:caching_sha2_password和sha256_password认证插件比mysql_native_password插件提供的密码加密更加安全,并且caching_sha2_password加密比sha256_password的加密性能更好。由于caching_sha2_password这样优秀的安全和性能特性,让他作为MySQL8.0的首选认证插件,这也是默认的认证插件插件而不是mysql_native_password。

具体你可以访问这个 caching_sha2_password Compatibility Issues and Solutions来了解, 已经使用了新的加密方式,访问不了的解决方法,简单总结一下就是
1、将加密方式改为旧的,在配置文件my.conf中添加如下:


  1. [ mysqld]
  2. default_authentication_plugin=mysql_native_password

2、使用支持新的加密方式的客户端(Client),比如等于或高于8.0.4版本的libmysqlclient
3、使用支持新的加密方式的连接驱动(Connector):


  1. MySQL Connector/C++ 1.1 .11 or higher or 8.0 .7 or higher.
  2. MySQL Connector/J 8.0 .9 or higher.
  3. MySQL Connector/NET 8.0 .10 or higher (through the classic MySQL protocol).
  4. MySQL Connector/Node.js 8.0 .9 or higher.
  5. PHP: the X DevAPI PHP extension (mysql_xdevapi) supports caching_sha2_password.

4、使用了新的加密方式,改为旧的加密方式,而root用户也要进行相应的更改才可以,因为root用户还是新的加方式,所以使用alter语句改为重置密码来覆盖新的加密方式的密码:


  1. ALTER USER 'root'@ 'localhost'
  2. IDENTIFIED WITH mysql_native_password
  3. BY 'password';

password是你将要设置的root用户的密码。




  
留言
https://blog.key9.cn/
用户登录
您还没有写任何评论内容!
您已经评论过了!
只能赞一次哦!
您已经收藏啦!