当前位置:网站首页>用户和权限修改用户密码
用户和权限修改用户密码
2022-07-27 18:00:00 【华为云】
30.7 修改用户密码
MySQL支持使用mysqladmin命令修改用户的密码,可以在MySQL命令行使用SET PASSWORD语句修改用户的密码,也可以使用GRANT语句修改用户的密码,还可以直接修改user数据表来修改用户的密码。
30.7.1 通过mysqladmin修改用户密码
通过mysqladmin即可以修改root用户的密码,也可以修改普通用户的密码。
(1)使用mysqladmin将root用户的密码修改为root。
[[email protected] ~]# mysqladmin -u root -h localhost -p password "root"Enter password: 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用户的原密码即可将root密码修改为root。
(2)使用mysqladmin将binghe用户的密码修改为binghe。
[[email protected] ~]# mysqladmin -u binghe -h localhost -p password "binghe"Enter password: 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.输入binghe用户的原密码即可将密码修改为binghe。
30.7.2 使用SET PASSWORD语句修改用户密码
使用SET PASSWORD语句可以修改其他用户的密码,也可以修改当前用户自身的密码。
(1)使用root用户登录MySQL后,修改binghe用户的密码为@Binghe123456。
mysql> SET PASSWORD FOR 'binghe_test'@'localhost' = PASSWORD('@Binghe123456');Query OK, 0 rows affected, 1 warning (0.12 sec)SQL语句执行成功,此时binghe_test用户的密码被修改为@Binghe123456。
(2)使用SET PASSWORD修改当前用户自身的密码时,省略FOR语句即可。
mysql> SET PASSWORD = PASSWORD('root');Query OK, 0 rows affected, 1 warning (0.00 sec)(3)如果知道密码的密文,可以使用密码的密文来修改用户的密码。例如,查看密码@Binghe123456的密文如下:
mysql> SELECT PASSWORD('@Binghe123456');+-------------------------------------------+| PASSWORD('@Binghe123456') |+-------------------------------------------+| *0DEB06AA6E096EB2F26EACEE157143ADB9481B5B |+-------------------------------------------+1 row in set, 1 warning (0.00 sec)使用密文修改用户的密码。
mysql> SET PASSWORD = '*0DEB06AA6E096EB2F26EACEE157143ADB9481B5B';Query OK, 0 rows affected, 1 warning (0.00 sec)注意:不管是root用户还是普通用户,都可以在MySQL命令行通过如下SQL语句修改自己的密码。
mysql> SET PASSWORD = PASSWORD('密码明文');或者:
mysql> SET PASSWORD = '密码密文';30.7.3 使用GRANT语句修改用户密码
MySQL支持使用GRANT语句修改用户的密码,但是不影响当前修改密码的用户权限。例如,使用GRANT语句修改binghe_test用户的密码为binghe_test。
mysql> GRANT USAGE ON *.* TO 'binghe_test'@'localhost' IDENTIFIED BY 'binghe_test';Query OK, 0 rows affected, 1 warning (0.00 sec)SQL语句执行成功,此时,binghe_test用户的密码被修改为binghe_test。
查看binghe_test用户的权限。
mysql> SHOW GRANTS FOR 'binghe_test'@'localhost';+----------------------------------------------------------+| Grants for [email protected] |+----------------------------------------------------------+| GRANT SELECT, INSERT ON *.* TO 'binghe_test'@'localhost' |+----------------------------------------------------------+1 row in set (0.00 sec)可以看到,修改binghe_test用户的密码时,并没有修改binghe_test用户的权限。
可以使用密码的密文修改用户的密码。
mysql> GRANT USAGE ON *.* TO 'binghe_test'@'localhost' IDENTIFIED BY PASSWORD '*0DEB06AA6E096EB2F26EACEE157143ADB9481B5B ';Query OK, 0 rows affected, 1 warning (0.00 sec)注意:使用GRANT语句修改用户的密码时,为了不影响用户的权限,必须使用如下形式的GRANT语句修改用户的密码。
GRANT USAGE ON *.* TO '用户名'@'主机名' IDENTIFIED BY '密码明文';或者:
GRANT USAGE ON *.* TO '用户名'@'主机名' IDENTIFIED BY PASSWORD '密码密文';30.7.4 通过操作user数据表修改用户密码
MySQL中的用户信息存储在mysql数据库下的user数据表中,可以通过修改user数据库中的密码字段来修改用户的密码,需要注意的是,MySQL 5.7以下版本中user表的密码字段与MySQL 5.7及以上版本的user表中的密码字段不同。
(1)在MySQL 5.6版本中修改binghe_test用户的密码为@Binghe123456。
mysql> UPDATE mysql.user SET password = PASSWORD('@Binghe123456') WHERE user = 'binghe_test' AND host = 'localhost';Query OK, 1 row affected, 1 warning (0.10 sec)Rows matched: 1 Changed: 1 Warnings: 1mysql> FLUSH PRIVILEGES;Query OK, 0 rows affected (0.00 sec)(2)在MySQL 5.7及以上版本中修改binghe_test用户的密码为@Binghe123456。
mysql> UPDATE mysql.user SET authentication_string = PASSWORD('@Binghe123456') WHERE user = 'binghe_test' AND host = 'localhost';Query OK, 1 row affected, 1 warning (0.10 sec)Rows matched: 1 Changed: 1 Warnings: 1mysql> FLUSH PRIVILEGES;Query OK, 0 rows affected (0.00 sec)30.7.5 忘记root密码的解决方案
修改用户密码中,有一种特殊的情况就是忘记root账户的密码时,如何修改root账户的密码,本节就简单介绍下忘记root账户密码的解决方案。
(1)编辑MySQL的配置文件my.conf,在[mysqld]下添加skip-grant-tables=1配置项,使MySQL在启动时不进行密码验证。
[[email protected] ~]# vim /data/mysql/conf/my.cnf[mysqld]skip-grant-tables=1保存后退出vim编辑器。
(2)重新启动MySQL服务。
[[email protected] ~]# service mysqld restartStopping MySQL: [ OK ]Starting MySQL: [ OK ](3)使用root账户登录MySQL。
[[email protected] ~]# mysql -uroot -pEnter password: Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 12Server version: 8.0.18 binghe editionCopyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>(4)修改root账户的密码。在MySQL 5.7以下的版本中,使用如下语句修改root账户的密码。
mysql> UPDATE mysql.user SET password=password('root') WHERE user='root' and host='localhost';Query OK, 1 row affected, 1 warning (0.10 sec)Rows matched: 1 Changed: 1 Warnings: 1mysql> FLUSH PRIVILEGES;Query OK, 0 rows affected (0.00 sec)在MySQL 5.7及以上版本中,使用如下语句修改root账户的密码。
mysql> UPDATE mysql.user SET authentication_string =password('root') WHERE user='root' and host='localhost';Query OK, 1 row affected, 1 warning (0.10 sec)Rows matched: 1 Changed: 1 Warnings: 1mysql> FLUSH PRIVILEGES;Query OK, 0 rows affected (0.00 sec)(5)删除my.cnf文件中的skip-grant-tables=1配置项,或者将skip-grant-tables=1配置项修改为skip-grant-tables=0。
[[email protected] ~]# vim /data/mysql/conf/my.cnf[mysqld]skip-grant-tables=0保存并退出vim编辑器。
(6)重启MySQL服务,即可使用root账户与新修改的密码登录MySQL。
边栏推荐
猜你喜欢
![[Alibaba security × ICDM 2022] 200000 bonus pool! The risk commodity inspection competition on the large-scale e-commerce map is in hot registration](/img/38/9fadea0d37053a3ebb73806a9963f1.jpg)
[Alibaba security × ICDM 2022] 200000 bonus pool! The risk commodity inspection competition on the large-scale e-commerce map is in hot registration

Preprocessing and macro definition

海康设备接入EasyCVR,出现告警信息缺失且不同步该如何解决?

Redis 事物学习

图解LeetCode——592. 分数加减运算(难度:中等)

EasyCVR平台关闭录像为何还会有TS切片文件生成?

IM即时通讯开发如何提升移动网络下图片传输速度和成功率

Oracle Xe installation and user operation

How to optimize the open source community experience through developer metrics

Understand the wonderful use of dowanward API, and easily grasp kubernetes environment variables
随机推荐
Adjust the array so that odd numbers all precede even numbers
CONDA common commands
Simple application of multipoint bidirectional republication and routing strategy
Following Huawei and MediaTek, the mobile phone chip manufacturer announced a donation of 7million yuan to Wuhan
Clickhouse 实现 MaterializedPostgreSQL
Technology sharing | how to do Assertion Verification in interface automated testing?
What are the apps of regular futures trading platforms in 2022, and are they safe?
ES6 -- Application of expansion operator
IE11 下载doc pdf等文件的方法
greedy
slf4j中如何进行log4j配置呢?
Pyqt5 rapid development and practice 4.5 button controls and 4.6 qcombobox (drop-down list box)
Apple Mobile Bluetooth networking
In 2019, China's smart machine Market: Huawei won nearly 4 components, firmly ranking first in China
Two years after its release, the price increased by $100, and the reverse growth of meta Quest 2
Injection attack
Understand │ what is cross domain? How to solve cross domain problems?
API for obtaining the latest raw data of Taobao app product details
站在巨人肩膀上学习,京东爆款架构师成长手册首发
You can understand it at a glance, eslint