{"id":3549,"date":"2019-10-25T03:15:10","date_gmt":"2019-10-25T03:15:10","guid":{"rendered":"http:\/\/blog.hostinger.io\/hostinger-tutorials\/?p=3549"},"modified":"2025-12-02T15:34:43","modified_gmt":"2025-12-02T15:34:43","slug":"how-create-mysql-user-and-grant-permissions","status":"publish","type":"post","link":"\/ca\/tutorials\/how-create-mysql-user-and-grant-permissions","title":{"rendered":"MySQL add user: how to create and grant privileges"},"content":{"rendered":"<p>Do you want to understand the basics of MySQL operations? Read on, as this article will show you how to create a user in MySQL. You&rsquo;ll also learn about several commands to grant privileges, revoke privileges, and delete existing users.<\/p><figure class=\"wp-block-image size-large\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/How-to-Speed-Up-Your-Website-EN.pdf\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"283\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/11\/eBook-Speed-Up-your-website-1024x283.png\" alt=\"\" class=\"wp-image-69260\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><p>\n\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-what-is-a-mysql-database\">What Is a MySQL Database<\/h2><p>In order to understand MySQL, you&rsquo;ll need to know what a database is. It&rsquo;s a virtual storage where you can save necessary data for building websites and web applications.<\/p><p>MySQL database can store user account details, such as usernames, passwords, email addresses, and any type of information that you want to keep for later use.<\/p><p>However, the stored data should be in some kind of order. That&rsquo;s why we have database management systems. These tools are used to communicate with the database and allow developers to structure, store, dump, and modify the data.<\/p><h2 class=\"wp-block-heading\" id=\"h-prerequisites-for-creating-a-mysql-user\">Prerequisites for Creating a MySQL User<\/h2><p>Before we continue to the next section of the tutorial, make sure that you have installed MySQL. If you haven&rsquo;t, we have great tutorials on how to install it on both <a href=\"\/ca\/tutorials\/how-to-install-mysql-ubuntu\">Ubuntu<\/a> and <a href=\"\/ca\/tutorials\/how-to-install-mysql-on-centos-7\" target=\"_blank\" rel=\"noreferrer noopener\">CentOS<\/a>.<\/p><p><div class=\"protip\">\n                    <h4 class=\"title\">Pro Tip<\/h4>\n                    <p> We will use the command line to access our Linux VPS as root. You can either use <a href=\"\/ca\/tutorials\/how-to-use-putty-ssh\" target=\"_blank\" rel=\"noopener noreferrer\">PuTTY<\/a> (Windows) or your terminal (macOS, Linux) and log in using your SSH root login information provided by your hosting provider.<\/p>\n                <\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-create-a-mysql-user-account-and-grant-all-privileges\">How to Create a MySQL User Account and Grant All Privileges<\/h2><p>Just as you start using MySQL, you&rsquo;ll be given a username and a password. These initial credentials will grant you <strong>root<\/strong> access or full control of all your databases and tables.<\/p><p>However, there are times when you&rsquo;ll need to give the database access to someone else without granting them full control.<\/p><p>For instance, you hire developers to maintain your databases, but you don&rsquo;t want to provide them with the ability to delete or modify any sensitive information.<\/p><p>In that case, you should give them the credentials of a non-root user. This way, you can keep track of what the developers can and cannot do with your data.<\/p><p>In this part, we will explain how to create a user account in MySQL with all privileges to your database. In a practical sense, it&rsquo;s not wise to give full control to a non-root user. However, it&rsquo;s still a good entry-point to learn about user privileges.<\/p><p>To create a new user account in MySQL, follow these steps:<\/p><ol class=\"wp-block-list\">\n<li>Access command line and enter MySQL server:\n<pre>mysql<\/pre>\n<\/li>\n\n\n\n<li>The script will return this result, which verifies that you are accessing a MySQL server.\n<pre>mysql&gt;<\/pre>\n<\/li>\n\n\n\n<li>Then, execute the following command:\n<pre>CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'password';<\/pre>\n<\/li>\n<\/ol><ol start=\"4\" class=\"wp-block-list\">\n<li><strong>new_user<\/strong>&nbsp;is the name we&rsquo;ve given to our new user account and the <strong>IDENTIFIED BY &lsquo;password&rsquo;<\/strong> section sets a passcode for this user. You can replace these values with your own, inside the quotation marks.<\/li>\n\n\n\n<li>In order to grant all privileges of the database for a newly created user, execute the following command:\n<pre>GRANT ALL PRIVILEGES ON * . * TO 'new_user'@'localhost';<\/pre>\n<\/li>\n\n\n\n<li>For changes to take effect immediately flush these privileges by typing in the command:\n<pre>FLUSH PRIVILEGES;<\/pre>\n<\/li>\n<\/ol><p>Once that is done, your new user account has the same access to the database as the root user.<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-grant-privileges-separately-for-a-mysql-user\">How to Grant Privileges Separately for a MySQL User<\/h2><p>In this part, we will explain how to grant privileges separately for a user account in MySQL.<\/p><p>When specifying the database name and table name, separate them with a . (period) and no spaces. This will give the root user fine-grain control over certain data.<\/p><p>Also, replace the <strong>PERMISSION_TYPE<\/strong> value with the kind of access you want to grant to your new user account.<\/p><p>Here are the most used commands in MySQL:<\/p><ul class=\"wp-block-list\">\n<li><strong>CREATE<\/strong> &mdash; enable users to create a database or table<\/li>\n\n\n\n<li><strong>SELECT<\/strong> &mdash; permit users to retrieve data<\/li>\n\n\n\n<li><strong>INSERT<\/strong> &mdash; let users add new entries in tables<\/li>\n\n\n\n<li><strong>UPDATE<\/strong> &mdash; allow users to modify existing entries in tables<\/li>\n\n\n\n<li><strong>DELETE<\/strong> &mdash; enable users to erase table entries<\/li>\n\n\n\n<li><strong>DROP<\/strong> &mdash; let users delete entire database tables<\/li>\n<\/ul><p><div class=\"protip\">\n                    <h4 class=\"title\">Pro Tip<\/h4>\n                    <p> Using the <strong>ALL PRIVILEGES<\/strong> permission type from before will allow all of the permissions listed above.  <\/p>\n                <\/div>\n\n\n\n<\/p><p>To use any of these options, simply replace <strong>PERMISSION_TYPE <\/strong>with the appropriate keyword. To apply multiple privileges, separate them with a comma. For example, we can assign <strong>CREATE<\/strong> and <strong>SELECT<\/strong> to our non-root MySQL user account with this command:<\/p><pre class=\"wp-block-preformatted ql-syntax\">GRANT CREATE, SELECT ON * . * TO 'user_name'@'localhost';\n<\/pre><p>Sometimes, you might come across a situation where you need to revoke given privileges from a user. You can do so by entering:<\/p><pre class=\"wp-block-preformatted ql-syntax\">REVOKE PERMISSION_TYPE ON database_name.table_name FROM &lsquo;user_name&rsquo;@&lsquo;localhost&rsquo;;\n<\/pre><p>For example, to withdraw all privileges for our non-root user we should use:<\/p><pre class=\"wp-block-preformatted ql-syntax\">REVOKE ALL PRIVILEGES ON * . * FROM 'user_name'@'localhost';\n<\/pre><p>Finally, you can entirely delete an existing user account by using the following command:<\/p><pre class=\"wp-block-preformatted ql-syntax\">DROP USER &lsquo;user_name&rsquo;@&lsquo;localhost&rsquo;;\n<\/pre><p><div class=\"protip\">\n                    <h4 class=\"title\">Pro Tip<\/h4>\n                    <p> Remember, you need to have root access to run any of these commands. Be sure to execute <strong>FLUSH PRIVILEGES;<\/strong> command after making your changes <\/p>\n                <\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-display-account-privileges-for-a-mysql-user\">How to Display Account Privileges for a MySQL User<\/h2><p>In order to find what privileges have already been granted to a MySQL user, you can use the <strong>SHOW GRANTS<\/strong> command:<\/p><pre class=\"wp-block-preformatted\">SHOW GRANTS FOR 'user_name'@'localhost';<\/pre><p>The output will look similar to this:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">+---------------------------------------------------------------------------+\n| Grants for user_name@localhost                                             |\n+---------------------------------------------------------------------------+\n| GRANT USAGE ON *.* TO 'user_name'@'localhost'                              |\n| GRANT ALL PRIVILEGES ON 'database_name'.* TO 'user_name'@'localhost'.      |\n| REVOKE ALL PRIVILEGES ON * . * FROM 'user_name'@'localhost';  |<\/pre><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">+---------------------------------------------------------------------------+\n3 rows in set (0.00 sec)<\/pre><?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-full\"><a class=\"hgr-tutorials-cta hgr-tutorials-cta-web-hosting\" href=\"\/ca\/web-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/11\/Web-hosting_in-text-banner.png\" alt=\"Hostinger web hosting banner\" class=\"wp-image-98604\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>A database is an essential part of every website and web application because it stores all user data. To manage and communicate with it more efficiently, you will need a database management system. That&rsquo;s why we recommend using the most popular and recommended option &mdash; MySQL, due to its reliability and ease of use.<\/p><p>In this tutorial, you have learned about some basic commands to perform several tasks in MySQL, including:<\/p><ul class=\"wp-block-list\">\n<li>How to create MySQL user and grant it all privileges<\/li>\n\n\n\n<li>How to give specific privileges to a user, revoke them, and remove a MySQL user entirely<\/li>\n\n\n\n<li>How to view what privileges a MySQL user already has<\/li>\n<\/ul><p>Good luck, and feel free to leave a comment below if you have any questions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Do you want to understand the basics of MySQL operations? Read on, as this article will show you how to create a user in MySQL. You&rsquo;ll also learn about several commands to grant privileges, revoke privileges, and delete existing users. What Is a MySQL Database In order to understand MySQL, you&rsquo;ll need to know what [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/ca\/tutorials\/how-create-mysql-user-and-grant-permissions\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"How to Create MySQL User and Grant Privileges in %currentyear%","rank_math_description":"Check out this article to learn the basics of MySQL databases. We'll show you how to create a MySQL user and grant or revoke its privileges.","rank_math_focus_keyword":"mysql create user","footnotes":""},"categories":[22701,22699],"tags":[],"class_list":["post-3549","post","type-post","status-publish","format-standard","hentry","category-pre-installed-applications","category-vps"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/how-create-mysql-user-and-grant-permissions","default":0},{"locale":"fr-FR","link":"https:\/\/www.hostinger.com\/fr\/tutoriels\/creer-un-utilisateur-mysql","default":0},{"locale":"es-ES","link":"https:\/\/www.hostinger.com\/es\/tutoriales\/como-crear-usuario-mysql","default":0},{"locale":"id-ID","link":"https:\/\/www.hostinger.com\/id\/tutorial\/cara-membuat-hak-akses-user-di-mysql","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/how-create-mysql-user-and-grant-permissions","default":0},{"locale":"es-MX","link":"https:\/\/www.hostinger.com\/mx\/tutoriales\/como-crear-usuario-mysql","default":0},{"locale":"es-CO","link":"https:\/\/www.hostinger.com\/co\/tutoriales\/como-crear-usuario-mysql","default":0},{"locale":"es-AR","link":"https:\/\/www.hostinger.com\/ar\/tutoriales\/como-crear-usuario-mysql","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/how-create-mysql-user-and-grant-permissions","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/how-create-mysql-user-and-grant-permissions","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/how-create-mysql-user-and-grant-permissions","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/how-create-mysql-user-and-grant-permissions","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/how-create-mysql-user-and-grant-permissions","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/how-create-mysql-user-and-grant-permissions","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/posts\/3549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/comments?post=3549"}],"version-history":[{"count":36,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/posts\/3549\/revisions"}],"predecessor-version":[{"id":138868,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/posts\/3549\/revisions\/138868"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/media?parent=3549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/categories?post=3549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/tags?post=3549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}