question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

mysql.createPool charset doesn't work

See original GitHub issue

I’m using node mysql to connect to database and I have the following createPool setup

const db = mysql.createPool({
    host: process.env.host,
    user: process.env.username,
    password: process.env.password,
    database: process.env.database,
    multipleStatements: true,
    charset: 'UTF8MB4_GENERAL_CI'
});

Now if I use `SHOW VARIABLES LIKE ‘character_set%’ in phpmyadmin database page I get

character_set_client	utf8mb4	
character_set_connection	utf8mb4	
character_set_database	utf8	
character_set_filesystem	binary	
character_set_results	utf8mb4	
character_set_server	latin2	
character_set_system	utf8	
character_sets_dir	/usr/share/mysql/charsets/	

On the other hand, if I use same query in node server db.query("SHOW VARIABLES LIKE 'character_set%'", ...) it returns

[
  RowDataPacket {
    Variable_name: 'character_set_client',
    Value: 'latin2'
  },
  RowDataPacket {
    Variable_name: 'character_set_connection',
    Value: 'latin2'
  },
  RowDataPacket {
    Variable_name: 'character_set_database',
    Value: 'utf8'
  },
  RowDataPacket {
    Variable_name: 'character_set_filesystem',
    Value: 'binary'
  },
  RowDataPacket {
    Variable_name: 'character_set_results',
    Value: 'latin2'
  },
  RowDataPacket {
    Variable_name: 'character_set_server',
    Value: 'latin2'
  },
  RowDataPacket {
    Variable_name: 'character_set_system',
    Value: 'utf8'
  },
  RowDataPacket {
    Variable_name: 'character_sets_dir',
    Value: '/usr/share/mysql/charsets/'
  }
]

And I’d expect them to be something related to charset I’ve set in createPool function. The issue here is that special characters returned by my database turn to � and I looked for solutions but none of them seem to work and I’m starting to think this is the issue.

Also if I use query db.query("SET character_set_client = 'utf8';" + "SET character_set_connection = 'utf8';" + "SET character_set_results = 'utf8';" + "SET character_set_server = 'utf8';" + "SHOW VARIABLES LIKE 'character_set%';", ...) it returns what I’d expect

[
    RowDataPacket {
      Variable_name: 'character_set_client',
      Value: 'utf8'
    },
    RowDataPacket {
      Variable_name: 'character_set_connection',
      Value: 'utf8'
    },
    RowDataPacket {
      Variable_name: 'character_set_database',
      Value: 'utf8'
    },
    RowDataPacket {
      Variable_name: 'character_set_filesystem',
      Value: 'binary'
    },
    RowDataPacket {
      Variable_name: 'character_set_results',
      Value: 'utf8'
    },
    RowDataPacket {
      Variable_name: 'character_set_server',
      Value: 'utf8'
    },
    RowDataPacket {
      Variable_name: 'character_set_system',
      Value: 'utf8'
    },
    RowDataPacket {
      Variable_name: 'character_sets_dir',
      Value: '/usr/share/mysql/charsets/'
    }
  ]

And also if used as multipleStatements in front of select query I’m using to get text from database, db.query("TONS OF SET VARIABLES HERE;" + "SELECT STUFF", ...) the special characters don’t turn to � anymore and it works as I think it should.

What am I missing? Why does createPool charset not affect my character_set variables? I also tried utf8, UTF8, utf8mb4, UTF8MB4 as charset.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dougwilsoncommented, Aug 19, 2021

You can use the connection pool event to run SET queries on all your pool connections: https://github.com/mysqljs/mysql#connection

0reactions
asasinmodecommented, Aug 19, 2021

Thanks a lot for detailed answer. It indeed looks like the issue is with my database hosting, so I’ll go ahead and ask them about it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mysql encoding error - node.js
So, in the end, I found the solution: I added a charset parameter to the node connection: var pool = mysql.createPool({ host ...
Read more >
Connection charset parameter ignored #508 - mysqljs/mysql
Using mysql@2.0.0-alpha7 : var mysql = require('mysql'); var client = mysql.createConnection({ host: 'localhost', port: 3306, user: '.
Read more >
Cannot use character encoding with connection parameters
mysql.jdbc.Driver(); c = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/mydb", a, "bogus"); You will receive: java.sql.
Read more >
MYSQL createPool not creating pool - JavaScript
I make a call to Mysql.createPool and it simply returns nothing. ... in favour of processes that do not require it during their...
Read more >
mysql - NodeJS problem with utf8_general_ci
node.js needs something like this to start with: var connection = mysql.createConnection({ ... , charset : 'utf8'});.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found