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

MySQL full unicode support: Difference between revisions

From MoodleDocs
(Clarify upgrade steps)
No edit summary
Line 56: Line 56:
$ php admin/cli/mysql_collation.php --collation=utf8mb4_unicode_ci
$ php admin/cli/mysql_collation.php --collation=utf8mb4_unicode_ci
</pre>
</pre>
'''NOTE: For large sites, this script will be slow. It is recommended to dump and reimport your data according to https://docs.moodle.org/33/en/Converting_your_MySQL_database_to_UTF8#Default_Mysql_character_set'''
* Adjust the $CFG->dboptions Array in your '''config.php''' to make sure that Moodle uses the right Collation when connecting to the MySQL Server:  
* Adjust the $CFG->dboptions Array in your '''config.php''' to make sure that Moodle uses the right Collation when connecting to the MySQL Server:  
<pre>
<pre>

Revision as of 22:23, 28 March 2017

UTF-8

UTF-8 is a character encoding that most websites use. It encodes each of the 1,112,064 valid code points. To store all of this information, four bytes is required. The most popular values are in the three byte region. MySQL by default only uses a three byte encoding and so values in the four byte range (eg. Asian characters and Emojis) can not be stored. Any attempt to enter a text that contains four byte characters will result in a Moodle database error.

MySQL does provide full four byte UTF-8 support, but it requires certain database settings to be configured. From version 3.3 on Moodle uses full UTF-8 for both MySQL and MariaDB by default. Existing databases will still run with partial support, but it is recommended to move over to full support.

Moodle comes with a Command Line Interface (CLI) script for converting to full UTF-8 for MySQL (and MariaDB). Before Moodle versions 3.1.5 and 3.2.2 this conversion tool would only change the Collation to some variant of 'utf8_bin'. 'utf8_unicode_ci' was the recommended Collation. We now recommend using 'utf8mb4_unicode_ci' which supports four byte characters (utf8_unicode_ci only supports three).

This script will attempt to change the database Collation, Character set, default table settings and column definitions.

To summarise:

  • Fresh installs of Moodle 3.1.5 and 3.2.2 onwards will use utf8mb4 by default, if the database server is configured appropriately (see below).
  • Sites upgrading to Moodle 3.1.5 or 3.2.2 can use the script to update to utf8mb4. In Moodle 3.3 a warning will show that the database isn't using full UTF-8 support and suggest moving to 'utf8mb4_unicode_ci', but you may choose to keep using 'utf8_*'.

File format

To allow for large indexes on columns that are a varchar, a combination of settings needs to be set. The file format for the system needs to be using "Barracuda". This allows for the row format to be set to "Compressed" or "Dynamic". To enable this setting see the upgrade steps listed below.

File per table

To enable this setting see the upgrade steps listed below.

Large prefix

This in conjunction with the row format being either "Compressed" or "Dynamic" allows for large varchar indexes above 191 characters. To enable this setting see the upgrade steps listed below.

Steps to upgrade

Most important: Please backup your database before making any changes or running the CLI script.

  • Change configuration settings for MySQL (exactly the same for MariaDB). This step is optional. You can run the script and it will try and make these changes itself. If errors occur then try manually changing these settings as listed below.
    • On Linux based systems you will want to alter my.cnf. This may be located in '/etc/mysql/'.
    • Make the following alterations to my.cnf:

[client] default-character-set = utf8mb4

[mysqld] innodb_file_format = Barracuda innodb_file_per_table = 1 innodb_large_prefix

character-set-client-handshake = FALSE character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci

[mysql] default-character-set = utf8mb4

  • Restart your MySQL server.
  • Run the CLI script to convert to the new Character set and Collation (requires Moodle 3.1.5, 3.2.2 or newer):
$ php admin/cli/mysql_collation.php --collation=utf8mb4_unicode_ci

NOTE: For large sites, this script will be slow. It is recommended to dump and reimport your data according to https://docs.moodle.org/33/en/Converting_your_MySQL_database_to_UTF8#Default_Mysql_character_set

  • Adjust the $CFG->dboptions Array in your config.php to make sure that Moodle uses the right Collation when connecting to the MySQL Server:
$CFG->dboptions = array(
  …
  'dbcollation' => 'utf8mb4_unicode_ci',
  …
);
  • Try adding some Emojis 🇦🇺👏🎓 to your Moodle site to verify that the upgrade was successful.