spier spier - 21 days ago 5
MySQL Question

MySQL command line formatting with UTF8

I have a database table that contains Swedish/Norwegian strings.

When I query some data, I get output like this:

Output with
set names latin1;



+-----------------------------------+
| name |
+-----------------------------------+
| Kid Interi##### |
| Bwg Homes |
| If Skadef####kring |
| Jangaard Export |
| Nordisk Film |
+-----------------------------------+


Now if I
set names utf8;
in order to see the characters with their proper encoding, then the formatting of the tabular output of the MySQL command line breaks.

Output with
set names utf8;



+-----------------------------------+
| name |
+-----------------------------------+
| Kid Interiør |
| Bwg Homes |
| If Skadeförsäkring |
| Jangaard Export |
| Nordisk Film |
+-----------------------------------+


Question:



This is not a big issue but it makes the output a bit harder to read. Does anybody know how to keep the tabular formatting intact?

Answer

Start the client with option --default-character-set=utf8.

mysql --default-character-set=utf8

This forces the character_set_client, character_set_connection and character_set_results variables to be UTF8.

More info on connection character sets here.

You can set it as a default option in your my.cnf file, in the [mysql] section to be included automatically each time you invoke the mysql client.

[mysql]
default-character-set=utf8