i just did a plain old mysql dump with something like this
mysqldump -h "192.168.0.6" -uroot -p db_name > my_sexy_dump_file.sql.
What happened is t开发者_JAVA百科hat dumping the database produced a file where all my utf-8 string were converted to latin1. at least it seems to me that that was done cause now i have 2 letters for each one i had previously. the result looks like this:
Կենտրոնական գրասենյակ
Ինեկոբանկում
the original text was armenian.
how can i fix this? executing the dump file produces an error. copy and pasting insert queries into an mysql client application like sqlyog just writes the corrupted string into the table.
This is how I do it to avoid losing charset information:
- Consider using the
-r
flag to mysqldump so you don't have to pipe the output through stdout->stdin - Make sure you use the
--set-charset
flag (or--opt
) so the dump file contains aSET NAMES
command. - Import using the
SOURCE filename
command.
Here's my full dump script (for Bash since I don't run Windows, but you can probably get some good stuff out of them):
#!/bin/bash
# Version 2.6
# Dump a database to file, to be used in migration or as a backup
# Emil Vikstrom 2009-2010
DB="$1";
FILENAME="$DB.$(date -I)"
TMP="$FILENAME.sql"
I=1
while [ -e "$TMP" ] || [ -e "$TMP.gz" ]; do
let I=$I+1
TMP="$FILENAME.$I.sql"
done
FILENAME="$TMP"
echo "[$(date +%R)] Dumping..."
mysqldump --single-transaction --hex-blob --add-drop-table --opt -e --quick --quote-names -r "$FILENAME" "$DB"
echo "[$(date +%R)] Compressing..."
gzip "$FILENAME"
echo "[$(date +%R)] Done: $FILENAME.gz"
And here's a matching import script:
#!/bin/bash
# Import a database SQL file
# Emil Vikstrom 2008
# Version 2.1
DUMPFILE="$1"
DATABASE="$2"
if [[ $DUMPFILE =~ "(.*)\.gz" ]]; then
if [ -e $DUMPFILE ]; then
echo "[$(date +%R)] Unzipping..."
gunzip "$DUMPFILE"
fi
DUMPFILE="${BASH_REMATCH[1]}"
fi
echo "[$(date +%R)] Importing $DUMPFILE..."
echo "SOURCE $DUMPFILE;" | mysql "$DATABASE"
echo "[$(date +%R)] Done"
I fixed this by creating a map of the whole alphabet and replacing all corrupted characters one by one. more details on what i did here: http://fictionalrealm.com/coding/2011/05/29/mysqldump-corrupted-utf8-data-when-dumping-on-windows-machine/
The problem is a bit more complicated when dealing with dumps via powershell. If you have iconv you can try fixing the encoding with something like this:
"C:\Program Files (x86)\GnuWin32\bin\iconv.exe" -f ISO-10646-UCS-2 -t 437//TRANSLIT "D:\all-db-dump.sql" > "D:\out.sql"
As instructed here: http://devmd.com/r/fixing-corrupt-mysql-dumps-windows-10-power-shell
精彩评论