Create database with MySQL

Last modified: October 11, 2007 - 16:17

This step is only necessary if you don't already have a database set-up (e.g. By your host). In the following examples, 'username' is an example MySQL user which has the CREATE and GRANT privileges. Use the appropriate user name for your system.

First, you must create a new database for your Drupal site here, 'databasename' is the name of the new database):

mysqladmin -u username -p create databasename

MySQL will prompt for the 'username' database password and then create the initial database files. Next you must login and set the access database rights:

mysql -u username -p

Again, you will be asked for the 'username' database password. At the MySQL prompt, enter following command:

GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON databasename.* TO 'username'@'localhost' IDENTIFIED BY 'password';

where

'databasename' is the name of your database
'username@localhost' is the username of your MySQL account
'password' is the password required for that username

Unless your database user has all of the privileges listed above, you will not be able to run Drupal.

If successful, MySQL will reply with:

Query OK, 0 rows affected

To activate the new permissions, enter the following command:

FLUSH PRIVILEGES;

the database should be created with UTF-8 (Unicode) encoding.

watch your syntax -- advice for noobs

Seth_Schneider - December 29, 2007 - 00:53

I'm totally new to MySQL and Drupal, so it took me a long time to get the syntax correct. I finally got it to work by having no single-quotes (') around the databasename, including the single-quotes around the 'username' and including them around the 'password'. Of course, you need to substitute the actual database name, actual username, and actual password.

Hope that's helpful for other newbies like me who are struggling with this very basic steps!

...if you need to create new DB or user...

ench0 - January 17, 2008 - 05:20

more noob advice:
If this is your first Drupal install or if you just want to install a newer version of Drupal and play with it it is a good idea to create a separate user and DB for the new Drupal install. The handbook above explains how to create a new DB but not how to create a new user.

Here's what you do:

* open phpMyAdmin - , i.e. go to http://localhost/phpmyadmin/ if you have local or ssh access to your web server box or go through cPanel if you have cPanel installed, or whatever other means your web host provides to access phpMyAdmin;
* inside phpMyAdmin click on the Privileges link, should be in the lower left corner and the URL should look something like: http://localhost/phpmyadmin/server_privileges.php?token=2345320ab75534eb...
* click Add a New User;
* fill in username, choose localhost if you have local or ssh access to the web server box otherwise ask your host or go with Any Host;
* if you have localhost or ssh access you may leave the password blank ('No Password') otherwise choose a strong password or let phpMyAdmin generate one for you;
* in the 'Database for user' radio button group select 'Create database with same name and grant all privileges'.
* click Go

The above (assuming localhost access and both, DB user and DB name equal to 'drupal56') is equivalent to the following SQL:

CREATE USER 'drupal56'@'localhost';

GRANT USAGE ON * . * TO 'drupal56'@'localhost' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;

CREATE DATABASE IF NOT EXISTS `drupal56` ;

GRANT ALL PRIVILEGES ON `drupal56` . * TO 'drupal56'@'localhost';

Note that this is a lazy man's way of doing it and the GRANT statement in the original handbook topic is more restrictive than the GRANT ALL PRIVILEGES above. If this is your production site you should replace the GRANT ALL ... above with the original suggested GRANT statement.

the original documentation does create user

jscoble - January 17, 2008 - 05:39

The following statement(s) creates the user with the appropriate grants, as originally posted. Explicit CREATE USER and GRANT USAGE are not necessary in most situations.

GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES
ON databasename.*
TO 'username'@'localhost' IDENTIFIED BY 'password';

FLUSH PRIVILEGES;

It avoids doing the grants the lazy way and grants the Drupal system user the minimum privileges needed to run, which is a better practice.

The MAX_* parameters in the GRANT USAGE statement are used to throttle the users usage when they are non zero values.

The MySQL GUI tools ,found at MySQL.com, are good tools for MySQL management and running queries. I prefer them to PHPMyAdmin.

Drupal 6 doesn't require TEMPORARY TABLE or LOCK TABLES

simon.males - February 19, 2009 - 12:29

I'm not very experienced in this, but as there is not a Drupal 6.x page I feel I should point out the obvious.

6.x doesn't require TEMPORARY TABLES or LOCK TABLES according to these release notes: http://drupal.org/drupal-6.0-beta3.

The catch is there is nothing stopping contributed modules issuing these commands. I'm not aware if there is a general consensus amongst Drupal 6.x module developers to not issue the above queries.

 
 

Drupal is a registered trademark of Dries Buytaert.