{"id":17010,"date":"2023-05-15T02:14:21","date_gmt":"2023-05-15T00:14:21","guid":{"rendered":"https:\/\/contabo.com\/blog\/?p=17010"},"modified":"2023-08-11T05:20:07","modified_gmt":"2023-08-11T03:20:07","slug":"how-to-create-a-user-and-grant-permissions-in-mysql","status":"publish","type":"post","link":"https:\/\/contabo.com\/blog\/how-to-create-a-user-and-grant-permissions-in-mysql\/","title":{"rendered":"How To Create a New User and Grant Permissions in MySQL"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/blog-head_how2-create-a-new-user-and-grant-permissions-in-MySQL.jpg\" alt=\"How To Create a New User and Grant Permissions in MySQL (head image)\" class=\"wp-image-17011\" srcset=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/blog-head_how2-create-a-new-user-and-grant-permissions-in-MySQL.jpg 1200w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/blog-head_how2-create-a-new-user-and-grant-permissions-in-MySQL-600x315.jpg 600w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/blog-head_how2-create-a-new-user-and-grant-permissions-in-MySQL-768x403.jpg 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/figure>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>MySQL, one of the most popular relational databases. This allows for the creation of multiple users with varying levels of access and permissions. Managing user accounts and permissions is crucial for maintaining database security and ensuring that authorized users have the appropriate level of access to perform their tasks. If you are new to MySQL or want to learn how to create a new user and grant permissions, you&#8217;ve come to the right place. In this article, we will provide you with a step-by-step guide on how to create a new user in MySQL. Moreover we will cover how to set up their permissions, and manage their access to the database. Whether you are a developer, database administrator, or simply interested in learning MySQL, this article will walk you through the process of creating and managing users in MySQL for improved database security and access control.<\/p>\n\n\n\n<p>If you want to learn how to back up a MySQL database to an Object Storage (e.g. <a href=\"https:\/\/contabo.com\/en\/object-storage\/\" target=\"_blank\" rel=\"noreferrer noopener\">Contabos S3-compatible Object Storage<\/a>) check out <a href=\"https:\/\/contabo.com\/blog\/how-to-back-up-mysql-to-object-storage\/\" target=\"_blank\" rel=\"noreferrer noopener\">this article<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-prerequisites-requirements-nbsp\">Prerequisites\/Requirements&nbsp;<\/h2>\n\n\n\n<p>To make use of this tutorial, you will require access to a MySQL database. The tutorial assumes that the database is installed on a virtual private server (VPS) 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.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-creating-a-mysql-user-nbsp\">Creating a MySQL User&nbsp;<\/h2>\n\n\n\n<p>After MySQL installation, a root user account is automatically created, granting full privileges over the MySQL server, including control over all databases, tables, and users. However, it is recommended to restrict the use of the root account to administrative tasks only. This section will detail how to utilize the root MySQL user to create a new user account and assign appropriate privileges.&nbsp;<\/p>\n\n\n\n<p>Use the following command to login to your MySQL Server as root:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql \u2013u root \u2013p&nbsp;<\/code><\/pre>\n\n\n\n<p>You will be prompted to enter your root-password.<\/p>\n\n\n\n<p>Now that you have access to the MySQL prompt, you can create a new user with the \u201cCREATE USER\u201d-command. It follows the following general syntax:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE USER 'username'@'host' IDENTIFIED BY 'password';&nbsp;<\/code><\/pre>\n\n\n\n<p>Following the &#8220;CREATE USER&#8221; command, you need to provide a username, followed by the &#8220;@&#8221; sign and the hostname from which the user will connect. If you only intend to allow local access from your local server, you can specify &#8220;localhost&#8221; as the hostname. While enclosing the username and host in single quotes may not always be mandatory, it can be beneficial in preventing potential errors.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-granting-permissions-to-a-user-nbsp\">Granting Permissions to a User&nbsp;<\/h2>\n\n\n\n<p>The purpose of granting privileges to a user in MySQL is to define the actions and operations that the user is allowed to perform on a database or specific database objects, such as tables, views, procedures, and functions. Privileges determine the level of access and control that a user has over the database and its objects.&nbsp;<\/p>\n\n\n\n<p>By granting privileges, you can control what actions a user can perform. This includes things like creating, modifying, or deleting data, as well as managing database structures and configurations. Privileges allow you to set fine-grained access control. This  ensures that users have the necessary permissions to perform their intended tasks while restricting them from unauthorized actions. With that you eliminate the risk that a user could potentially compromise the security or integrity of the database.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-general-syntax\">General Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>GRANT PRIVILEGE ON database.table TO 'username'@'host';&nbsp;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-granting-privileges\">Granting Privileges<\/h3>\n\n\n\n<p>The PRIVILEGE value in the provided syntax determines the actions that a user is authorized to perform on the specified database and table. Multiple privileges can be granted to the same user in a single command, separated by commas. Additionally, global privileges can be granted by using asterisks (*) in place of specific database and table names, as asterisks represent &#8220;all&#8221; databases or tables in SQL.&nbsp;<\/p>\n\n\n\n<p>For example, the following command grants a user the following global privileges, actions on databases, tables, and users:<\/p>\n\n\n\n<p>CREATE<\/p>\n\n\n\n<p>ALTER<\/p>\n\n\n\n<p>DROP<\/p>\n\n\n\n<p>It also allows the user to perform INSERT, UPDATE, and DELETE operations on any table, query data with SELECT, create foreign keys with REFERENCES, and perform FLUSH operations with RELOAD privilege.&nbsp;&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GRANT CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, SELECT, REFERENCES, RELOAD on *.* TO 'username'@'localhost' WITH GRANT OPTION;&nbsp;<\/code><\/pre>\n\n\n\n<p>However, it is important to only grant users the permissions they actually require, and adjust user privileges accordingly.&nbsp;<\/p>\n\n\n\n<p>The complete list of available privileges can be found in the <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#privileges-provided-summary\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">official MySQL documentation<\/a>.&nbsp;<\/p>\n\n\n\n<p>Some users require all permissions (e.g. if you are planning to create a user to install WordPress or Nextcloud). To do this, replace the names of the individual permissions with the word \u201call\u201d. The command will then look like this:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost';&nbsp;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-refresh-the-database\">Refresh the Database<\/h3>\n\n\n\n<p>To refresh the user\u2019s permissions, use this command:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FLUSH PRIVILEGES;&nbsp;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-revoke-permissions-from-a-user-nbsp\">Revoke Permissions from a User&nbsp;<\/h2>\n\n\n\n<p>If you need to revoke a permission, the structure is almost identical to granting it:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>REVOKE type_of_permission ON database_name.table_name FROM 'username'@'host';&nbsp;<\/code><\/pre>\n\n\n\n<p>Note that when revoking permissions, the syntax requires that you use FROM, instead of TO. TO would be used when granting the permissions.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-other-useful-mysql-commands-nbsp\">Other useful MySQL Commands&nbsp;<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-display-permissions-of-a-user\">Display permissions of a user<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SHOW GRANTS FOR 'username'@'host';&nbsp;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-delete-a-user\">Delete a user<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>DROP USER 'username'@'localhost';&nbsp;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-exit-mysql\">Exit MySQL<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>exit;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Looking to create a new user and grant permissions in MySQL? Our beginner-friendly step-by-step guide has got you covered!<\/p>\n","protected":false},"author":50,"featured_media":17011,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[18],"tags":[],"ppma_author":[1491],"class_list":["post-17010","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"uagb_featured_image_src":{"full":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/blog-head_how2-create-a-new-user-and-grant-permissions-in-MySQL.jpg",1200,630,false],"thumbnail":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/blog-head_how2-create-a-new-user-and-grant-permissions-in-MySQL-150x150.jpg",150,150,true],"medium":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/blog-head_how2-create-a-new-user-and-grant-permissions-in-MySQL-600x315.jpg",600,315,true],"medium_large":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/blog-head_how2-create-a-new-user-and-grant-permissions-in-MySQL-768x403.jpg",768,403,true],"large":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/blog-head_how2-create-a-new-user-and-grant-permissions-in-MySQL.jpg",1200,630,false],"1536x1536":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/blog-head_how2-create-a-new-user-and-grant-permissions-in-MySQL.jpg",1200,630,false],"2048x2048":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2023\/05\/blog-head_how2-create-a-new-user-and-grant-permissions-in-MySQL.jpg",1200,630,false]},"uagb_author_info":{"display_name":"Tobias Mildenberger","author_link":"https:\/\/contabo.com\/blog\/author\/tobias\/"},"uagb_comment_info":0,"uagb_excerpt":"Looking to create a new user and grant permissions in MySQL? Our beginner-friendly step-by-step guide has got you covered!","authors":[{"term_id":1491,"user_id":50,"is_guest":0,"slug":"tobias","display_name":"Tobias Mildenberger","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/077178d5dce6c3d4c0c0396857a7e544bfdf8adf04145fff5160b33a22e28b1f?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/17010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/users\/50"}],"replies":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/comments?post=17010"}],"version-history":[{"count":4,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/17010\/revisions"}],"predecessor-version":[{"id":17268,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/17010\/revisions\/17268"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/media\/17011"}],"wp:attachment":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/media?parent=17010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/categories?post=17010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/tags?post=17010"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=17010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}