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: Difference between revisions

From MoodleDocs
(switching pages)
 
m (Reverted edits by Lexusdriverwatchout@gmail.com (talk) to last revision by Chris Fryer)
 
(13 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{Review}}
{{Template:Installing Moodle}}
{{Template:Installing Moodle}}
Moodle supports [[MySQL]], [[PostgreSQL]] and MS SQL 2005 and Oracle databases.  Other databases may also work with Moodle.


::'''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 [http://moodle.org/mod/forum/discuss.php?d=58552 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 <code>sql-mode=''</code>. You have to restart MySQL after changing this setting. <br><br> If you do not have access to your server, use PHPMyAdmin (or another MySQL client) and enter the command <code>SET @@global.sql_mode='';</code> (be sure to use single quotes, and don't forget the semicolon).
If your Moodle installer, does not create or add to an existing MySQL database, these instructions may help you.
====Using a hosted server====
<table style=background-color:lightCyan border=1 cellpadding=5 cellspacing=0 ><tr><td>
If you are using a webhost, they will probably have a control panel web interface for you to create your database.


The '''[http://www.cpanel.com/ cPanel]''' system is one of the most popular of these.  
==MySQL==
To create a database using cPanel:
===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 [[Windows_installation_using_XAMPP#Create_the_MySQL_database|create the MySQL database]] section in the XAMPP installation page.


# Click on the '''MySQL Databases''' icon.
===MySQL client===
# Type '''moodle''' in the New Database field and click '''Create Database'''.
In Linux, here are some command line prompts using a MySQL Client program (commands which you type-in are shown in bold):
# Type a ''username'' and ''password'' (not one you use elsewhere) in the respective fields and click '''Create  User'''.<br>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.
# Now use the '''Add Users to Databases''' button and give this new user account '''ALL''' rights to the new database.
</td></tr></table>


Continue with [[Installing_Moodle#Creating_the_data_directory |Creating the data directory]]
*Start the MySQL Client program:
  #'''mysql -u root -p'''


====Using a SQLite database====
Enter password: '''MartinMoodle'''
<table style=background-color:lightCyan border=1 cellpadding=5 cellspacing=0 ><tr><td>
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 [[Installing_Moodle#Creating_the_data_directory |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 [[Installing_Moodle#Creating_the_data_directory |Creating the data directory]]
</td></tr></table>
====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.
  Welcome to the MySQL monitor.  Commands end with ; or \g.
  Your MySQL connection id is 2 to server version: 5.0.22-log
  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.
  Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
  mysql>
  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.)  
:''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;'''


- 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                |
  | Database                |
Line 55: Line 37:
  3 rows in set (0.03 sec)
  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.
* 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;'''
  mysql> '''CREATE DATABASE moodle;'''
 
  Query OK, 1 row affected (0.00 sec)
  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):
* 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;'''
  mysql> '''ALTER DATABASE moodle DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;'''
 
  Query OK, 1 row affected (0.00 sec)
  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.
* 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,'''
  mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,CREATE TEMPORARY TABLES,DROP,INDEX,ALTER ON moodle.* TO moodleuser@localhost IDENTIFIED BY 'yourpassword';
    -> '''DROP,INDEX,ALTER ON moodle.*'''
    -> '''TO moodleuser@localhost IDENTIFIED BY 'yourpassword';'''
  Query OK, 0 rows affected (0.01 sec)
  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.
:'''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'''


- Exit the MySQL Client program:
==Alternative==
  mysql> '''QUIT'''
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. 
  Bye
create database moodle default character set utf8;
  #
  grant all privileges on moodle.* to 'moodleuser'@'localhost' identified by 'yourpassword';
  flush privileges;
  quit


- Reload the grant tables using the mysqladmin program:
:''Note:'' 'Grant all' gives a lot of privilege to the moodleuser account.
#'''mysqladmin -u root -p reload'''
 
Enter password:
==PostgreSQL==
#


And some example command lines for those who wish to use a PostgreSQL database:
And some example command lines for those who wish to use a PostgreSQL database:
Line 91: Line 75:
   > su - root
   > su - root
   # /etc/init.d/postgresql reload
   # /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:
If the Postgres create database command above (>psql -c "create database moodle...") gives an error message you may want to try:
Line 96: Line 86:


If the create database command asks you for a password, run the line containing 'encrypted password' first before proceeding.
If the create database command asks you for a password, run the line containing 'encrypted password' first before proceeding.
==Using a SQLite database==
<table style=background-color:lightCyan border=1 cellpadding=5 cellspacing=0 ><tr><td>
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 [[Installing_Moodle#Creating_the_data_directory |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 [[Installing_Moodle#Creating_the_data_directory |Creating the data directory]]
</td></tr></table>
==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 '''[http://www.cpanel.com/ cPanel]''' system is one of the most popular of these.
To create a database using cPanel:
# Click on the '''MySQL Databases''' icon.
# Type '''moodle''' in the New Database field and click '''Create Database'''.
# Type a ''username'' and ''password'' (not one you use elsewhere) in the respective fields and click '''Create  User'''.<br>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.
# Now use the '''Add Users to Databases''' button and give this new user account '''ALL''' rights to the new database.
Continue with [[Installing_Moodle#Creating_the_data_directory |Creating the data directory]]
==Caution- be aware of Moodle version requirements==
See the [[http://download.moodle.org/ 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==
==See also==
*[[Installing Moodle/Install draft]]
*[[Installing Moodle]]
* Step-by-step instructions on  [https://docs.moodle.org/en/Step-by-step_Install_Guide_for_Ubuntu installation for Ubuntu(Debian)]
* Step-by-step instructions on  [https://docs.moodle.org/en/Step-by-step_Install_Guide_for_Ubuntu installation for Ubuntu(Debian)]
[[Category:SQL databases]]

Latest revision as of 06:11, 27 April 2012

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.

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