How To Create A New Username In MySQL

MySQL by default only creates one username, which is root. Creating new usernames in MySQL could make it easier to use MySQL and potentially more secure. This article will describe step-by-step how to create a new username in MySQL in Ubuntu (18.04, 16.04 and 14.04) and the steps will be similar in Debian.

How To Create A New Username In Ubuntu 18.04 and Ubuntu 16.04

Since the introduction of Ubuntu 16.04, the steps for how to initially logging into MySQL have changed a little. You will no longer be able to login by default by doing mysql -u root -p. You will need to do
sudo mysql. While I definitely preferred logging into to MySQL with Ubuntu 14.04, it’s not really much harder in Ubuntu 18.04 and Ubuntu 16.04

Then use the following steps to create a new username in Ubuntu 18.04/16.04.
Login To MySQL
sudo mysql
For the above command, make sure to use your password for your account in Ubuntu, not password that you setup for root in MySQL (assuming they are different).

Then once you are logged in, you will create a new username with the following steps. I am going to assume we will choose a username exampleusername and have its password be abcdefg12345. Also, I am assuming that you have already created called exampledotcomdatabase.  If you haven’t already created a database, you can learn how to  how to create a new database in MySQL here. You can modify the username and password however you like.

CREATE USER 'exampleusername'@'localhost' IDENTIFIED BY 'abcdefg12345';
GRANT ALL PRIVILEGES ON exampledotcomdatabase . * TO 'exampleusername'@'localhost';
FLUSH PRIVILEGES;
exit;

Then after exiting, you can log back into mysql with your new username. Make sure to use the MySQL password you setup for it.
mysql -u exampleusername -p

How To Create A New Username In Ubuntu 14.04

mysql -u root -p

Make sure to use the password for root that you setup with your mySQL installation.

Then once you are logged in, you will create a new username with the following steps. I am going to assume we will choose a username exampleusername and have its password be abcdefg12345. Also, I am assuming that you have already created called exampledotcomdatabase. You can modify the username and password however you like.

CREATE USER 'exampleusername'@'localhost' IDENTIFIED BY 'abcdefg12345';
GRANT ALL PRIVILEGES ON exampledotcomdatabase . * TO 'exampleusername'@'localhost';
FLUSH PRIVILEGES;
exit;

Then after exiting, you can log back into MySQL with your new username. Make sure to use the MySQL password you setup for it.
mysql -u exampleusername -p

Did you like this article? Do you have anything to add? Let’s discuss it in the comments below.

Leave a Reply

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