{"id":3584,"date":"2023-11-17T17:35:50","date_gmt":"2023-11-17T09:35:50","guid":{"rendered":"https:\/\/www.servergigabit.com\/guide\/?post_type=kb&#038;p=3584"},"modified":"2026-03-17T11:12:49","modified_gmt":"2026-03-17T03:12:49","slug":"mysql-user-management","status":"publish","type":"kb","link":"https:\/\/www.servergigabit.com\/guide\/kb\/mysql-user-management","title":{"rendered":"The Ultimate Guide to MySQL: Creating and Managing User Accounts Safely"},"content":{"rendered":"<header class=\"entry-header \">\n<div class=\"entry-meta\"><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone wp-image-3585\" src=\"https:\/\/www.servergigabit.com\/guide\/wp-content\/uploads\/2023\/11\/What_is_SQL_Database-300x169.png\" alt=\"MySQL\" width=\"733\" height=\"413\" srcset=\"https:\/\/www.servergigabit.com\/guide\/wp-content\/uploads\/2023\/11\/What_is_SQL_Database-300x169.png 300w, https:\/\/www.servergigabit.com\/guide\/wp-content\/uploads\/2023\/11\/What_is_SQL_Database-1024x576.png 1024w, https:\/\/www.servergigabit.com\/guide\/wp-content\/uploads\/2023\/11\/What_is_SQL_Database-768x432.png 768w, https:\/\/www.servergigabit.com\/guide\/wp-content\/uploads\/2023\/11\/What_is_SQL_Database-1536x864.png 1536w, https:\/\/www.servergigabit.com\/guide\/wp-content\/uploads\/2023\/11\/What_is_SQL_Database-370x208.png 370w, https:\/\/www.servergigabit.com\/guide\/wp-content\/uploads\/2023\/11\/What_is_SQL_Database-1230x692.png 1230w, https:\/\/www.servergigabit.com\/guide\/wp-content\/uploads\/2023\/11\/What_is_SQL_Database-800x450.png 800w, https:\/\/www.servergigabit.com\/guide\/wp-content\/uploads\/2023\/11\/What_is_SQL_Database-390x219.png 390w, https:\/\/www.servergigabit.com\/guide\/wp-content\/uploads\/2023\/11\/What_is_SQL_Database.png 1680w\" sizes=\"(max-width: 733px) 100vw, 733px\" \/><\/div>\n<\/header>\n<div class=\"entry-content clear\">\n<div class=\"wp-block-spacer\" aria-hidden=\"true\"><\/div>\n<p><a href=\"https:\/\/www.mysql.com\/\" rel=\"nofollow noopener\" target=\"_blank\">MySQL<\/a> is a popular relational database management system (RDBMS) that allows users to create, read, update, and delete (CRUD) data. Managing user accounts and permissions is crucial for maintaining database security and ensuring that users have the appropriate level of access to perform their tasks. This guide provides a comprehensive overview of creating and managing user accounts in MySQL, including granting and revoking permissions.<\/p>\n<h2 id=\"h-prerequisites-requirements-nbsp\" class=\"wp-block-heading\"><span id=\"prerequisitesrequirements\" class=\"uag-toc__heading-anchor\"><\/span><strong>Prerequisites\/Requirements\u00a0<\/strong><\/h2>\n<p data-sourcepos=\"9:1-9:149\">To follow this guide, you will need access to a MySQL database. The tutorial assumes that the database is installed on a <a href=\"https:\/\/www.servergigabit.com\/\">virtual private server (VPS)<\/a> running Ubuntu or Debian. However, the process for creating a new MySQL user and assigning permissions is generally similar, regardless of the underlying operating system of your server.<\/p>\n<h2 id=\"h-creating-a-mysql-user-nbsp\" class=\"wp-block-heading\"><span id=\"creating-a-mysql-user\" class=\"uag-toc__heading-anchor\"><\/span><strong>Creating A MySQL User\u00a0<\/strong><\/h2>\n<ol data-sourcepos=\"13:1-14:0\">\n<li data-sourcepos=\"13:1-14:0\">Login to the MySQL server as root:<\/li>\n<\/ol>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\">mysql -u root -p<\/code><\/pre>\n<\/div>\n<ol start=\"2\" data-sourcepos=\"19:1-22:0\">\n<li data-sourcepos=\"19:1-20:0\">\n<p data-sourcepos=\"19:4-19:42\">Enter your root password when prompted.<\/p>\n<\/li>\n<li data-sourcepos=\"21:1-22:0\">\n<p data-sourcepos=\"21:4-21:53\">Create a new user using the <code class=\"\">CREATE USER<\/code> command:<\/p>\n<\/li>\n<\/ol>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">USER<\/span> <span class=\"hljs-string\">'username'<\/span>@<span class=\"hljs-string\">'host'<\/span> IDENTIFIED <span class=\"hljs-keyword\">BY<\/span> <span class=\"hljs-string\">'password'<\/span>;<\/code><\/pre>\n<\/div>\n<p data-sourcepos=\"27:1-27:95\">Replace <code class=\"\">username<\/code> with the desired username, <code class=\"\">host<\/code> with the hostname from which the user will connect, and <code class=\"\">password<\/code> with the desired password. Enclose the username and host in single quotes for consistency and to avoid potential errors.<\/p>\n<h2><strong><span style=\"color: #1d1d1d; font-family: Montserrat, sans-serif; font-size: 3.2143rem; letter-spacing: -0.3px;\">Granting Permissions to a User\u00a0<\/span><\/strong><\/h2>\n<ol data-sourcepos=\"31:1-32:0\">\n<li data-sourcepos=\"31:1-32:0\">To grant permissions to a user,\u00a0use the\u00a0<code class=\"\">GRANT<\/code>\u00a0command:<\/li>\n<\/ol>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\"><span class=\"hljs-keyword\">GRANT<\/span> PRIVILEGE <span class=\"hljs-keyword\">ON<\/span> database.table <span class=\"hljs-keyword\">TO<\/span> <span class=\"hljs-string\">'username'<\/span>@<span class=\"hljs-string\">'host'<\/span>;<\/code><\/pre>\n<\/div>\n<p data-sourcepos=\"37:1-37:325\">Replace <code class=\"\">PRIVILEGE<\/code> with the specific privilege you want to grant, <code class=\"\">database<\/code> with the name of the database the privilege applies to, <code class=\"\">table<\/code> with the name of the table the privilege applies to, <code class=\"\">username<\/code> with the username of the user to grant the privilege to, and <code class=\"\">host<\/code> with the hostname from which the user will connect.<\/p>\n<ol start=\"2\" data-sourcepos=\"39:1-40:0\">\n<li data-sourcepos=\"39:1-40:0\">For example,\u00a0to grant a user the\u00a0<code class=\"\">SELECT<\/code><span class=\"animating\">\u00a0privilege on all tables in the\u00a0<\/span><code class=\"\">mydatabase<\/code><span class=\"animating\">\u00a0database:<\/span><\/li>\n<\/ol>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\"><span class=\"hljs-keyword\">GRANT<\/span> <span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">ON<\/span> mydatabase.<span class=\"hljs-operator\">*<\/span> <span class=\"hljs-keyword\">TO<\/span> <span class=\"hljs-string\">'username'<\/span>@<span class=\"hljs-string\">'localhost'<\/span>;<\/code><\/pre>\n<\/div>\n<ol start=\"3\" data-sourcepos=\"45:1-46:0\">\n<li class=\"animating\" data-sourcepos=\"45:1-46:0\"><span class=\"animating\">To grant multiple privileges in a single command,<\/span><span class=\"animating\">\u00a0separate them with commas:<\/span><\/li>\n<\/ol>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\"><span class=\"hljs-keyword\">GRANT<\/span> <span class=\"hljs-keyword\">SELECT<\/span>, <span class=\"hljs-keyword\">INSERT<\/span>, UPDATE <span class=\"hljs-keyword\">ON<\/span> mydatabase.<span class=\"hljs-operator\">*<\/span> <span class=\"hljs-keyword\">TO<\/span> <span class=\"hljs-string\">'username'<\/span>@<span class=\"hljs-string\">'localhost'<\/span>;<\/code><\/pre>\n<\/div>\n<ol start=\"4\" data-sourcepos=\"51:1-51:103\">\n<li class=\"animating\" data-sourcepos=\"51:1-51:103\"><span class=\"animating\">To grant global privileges (permissions that apply to all databases and tables),<\/span><span class=\"animating\">\u00a0use asterisks (<\/span><code class=\"\">*<\/code><span class=\"animating\">) instead of specific database and table names:<\/span><\/li>\n<\/ol>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\"><span class=\"hljs-keyword\">GRANT<\/span> <span class=\"hljs-keyword\">ALL<\/span> PRIVILEGES <span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-operator\">*<\/span>.<span class=\"hljs-operator\">*<\/span> <span class=\"hljs-keyword\">TO<\/span> <span class=\"hljs-string\">'username'<\/span>@<span class=\"hljs-string\">'localhost'<\/span>;<\/code><\/pre>\n<\/div>\n<ol start=\"5\" data-sourcepos=\"57:1-58:0\">\n<li class=\"animating\" data-sourcepos=\"57:1-58:0\"><span class=\"animating\">After granting permissions,<\/span><span class=\"animating\">\u00a0refresh the database to ensure the changes are applied:<\/span><\/li>\n<\/ol>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\">FLUSH PRIVILEGES;<\/code><\/pre>\n<\/div>\n<h2 id=\"h-revoke-permissions-from-a-user-nbsp\" class=\"wp-block-heading\"><span id=\"revoke-permissions-from-a-user\" class=\"uag-toc__heading-anchor\"><\/span><strong>Revoke Permissions from a User<\/strong><\/h2>\n<p data-sourcepos=\"65:1-65:60\"><span class=\"animating\">To revoke permissions from a user,<\/span><span class=\"animating\"> use the <\/span><code class=\"animating\">REVOKE<\/code><span class=\"animating\"> command:<\/span><\/p>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\"><span class=\"hljs-keyword\">REVOKE<\/span> type_of_permission <span class=\"hljs-keyword\">ON<\/span> database_name.table_name <span class=\"hljs-keyword\">FROM<\/span> <span class=\"hljs-string\">'username'<\/span>@<span class=\"hljs-string\">'host'<\/span>;<\/code><\/pre>\n<\/div>\n<p data-sourcepos=\"71:1-71:351\"><span class=\"animating\">Replace <\/span><code class=\"animating\">type_of_permission<\/code><span class=\"animating\"> with the specific permission you want to revoke, <\/span><code class=\"animating\">database_name<\/code><span class=\"animating\"> with the name of the database the permission applies to, <\/span><code class=\"animating\">table_name<\/code><span class=\"animating\"> with the name of the table the permission applies to, <\/span><code class=\"animating\">username<\/code><span class=\"animating\"> with the username of the user to revoke the privilege from,<\/span><span class=\"animating\"> and <\/span><code class=\"animating\">host<\/code><span class=\"animating\"> with the hostname from which the user will connect.<\/span><\/p>\n<h2 id=\"h-other-useful-mysql-commands-nbsp\" class=\"wp-block-heading\"><span id=\"other-useful-mysql-commands\" class=\"uag-toc__heading-anchor\"><\/span><strong>Other Useful MySQL Commands\u00a0<\/strong><\/h2>\n<ol data-sourcepos=\"75:1-76:0\">\n<li class=\"animating\" data-sourcepos=\"75:1-76:0\"><span class=\"animating\">Display the permissions of a user:<\/span><\/li>\n<\/ol>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\"><span class=\"hljs-keyword\">SHOW<\/span> GRANTS <span class=\"hljs-keyword\">FOR<\/span> <span class=\"hljs-string\">'username'<\/span>@<span class=\"hljs-string\">'host'<\/span>;<\/code><\/pre>\n<\/div>\n<ol start=\"2\" data-sourcepos=\"81:1-82:0\">\n<li class=\"animating\" data-sourcepos=\"81:1-82:0\"><span class=\"animating\">Delete a user:<\/span><\/li>\n<\/ol>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\"><span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">USER<\/span> <span class=\"hljs-string\">'username'<\/span>@<span class=\"hljs-string\">'localhost'<\/span>;<\/code><\/pre>\n<\/div>\n<ol start=\"3\" data-sourcepos=\"87:1-88:0\">\n<li class=\"animating\" data-sourcepos=\"87:1-88:0\"><span class=\"animating\">Exit MySQL:<\/span><\/li>\n<\/ol>\n<div class=\"code-block ng-star-inserted\">\n<pre><code class=\"code-container\" role=\"text\">exit;<\/code><\/pre>\n<\/div>\n<h2 data-sourcepos=\"93:1-93:14\"><strong class=\"animating\">Conclusion<\/strong><\/h2>\n<p data-sourcepos=\"95:1-95:353\"><span class=\"animating\">This guide has provided a detailed explanation of how to create and manage user accounts in MySQL.<\/span><span class=\"animating\"> By following these steps,<\/span><span class=\"animating\"> you can effectively control user access and maintain the security of your database.<\/span><span class=\"animating\"> Remember to grant users the minimum permissions necessary for them to perform their tasks and revoke permissions when they are no longer needed.<\/span><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>MySQL is a popular relational database management system (RDBMS) that allows users to create, read, update, and delete (CRUD) data. Managing user accounts and permissions is crucial for maintaining database security and ensuring that users have the appropriate level of access to perform their tasks. This guide provides a comprehensive overview of creating and managing user accounts in MySQL, including&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"kbtopic":[1099],"kbtag":[1215],"class_list":["post-3584","kb","type-kb","status-publish","hentry","kbtopic-tutorials","kbtag-mysql"],"_links":{"self":[{"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/kb\/3584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/kb"}],"about":[{"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/types\/kb"}],"author":[{"embeddable":true,"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/comments?post=3584"}],"version-history":[{"count":8,"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/kb\/3584\/revisions"}],"predecessor-version":[{"id":6349,"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/kb\/3584\/revisions\/6349"}],"wp:attachment":[{"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/media?parent=3584"}],"wp:term":[{"taxonomy":"kbtopic","embeddable":true,"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/kbtopic?post=3584"},{"taxonomy":"kbtag","embeddable":true,"href":"https:\/\/www.servergigabit.com\/guide\/wp-json\/wp\/v2\/kbtag?post=3584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}