Вы находитесь на странице: 1из 4

Create a MySQL User on Linux via Command Line

Login to MySQL
First well login to the MySQL server from the command line with the following command:
mysql -u root -p
In this case, Ive specified the user with the flag, and then used the flag so MySQL prompts for a
password. Enter your current password to complete the login.
If you need to change your root (or any other) password in the database, then follow this tutorial on
changing a password for MySQL via the command line.
You should now be at a MySQL prompt that looks very similar to this:
mysql>
Create MySQL User
Well create a user with the name testuser , and the password test123test!.
CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'test123test!';
Thats it, congratulations! In just one command youve created your first MySQL user. However,
this user wont be able to do anything with MySQL until they are granted additional privileges. In
fact, they wont even be able to login without additional permissions.
View a List of MySQL Users
Viewing a full list of MySQL users, including the host theyre associated with, can be done with the
following select statement:
SELECT User,Host FROM mysql.user;

Grant Permissions to a MySQL User on Linux via Command Line


Login to MySQL
First well login to the MySQL server from the command line with the following command:
mysql -u root -p
In this case, Ive specified the user with the flag, and then used the flag so MySQL prompts for a
password. Enter your current password to complete the login.
If you need to change your root (or any other) password in the database, then follow this tutorial on
changing a password for MySQL via the command line.
You should now be at a MySQL prompt that looks very similar to this:
mysql>
If you havent yet created a MySQL user, please refer to our tutorial on creating a MySQL user.
Grant Permissions to MySQL User
The basic syntax for granting permissions is as follows:

GRANT permission ON database.table TO 'user'@'localhost';


Here is a short list of commonly used :
ALL Allow complete access to a specific database. If a database is not specified, then
allow complete access to the entirety of MySQL.
CREATE Allow a user to create databases and tables.
DELETE Allow a user to delete rows from a table.
DROP Allow a user to drop databases and tables.
EXECUTE Allow a user to execute stored routines.
GRANT OPTION Allow a user to grant or remove another users privileges.
INSERT Allow a user to insert rows from a table.
SELECT Allow a user to select data from a database.
SHOW DATABASES- Allow a user to view a list of all databases.
UPDATE Allow a user to update rows in a table.
Example #1: To grant permissions for all databases and all tables to the user we created in the
previous tutorial, , use the following command:
GRANT CREATE ON *.* TO 'testuser'@'localhost';
Using an asterisk (*) in the place of the or is a completely valid option, and implies all databases or
all tables.
Example #2: To grant the ability to drop tables in the specific database, , use the permission:
GRANT DROP ON tutorial_database.* TO 'testuser'@'localhost';
When finished making your permission changes, its good practice to reload all the privileges with
the command!
FLUSH PRIVILEGES;
View Grants for MySQL User
After youve granted permissions to a MySQL user youll probably want to double check them. Use
the following command to check the grants for :
SHOW GRANTS FOR 'testuser'@'localhost';

Remove Permissions for a MySQL User on Linux via Command Line


Login to MySQL
First well login to the MySQL server from the command line with the following command:
mysql -u root -p
In this case, Ive specified the user with the flag, and then used the flag so MySQL prompts for a
password. Enter your current password to complete the login.
If you need to change your root (or any other) password in the database, then follow this tutorial on
changing a password for MySQL via the command line.
You should now be at a MySQL prompt that looks very similar to this:
mysql>
If you havent yet created a MySQL user, please refer to our tutorial on creating a MySQL user.

View Grants for MySQL User


Use the following command to check the grants for the user :
SHOW GRANTS FOR 'testuser'@'localhost';
Revoke Permissions to MySQL User
The basic syntax for revoking permissions is as follows:
REVOKE permission ON database.table FROM 'user'@'localhost';
Here is a short list of commonly used :
ALL Allow complete access to a specific database. If a database is not specified, then
allow complete access to the entirety of MySQL.
CREATE Allow a user to create databases and tables.
DELETE Allow a user to delete rows from a table.
DROP Allow a user to drop databases and tables.
EXECUTE Allow a user to execute stored routines.
GRANT OPTION Allow a user to grant or remove another users privileges.
INSERT Allow a user to insert rows from a table.
SELECT Allow a user to select data from a database.
SHOW DATABASES- Allow a user to view a list of all databases.
UPDATE Allow a user to update rows in a table.
Example #1: To revoke permissions for all databases and all tables from the user we created in a
previous tutorial, , use the following command:
REVOKE CREATE ON *.* FROM 'testuser'@'localhost';
Using an asterisk (*) in the place of the or is a completely valid option, and implies all databases or
all tables.
Example #2: To revoke the ability to drop tables in the specific database, , use the permission:
REVOKE DROP ON tutorial_database.* FROM 'testuser'@'localhost';
Note: If the specified user does not have the specified permission, then you will receive an error. Be
sure to use the SHOW GRANTS command, as demonstrated above, to see what permissions are
granted.
When finished making your permission changes, its good practice to reload all the privileges with
the command!
FLUSH PRIVILEGES;

Remove a MySQL User on Linux via Command Line


Login to MySQL
First well login to the MySQL server from the command line with the following command:
mysql -u root -p
In this case, Ive specified the user with the flag, and then used the flag so MySQL prompts for a
password. Enter your current password to complete the login.

If you need to change your root (or any other) password in the database, then follow this tutorial on
changing a password for MySQL via the command line.
You should now be at a MySQL prompt that looks very similar to this:
mysql>
If you havent yet created a MySQL user, please refer to our tutorial on creating a MySQL user.
View a List of MySQL Users
Viewing a full list of MySQL users, including the host theyre associated with, can be done with the
following select statement:
SELECT User,Host FROM mysql.user;
Remove a MySQL User
To remove a user from MySQL, we again use the command.
It only takes one simple command to delete a user in MySQL, but BEWARE; dropping a user can
not be undone! The command is as follows:
DROP USER 'testuser'@'localhost';
If a user of the name does not exist, then youll receive this error:
ERROR 1396 (HY000): Operation DROP USER failed for
'testuser'@'localhost'
Refer to the section above if you receive the above error, and double check the username and host.

Вам также может понравиться