在 MySQL 中重命名 Root @localhost 用户名?

mysqlmysqli database

重命名 Root @localhost 的语法如下

UPDATE MySQL.user SET user = ‘yourNewRootName’ WHERE user = 'root';

为了理解上述概念,让我们检查所有用户名和主机。查询如下

mysql> select user,host from MySQL.user;

以下是输出 −

+------------------+-----------+
| user             | host      |
+------------------+-----------+
| Bob              | %         |
| Manish           | %         |
| User2            | %         | 
| mysql.infoschema | %         |
| mysql.session    | %         |
| mysql.sys        | %         |
| root             | %         |
| @UserName@       | localhost |
| Adam Smith       | localhost |
| John             | localhost |
| John Doe         | localhost |
| User1            | localhost |
| am               | localhost |
| hbstudent        | localhost |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
+------------------+-----------+
16 rows in set (0.00 sec)

以下查询将用户 ‘root’ 重命名为其他名称

mysql> UPDATE mysql.user set user = 'MyRoot' where user = 'root';
Query OK, 1 row affected (0.13 sec)
Rows matched: 1 Changed: 1 Warnings: 0

让我们检查用户 ‘root’ 是否已更新为新名称 ‘MyRoot’。查询如下

mysql> select user,host from MySQL.user;

以下是输出 −

+------------------+-----------+
| user             | host      |
+------------------+-----------+
| Bob              | %         |
| Manish           | %         |
| MyRoot           | %         |   
| User2            | %         |
| mysql.infoschema | %         |
| mysql.session    | %         |
| mysql.sys        | %         |
| @UserName@       | localhost |
| Adam Smith       | localhost |
| John             | localhost |
| John Doe         | localhost |
| User1            | localhost |
| am               | localhost |
| hbstudent        | localhost |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
+------------------+-----------+
16 rows in set (0.00 sec)

查看示例输出,根已成功更新为新名称"MyRoot"。


相关文章