mysqlconnection-stringutf8mb4

Mysql utf8mb4 connection string (Node.js, serverless-mysql package)


I've changed my Mysql defaults to utf8mb4, however emojis are still getting refused by mysql.

Error: ER_TRUNCATED_WRONG_VALUE_FOR_FIELD: Incorrect string value: '\xF0\x9F\x92\xA9</...'

I'm thinking this might be because of my connection string.

I'm connecting and sending queries to Mysql with the serverless-mysql package, which is based on the mysql package.

I think I should be able to create a connection as so:

var connection = mysql.createConnection('mysql://user:pass@host/db?debug=true&charset=BIG5_CHINESE_CI&timezone=-0700');

However I'm not entirely sure what my connection string should be for me to support utf8mb4 and be able to save emojis to my database. What should my string look like?


Solution

  • Turns out the package itself supports adding a setting for charset. So I changed my config to:

    const db = mysql({
      config: {
        host: process.env.DB_HOST,
        database: process.env.DB_NAME,
        user: process.env.DB_USER,
        password: process.env.DB_PASS,
        charset: 'utf8mb4'
      }
    });
    

    And I can now send emojis to Mysql without errors.