[5.x]: MySQL DSN in config array bypasses charset

Open Beginner friendly
#20,248 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
62/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
mariadb, mysql, php
Domain
databases

Research direction

Start with DbConfig::init() in the craft config classes, which skips _updateDsn() when a DSN is supplied, so driver stays null; App::dbConfig() reads the driver from the DSN, so compare the two. getCharset() should return utf8mb4 for MySQL once the driver is resolved. Done when new DbConfig(['dsn' => 'mysql:host=localhost;dbname=example'])->getCharset() returns utf8mb4 and the driver is set, with a unit test covering this case.

Written by the indexing model from the issue text.

Description

bug repo:cms
What happened?

When a MySQL DSN is supplied through a database config array, DbConfig::$driver remains null. This causes getCharset() to return utf8 instead of normalizing it to utf8mb4.

On MariaDB, creating a table with Craft’s default collation then produces an incompatible combination:

DEFAULT CHARACTER SET = utf8 DEFAULT COLLATE = utf8mb4_unicode_ci

MariaDB rejects this with:

COLLATION 'utf8mb4_unicode_ci' is not valid for CHARACTER SET 'utf8mb3'
Steps to reproduce

This can be reproduced without connecting to a database:

$config = new \craft\config\DbConfig([
    'dsn' => 'mysql:host=localhost;dbname=example',
]);

var_dump($config->driver);       // null
var_dump($config->getCharset()); // "utf8"

For comparison, explicitly supplying the driver returns the expected charset:

$config = new \craft\config\DbConfig([
    'dsn' => 'mysql:host=localhost;dbname=example',
    'driver' => 'mysql',
]);

var_dump($config->getCharset()); // "utf8mb4"
Expected behavior

Craft should recognize the MySQL driver from the DSN and normalize the default charset to utf8mb4, consistently with the fluent dsn() method.

Additional information

DbConfig::init() skips _updateDsn() when a DSN is already supplied, leaving the driver unset. Meanwhile, App::dbConfig() reads the driver directly from the DSN, so the connection can work despite the missing configured driver.

Explicitly setting charset to utf8mb4 works around the issue.

Found while investigating Blitz #929.

Tested on Craft 5.11.5.1, but may affect other versions as well.

Craft CMS version

5.11.5.1

PHP version

No response

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions
Dominant language
PHP
Stars
3.6k
Forks
712
Avg merge
13h 5m
Merged PRs (30d)
328

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from craftcms/cms

All issues in craftcms/cms

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.