MySQL: Create Database – Command Line

From this small tutorial you will learn how to create a MySQL database from the command-line in Linux.

I will show the general MySQL CREATE DATABASE syntax for creating a database with a default character set.

Additionally i will show how to create a user in MySQL, set him a password, grant all privileges on this newly created database and allow him to access it locally.

Cool Tip: Do you have a backup? You MUST have it! Backup MySQL databases from the command-line! This is really easy! Read more →

Log in to MySQL server from the command-line as root:

$ mysql -u root -p

Create MySQL Database

Use the following syntax to create a database in MySQL:

mysql> CREATE DATABASE db_name;

Create a new user:

mysql> GRANT ALL PRIVILEGES ON db_name.* TO user_name@localhost IDENTIFIED BY 'user_password';

With the above command we have granted all the privileges on the newly created database db_name to the user user_name and have set him a password user_password.

By specifying the localhost after the @ character we allow this user to connect to the MySQL database only from the server on which this database is hosted on.

You can change the localhost to an IP address or a hostname if you want to permit the user to connect to the database remotely.

Cool Tip: List MySQL users, their passwords and granted privileges from the command-line prompt! Read more →

Now you can log in to MySQL server from the command-line as this user:

$ mysql -u user_name -p

To show the all databases available for the current user, run:

mysql> SHOW DATABASES;

To access the newly created MySQL database, run:

mysql> USE db_name;

You can also immediately access the database from the command-line by explicitly specifying its name:

$ mysql -u user_name -p -В db_name

To explicitly specify an IP address or a hostname of MySQL server, run:

$ mysql -u user_name -p -h hostname