How can I convert entire MySQL database character-set to UTF-8 and collation to UTF-8?
相关问题
- sqlyog export query result as csv
- NOT DISTINCT query in mySQL
- MySQL: conduct a basic search
- Why sometimes there is one of more gap(s) in the v
- mySQL alter table on update, current timestamp
In case the data is not in the same character set you might consider this snippet from http://dev.mysql.com/doc/refman/5.0/en/charset-conversion.html
Here is an example:
Make sure to choose the right collation, or you might get unique key conflicts. e.g. Éleanore and Eleanore might be considered the same in some collations.
Aside:
I had a situation where certain characters "broke" in emails even though they were stored as UTF-8 in the database. If you are sending emails using utf8 data, you might want to also convert your emails to send in UTF8.
In PHPMailer, just update this line:
public $CharSet = 'utf-8';
On the commandline shell
If you're one the commandline shell, you can do this very quickly. Just fill in "dbname" :D
One-liner for simple copy/paste
The safest way is to modify the columns first to a binary type and then modify it back to it type using the desired charset.
Each column type have its respective binary type, as follows:
Eg.:
I tried in several latin1 tables and it kept all the diacritics.
You can extract this query for all columns doing this:
After you do this on all your columns then you do it on all tables:
To generate this query for all your table, use the following query:
And now that you modified all your columns and tables, do the same on the database:
Make a backup!
Then you need to set the default char sets on the database. This does not convert existing tables, it only sets the default for newly created tables.
Then, you will need to convert the char set on all existing tables and their columns. This assumes that your current data is actually in the current char set. If your columns are set to one char set but your data is really stored in another then you will need to check the MySQL manual on how to handle this.
For databases that have a high number of tables you can use a simple php script to update the charset of the database and all of the tables using the following:
If you cannot get your tables to convert or your table is always set to some non-utf8 character set, but you want utf8, your best bet might be to wipe it out and start over again and explicitly specify: