How take mysqldump with UTF8?

I had the problem, that even with applied utf-8 flags when creating the dump I could not avoid broken characters importing a dump that was created from a DB with many text columns using latin1. Some googling and especially this site helped me to finally figure it out.

  1. mysqldump with --skip-set-charset --default-character-set=latin1 flags, to avoid MySQL attempt of reconversion and setting a charset.

  2. fix the dump by replacing the charset strings using sed on terminal

    sed -i 's/latin1_swedish_ci/utf8mb4/g' mysqlfile.sql
    sed -i 's/latin1/utf8mb4/g' mysqlfile.sql

to make sure you don't miss anything you can do grep -i 'latin1' mysqlfile.sql before step 2 - and then come up with more sed orders. Introduction to sed here

  1. create a clean DB

    CREATE DATABASE mydatabase CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;

  2. apply fixed dump


Hi please try the following.

mysqldump -u [username] –p[password] --default-character-set=utf8 -N --routines --skip-triggers --databases [database_name] > [dump_file.sql]

Tags:

Mysql