mysql修改复杂密码设置(MySQL修改密码的几种方式)
mysql修改复杂密码设置
MySQL修改密码的几种方式前言:
在日常使用数据库的过程中,难免会遇到需要修改账号密码的情景,比如密码太简单需要修改、密码过期需要修改、忘记密码需要修改等。本篇文章将会介绍需要修改密码的场景及修改密码的几种方式。
1.忘记 root 密码
忘记 root 密码的场景还是比较常见的,特别是自己搭的测试环境经过好久没用过时,很容易记不得当时设置的密码。这个时候一般常用的方法是跳过权限验证,然后更改 root 密码,之后再启用权限验证。以 MySQL 5.7 版本为例简单讲下主要过程:
首先修改配置文件,在[mysqld]部分加上一句:skip-grant-tables ,加上此参数的目的是跳过权限验证。然后重启数据库,数据库再次启动后,我们就可以不用密码直接登录数据库修改密码了。
|
# skip- grant -tables 模式下修改root密码 [root@host ~]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 16 Server version: 5.7.23-log MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and / or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and / or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> update mysql. user set authentication_string = password ( 'xxxxxx' ) where user = 'root' and host = 'localhost' ; Query OK, 0 rows affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 0 Warnings: 1 mysql> flush privileges ; Query OK, 0 rows affected (0.01 sec) |
修改完 root 密码后,再次去除 skip-grant-tables 参数,然后重启下数据库即可。
2.几种修改密码的方法
除去忘记密码,可能还有其他情景需要修改密码,这时候就可以采取普通方式修改密码了。还是以 MySQL 5.7 版本为例,介绍几种常用的修改密码的方法。
使用 alter user 修改
比如如果想更改 testuser 账号的密码,我们可以使用 root 账号登录,然后执行 alter user 命令更改 testuser 账号的密码。
|
mysql> alter user 'testuser' @ '%' identified by 'Password1' ; Query OK, 0 rows affected (0.01 sec) mysql> flush privileges ; Query OK, 0 rows affected (0.00 sec) |
使用 SET PASSWORD 命令
使用 SET PASSWORD 修改密码命令格式为 SET PASSWORD FOR 'username'@'host' = PASSWORD('newpass'); 同样是使用 root 账号可修改其他账号的密码。
|
mysql> SET PASSWORD FOR 'testuser' @ '%' = PASSWORD ( 'Password2' ); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges ; Query OK, 0 rows affected (0.00 sec) |
使用 mysqladmin 修改密码
使用 mysqladmin 命令修改账号密码格式为 mysqladmin -u用户名 -p旧密码 password 新密码
|
[root@host ~]# mysqladmin -utestuser -pPassword2 password Password3 mysqladmin: [Warning] Using a password on the command line interface can be insecure. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety. [root@host ~]# mysql -utestuser -pPassword3 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2388 Server version: 5.7.23-log MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and / or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and / or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> |
直接 update user 表
其实 MySQL 所以的账号信息都存储在 mysql.user 表里面,我们也可以直接通过 update user 表来修改密码。
|
# 5.7及之后版本 mysql> update mysql. user set authentication_string = password ( 'Password4' ) where user = 'testuser' and host = '%' ; Query OK, 1 row affected, 1 warning (0.06 sec) Rows matched: 1 Changed: 1 Warnings: 1 mysql> flush privileges ; Query OK, 0 rows affected (0.01 sec) # 5.6及之前版本 update mysql. user set password = password ( '新密码' ) where user = '用户名' and host= 'host' ; |
3.设置 login-path 本地快捷登陆
为了防止密码暴露及忘记密码,我们还可以设置 login-path 来实现在本地不输密码快捷登录。
login-path 是 MySQL 5.6 开始支持的新特性。通过借助 mysql_config_editor 工具将登陆 MySQL 服务的认证信息加密保存在 .mylogin.cnf 文件(默认位于用户主目录)。MySQL 客户端工具可通过读取该加密文件连接 MySQL ,实现快捷登录。
假设我们想配置 root 账号在本地快捷登录,可以这么做:
|
# 执行回车后需要输入一次root密码 [root@host ~]# mysql_config_editor set --login-path=root -uroot -hlocalhost -p -P3306 Enter password : # 配置完成后可以使用login-path登录 [root@host ~]# mysql --login-path=root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2919 Server version: 5.7.23-log MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and / or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and / or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> |
总结:
本篇文章主要介绍了修改数据库账号密码的几种方法,基本涵盖了所有的场景。这里也提醒下各位,数据库账号最好限制ip段登录,密码尽量复杂些,最好能够定期修改,特别是重要的环境不能有半点马虎。年底了,安全才是王道。
以上就是MySQL修改密码的几种方式的详细内容,更多关于MySQL修改密码的资料请关注开心学习网其它相关文章!
原文链接:https://mp.weixin.qq.com/s/7yaDpqGCkZRnUwNY4d8tsg
- mysql查询语法总结(MySQL全面瓦解之查询的过滤条件详解)
- mysql怎么创建联合索引(mysql的联合索引复合索引的实现)
- mysql xml转换json(Mysql将查询结果集转换为JSON数据的实例代码)
- mysql快速创建索引(MySQL创建高性能索引的全步骤)
- mysql修改表结构的关键字(查看修改MySQL表结构命令)
- 最新版MySQL 8.0.22下载安装超详细教程(Windows 64位)(最新版MySQL 8.0.22下载安装超详细教程Windows 64位)
- docker查找redis配置文件(解决docker重启redis,mysql数据丢失的问题)
- mysql有数据但筛选值为空(解决从集合运算到mysql的not like找不出NULL的问题)
- 对mysql索引的理解(详解MySQL 8.0 之不可见索引)
- django mysql设置(Django框架使用mysql视图操作示例)
- mysql数值类型图解(详解mysql持久化统计信息)
- mysql删除表内所有数据(mysql 大表批量删除大量数据的实现方法)
- win10下安装mysql8.0.23 及 “服务没有响应控制功能”问题解决办法(win10下安装mysql8.0.23 及 “服务没有响应控制功能”问题解决办法)
- mysql日志使用(MySQL 日志相关知识总结)
- 关于mysql性能优化的描述(Mysql性能优化之索引下推)
- 终于有人将mysql 索引讲清楚了(MySQL 索引的一些细节分享)
- Google 推出了一个游戏生成器,让不会编程的你也能自己设计游戏(推出了一个游戏生成器)
- 二胎家庭老大爱闹情绪,用这招很有效(二胎家庭老大爱闹情绪)
- 一个30岁男人外遇失败的全过程(一个30岁男人外遇失败的全过程)
- 《无敌破坏王2》 不聊彩蛋,聊聊我从动画里看到的现实那些事儿(无敌破坏王2不聊彩蛋)
- 《寄生虫》 三观不正 人类悲欢从来不相通,感同身受也并非本能(寄生虫三观不正)
- 这部动漫中的女孩子,可比101女孩更加励志(这部动漫中的女孩子)
热门推荐
- host切换工具SwitchHosts
- python3yield使用教程(python中yield的用法详解——最简单,最清晰的解释)
- python数组矩阵操作(Python矩阵和Numpy数组的那些事儿)
- htmlcanvas的使用(html2canvas生成清晰的图片实现打印的示例代码)
- 云服务器选择七个原则(如何选择一个好的云服务器?)
- python实战方法(Python常用特殊方法实例总结)
- phpstudy创建网站无权限(phpStudy配置多站点多域名和多端口的方法)
- mysql快速导入1亿数据(mysql批量新增和存储的方法实例)
- cookie政策及设置(详解操作cookie的原生方法cookieStore)
- sqlserver2008数据库的备份(sql server 2008 压缩备份数据库20g)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9