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修改密码的资料请关注我们其它相关文章!

(0)

相关推荐

  • mysql5.7及mysql 8.0版本修改root密码的方法小结

    mysql5.7版本: 方法1: 用SET PASSWORD命令 格式:mysql> set password for 用户名@localhost = password('新密码'); 例子:mysql> set password for root@localhost = password('123'); mysql5.7和mysql8.0以上都可以用 方法2:用mysqladmin 格式:mysqladmin -u用户名 -p password 新密码 例子:mysqladmin -uroo

  • mysql允许外网访问以及修改mysql账号密码实操方法

    mysql的root账户,我在连接时通常用的是localhost或127.0.0.1,公司的测试服务器上的mysql也是localhost所以我想访问无法访问,测试暂停. 解决方法如下: 1,修改表,登录mysql数据库,切换到mysql数据库,使用sql语句查看"select host,user from user ;" mysql -u root -pvmwaremysql>use mysql; mysql>update user set host = '%' wher

  • MySQL修改root密码的4种方法(小结)

    方法1: 用SET PASSWORD命令  首先登录MySQL. 格式:mysql> set password for 用户名@localhost = password('新密码'); 例子:mysql> set password for root@localhost = password('123'); 方法2:用mysqladmin  格式:mysqladmin -u用户名 -p旧密码 password 新密码 例子:mysqladmin -uroot -p123456 password

  • Windows7下安装使用MySQL8.0.16修改密码、连接Navicat问题

    在安装MySQL时遇到几个问题,网上查找的答案大同小异,并没有很好的解决我的问题,完成之余记录下来方便以后查看. 1.官网下载8.0.16版本MySQL 2.解压缩并将解压的文件放在C:\Program Files\MySQL下 3.配置环境变量,将C:\Program Files\MySQL\bin 添加到系统变量path中 4.添加配置文件,在MySQL根目录下创建my.ini 文件内容如下,修改'basedir='.'datadir='地址 [mysqld] # 设置3306端口 port

  • Windows10下mysql 8.0.19 winx64安装教程及修改初始密码

    本文为大家分享了mysql 8.0.19 winx64安装教程,供大家参考,具体内容如下 1. 下载mysql-8.0.19-winx64 1.1 进入地址:https://dev.mysql.com/downloads/mysql/ 1.2 解压zip包,并将解压文件 mysql-8.0.19-winx64 文件放在想放的位置,比如 D 盘下,如图: 1.3 配置环境变量(目的是为了避免在CMD窗口下操作时反复切换路径) 在Path下添加 D:\mysql-8.0.19-winx64\bin

  • MySQL 如何修改root用户的密码

    方法1:用SET PASSWORD命令 mysql> set password for 用户名@localhost = password('新密码'); -- 举例 mysql> set password for root@localhost = password('123'); 方法2:用mysqladmin mysql> mysqladmin -u用户名 -p旧密码 password 新密码; -- 举例 mysql> mysqladmin -uroot -p123456 pa

  • mysql 8.0.16 winx64及Linux修改root用户密码 的方法

    连接数据库等基础操作请自行解决哈,本篇是重点记录如何改密码. 一.查询用户密码: 查询用户密码命令: select host, user, authentication_string from mysql.user ; host:允许用户登录的ip'位置'%表示可以远程: user:当前数据库的用户名: authentication_string:用户密码(后面有提到此字段): 二. 设置(或修改)用户密码: 默认root密码为空的话 ,下面使用navicat就无法连接(之前我装的5.7好像还可

  • mysql8.0忘记密码修改与net命令服务名无效问题

    cmd中输入net start mysql 提示:服务名无效 请进入MySQL的bin目录,并在bin目录打开命令行窗口,或设置系统环境变量,在命令行窗口输入:mysqld --install,回车,提示:Service successfully installed. 表示安装MySQL服务成功,命令行窗口输入:net start mysql ,可以正常启动 mysql8.0忘记密码修改: 亲测下面这个代码是可行的 明明在前面安装MySQL的时候有要你设置一个密码,但是其实那个密码貌似用不到,这

  • MySql8.0以上版本正确修改ROOT密码的方法

    部署环境: 安装版本red hat Cent 7.0 MYSQL 版本 8.0.2.0 成功部署完毕后出现故障情况: 1.      正常启动MYSQL服务后,敲Linux中root账户和密码进入不去. 2.      从/etc/my.cnf 配置文件中加入skip-grant-table后正常登陆,但是不能创建用户等多操作 总结来说: 想进去mysql后不能操作多指令,操作多指令又不能进去mysql,死循环 挖坑环节: 网上找了很多办法,首先加入skip-grant-table.后进去刷新权

  • 解决MySQL8.0安装第一次登陆修改密码时出现的问题

    下面给大家介绍下mysql 8.0.16 初次登录修改密码 mysql数据库初始化后初次登录需要修改密码 初次登录会碰到下面这个错误 ql> alter user root identified by 'password'; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. 需要使用下面的命令来修改密码 mysql> alter

  • Ubuntu查看修改mysql的登录名和密码、安装phpmyadmin

    安装好mysql后,在终端输入 mysql -u root -p 按回车,输入密码后提示access denied......ues password YES/NO的错误 原因是用户名或密码不对! 查看.修改mysql的用户名和密码 第一步: 这时你需要进入/etc/mysql目录下,然后sudo vim/vi debian.cnf查看里面的用户名和密码,然后使用这个文件中的用户名和密码进入mysql,假如debian.cnf中的用户名为debian-sys-maint,则: mysql -u

随机推荐