In node js I have very simple code for making requests to database:
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'user',
password: 'password',
database: "databasename"
});
connection.connect();
connection.query('SELECT * FROM main_categories', function (error, results, fields) {
if (error) throw error;
console.log(results);
});
connection.end();
I'm creating database and table with this sql script:
CREATE DATABASE databasename
CHARACTER SET utf8
COLLATE utf8_unicode_ci;
CREATE TABLE main_categories (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);
When I'm trying to select code just in mysql console everything is looks good, but when I'm trying to get results in node.js, in console logs I see it something like this:
[ RowDataPacket { id: 1, name: 'Візочки' },
RowDataPacket { id: 2, name: 'ДитÑчий транÑпорт' },
RowDataPacket { id: 3, name: 'ÐвтокріÑла' },
RowDataPacket { id: 4, name: 'Іграшки' },
RowDataPacket { id: 5, name: 'ДитÑча кімната' },
RowDataPacket { id: 6, name: 'Ð’Ñе Ð´Ð»Ñ Ð³Ð¾Ð´ÑƒÐ²Ð°Ð½Ð½Ñ' },
RowDataPacket { id: 7, name: 'ÐкÑеÑуари Ð´Ð»Ñ Ð¼Ð°Ð¼' },
RowDataPacket { id: 8, name: 'Гігієна та доглÑд' } ]
I've tried to add charset
in config object when using mysql.createConnection
and any other solutions but no results unfortunately.
UPD: It might be helpful to see database variables:
mysql> SHOW variables WHERE variable_name LIKE '%coll%' OR variable_name LIKE '%char%';
+--------------------------+-----------------------------------------------------------+
| Variable_name | Value |
+--------------------------+-----------------------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql-5.7.19-macos10.12-x86_64/share/charsets/ |
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | latin1_swedish_ci |
+--------------------------+-----------------------------------------------------------+