Example of the RENAME USER command in MySQL

Naming your users and specifying their host does not have to be permanent. However, to implement any changes to those two fields, it is better to use the RENAME USER command as opposed to directly updating MySQL system tables. Read on to learn about this command with me…

Photo by Randy Fath on Unsplash

Note: All data, names or naming found within the database presented in this post, are strictly used for practice, learning, instruction, and testing purposes. It by no means depicts actual data belonging to or being used by any party or organization.

OS and DB used:


  • Xubuntu Linux 16.04.5 LTS (Xenial Xerus)
  • MySQL 8.0.14

Note: All examples in this blog post are performed on a single-user development/learning workstation environment. With that being said, as always, be sure and implement those best practices for you and your particular needs, goals, and outcomes.

I create this simple user:

1
2
mysql> CREATE USER 'user_a' IDENTIFIED BY 'password_a';
Query OK, 0 rows affected (0.08 sec)

Notice, I did not specify a connection host for this user. Here is what is stored in the mysql.user system table:

1
2
3
4
5
6
7
8
9
mysql> SELECT User, Host
    -> FROM mysql.user
    -> WHERE User = 'user_a';
+--------+------+
| User   | Host |
+--------+------+
| user_a | %    |
+--------+------+
1 row in set (0.00 sec)

By default, the % character is used if a host is not specified.

Suppose I need to change this for any number of reasons. One being, the % character is often used as a wild card character. Second, and perhaps intertwined, logging in on the command-line with that character as the host, is difficult at best:

1
2
3
4
5
6
7
:~$ mysql --host=% -u user_a -p
Enter password:
ERROR 2005 (HY000): Unknown MySQL server host '%' (0)

:~$ mysql --host='%' -u user_a -p
Enter password:
ERROR 2005 (HY000): Unknown MySQL server host '%' (0)

Although user ‘user_a’ can authenticate without specifying a host, imagine I want to go ahead and change that value to ‘localhost’ for whatever reason. Can I just directly UPDATE the mysql.user table?

Well, you could (barring you have the correct privileges). But, from what I gather, that is not advisable for a number of reasons.

However, the RENAME USER command will accomplish this with minimal fuss:

1
2
mysql> RENAME USER 'user_a'@'%' TO 'user_a'@'localhost';
Query OK, 0 rows affected (0.09 sec)

And view the changes:

1
2
3
4
5
6
7
8
9
mysql> SELECT User, Host
    -> FROM mysql.user
    -> WHERE User = 'user_a';
+--------+-----------+
| User   | Host      |
+--------+-----------+
| user_a | localhost |
+--------+-----------+
1 row in set (0.00 sec)

Better yet, let’s change the name of ‘user_a’ as well using RENAME USER:

1
2
mysql> RENAME USER 'user_a'@'localhost' TO 'user_ab'@'localhost';
Query OK, 0 rows affected (0.10 sec)

That change is reflected in the mysql.user table as well:

1
2
3
4
5
6
7
8
9
mysql> SELECT User, Host
    -> FROM mysql.user
    -> WHERE User = 'user_ab';
+---------+-----------+
| User    | Host      |
+---------+-----------+
| user_ab | localhost |
+---------+-----------+
1 row in set (0.00 sec)

One key reason to use RENAME USER, as covered in the linking documentation below, is that privileges carryover from old to new user.

Additional reading:

Like what you have read? See anything incorrect? Please comment below and thanks for reading!!!

Explore the official MySQL 8.0 Online Manual for more information.

A Call To Action!

Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.

Visit the Portfolio-Projects page to see blog post/technical writing I have completed for clients.

Have I mentioned how much I love a cup of coffee?!?!

To receive email notifications (Never Spam) from this blog (“Digital Owl’s Prose”) for the latest blog posts as they are published, please subscribe (of your own volition) by clicking the ‘Click To Subscribe!’ button in the sidebar on the homepage! (Feel free at any time to review the Digital Owl’s Prose Privacy Policy Page for any questions you may have about: email updates, opt-in, opt-out, contact forms, etc…)

Be sure and visit the “Best Of” page for a collection of my best blog posts.


Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.

Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, is performed on a personal development/learning workstation-environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.

Advertisements

Hey thanks for commenting! Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.