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?
Short answer
Start the client with option
--default-character-set=utf8:You can set this as a default in the
/etc/mysql/my.cnffile.The short answer did not work, read below
The command above forces the
character_set_client,character_set_connectionandcharacter_set_resultsconfig variables to beutf8.In order to check the values for all the charset related config variables you can run:
The
character_set_databasegives you the character set of the current database (schema) that you are in. The schema and tables are created by default with the charset specified in thecharacter_set_server, unless it is specified explicitly in theCREATEstatement.The
character_set_servercan be changed in themy.cnffile:Additionally, tables and columns can have their own charset which might be different from their parent table or schema. To specifically check the values of each table and column in a database see this answer:
How do I see what character set a MySQL database / table / column is?
If you want to change the character set of existing tables and columns, see this answer: How to convert an entire MySQL database characterset and collation to UTF-8?
More info on connection character sets in the mysql docsumentation.
Everything is set to utf8, but I still see weird characters
Even if all the charsets variables, tables and columns are set to
utf8, there might be cases where you see weird characters on your screen. For example, somebody might have written Unicode characters in autf8column, through a client withlatin1connection (for example by runningmysql --default-character-set=latin1). In this case you need to connect to the database with the same charset as the values were written. You can also retrieve and rewrite them through the correct encoding.NOTE: As the comments point out, the mysql
utf8encoding is not a true and full implementation of UTF-8. If a full implementation of UTF-8 is needed, one can use theutf8mb4charset:More info here: What is the difference between utf8mb4 and utf8 charsets in MySQL?