How to create MySql user via terminal in Linux
Posted by admin on March 20 2015 15:05:41

Login to MySQL

1st you must login to the MySQL server from the terminal with the following command:

mysql -u root -p

In this case is used user root with the -u option, and then used the -p option so in this way MySQL prompts for a password. Please, enter your root user password to complete the login.

You should now be logged in MySQL prompt, this prompt looks very like this:


Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7251 Server version: 5.5.41-0ubuntu0.14.10.1 (Ubuntu)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Create MySQL User

In the following example you will create a user with the name "exampleuser", and the password example123password!

CREATE USER 'exampleuser'@'localhost' IDENTIFIED BY 'example123password!';

Here is it, congratulations! You already are you have created your first MySQL user. However, this user can not do anything with MySQL until are not granted additional privileges. So here you will find how to do this:

Grant permissions

Here is a short list of commonly used permissions :

Allow a user to:
ALL – complete access to a specific database. If a database is not specified, then allow complete access to the entirety of MySQL.
CREATE – create databases and tables.
DELETE – delete rows from a table.
DROP – drop databases and tables.
EXECUTE – execute stored routines.
GRANT OPTION – grant or remove another user’s privileges.
INSERT – insert rows from a table.
SELECT – select data from a database.
SHOW DATABASES - view a list of all databases.
UPDATE – update rows in a table.

GRANT CREATE ON *.* TO 'exampleuser'@'localhost';
GRANT DROP ON tutorial_database.* TO 'exampleuser'@'localhost';
FLUSH PRIVILEGES;

To List/View Grants for MySQL User please, use the following command: SHOW GRANTS FOR 'exampleuser'@'localhost';