1

I have a mysql db with all the tables using utf8_general_ci collation. I'm using it with my PHP website and there are completely no issues with encoding.

Now I wanted to use the data from mysql tables with my node.js program. I have used node.js mysql package. This is the code I'm using to retrieve the data from mysql db:

const db = require('mysql');
const mongoose = require('mongoose');
const Event = require('./event')

mongoose.connect("mongodb+srv://xx@clustexxxx.mongodb.net/xx?retryWrites=true&w=majority" , {      useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
    console.log('Connected to db');
})
.catch(() => {
    console.log('Connection failed!');
});

const connection = db.createConnection({
   host     : 'xx',
   user     : 'xx',
   password : 'xx',
   database : 'xx',
   charset: 'UTF8_GENERAL_CI'
});

connection.connect();

var options = {
sql: `SELECT XXXXX`, 
nestTables: false
};

connection.query(options, function (error, results, fields) {
   if (error) throw error;

   results.forEach(element => {
      console.log(element.title);
      //here I'm saving data to mongoDB
   })
});

connection.end();

Once I retrieve the data with node.js, all the special characters from German, French, polish etc are broken eg: Gl�ckstadt, Mu�bach

Any advise what should I do additionally ?

luk
  • 205
  • 3
  • 14
  • 2
    See "black diamond" in https://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored – Rick James Nov 09 '20 at 03:58
  • @RickJames thank you very much, It took me some time to find the root cause. The issue was with the server configuration. My hosting company by default sets the phpmyadmin encoding with ISO 8859-2 Central European (latin2). Event though I have my database / columns with utf8mb4/utf8 it was giving me black diamonds. It is great content you created with all these steps – luk Nov 14 '20 at 17:02

0 Answers0