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:

GRANT INSERT, DELETE, UPDATE, SELECT
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:

FLUSH PRIVILEGES;

Posted by Dave on April 8, 2009 in mysql

Write a Comment on Create a MySQL Database from the Command Line

Subscribe

Follow comments by subscribing to the Create a MySQL Database from the Command Line Comments RSS feed.

More

Read more posts by Dave

Getting Started With Zend Framework Zend Framework 1.8.0 released!