Create a MySQL Database from the Command Line

Every now and then I find myself needing to create a MySQL database while on the command line. Often it is just as fast or faster than loading up another program or accessing phpMyAdmin in a browser. So, first things first, we need to connect to the database:

mysql -u root -p

MySQL will prompt for the root password. Once your logged on you can issue the following command:

CREATE DATABASE databasename;

Next we need to give a user access to the new database:

ON databasename.*
TO ‘username’@’localhost’ IDENTIFIED BY ‘password’;

Where ‘databasename’ is the name of the database we created, ‘username’ is the account allowed to access this database, ‘localhost’ is the host this user is allowed to connect from, ‘password’ is the password required for this user.

To activate the new permissions, issue the following command:



  Comments: 1

  1. If you’re already all set up with privs (or have one user), you can also do this:

    mysql -u$USER -p$PASSWORD -h$HOST -e ‘create database x’

    This will execute the command and exit without having to even go in to the mysql prompt.

Your feedback