How To Create A User And Database Through The MySQL Command Line

Most people can figure out how to create a database and user if they have access to web-based tools, such as phpMyAdmin. It's more difficult if you don't, but you can still do it. You just have to use the MySQL command line.

From the Linux command line, type:

mysql -u root -p

Enter your password when prompted, and you'll find yourself at the MySQL command line.

Here's how you can create a new database. Type this command, replacing [DATABASE_NAME] with your database's name:

CREATE DATABASE [DATABASE_NAME];

MySQL should tell you that the query was OK.

Next you want to create a user. Type this command, replacing [USER_NAME] with your user name and [PASSWORD] with your password:

CREATE USER '[USER_NAME]'@'localhost' IDENTIFIED BY '[PASSWORD]';

MySQL should tell you that the query was OK.

Finally, you want to give your user access to your database. Type this command, replacing [DATABASE_NAME] with your database name and [USER_NAME] with your user name:

GRANT ALL PRIVILEGES ON [DATABASE_NAME].* TO '[USER_NAME]'@'localhost';

MySQL should tell you that the query was OK.

Now you want to flush privileges to make sure everything works as it should. Type this command:

FLUSH PRIVILEGES;

MySQL should tell you that the query was OK.

Now you are done, so you can tell MySQL to quit. Type this command:

quit

MySQL will say, Bye. That's all there is to creating a database and user via the MySQL command line.