FAQ:MySQL
		
		
		
		
		
		跳到导航
		跳到搜索
		
		
	
MySQL 忘记并重置密码
步骤:
- 无密码模式启动 MySQL;
 - 连接并修改密码;
 
设置跳过权限验证
MySQL 5.7 :
- 修改配置文件(“my.ini”或“/etc/my.cnf”):
[mysqld] ... # 跳过权限验证 skip-grant-tables
 - 重启服务器;
 - 连接到 MySQL;
 
MySQL 8+ :
- 停止服务;
 - 命令行:执行“mysqld --console --skip-grant-tables --shared-memory”(保持窗口);
 - 连接到 MySQL;
 
- 修改配置文件的方式(“skip-grant-tables”)已失效,服务启动后马上停止!
- 此方式连接时会出现“2003 - can't connect to mysql server on 'localhost' (10060 "Unknown error") ”
 
 
修改密码
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:
# 以下等效: UPDATE mysql.user SET authentication_string=PASSWORD('@eijux') WHERE user='root' and host='localhost'; UPDATE mysql.user SET authentication_string=UPPER(CONCAT('*',SHA1(UNHEX(SHA1('@eijux'))))) WHERE user='root' and host='localhost'; alter mysql.user 'root'@'localhost' identified by '@eijux';
 - MySQL 8:
# 使用配置的认证插件加密 alter mysql.user 'root'@'localhost' identified by '@eijux'; # 使用“mysql_native_password”加密: alter mysql.user 'root'@'localhost' identified with mysql_native_password by '@eijux'; # 使用“caching_sha2_password”加密: alter mysql.user 'root'@'localhost' identified with caching_sha2_password by '@eijux';
 
修改密码策略:【???】
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 时,启动不到一分钟就自动停止了【该方法已失效】