网站地图    收藏    合作   
<

快捷菜单 返回顶部

在上一节我们学习了在 MySQL 数据库中创建用户,接下来我们将介绍如何修改 MySQL 数据库中用户的账号和口令。

修改用户账号

可以使用 RENAME USER 语句修改一个或多个已经存在的 MySQL 用户账号。

语法格式:

RENAME USER <旧用户> TO <新用户>

语法说明如下:
使用 RENAME USER 语句时应该注意以下几点:
RENAME USER 语句用于对原有的 MySQL 账户进行重命名。
若系统中旧账户不存在或者新账户已存在,则该语句执行时会出现错误。
要使用 RENAME USER 语句,必须拥有 MySQL 中的 MySQL 数据库的 UPDATE 权限或全局 CREATE USER 权限。

【实例 1】使用 RENAME USER 语句将用户名 james 修改为 jack,主机是 localhost。输入的 SQL 语句和执行过程如下所示。
1
2
3
mysql> RENAME USER james@'localhost'
    -> TO jack@'localhost';
Query OK, 0 rows affected (0.03 sec)
在 Windows 命令行工具中,使用 jack 和密码 tiger 登录数据库服务器,如下所示。
1
2
3
4
5
6
7
8
9
10
C:\Users\USER>mysql -h localhost -u jack -p
Enter password: *****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.20-log MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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.

修改用户口令

可以使用 SET PASSWORD 语句修改一个用户的登录口令。

语法格式:
1
2
3
4
5
6
SET PASSWORD [ FOR <用户名> ] =
{
    PASSWORD('新明文口令')
    | OLD_PASSWORD('旧明文口令')
    | '加密口令值'
}
语法说明如下。

注意:PASSWORD() 函数为单向加密函数,一旦加密后不能解密出原明文。

使用 SET PASSWORD 语句应注意以下几点:
【实例 2】使用 SET 语句将用户名为 jack 的密码修改为 lion,主机是 localhost。输入的 SQL 语句和执行过程如下所示。
1
2
3
mysql> SET PASSWORD FOR 'jack'@'localhost'=
    -> PASSWORD('lion');
Query OK, 0 rows affected, 1 warning (0.03 sec)
在 Windows 命令行工具中,使用 jack 和密码 lion 登录数据库服务器,如下所示。
1
2
3
4
5
6
7
8
9
10
C:\Users\USER>mysql -h localhost -u jack -p
Enter password: *****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.20-log MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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.

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com