“FAQ:MySQL”的版本间差异
跳到导航
跳到搜索
(→其他:) |
(→其他:) |
||
第22行: | 第22行: | ||
=== 其他: === | === 其他: === | ||
* MySQL5.7更新密码: | |||
*: <syntaxhighlight lang="mysql"> | |||
UPDATE mysql.user SET password=PASSWORD('newpassword') WHERE user='root' and host='localhost'; | |||
</syntaxhighlight> | |||
*: <syntaxhighlight lang="mysql"> | |||
UPDATE mysql.user SET authentication_string=UPPER(CONCAT('*',SHA1(UNHEX(SHA1('admin'))))) WHERE user='root' and host='localhost'; | |||
</syntaxhighlight> | |||
** <syntaxhighlight lang="mysql" inline>PASSWORD('admin')</syntaxhighlight> 等同 <syntaxhighlight lang="mysql" inline>UPPER(CONCAT('*',SHA1(UNHEX(SHA1('admin')))))</syntaxhighlight>,等于字符串“*4ACFE3202A5FF5CF467898FC58AAB1D615029441”。 | |||
* Linux服务操作 | * Linux服务操作 | ||
*: <syntaxhighlight lang="Bash"> | *: <syntaxhighlight lang="Bash"> | ||
第38行: | 第46行: | ||
* (用指定加密方式)更改用户密码: | * (用指定加密方式)更改用户密码: | ||
*: <syntaxhighlight lang="mysql"> | *: <syntaxhighlight lang="mysql"> | ||
alter user 'root'@'localhost' identified with caching_sha2_password by 'newpassword'; | alter mysql.user 'root'@'localhost' identified with caching_sha2_password by 'newpassword'; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
*: <syntaxhighlight lang="mysql"> | *: <syntaxhighlight lang="mysql"> | ||
alter user 'root'@'localhost' identified with mysql_native_password by 'newpassword'; | alter mysql.user 'root'@'localhost' identified with mysql_native_password by 'newpassword'; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
* 命令行方式无密码启动MySQL(不修改配置文件):【???】 | * 命令行方式无密码启动MySQL(不修改配置文件):【???】 | ||
第51行: | 第59行: | ||
* (进入mysql后)把 authentication_string 设置为空,所以可以免密码登录【???】: | * (进入mysql后)把 authentication_string 设置为空,所以可以免密码登录【???】: | ||
*: <syntaxhighlight lang="mysql"> | *: <syntaxhighlight lang="mysql"> | ||
update mysql.user set authentication_string='' where user='root'; | |||
</syntaxhighlight> | </syntaxhighlight> | ||
* 查看MySQL密码校验相关设置: | * 查看MySQL密码校验相关设置: |
2020年10月7日 (三) 03:34的版本
MySQL8忘记并重置密码
思路:
- 密码丢失,必须采用无密码启动的方式进入系统,才能进行修改密码操作;
- (Mysql5.7+)password字段和password()函数,密码为authentication_string字段;
- mysql8.0之前的版本加密规则是mysql_native_password,8.0之后是caching_sha2_password;
步骤:
- 修改配置文件
vim /etc/my.cnf
,添加skip-grant-table
(跳过权限验证); - 重启mysql服务:
systemctl restart mysqld
; - mysql登录(无需密码):
mysql -uroot -p
; - 刷新权限表:
flush privileges;
; - 重置密码:
use mysql; alter user 'root'@'localhost' identified by 'newpassword';
- 重置配置文件
vim /etc/my.cnf
,删除skip-grant-table
; - 重启mysql服务:
systemctl restart mysqld
;
其他:
- MySQL5.7更新密码:
UPDATE mysql.user SET password=PASSWORD('newpassword') WHERE user='root' and host='localhost';
UPDATE mysql.user SET authentication_string=UPPER(CONCAT('*',SHA1(UNHEX(SHA1('admin'))))) WHERE user='root' and host='localhost';
PASSWORD('admin')
等同UPPER(CONCAT('*',SHA1(UNHEX(SHA1('admin')))))
,等于字符串“*4ACFE3202A5FF5CF467898FC58AAB1D615029441”。
- Linux服务操作
systemctl start mysqld //开启服务 systemctl stop mysqld //停止服务 systemctl restart mysqld //重启服务 systemctl status mysqld //服务状态查看
- 查看用户相关信息:
select host, user, authentication_string, plugin from user;
- host:用户登录方式,
'localhost'
本地登录,'%'
可远程登录; - authentication_string:用户密码;
- plugin:密码加密方式;
- (用指定加密方式)更改用户密码:
alter mysql.user 'root'@'localhost' identified with caching_sha2_password by 'newpassword';
alter mysql.user 'root'@'localhost' identified with mysql_native_password by 'newpassword';
- 命令行方式无密码启动MySQL(不修改配置文件):【???】
- 停止MySQL服务;
- 命令行启动Mysql:
mysqld --console --skip-grant-tables --shared-memory
- (进入mysql后)把 authentication_string 设置为空,所以可以免密码登录【???】:
update mysql.user set authentication_string='' where user='root';
- 查看MySQL密码校验相关设置:
SHOW VARIABLES LIKE 'validate_password.%';
- 修改密码策略:
set global validate_password.length = 6; //密码长度 set global validate_password.policy = 'LOW'; //密码策略级别 FLUSH PRIVILEGES; //刷新权限