22

I am trying to take mysql dump with command:

mysqldump -u xxxx -p dbxxx > xxxx270613.sql

what is command to take mysqldump with UTF8 ?

3
  • What do you mean by "take mysqldump with UTF8"? Whats the problem?
    – Nico Haase
    Mar 20, 2018 at 8:40
  • Are you looking for something like makandracards.com/makandra/…?
    – Nico Haase
    Mar 20, 2018 at 8:41
  • @NicoHaase I was facing problem when I was importing sql file. There are some restrictions with other character set's. So There is standard with UTF8 and It fixes lot of mysql import issues.
    – flik
    Mar 21, 2018 at 3:09

3 Answers 3

31

You can use the following:

mysqldump -u [username] –p[password] --default-character-set=utf8mb4 -N --routines --skip-triggers --databases [database_name] > [dump_file.sql]
5
  • 17
    Never use utf8; as of 2021 use nothing less than utf8mb4.
    – John
    May 13, 2021 at 0:46
  • 1
    @John Why? Can you please explain? Oct 26, 2021 at 14:51
  • 3
    @RaulChiarella You need to use utf8mb4, not utf or a lot of UTF characters will get corrupted. Example: HTTP GET Request: SET CHARACTER SET 'utf8mb4', HTTP POST Requests: SET NAMES 'utf8mb4'.
    – John
    Oct 27, 2021 at 9:29
  • Thanks. I will use utf8mb4 from now on. Oct 27, 2021 at 18:27
  • 1
    Here's an explanation of utf8 (utff8mb3) vs utf8mb4: stackoverflow.com/a/30074553
    – scharette
    Jan 26 at 18:40
20

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

2
  • 1
    Thank you for this ! It fixed my issue. Jun 24, 2020 at 5:47
  • 1
    Wow, incredible answer, upvoted! It's unbelievable that setting default-character-set=utf8 would actually cause utf-8 corruption, thank you! Nov 10, 2021 at 18:59
14

--default-character-set=utf8 is the option you are looking for the one can be used together with these others:

mysqldump --events \
 --routines \
 --triggers \
 --add-drop-database \
 --compress \
 --hex-blob \
 --opt \
 --skip-comments \ 
 --single-transaction \
 --skip-set-charset \
 --default-character-set=utf8 \
 --databases dbname > my.dump

Also, check the --hex-blob it helps to dump binary strings in hexadecimal format, so I can guaranty (be more portable) making the import to work.

The --databases option causes all names on the command line to be treated as database names. Without this option, mysqldump treats the first name as a database name and those following as table names.

With --all-databases or --databases, mysqldump writes CREATE DATABASE and USE statements prior to the dump output for each database. This ensures that when the dump file is reloaded, it creates each database if it does not exist and makes it the default database so database contents are loaded into the same database from which they came. If you want to cause the dump file to force a drop of each database before recreating it, use the --add-drop-database option as well. In this case, mysqldump writes a DROP DATABASE statement preceding each CREATE DATABASE statement.

This helps to restore using:

# mysql < dump.sql

Instead of:

# mysql dbname < dump.sql 
1
  • 11
    Never use utf8; as of 2021 use nothing less than utf8mb4.
    – John
    May 13, 2021 at 0:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.