Back to blog

Moodle charset fix

2/28/2025 · 1 min · Moodle

Share

Moodle is an open-source LMS widely used by institutions and educators for virtual learning environments. The name stands for Modular Object-Oriented Dynamic Learning Environment.

One critical point in Moodle operations is database encoding. Charset issues can lead to:

Even with debug enabled, root cause often appears only in server error_log. If the issue is charset/collation related, the flow below is usually effective.

Backup system and database

Always back up before changing anything.

Example with a test clone:

This can be done from jailed shell without root.

Check current charset/collation

mysql -e "USE banco_01_fix; SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%';"

Typical output:

utf8mb4_general_ci

Change charset/collation

Moodle recommends utf8mb4_unicode_ci.

Edit Moodle config.php and temporarily point to clone DB:

$CFG->dbname = 'banco_01_fix'; // original was banco_01

Run Moodle collation conversion:

php admin/cli/mysql_collation.php --collation=utf8mb4_unicode_ci
Warning: this may take time on large databases.

Set collation in config.php:

$CFG->dboptions = array(
  'dbcollation' => 'utf8mb4_unicode_ci',
);

Force MySQL UTF-8 (optional)

Warning: use only as a last resort.

Edit /etc/mysql/my.cnf:

[client]
default-character-set = utf8mb4

[mysqld]
character_set_server = utf8mb4
collation_server = utf8mb4_unicode_ci
skip-character-set-client-handshake

Then repair/optimize databases:

mysqlcheck -u root -p --auto-repair --optimize --all-databases

Important notes

CC BY-NC

This post is licensed under CC BY-NC.

Comments

Join the discussion below.