Setting Up the Database

Drupal does not create the database for you. For this, you will need to become familiar with the tools provided by the database management system that you have chosen to use. For MySQL, the PHP-based web application phpMyAdmin ( is popular. For PostgreSQL, phpPgAdmin ( is a common choice. Both are often included as standard fare by web hosting companies selling hosting packages.

Note All of the examples for working with the database server in this chapter are MySQL-specific.

No matter which database manager you choose, you need to take the following steps to prepare the database for use by Drupal.

Create the database: It is not particularly important what you name the database. It is important that you know what the name is. Some hosts will prefix the name you provide. Similarly, some web hosts will truncate the name to fit into a certain number of characters. So, make sure to double-check what the database is actually called, because you will need to know this when you're configuring Drupal.

Create the database user and assign rights: Access to any database is granted on a databaseuser basis. Do not use the root user or the database admin user to access your database, as this presents a security risk. Instead, you need to create a user who has permissions to access the Drupal database. This can usually be done with the same tools that you used to create the database. Create the user account and grant it SELECT, INSERT, UPDATE, DELETE, CREATE, and LOCK TABLES privileges. If you're using a command-line tool, don't forget to use FLUSH PRIVILEGES as well.

Import the schema: Once the database is created and a user is assigned, with the appropriate privileges, it is time to import the schema for the core Drupal database. The SQL instructions to do this are found in the database/database.mysql and database.pgsql files that come with your Drupal distribution. In order to import the schema, execute the instructions in the file appropriate to your database. There are command-line methods for doing this, and the database management tools mentioned earlier can help you do this as well.

As an example, let's go through the GNU/Linux command-line versions of these steps for a MySQL database. First, create the database:

$ mysqladmin -u db_user -p create db_name db_user is an existing MySQL user who has the rights to create databases. This will often be root. The db_name is the name of the database you wish to create.

You also need a database user who is allowed to connect to the database you just created. This might be the db_user from the previous command, in which case you can skip this step. Otherwise, to create a new database user, connect to the MySQL server as root and use GRANT to create a new user:

$ mysql -u root -p mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,LOCK TABLES^ ON db_name.* TO 'db_user'@'localhost' IDENTIFIED BY 'password';

db_name is the database name, db_user is the name this new user will have, and password is the password that will be used when making a database connection. This example assumes that the user will be accessing the database only from the current machine, or localhost. Replace localhost with the appropriate domain or IP address if the connection will be made to a different machine.

Finally, import the database schema from the database/database.mysql file that came with the Drupal distribution:

$ mysql -u db_user -p db_name < database/database.mysql

Was this article helpful?

0 0

Post a comment