Full introduction to node. js mysql-native Chinese garbled characters

Source: Internet
Author: User

I read a performance comparison article and decided to use mysql-native as the database connector of the project based on the actual situation. Using npm install mysql-native, we made a simple test connection [html] mysql = require ('mysql-native ') [html] dbConn = mysql. createTCPClient ('*******'); dbConn. auto_prepare = true; var auth = dbConn. auth ('*****', '*****', '*****'); // Database Name, user name, and password dbConn. set ('row _ as_hash ', false); the mysql show processlist shows that the connection is normal. The first thing is to test the reading and writing of Chinese characters and create a table named test_cn. Given the compatibility of each operating system, the project's database is configured as a UTF-8. [Html] create table 'test _ cn' ('A' varchar (20) default null) ENGINE = InnoDB default charset = utf8 write code [html] var SQL = "insert into test_cn (aa) values ('my')"; dbConn. query (SQL); Set Character Set found to be garbled, Baidu according to the increase of https://github.com/vfasky/node-mysql-sexy-query/commit/a2bd1bb5fba923ced3a832d584f2a8f3205b5563 [html dbConn. set ('charset', 'utf8 _ general_cs '); Re-execute and check whether the code is garbled. If it is suspected that the mysql-native code has not been modified to make it take effect, then directly go to node_module. In s/mysql_native/lib, locate auth. js reads charset settings, line 67 [html] this. client. charset = this. client. get ('charset ')? Charset. by_name (this. client. get ('charset'): charset. by_number (c. serverStatus. serverLanguage); but in query. js line: 105 lines, found this. connection. charset is empty, so [html] this. client. charset = this. client. get ('charset ')? .............. Modify [very critical] [html] this. client. connection. charset = this. client. get ('charset ')? Charset. by_name (this. client. get ('charset'): charset. by_number (c. serverStatus. serverLanguage); <span style = "white-space: pre"> </span> this. client. charset = this. client. get ('charset ')? Charset. by_name (this. client. get ('charset'): charset. by_number (c. serverStatus. serverLanguage); that is, replace the character set combination of client and connection. finally, charset is executed. the execution of js methods found that MYSQL connection properties are still garbled [html] var d = dbConn. query ("show variables like '% char %'"); function dump_rows (cmd) {cmd. addListener ('row', function (r) {console. dir (r) ;}) ;}dump_rows (d); from the output, [html] ['character _ set_client ', 'latin1'] ['character _ se T_connection ', 'latin1'] ['character _ set_database', 'utf8'] ['character _ set_filesystem ', 'binary'] ['character _ set_results ', 'latin1'] ['character _ set_server ', 'utf8'] ['character _ set_system', 'utf8'] to locate the problem, the mysql Server considers that the mysql-native module is connected Based on latin1, so [html] dbConn. query ("SET character_set_client = utf8, character_set_connection = utf8"); the written Chinese is correct.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.