MySQL - 重新命名使用者語句



MySQL 重新命名使用者語句

您可以使用 RENAME USER 語句更改 MySQL 中現有使用者帳戶的名稱。要建立使用者帳戶,當前帳戶需要具有 CREATE USER 許可權,或者對 MySQL 系統架構具有 UPDATE 許可權。

語法

以下是 MySQL RENAME USER 語句的語法:

RENAME USER old_name TO new_name

其中,`old_name` 是您需要更改的名稱,`new_name` 是所需的名稱。重新命名使用者時,您需要確保給定的 `old_name` 存在,並且沒有使用者使用所需的名稱。

示例

假設我們建立了一個名為 **sample** 的使用者,密碼為 **123456**。首先,請確保您已使用具有管理員許可權(root)的使用者登入。

CREATE USER 'sample'@'localhost' IDENTIFIED BY '123456';

您可以使用以下查詢驗證使用者列表:

select user from MySQl.user;

輸出

以下是上述查詢的輸出:

user
mysql.infoschema
mysql.session
mysql.sys
myuser
openkm
root
sample

以下查詢更改上面建立的使用者名稱稱。

RENAME USER 'sample'@'localhost' TO 'newUser'@'localhost';

驗證

如果您再次驗證資料庫中的使用者列表,您可以觀察到舊使用者的名稱已更改:

select user from MySQl.user;

輸出

上述查詢將產生以下輸出:

user
mysql.infoschema
mysql.session
mysql.sys
myuser
openkm
root
newUser

使用新使用者名稱登入

開啟命令提示符,瀏覽到 MySQL 安裝資料夾的 bin 資料夾,並執行命令 `mysql -u new_user_name -p`,例如:

MySQL_Installation_Directorybin>mysql -u sample -p
Enter password: ******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.22 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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.
廣告