Fix mysql database encoding after import

By | 27/07/2015

After hours of research and tests, I finally managed to fix the encoding of a database after it was imported on another server. Usually an export from a server works on another server even if the database charset is different, but it seems this bug causes the issue.

This was the post that contains the right solution for this problem.
In case this link is no more available, I included the solution below:

#!/bin/bash -e

DB_HOST="$1"
DB_USER="$2"
DB_PASSWORD="$3"
DB_NAME="$4"

mysqldump -h "$DB_HOST -u "$DB_USER" -p"$DB_PASSWORD" --opt --quote-names \
--skip-set-charset --default-character-set=latin1 "$DB_NAME" > /tmp/$DB_NAME.sql

mysql -h "$DB_HOST" -u "$DB_USER" -p"$DB_PASSWORD" \
--default-character-set=utf8 "$DB_NAME" < /tmp/$DB_NAME.sql

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.