Langsung ke konten utama

Create MySQL Users Accounts and Grant Privileges

https://linuxize.com/post/how-to-create-mysql-user-accounts-and-grant-privileges/

Before you Begin

We are assuming that you already have MySQL or MariaDB server installed on your system.

All commands are executed inside the MySQL shell as root or administrative user. The minimum privileges required to create user accounts and define their privileges is CREATE USER and GRANT.

To access the MySQL shell type the following command and enter your MySQL root user password when prompted:

mysql -u root -p

If you have MySQL version 5.7 or later that uses the auth_socket plugin login as root by typing:

sudo mysql

Create a new MySQL User Account

A user account in MySQL consists of two parts: user name and host name.

To create a new MySQL user account, run the following command:

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'user_password';

 

Replace newuser with the new user name, and user_password with the user password.

In the example above, the hostname part is set to localhost, which means that the user will be able to connect to the MySQL server only from the localhost (i.e. from the system where MySQL Server runs).

To grant access from another host, change the hostname part with the remote machine IP. For example, to grant access from a machine with IP 10.8.0.5 you would run:

CREATE USER 'newuser'@'10.8.0.5' IDENTIFIED BY 'user_password';

To create a user that can connect from any host, use the '%' wildcard as a host part:

CREATE USER 'newuser'@'%' IDENTIFIED BY 'user_password';

Grant Privileges to a MySQL User Account

There are multiple types of privileges that can be granted to a user account. You can find a full list of privileges supported by MySQL here .

The most commonly used privileges are:

· ALL PRIVILEGES – Grants all privileges to a user account.

· CREATE – The user account is allowed to create databases and tables.

· DROP - The user account is allowed to drop databases and tables.

· DELETE - The user account is allowed to delete rows from a specific table.

· INSERT - The user account is allowed to insert rows into a specific table.

· SELECT – The user account is allowed to read a database.

· UPDATE - The user account is allowed to update table rows.

To grant specific privileges to a user account, use the following syntax:

GRANT permission1, permission2 ON database_name.table_name TO 'database_user'@'localhost';

Here are some examples:

Grand all privileges to a user account over a specific database:

GRANT ALL PRIVILEGES ON database_name.* TO 'database_user'@'localhost';

Grand all privileges to a user account on all databases:

GRANT ALL PRIVILEGES ON *.* TO 'database_user'@'localhost';

Grand all privileges to a user account over a specific table from a database:

GRANT ALL PRIVILEGES ON database_name.table_name TO 'database_user'@'localhost';

Grant multiple privileges to a user account over a specific database:

GRANT SELECT, INSERT, DELETE ON database_name.* TO database_user@'localhost';

Display MySQL User Account Privileges

To find the privilege(s) granted to a specific MySQL user account, use the SHOW GRANTS statement:

SHOW GRANTS FOR 'database_user'@'localhost';

 

The output will look something like below:

+-------------------------------------------------------------------------+

| Grants for database_user@localhost                                      |

+-------------------------------------------------------------------------+

| GRANT USAGE ON *.* TO 'database_user'@'localhost'                       |

| GRANT ALL PRIVILEGES ON `database_name`.* TO 'database_user'@'localhost'|

+-------------------------------------------------------------------------+

2 rows in set (0.00 sec)

Revoke Privileges from a MySQL User Account

The syntax to revoke one or more privileges from a user account is almost identical as when granting privileges.

To revoke all privileges from a user account over a specific database, run the following command:

REVOKE ALL PRIVILEGES ON database_name.* FROM 'database_user'@'localhost';

Remove an Existing MySQL User Account

To delete a MySQL user account use the DROP USER statement:

DROP USER 'user'@'localhost'

The command above will remove the user account and its privileges.

Conclusion

This tutorial covers only the basics, but it should be a good starting for anyone who wants to learn how to create new MySQL user accounts and grant privileges.

If you have any questions or feedback, feel free to leave a comment.

 

Komentar

Postingan populer dari blog ini

CREATE CROSS TAB QUERY IN MYSQL

MySQL Multi-Aggregated Rows in Crosstab Queries MySQL’s crosstabs contain aggregate functions on two or more fields, presented in a tabular format. In a multi-aggregate crosstab query, two different functions can be applied to the same field or the same function can be applied to multiple fields on the same (row or column) axis. Rob Gravelle shows you how to apply two different functions to the same field in order to create grouping levels in the row axis. Today’s topic of discussion is crosstabs, which contain multiple aggregate functions in the row axis of a tabular resultset. Recall from the the  All About the Crosstab Query  article that an aggregate function is one that summarizes a group of related data in some way. Examples of aggregate functions include COUNT, SUM, AVG, MIN, and MAX. In a multi-aggregate crosstab query, two different functions can be applied to the same field or the same function can be applied to two or more fields. Today we’ll create a query...

Mengatasi "This app can’t run on your PC Windows 10"

  Salah satu pesan error yang sering muncul saat aplikasi tidak bisa dibuka di Windows 10 adalah “ This app can’t run on your PC ,   to find a version for your PC check with the software publisher “. Masalah seperti ini cukup umum dan dialami banyak orang, terutama saat menjalankan aplikasi yang bukan dari Microsoft. Penyebab utama terjadinya masalah ini adalah karena masalah kompatibilitas antara aplikasi dengan versi Windows yang dianggap tidak sesuai oleh sistem. Bisa juga karena aplikasi atau game yang akan jalankan tersebut terkena filter oleh Windows sehingga prosesnya diblokir. Windows 10 memiliki fitur untuk memblokir aplikasi tidak dikenal yang berasal dari  unverified developers , fitur ini secara default akan aktif dengan tujuan untuk mencegah masuknya aplikasi yang mengandung malware dan virus. Penyebab lainnya bisa juga karena file aplikasi yang rusak, file sistem yang korup, atau masalah yang disebabkan oleh malware dan virus. Pada kesempatan kali ini  ...

Linux Basic Command Cheat Sheet

 https://www.guru99.com/linux-commands-cheat-sheet.html Linux Command Cheat Sheet In this Linux/Unix command line cheat sheet, you will learn: Basic Linux commands File Permission commands Environment Variables command User management commands of linux Networking command Process command VI Editing Commands Basic Linux commands Command Description ls Lists all files and directories in the present working directory ls -R Lists files in sub-directories as well ls -a Lists hidden files as well ls -al Lists files and directories with detailed information like permissions,size, owner, etc. cd or cd ~ Navigate to HOME directory cd .. Move one level up cd To change to a particular directory cd / Move to the root directory cat > filename Creates a new file cat filename Displays the file content cat file1 file2 > file3 Joins two files (file1, file2) and stores the output in a new file (file3) mv file "new file path" Moves the files to the new location mv filename new_file_name Re...