Telling MySQL connection to use UTF-8 with Django - mysql

I've uploaded some data to a MySQL (5.5.15 for osx10.6) database using UTF8 encoding, though for some reason I had to specify its encoding as latin1 when I LOADed it.
I reckon this part is good because when I write to an OUTFILE, my unicode 'nu' characters come out OK in a terminal and in Vim.
However, when I look at them within a MySQL session, and when I try to edit the fields from Django admin, I get mangled characters (latin1?).
So, my question is: how to I tell a MySQL client and (especially) Django to read my database as UTF-8, the way it oughta?
At the command line, I tried
--default_character_set=utf8
and also
'SET NAMES UTF8;'
at the MySQL prompt, but they do not work.
When I look at VARIABLES LIKE 'char%', they're all set to utf8 apart from character_set_server which is latin1. If I set it to utf8.... that doesn't work either.
I'd be grateful if someone could give me some pointers here, especially about how to configure Django to talk to my database properly.
Thanks!

Add in your .cnf:
[mysqld]
character-set-server = utf8
collation-server = utf8_general_ci
skip-character-set-client-handshake

Related

Convert phpMyAdmin exported database to older version. utf8mb4 issues

I want to move my MySQL database to an older version server (5.7 to 5.1).
I get errors because it is created using utf8mb4 .
If i manually change utf8mb4 to utf8 the data become unreadable because of multilinguality.
I have access only to phpMyAdmin in both servers so I can't use mysqldump.
Any ideas?
It seems I've figured out a solution.
Use at export mysql40 compatibility mode, replace utf8mb4 with utf8 and change the character set of tables from phpmyadmin to utf8_unicode_ci.
Hope this will save some time from a fellow in future.

Using Chinese characters in mysql table

I'm trying to use Chinese characters in mysql table. On my first try I wrote something like 中文考试 and my table came back with 还有一个考试.
After research, it was suggested that I use the UTF-8 as my character set. I went back to my table and altered the character set with the command
ALTER TABLE posts CONVERT TO CHARACTER SET utf8;
I tried writing Chinese characters again and got the same result - ( 还有一个考试.).
Here is a copy of my table as it exist right now:
How can I get my table to accept Chinese characters?
After research, it was suggested that I use the UTF-8 as my character
set.
You need to make sure your entire chain from the connection, to the database, to the tables is all UTF8 clean. I have a detailed answer to a similar question here.
But in your case, check the actual MySQL server my.cnf file. The following would set the whole chain to UTF-8:
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
The my.cnf is the main MySQL configuration file. Depending on your OS it can be located in a few places. For example in Ubuntu 12.04 it can be found here:
/etc/mysql/my.cnf
So figure out where yours is located, open up that file, edit it, restart MySQL & you should be good to go.

Character encoding - MySQL 5.1.67 on CentOS 6.3

I've been looking for some hours for an answer for this one and it seems to be not yet covered. I will try to be as succinct as possible.
I have a client running a web app with some character encoding problems. Specifics:
CentOS 6.3
MySQL Server 5.1.67
ALL tables set to UTF-8
SHOW FULL COLUMNS verifies also that all text columns in each table are UTF-8
Data going into database is UTF-8
Data is served to web clients as UTF-8
No bad characters in output data; forcing browser to Latin1 causes all manner of problems
So, basically, everything is UTF-8, and everything is working perfectly.
EXCEPT... all connections to MySQL must be Latin1, or else the whole system falls apart. I have verified this with the console MySQL client, PHP/Mysql, PHP/Mysqli and the Ruby mysql gem v2.8.1. Issuing a command like "SET NAMES utf8" or using the various API methods to change the connection character set to UTF-8 will cause all multibyte characters to become garbled and unrecognizable.
At the moment there is no major problem here, except of course that using a Latin1 connection to the server does not work at all in my Ubuntu test environment and so my programs keep breaking when I move them into production. But I have a nagging feeling that something cannot possibly be right and it's going to come back and bite my client later on.
MySQL is reporting this upon initial connection to the server:
character_set_client: latin1
character_set_connection: latin1
character_set_database: latin1
character_set_filesystem: binary
character_set_results: latin1
character_set_server: latin1
character_set_system: utf8
And these are the settings that work for UTF-8 data. If I change anything else to UTF-8, multi-byte characters die a miserable death.
If anybody has any idea what is going wrong here and where I can find any better documentation on it, that would be enormously appreciated.

MySQL Accented character

Last week I migrated my db in another platform. I did a mysqldump export from phpmyadmin panel and then I imported it in a new platform using the bigdump script.
The sql dump file that I have imported was originally stored in a db that was setting in this manner:
MySQL charset: UTF-8 Unicode (utf8)
MySQL connection collation: utf8_unicode_ci
I exported the db choosing utf8 character set but if I look inside the mysqldump file for every table appears:
DEFAULT CHARSET=latin1
Now I have a problem with the accented letter (like à, è, ò, ì) that are displayed like ò , Ã...etc.
For manage my new db I use MySQL Workbench and if i prompt:
show variables like 'char%';
I see that all the values are set in utf8.
How can I solve the problem? I'm not a Mysql and db expert!
You can try changing the current character set of a table to the original:
alter table TABLE_NAME convert to character set utf8 collate utf8_unicode_ci;
Your tables seem to use latin1, despite the utf8 settings you mentioned. So you have several options here:
Take the created dump as is and send it to the server using the latin1 connection charset (not utf8). This will however create the tables with latin1 charset as they are on the source server.
Convert the dump to utf-8 if you have a tool that can do it. You have to change the charset settings for your tables in the script too, though.
Convert your tables to utf8 and do the dump again.
Combine 1 + 3, but convert your target tables instead. Useful if you cannot change the source tables.
Have you actually tried MySQL Workbench to restore the dump to your new server? I'm quite sure it should be able to handle this case with a latin1 encoded backup. See Server Administration section -> Data Import/Restore.

Loading UTF-8 encoded dump into MySQL

I've been pulling my hear out over this problem for a few hours yesterday:
I've a database on MySQL 4.1.22 server with encoding set to "UTF-8 Unicode (utf8)" (as reported by phpMyAdmin). Tables in this database have default charset set to latin2. But, the web application (CMS Made Simple written in PHP) using it displays pages in utf8...
However screwed up this may be, it actually works. The web app displays characters correctly (mostly Czech and Polish are used).
I run: "mysqldump -u xxx -p -h yyy dbname > dump.sql". This gives me an SQL script which:
looks perfect in any editor (like Notepad+) when displaying in UTF-8 - all characters display properly
all tables in the script have default charset set to latin2
it has "/*!40101 SET NAMES latin2 */;" line at the beginning (among other settings)
Now, I want to export this database to another server running on MySQL 5.0.67, also with server encoding set to "UTF-8 Unicode (utf8)". I copied the whole CMS Made Simple installation over, copied the dump.sql script and ran "mysql -h ddd -u zzz -p dbname < dump.sql". After that, all the characters are scrambled when displaying CMSMS web pages.
I tried setting:
SET character_set_client = utf8;
SET character_set_connection = latin2;
And all combinations (just to be safe, even if it doesn't make any sense to me): latin2/utf8, latin2/latin2, utf8/utf8, etc. - doesn't help. All characters still scrambled, however sometimes in a different way :).
I also tried replacing all latin2 settings with utf8 in the script (set names and default charsets for tables). Nothing.
Are there any MySQL experts here who could explain in just a few words (I'm sure it's simple after all) how this whole encoding stuff really works? I read 9.1.4. Connection Character Sets and Collations but found nothing helpful there.
Thanks,
Matt
Did you try adding the --default-character-set=name option, like this:
mysql --default-character-set=utf8 -h ddd -u zzz -p dbname < dump.sql
I had that problem before and it worked after using that option.
Hope it helps!
Ugh... ok, seems I found a solution.
MySQL isn't the culprit here. I did a simple dump and load now, with no changes to the dump.sql script - meaning I left "set names latin2" and tables charsets as they were. Then I switched my original CMSMS installation over to the new database and... it worked correctly. So actually encoding in the database is ok, or at least it works fine with CMSMS installation I had at my old hosting provider (CMSMS apparently does funny things with characters encoding).
To make it work on my new hosting provider, I actually had to add this line to lib/adodb/drivers/adodb-mysql.inc.php in CMSMS installation:
mysql_query('set names latin2',$this->_connectionID);
This is a slightly modified solution from this post. You can find the exact line there as well. So it looks like mysql client configuration issue.
SOLUTION for me:
set this option in your php file, after mysql_connect (or after mysql_select_db)..
mysql_query("SET NAMES 'utf8'");