Note: You are currently viewing documentation for Moodle 3.2. Up-to-date documentation for the latest stable version of Moodle is probably available here: MySQL.

MySQL: Difference between revisions

From MoodleDocs
No edit summary
m (UTF-8 update (utf8mb4))
 
(17 intermediate revisions by 12 users not shown)
Line 1: Line 1:
{{Installing Moodle}}
MySQL is one of the supported databases that underpins a Moodle installation.  
MySQL is one of the supported databases that underpins a Moodle installation.  


MySQL describes itself as "the most popular Open Source SQL database management system, is developed, distributed, and supported by MySQL AB. MySQL AB is a commercial company, founded by the MySQL developers. It is a second generation Open Source company that unites Open Source values and methodology with a successful business model."
== Installing MySQL ==
 
* If you are running Linux your preference should be to install using your distributions package manager. This ensures you will get any available updates.  However, you can also use apt-get or yum depending on the distribution that you are running.
* There are installers available for most popular operating systems at http://www.mysql.com/downloads/mysql/.
* It is possible and reasonably straightforward to build mysql from source but it is not recommended (the pre-built binaries are supposedly better optimised).
* Make sure you set a password for the 'root' user (see http://dev.mysql.com/doc/refman/5.0/en/default-privileges.html).
* Consider installing and configuring my.cnf (the MySQL settings file) to suit your needs. The default configuration is usually very conservative in respect of memory usage versus performance. Increase the 'max_allowed_packet' setting to at least 4 megabytes. Also make sure that your collation and character set is using full UTF-8 support. Please view [[MySQL_full_unicode_support#Steps_to_upgrade.]]
* If you are going to use Master/Slave replication, you must add binlog_format = 'ROW' into your my.cnf within [mysqld]. Otherwise, Moodle will not be able to write to the database.
 
== Creating Moodle database ==
 
These are the steps to create an empty Moodle database. Substitute your own database name, user name and password as appropriate.
 
The instructions assume that the web server and MySQL server are on the same machine. In this case the 'dbhost' is 'localhost'. If they are on different machines substitute the name of the web server for 'localhost' in the following instructions and the 'dbhost' setting will be the name of the database server.
Databases have a "Character set" and a "Collation". For Moodle the Character set should be '''utf8''' and the Collation '''utf8_unicode_ci'''. You may get the option to set these values when you create the database. If you are not given a choice, the default options are probably good. An install on an old server may have the wrong settings.
 
=== Command line ===
 
* To create a database using the 'mysql' command line client, first log into MySQL
<pre>
$ mysql -u root -p
Enter password:
</pre>
(Enter the password you previously set - or been given - for the MySQL 'root' user). After some pre-amble this should take you to the ''mysql>'' prompt.
* Create a new database (called 'moodle' - substitute your own name if required).  We recommend you use '''utf8mb4_unicode_ci''' for Collation.
<pre>
mysql> CREATE DATABASE moodle DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
</pre>
* Add a user/password with the minimum needed permissions:
<pre>
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,CREATE TEMPORARY TABLES,DROP,INDEX,ALTER ON moodle.* TO moodleuser@localhost IDENTIFIED BY 'yourpassword';
</pre>
...which creates a user called 'moodleuser' with a password 'yourpassword'. Make sure you invent a strong password and resist the temptation to 'GRANT ALL'.
 
=== phpMyAdmin ===
 
[http://www.phpmyadmin.net/ phpMyAdmin] is a web based administration tool for MySQL. If this is available you can use it to create a new database. Make sure that you select 'UTF8' as the default character set.
 
==Which database belongs to which Moodle==
If you have installed several Moodle installations on the same server, there will be several databases in your MySQL server. The names might be quite poor reflections of the content like  _mdl1 _mdl2 _mdl3 . So how do I see which database goes with which Moodle installation? You can go in with phpMyAdmin and in the various databases check for the table "mdl_course". There you will easily see the name of that Moodle Installation. In table mdl_config you can see the Moodle version. The main URL for the site is not in the database except where there are absolute links.


MySQL comes with an array of [http://dev.mysql.com/doc/refman/5.0/en/storage-engines.html storage engines]. The popular ones being MyISAM and InnoDB. Since MySQL 5.5.5, MyISAM was dropped as default and InnoDB was made the [http://dev.mysql.com/doc/refman/5.5/en/innodb-default-se.html default storage engine] of choice. InnoDB is more well supportable than MyISAM due to known issues with MyISAM.
== See also ==
== See also ==


* [[MariaDB]]
* [[MySQL full unicode support]]
* [http://www.mysql.com/ The MySQL homepage]
* [http://www.mysql.com/ The MySQL homepage]
* [http://moodle.org/mod/forum/view.php?id=45 Using Moodle databases forum]
* [http://en.wikipedia.org/wiki/MySQL Wikipedia article about ''MySQL'']
* [http://en.wikipedia.org/wiki/MySQL Wikipedia article about ''MySQL'']
* [http://forums.mysql.com/read.php?24,92131,92131 List of articles on MySQL performance tuning]


[[Category:Administrator]]
[[Category:Developer]]
[[Category:SQL databases]]
[[Category:SQL databases]]


[[ja:MySQL]]
[[ja:MySQL]]
[[de:MySQL]]
[[de:MySQL]]
[[es:MySQL]]

Latest revision as of 02:22, 5 May 2017

MySQL is one of the supported databases that underpins a Moodle installation.

Installing MySQL

  • If you are running Linux your preference should be to install using your distributions package manager. This ensures you will get any available updates. However, you can also use apt-get or yum depending on the distribution that you are running.
  • There are installers available for most popular operating systems at http://www.mysql.com/downloads/mysql/.
  • It is possible and reasonably straightforward to build mysql from source but it is not recommended (the pre-built binaries are supposedly better optimised).
  • Make sure you set a password for the 'root' user (see http://dev.mysql.com/doc/refman/5.0/en/default-privileges.html).
  • Consider installing and configuring my.cnf (the MySQL settings file) to suit your needs. The default configuration is usually very conservative in respect of memory usage versus performance. Increase the 'max_allowed_packet' setting to at least 4 megabytes. Also make sure that your collation and character set is using full UTF-8 support. Please view MySQL_full_unicode_support#Steps_to_upgrade.
  • If you are going to use Master/Slave replication, you must add binlog_format = 'ROW' into your my.cnf within [mysqld]. Otherwise, Moodle will not be able to write to the database.

Creating Moodle database

These are the steps to create an empty Moodle database. Substitute your own database name, user name and password as appropriate.

The instructions assume that the web server and MySQL server are on the same machine. In this case the 'dbhost' is 'localhost'. If they are on different machines substitute the name of the web server for 'localhost' in the following instructions and the 'dbhost' setting will be the name of the database server. Databases have a "Character set" and a "Collation". For Moodle the Character set should be utf8 and the Collation utf8_unicode_ci. You may get the option to set these values when you create the database. If you are not given a choice, the default options are probably good. An install on an old server may have the wrong settings.

Command line

  • To create a database using the 'mysql' command line client, first log into MySQL
$ mysql -u root -p
Enter password: 

(Enter the password you previously set - or been given - for the MySQL 'root' user). After some pre-amble this should take you to the mysql> prompt.

  • Create a new database (called 'moodle' - substitute your own name if required). We recommend you use utf8mb4_unicode_ci for Collation.
mysql> CREATE DATABASE moodle DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
  • Add a user/password with the minimum needed permissions:
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,CREATE TEMPORARY TABLES,DROP,INDEX,ALTER ON moodle.* TO moodleuser@localhost IDENTIFIED BY 'yourpassword';

...which creates a user called 'moodleuser' with a password 'yourpassword'. Make sure you invent a strong password and resist the temptation to 'GRANT ALL'.

phpMyAdmin

phpMyAdmin is a web based administration tool for MySQL. If this is available you can use it to create a new database. Make sure that you select 'UTF8' as the default character set.

Which database belongs to which Moodle

If you have installed several Moodle installations on the same server, there will be several databases in your MySQL server. The names might be quite poor reflections of the content like _mdl1 _mdl2 _mdl3 . So how do I see which database goes with which Moodle installation? You can go in with phpMyAdmin and in the various databases check for the table "mdl_course". There you will easily see the name of that Moodle Installation. In table mdl_config you can see the Moodle version. The main URL for the site is not in the database except where there are absolute links.

See also