Skip to content

Instantly share code, notes, and snippets.

@demofly
Last active May 31, 2016 10:36
Show Gist options
  • Save demofly/46b77f9e1df0695b0aae to your computer and use it in GitHub Desktop.
Save demofly/46b77f9e1df0695b0aae to your computer and use it in GitHub Desktop.
How to migrate an existing MySQL 5.5 DB from latin1 to UTF-8.
Convert old latin1 DB to a new UTF8 one:
DBS="db1 db2"
for dbname in $DBS
do
mysqldump "${dbname}" > "${dbname}"latin1.sql
cat "${dbname}"latin1.sql | sed "s#CHARSET=latin1#CHARSET=utf8#" | iconv -f iso8859-1 -t utf8 > "${dbname}"utf8.sql
done
====
Change in /etc/mysql/my.cnf:
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
#skip-character-set-client-handshake
====
Then, restart DB with new settings in my.cnf:
/etc/init.d/mysql restart
Result is
mysql> show variables like "%character%";show variables like "%collation%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_unicode_ci |
| collation_database | utf8_unicode_ci |
| collation_server | utf8_unicode_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)
====
Then, recreate the DB in a new collation and charset:
for dbname in $DBS
do
echo "drop database ${dbname}; create database ${dbname}" | mysql
done
Then, put the prepared data back:
for dbname in $DBS
do
cat "${dbname}"utf8.sql | mysql "${dbname}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment