FAQ:MySQL

来自Wikioe
跳到导航 跳到搜索


MySQL 忘记并重置密码

步骤:

  1. 无密码模式启动 MySQL;
  2. 连接并修改密码;

设置跳过权限验证

MySQL 5.7

  1. 修改配置文件(“my.ini”或“/etc/my.cnf”):
    [mysqld]
    ...
    
    # 跳过权限验证
    skip-grant-tables
    
    ...
    
  2. 重启服务器;
  3. 连接到 MySQL;


MySQL 8+

  1. 方法一:
    1. 停止服务;
    2. 命令行:执行“mysqld --console --skip-grant-tables --shared-memory”(保持窗口);
    3. 连接到 MySQL;
  2. 方法二:
    1. 停止服务;
    2. 创建一个“包含一条密码修改命令”的文本文件“mysqlc.txt”:
      ALTER USER 'root'@'localhost' IDENTIFIED BY '';
      
    3. 命令行:启动服务器,并指定命令文件:“mysqld --init-file="D:\Program Files\MySQL\mysqlc.txt" --console”;
    4. 关闭命令窗口,重启服务器;


  • MySQL 8 中修改配置文件的方式(“skip-grant-tables”)已失效:服务启动后马上停止!

修改密码

USE mysql;

FLUSH PRIVILEGES;

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

FLUSH PRIVILEGES;
  • 修改密码前先刷新权限,避免“1290 - The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement”问题;
  • 通过“password_last_changed”(密码修改时间)字段,可以确定修改是否成功;

密码相关

user 表字段:

SELECT host, user, authentication_string, plugin FROM USER;
  • host”:登录方式,“localhost”(本地登录)“%”(可远程登录);
  • authentication_string”:密码字段,相同密码根据不同认证插件得到的字段也不同;
    • 5.7以前的密码字段是“password”;
  • plugin”:认证插件;
  • password_last_changed”:上次修改密码的时间;


认证插件:【默认插件设置在配置文件中】

  • MySQL 5.7:默认“mysql_native_password”;
  • MySQL 8:默认“caching_sha2_password”,可选“mysql_native_password”;


更新密码:

  • MySQL 5.7:
    MySQL 5.7.5 及以前:
    SET PASSWORD FOR 'root'@'localhost' = PASSWORD('@eijux');
    
    MySQL 5.7.6 及以后:
    # 以下等效:
    
    UPDATE USER SET authentication_string=PASSWORD('@eijux') WHERE user='root' AND host='localhost';
    
    UPDATE USER SET authentication_string=UPPER(CONCAT('*',SHA1(UNHEX(SHA1('@eijux'))))) WHERE user='root' AND host='localhost';
    
    ALTER USER 'root'@'localhost' IDENTIFIED BY '@eijux';
    
  • MySQL 8:
    # 使用配置的认证插件加密
    ALTER USER 'root'@'localhost' IDENTIFIED BY '@eijux';
    
    # 使用“mysql_native_password”加密:
    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '@eijux';
    
    # 使用“caching_sha2_password”加密:
    ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY '@eijux';
    
    • MySQL 8 中不能在使用“password”函数,但可以将密码字段置空:
      UPDATE mysql.user SET authentication_string='' WHERE user='root' AND host='localhost';
      


修改密码策略:【???】

SET GLOBAL validate_password.length = 6;		//密码长度

SET GLOBAL validate_password.policy = 'LOW';	//密码策略级别

FLUSH PRIVILEGES;								//刷新权限

Linux 服务

systemctl start mysqld		//开启服务
systemctl stop mysqld 		//停止服务
systemctl restart mysqld 	//重启服务
systemctl status mysqld 	//服务状态查看

错误信息

“1045-Access denied for user 'root'@'localhost'(using password:YES)”

如果连接到 MySQL 时出现该提示,即 root 用户对于 localhost 之外的连接被拒绝,需要修改其 host 为“%”:

USE mysql;

UPDATE USER SET host = '%' WHERE user = 'root';

FLUSH PRIVILEGES;

“1290 - The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement”

执行权限相关命令时出现该提示,则需要在该命令前先执行:

FLUSH PRIVILEGES;

“2003 - can't connect to mysql server on 'localhost' (10060 "Unknown error") ”

检查下服务是否启动,可能已崩溃。

  • MySQL 8 用 skip-grant-tables 时,启动不到一分钟就自动停止了【该方法已失效】