Creating a user in MySQL on Linux

Creating a MySQL user is a simple one-step process, begin by opening the MySQL shell. On Linux you do this by running the command mysql. To create the user, run the following command:

mysql> CREATE USER 'username'@'%' IDENTIFIED BY 'password';

Depending on where you want the user to connect from, you can replace the %-sign with ‘localhost’ or a specific IP-adress of another machine. % works as a wildstar, and the user can connect from anywhere.

Currently, the user doesn’t have permission to alter any database. To give the user all privileges on a specific table of a database, run the following command:

mysql> GRANT ALL PRIVILEGES ON database.table TO 'username'@'%';

To give the user permission to alter all tables on a database, use a wildstar instead of a table name:

mysql> GRANT ALL PRIVILEGES ON database.* TO 'username'@'%';

Everytime you update a user’s privileges, you have to flush the privileges to make sure the new ones are applied:

mysql> FLUSH PRIVILEGES;

Note that GRANT ALL PRIVILEGES can cause issues if someone malicious gains access to the user, for example they can drop the whole table which erases all of the data in it. For more specific info on privileges, read our guide on MySQL privileges.

Leave a Reply

Your email address will not be published. Required fields are marked *