Note: You are currently viewing documentation for Moodle 2.0. Up-to-date documentation for the latest stable version is available here: Create Moodle site database.

Create Moodle site database

From MoodleDocs
Revision as of 12:22, 2 March 2009 by chris collman (talk | contribs) (switching pages)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This page requires a review. Please do so and remove this template when finished.


Warning: Bear in mind that, as of Moodle version 1.5.x, Moodle doesn't work with MySQL 5.x's strict mode setting (STRICT_TRANS_TABLES and/or STRICT_ALL_TABLES) -- see forum discussion. So if you are using MySQL 5.x, edit MySQL's configuration file (called "my.ini" in Windows and "my.cnf" on Unix/Linux) and comment out that option or set it to sql-mode=. You have to restart MySQL after changing this setting.

If you do not have access to your server, use PHPMyAdmin (or another MySQL client) and enter the command SET @@global.sql_mode=
; (be sure to use single quotes, and don't forget the semicolon).

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

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 the command line

If you have access to Unix or Windows command lines then you can do the same sort of thing by typing commands. You should do this, if you want to use a MySQL database, using the MySQL Client program as follows (commands which you type-in are shown in bold):

- Start the MySQL Client program:

#mysql -u root -p
Enter password:
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>

- 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.
Note: For MySQL 4.0.1 or earlier, you don't need the CREATE TEMPORARY TABLES permission.

- Exit the MySQL Client program:

mysql> QUIT
Bye
#

- Reload the grant tables using the mysqladmin program:

#mysqladmin -u root -p reload
Enter password:
#

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 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.

See also