How to a Create Database and User with Permissions in MySQL Server
March 6, 2023 2023-07-02 3:53How to a Create Database and User with Permissions in MySQL Server
We going to discuss about creating MySQL database and users with different permissions to manage our database. In MySQL, there are several types of permissions that can be granted to users to control their access to databases, tables, and other objects within the database. Some of the most commonly used permission types include below
which we are going to use the permission in this post.
- SELECT – allows the user to read data from a table or view
- INSERT – allows the user to insert new data into a table
- UPDATE – allows the user to modify existing data in a table
- DELETE – allows the user to delete data from a table
- CREATE – allows the user to create new databases, tables, or other objects
- DROP – allows the user to delete databases, tables, or other objects
- ALTER – allows the user to modify the structure of a table
- GRANT – allows the user to grant permissions to other users or roles
Login to your MySQL root user and do the steps one by one. Assume that, your going to create database named linuxdb_name and user named linuxuser_name.
local@host$ mysql
Create a new Database:
> CREATE DATABASE linuxdb_name;
Query OK, 0 rows affected (0.01 sec)
Create a User account to access the database,
> CREATE USER linuxuser_name;
Query OK, 0 rows affected (0.0 sec)
Another option is create MySQL user with password then use this query.
> CREATE USER 'linuxuser_name'@'localhost' IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.02 sec)
Permission Settings:
We need to map this user to our database with appropriate permissions.
> GRANT ALL PRIVILEGES ON *.* TO 'linuxuser_name@'localhost' WITH GRANT OPTION;
Give permission with password,
> GRANT ALL PRIVILEGES ON *.* TO 'linuxuser_name'@'localhost' IDENTIFIED BY 'password';
Above query will give access to all the database.
Set user permission to access the particular database using the command below,
> GRANT ALL PRIVILEGES ON linuxdb.* TO 'linuxuser'@'localhost' WITH GRANT OPTION;
## (or)
> GRANT ALL PRIVILEGES ON linuxdb.* TO 'linuxuser'@'localhost' IDENTIFIED BY 'password';
Query given below is an example of setting permissions with few options to “linuxuser”.