Docs for other versions: 2.6 / 2.5 / 2.4 / 2.3 / 2.2 / 2.1 / 2.0 / 1.9

Create Moodle site database

From MoodleDocs
Jump to: navigation, search

Moodle supports MySQL, PostgreSQL and MS SQL 2005 and Oracle databases. Other databases may also work with Moodle.

If your Moodle installer, does not create or add to an existing MySQL database, these instructions may help you.

Contents

MySQL

phpMyAdmin

If you have access to phpMyAdmin you can use this program to create an empty database which the Moodle Installer will then populate with its tables.

Tip: Be sure that it includes one of the UTF standard formats for Moodle 2.x.

See the create the MySQL database section in the XAMPP installation page.

MySQL client

In Linux, here are some command line prompts using a MySQL Client program (commands which you type-in are shown in bold):

  • Start the MySQL Client program:
 #mysql -u root -p
Enter password: MartinMoodle
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2 to server version: 5.0.22-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
Note: The prompt changes to "mysql>" to indicate that you are now working in the MySQL Client program. When working in MySQL, all commands which you type-in must end in a semi-colon. (If you hit the Enter key without the final semi-colon, you'll get the line continuation symbol '->'; this is your second chance to type the semi-colon and hit Enter.)
  • Begin by checking for any existing databases called "moodle" - if there are any you should change the name in all the commands which follow:
 mysql> SHOW DATABASES;
+-------------------------+
| Database                |
+-------------------------+
| information_schema      |
| mysql                   |
| test                    |
+-------------------------+
3 rows in set (0.03 sec)
  • Create a database to store the Moodle tables. We'll call this "moodle", as there are none with that name already in the above list, but change it if you need to.
 mysql> CREATE DATABASE moodle;
Query OK, 1 row affected (0.00 sec)
  • Change the default character set and collation of the "moodle" database to UTF8. Leave this out if you are installing Moodle 1.5 or earlier):
 mysql> ALTER DATABASE moodle DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Query OK, 1 row affected (0.00 sec)
  • Create a username and password to access the database "moodle" and grant database access permissions. We'll call the user "moodleuser" and set the password as "yourpassword". It's a good idea to change these for your installation however most people keep the username as "moodleuser". Remember the username and password you have set, as you'll need it in the configuration screens later. This is a long command so has been split over several lines by pressing the Return key.
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,CREATE TEMPORARY TABLES,DROP,INDEX,ALTER ON moodle.* TO moodleuser@localhost IDENTIFIED BY 'yourpassword';
Query OK, 0 rows affected (0.01 sec)
Security Warnings: Never leave the password as the one shown here. Make sure you have a strong password (a mixture of letters and numbers, upper and lower case). Avoid granting "ALL" permissions on the database.
  • Exit the MySQL Client program:
 mysql> QUIT

Alternative

If you want to run all this at once without so many commands then copy and paste the lines below into the mysql command line.

create database moodle default character set utf8;
grant all privileges on moodle.* to 'moodleuser'@'localhost' identified by 'yourpassword';
flush privileges;
quit
Note: 'Grant all' gives a lot of privilege to the moodleuser account.

PostgreSQL

And some example command lines for those who wish to use a PostgreSQL database:

  # su - postgres
  > psql -c "create user moodleuser createdb;" template1
  > psql -c "alter user moodleuser with encrypted password 'yourpassword';" template1
  > psql -c "create database moodle with encoding 'unicode';" -U moodleuser template1
  > psql -c "alter user moodleuser nocreatedb;" template1
  > su - root
  # /etc/init.d/postgresql reload


If the Postgres create user command above (>psql -c "create user moodleuser...") gives an error message you may want to try initdb as postgres, then restarting postgresql as root, then start steps again from the beginning:

initdb -D data
su
# /etc/init.d/postgresql restart

If the Postgres create database command above (>psql -c "create database moodle...") gives an error message you may want to try:

psql -c "create database moodle with template=template1 encoding = 'unicode' owner =  moodleuser 
location = '/var/mydata';"

If the create database command asks you for a password, run the line containing 'encrypted password' first before proceeding.

Using a SQLite database

SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine.

Moodle 2.0 offers experimental support for SQLite3 database installations. In this case, no database setup is required. The database file will be created by the installation script. By default, the database file will be store in Moodle's data directory (see Creating the data directory). During install, the web server must have write access on the directory where the database file will be stored. After installation, the web server must have read-write access to the database file.

Continue with Creating the data directory

Using a hosted server

If you are using a webhost, they will probably have a control panel web interface for you to create your database.

The cPanel system is one of the most popular of these. To create a database using cPanel:

  1. Click on the MySQL Databases icon.
  2. Type moodle in the New Database field and click Create Database.
  3. Type a username and password (not one you use elsewhere) in the respective fields and click Create User.
    Note that the username and database names may be prefixed by your cPanel account name and an underscore, and truncated to 16 characters. When entering this information into the Moodle installer - use the full names.
  4. Now use the Add Users to Databases button and give this new user account ALL rights to the new database.

Continue with Creating the data directory

Caution- be aware of Moodle version requirements

See the [download page] at Moodle.org or PHP settings by Moodle version for supported database requirements for each version of Moodle.

For example Moodle 2.0 requires:

   * MySQL 5.0.25 or later (InnoDB storage engine highly recommended)
   * PostgreSQL 8.3 or later
   * Oracle 10.2 or later
   * MS SQL 2005 or later

See also