Bitbang3r
Bitbang3r

Reputation: 6904

Rendering Chinese/UTF8 characters in MySQL Select using PuTTY & commandline client

Is there any good, straightforward way to connect to a MySQL database using MySQL's normal commandline client while connected using PuTTY and get it to render UTF8 fields that include non-Western characters properly?

My tables use UTF8 encoding, and in normal use the values come from an Android app and are displayed by an Android app. The problem is that occasionally, Things Go Wrong, and when they do, it's almost impossible for me to figure out what's going on because MySQL's commandline client forcibly casts UTF8 values to (what appears to be) ISO-8859-1 (ie, quasi-random gibberish when shown on the screen). For what it's worth, Toad for MySQL (both free and beta) seem to mangle UTF8 output the same way.

On a semi-related note, my favorite monospaced font is Andale Mono (yeah, I really like the forcibly-disambiguated 0/O and 1/l characters). I'm pretty sure it doesn't include CJK characters. Is there any (free) utility that can be used to rip the lower 127 or 256 characters from one Truetype font (like Andale Mono), and create a new Truetype font based on some UTF8 CJK Truetype font that replaces the lower 127 or 256 characters with the font data ripped from Andale Mono?

Upvotes: 1

Views: 3506

Answers (1)

Michel Feldheim
Michel Feldheim

Reputation: 18250

First you should make sure that your console encoding is set to UTF-8. Using PuTTY you need to set the charset dropdown in "Window" > "Translation" to UTF-8

Second MySQL distincts the data charset and the connection charset. When your data is UTF-8 encoded but your connection charset is set to e.g. "ISO-8859-1" MySQL will automatically convert the output.

The easiest way to set the charsets permanently is to update your client my.cnf with the following:

[client]
default-character-set=utf8

Detailed information about the connection charset you can find here: http://dev.mysql.com/doc/refman/5.5/en/charset-connection.html

When using the MySQL API functions ( PHP client e.g. ) you can set the connection charset by sending the query

SET NAMES utf8

Various implementations of the MySQL API also support setting the charset directly. e.g. http://www.php.net/manual/en/mysqli.set-charset.php

Upvotes: 3

Related Questions