在 MySQL 中使用 RENAME USER 语句重命名用户
本文介绍了在 MySQL 中如何使用 RENAME USER 语句在数据库服务器中重命名一个或多个用户。
某些特定的场景下,您可能想要重命名已有的用户,比如:
- 此用户账户已经被泄露
- 将用户名改为一个更有意义的用户名
要从 MySQL 服务器中重命名一个或者多个已有的用户账户,请使用 RENAME USER 语句。
MySQL RENAME USER 语句语法
RENAME USER 语句重命名一个或多个现有帐户。以下是 RENAME USER 语句的基本语法:
RENAME USER
    user_account TO new_user_account
    [, user_account2 TO new_user_account2]
    [, ...];
在这个语法中:
- 在 TO关键字之前指定要重命名的现有用户账户。
- 在 TO关键字之后指定新用户账户。
您不能使用一个已有的用户账户作为新的用户账户,否则您将收到错误消息。
RENAME USER 将旧用户的所有权限转移给新用户。但是,它不会删除或使依赖于旧用户的数据库对象无效。
例如,假设您有一个存储过程,其 DEFINER 属性指定了旧用户。这个存储过程在定义者安全上下文中执行。如果重命名旧用户,则执行存储过程时会出现错误。
MySQL RENAME USER 实例
让我们举一些使用 MySQL RENAME USER 语句的例子。
请按照以下步骤运行 MySQL RENAME USER 实例。
- 
使用 mysql 客户端工具连接到 MySQL 服务器: mysql -u root -p输入 root帐户的密码并按Enter:Enter password: ********
- 
显示当前 MySQL 服务器的所有用户: SELECT user, host FROM mysql.user;这是当前的用户列表: +------------------+-----------+ | user | host | +------------------+-----------+ | root | % | | sqliz | % | | test_role1 | % | | test_role2 | % | | testuser | % | | mysql.infoschema | localhost | | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +------------------+-----------+
- 
创建 3 个新用户: test_user1,test_user2和test_user3,CREATE user 'test_user1'@'%' IDENTIFIED by 'SqLiZ9879123!'; CREATE user 'test_user2'@'%' IDENTIFIED by 'SqLiZ9879123!'; CREATE user 'test_user3'@'%' IDENTIFIED by 'SqLiZ9879123!';
- 
显示当前 MySQL 服务器的所有用户: SELECT user, host FROM mysql.user;这是当前的用户列表: +------------------+-----------+ | user | host | +------------------+-----------+ | root | % | | sqliz | % | | test_role1 | % | | test_role2 | % | | test_user1 | % | | test_user2 | % | | test_user3 | % | | testuser | % | | mysql.infoschema | localhost | | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +------------------+-----------+
- 
使用 RENAME USER将用户'test_user1'@'%'重命名为'test_user1_new'@'%':RENAME USER 'test_user1'@'%' TO 'test_user1_new'@'%';
- 
显示当前 MySQL 服务器的所有用户: SELECT user, host FROM mysql.user;这是当前的用户列表: +------------------+-----------+ | user | host | +------------------+-----------+ | root | % | | sqliz | % | | test_role1 | % | | test_role2 | % | | test_user1_new | % | | test_user2 | % | | test_user3 | % | | testuser | % | | mysql.infoschema | localhost | | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +------------------+-----------+这里,用户 'test_user1'@'%'已经被重命名为了'test_user1_new'@'%'。
- 
使用 RENAME USER将用户'test_user2'@'%'重命名为'test_user2_new'@'%'并且将用户'test_user3'@'%'重命名为'test_user3_new'@'%':RENAME USER 'test_user2'@'%' TO 'test_user2_new'@'%'; RENAME USER 'test_user3'@'%' TO 'test_user3_new'@'%';
- 
显示当前 MySQL 服务器的所有用户: SELECT user, host FROM mysql.user;这是当前的用户列表: +------------------+-----------+ | user | host | +------------------+-----------+ | root | % | | sqliz | % | | test_role1 | % | | test_role2 | % | | test_user1_new | % | | test_user2_new | % | | test_user3_new | % | | testuser | % | | mysql.infoschema | localhost | | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +------------------+-----------+这里,用户 'test_user2'@'%'已经被重命名为'test_user2_new'@'%'并且用户'test_user3'@'%'已经被重命名为'test_user3_new'@'%'。
结论
在本文中,您学习了如何使用 MySQL RENAME USER 语句从 MySQL 数据库服务器中重命名一个或多个用户账户。
您可以在一个语句中重命名一个用户账户,也可以在一个语句中重命名多个用户账户。