E
E
Elizaveta Cezurk2021-02-24 20:15:18
MySQL
Elizaveta Cezurk, 2021-02-24 20:15:18

Is it possible to convert latin1 to utf-8?

The hosting had latin1 encoding for a very long time, I decided to look into the database, and instead of Russian characters there were krakozyabras
I understood the reason, but I don’t know how to fix and recode latin1 to utf-8 without losing the base itself ...
How to do it right, so that nothing didn't it break?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Kuznetsov, 2021-02-25
@semechki

Save the database backup to a file

mysqldump --user=USERNAME -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset -r file.sql DATABASE_NAME

Converting to UTF-8
iconv -f utf8 -t cp1252 file.sql | iconv -f cp1251 -t utf8 | sed -e 's/latin1/utf8/g' > file-utf8.sql

Emptying the base
mysql --user=USERNAME -p --execute="DROP DATABASE DATABASE_NAME; CREATE DATABASE DATABASE_NAME CHARACTER SET utf8 COLLATE utf8_general_ci;"

Uploading the corrected database
mysql --user=USERNAME --max_allowed_packet=16M -p --default-character-set=utf8 DATABASE_NAME < file-utf8.sql

Something like this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question