Search code examples
hacklang

Change collation of AsyncMysqlClient


How can I change the collation of AsyncMysqlClient (or AsyncMysqlConnection, I am not sure which one of them) to utf8?

I read the documentation, but I cannot find any method for changing the charset. I am probably missing it, if it's actually there.


Solution

  • For some reason, we don't actually expose a way to set the encoding of an async connection in the API. It would technically be possible to use AsyncMysqlClient::adoptConnection to create the connection via MySQLi and set the encoding before passing it to the async system, but I wouldn't recommend doing it that way.

    Instead, as suggested here, you could set the encoding of the connection explicitly via SQL with:

    SET collation_connection = utf8mb4_unicode_ci;
    SET NAMES utf8;
    

    If you have access to the configuration of the MySQL server itself, you can use init_connect to have the statements run at the start of every, non-root, connection:

    [mysqld]
    init_connect='SET collation_connection = utf8mb4_unicode_ci; SET NAMES utf8;'
    default-character-set=utf8
    character-set-server=utf8
    collation-server=utf8mb4_unicode_ci