MySQL 8.0.12忘记root密码如何找回

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_20846487/article/details/83189469

今天安装了win7 64bit的MySQL 社区版,版本号为 8.0.12,安装步骤:(参考https://www.cnblogs.com/reyinever/p/8551977.html)

  1. 下载、解压介质
  2. 设置MySQL_HOME环境变量,并将";%MYSQL_HOME%\bin"添加到PATH最后(不要忽略首位分号)
  3. 生成data文件(管理员运行CMD,cd到bin/,“mysqld --initialize-insecure --user=mysql”)
  4. “mysql -install”(避免启动mysql报错“服务名无效”)
  5. 启动mysql,net start mysql

启动后首次使用root连接无需密码,登录后尝试更改root密码:“update mysql.user set authentication_string=password(‘123456’) where user = ‘root’;”
结果报错,报错信息为“”
删掉password字样,update执行成功,flush后重连,结果使用新密码死活无法登录root

只好设置跳过密码验证

  1. 停止mysql服务:“net stop mysql”
  2. 设置跳过验证:“mysqld --shared-memory --skip-grant-tables”,(注意:一定要有–shared-memory,否则无法正常设置–skip-grant-tables并启动mysql服务)
  3. 无密码登录:在mysql/bin/目录下新开一个CMD窗口,无需重复启动mysql,直接输入"mysql",此时应该可以连接成功

重设root密码时又遇到问题
由于此时处于"–skip-grant-tables",故使用"ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘123456’" 无法修改root密码,会报错;
尝试使用“update mysql.user set _string=password(‘123456’) where user = ‘root’”;再次报错,提示(‘123456’)附近有语法错误,删去‘password()’字样可执行,但此时依旧无法使用新密码登录.

查阅资料无果,猜测是不使用password()设密码时,密码原样存入user表,但登录是会对输入密码进行编码(MySQL的密码认证插件是“mysql_native_password”,而现在使用的是“caching_sha2_password”),且尚不知道如何在设置密码时使用相同编码策略。

最后使用update语句,将root密码设置为空update mysql.user set authertication_string=’’ where user = ‘root’;),竟然可以"mysql -uroot -p"显示"Enter password:"时直接回车,无密码正常登录。

原因:

  1. MySQL 在8.0.11中移除了password();(The PASSWORD() function. Additionally, PASSWORD() removal means that SET PASSWORD … = PASSWORD(‘auth_string’) syntax is no longer available.
  2. MySQL 8.0.4中改了默认加密方式为“caching_sha2_password”(Starting with MySQL 8.0.4, we are changing the default authentication plugin for MySQL server from mysql_native_password to caching_sha2_password.

猜你喜欢

转载自blog.csdn.net/sinat_20846487/article/details/83189469