当前位置:网站首页>用户和权限修改用户密码
用户和权限修改用户密码
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。
边栏推荐
- Preprocessing and macro definition
- In 2019, the global semiconductor market revenue was $418.3 billion, a year-on-year decrease of 11.9%
- 一个程序员的水平能差到什么程度?
- Linked list~~~
- Apple Mobile Bluetooth networking
- DP (dynamic programming)
- You can understand it at a glance, eslint
- set--数据解构
- Under the epidemic, I left my job for a year, and my income increased 10 times
- Slf4j introduction
猜你喜欢

You can understand it at a glance, eslint

Oracle +JDBC

antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key
![[benefit activity] stack a buff for your code! Click](/img/2d/dabf0ad5d7bd553dada5921abf6c06.png)
[benefit activity] stack a buff for your code! Click "tea" to receive the gift

发布2年后涨价100美元,Meta Quest 2的逆生长

I'm also drunk. Eureka delayed registration and this pit

Flask-MDict搭建在线Mdict词典服务

Interviewer: what is the abstract factory model?

Swiftui view onReceive method receives "redundant" event resolution

Scrollintoview realizes simple anchor location (example: select city list)
随机推荐
MySQL 日志查询日志
最新获得淘宝app商品详情原数据 的API
Under the epidemic, I left my job for a year, and my income increased 10 times
Is it safe for CICC fortune to open an account? What is the use of opening an account
Two years after its release, the price increased by $100, and the reverse growth of meta Quest 2
十年测试老鸟聊聊移动端兼容性测试
C语言--数组
js跳转页面并刷新(本页面跳转)
软件测试面试题:已知一个队列,如: [1, 3, 5, 7], 如何把第一个数字,放到第三个位置,得到:[3, 5, 1, 7]
Common ways to keep requests idempotent
set--数据解构
Understand the wonderful use of dowanward API, and easily grasp kubernetes environment variables
如何监控NVIDIA Jetson的的运行状态和使用情况
数仓搭建——DWD层
Some contents related to cmsis-rtos
Understand │ what is cross domain? How to solve cross domain problems?
Koin simple to use
Why do we need third-party payment?
软件测试面试题:统计在一个队列中的数字,有多少个正数,多少个负数,如[1, 3, 5, 7, 0, -1, -9, -4, -5, 8]
Summary of simple topics