How to Create a new user in MySQL

PHOTO: Unsplash.com

MySQL is a popular open-source database management system (DBMS) used in millions of applications across the world. Unlike other database systems, you can create a new user in MySQL without sophisticated tools or commands in your access diameter. To successfully create your user, all you need is access to the MySQL console or a GUI tool such as PHPMyAdmin.

As a norm, each time you create a new user in MySQL, you must assign the user correct privileges to access and manage databases on your MySQL cluster. Failure to assign the user correct privileges may lead to little or no access to all MySQL databases.

In this article, we’ll create a new user in MySQL identified with the name `example-user` you will have to replace with your desired username. It’s also important to note that you must have administrative access to the MySQL Shell, then, follow the steps in this article to create your new database user account.

Step 1: Log in to the MySQL Console

To log in to the MySQL console, make sure you have a user account with administrative privileges. If you are running Linux, you can simply use the sudo command to attain root privileges and log in without a password as below.

$ sudo mysql

If you have an administrative username and password, run the following command:

$ mysql -u username-here -p

Step 2: Create a new MySQL User

Once logged in to the MySQL console, create the user example-user by running the following command:

CREATE USER 'example-user'@'localhost' IDENTIFIED BY 'strong-passsword';

In the above command, example-user is the new MySQL user account. localhost defines that you are running MySQL client on the same database server.strong-password represents the user password which must be strong enough to protect your MySQL server.

Step 3: Grant the MYSQL User Privileges to Databases

Now that the user is created, you must grant the MySQL user necessary privileges to one or more databases. These privileges determine what the user can do on the database whether create, read, update, or delete data which you can set as below.

GRANT <PRIVILEGE> ON <Database> TO <user> @ <database-server>

The privileges determine what the user can do in MySQL. For example, you can grant the user the privileges to create, read, update, and delete data, or you can limit the user to only read from the database.

Grant the MySQL user full privileges to all databases.

GRANT ALL PRIVILEGES ON .* TO 'example-user'@'localhost';

Grant the MySQL user full privileges to a single database. For example, the example database.

GRANT ALL PRIVILEGES ON example.* TO 'example-user'@'localhost';

Once applied, the MySQL user only has privileges to the selected databases, and not any other on the server. This is important when grouping users in form of departments to limit what they can access.

Step 4: Flush Privileges to Save MySQL user permissions

Once you create a new user in MySQL or change permissions, you must refresh the database privileges for changes to take effect. To flush privileges, simply run the following command:

FLUSH PRIVILEGES

Upon flushing privileges, the MySQL privilege tables are refreshed with new data. This ensures that your changes to the database server users and permissions are applied.

Test the new MySQL User

To verify that the new MySQL user is well created and the assigned password works well, exit the MySQL console. Then, log in again using the new MySQL user as described below.

First, exit the MYSQL console.

EXIT

Log in as the new user.

$ mysql -u example-user -p

Enter the user password when prompted.

Verify that the user has access to the assigned databases.

SHOW DATABASES;

Exit the MySQL console.

EXIT

Do More each time you Create a New User in MySQL

In this article, you have successfully set up a new user in MySQL and assigned the necessary privileges to allow the user access available databases. The above process is important when setting up new applications such as websites that require database access to keep the rest of your databases safe. For more information about MySQL, please visit its official documentation.

Exit mobile version