Jump to content

Recommended Posts

How do you use mysqldump to backup a utf8 encoded database?

 

Whenever I use it i get an output file in ANSI encoding.

 

I ran:

 

SHOW VARIABLES LIKE 'character_set%';

 

I get:

 

character_set_client	utf8
character_set_connection	utf8
character_set_database	utf8
character_set_filesystem	binary
character_set_results	utf8
character_set_server	utf8
character_set_system	utf8
character_sets_dir	C:\xampp\mysql\share\charsets\

Link to comment
https://forums.phpfreaks.com/topic/142236-solved-mysqldump-and-utf8-encoding/
Share on other sites

I'm using:

 

c:\xampp\mysql\bin\mysqldump.exe --default-character-set=utf8 --user=ADMINUSER --password=PASSWORD --opt DATABASENAME > c:\xampp\htdocs\webman1\backups\database\sql\database_full_backup.sql

 

Where ADMINUSER, PASSWORD, DATABASENAME are actual values.

 

This does run and work but the file character encoding is not utf8...

 

Example file output for foreign characters:

 

უსთáƒáƒ•áƒ”ლი ம௠இகழà¯à®šà¯à®šà®¿à®šà¯ŠÕ¸Ö‚Õ¿Õ¥Õ¬ Ö‡ Õ«Õ¶Õ®Õ« Õ¡Õ¶Õ

When I restore it inserts garbled text e.g:

 

უსთáƒáƒ•áƒ”ლი ம௠இகழà¯à®šà¯à®šà®¿à®šà¯ŠÕ¸Ö‚Õ¿Õ¥Õ¬ Ö‡ Õ«Õ¶Õ®Õ« Õ¡Õ¶Õ

 

instead of the actual korean, french or german characters.

c:\xampp\mysql\bin\mysqldump.exe --default-character-set=utf8 --user=ADMINUSER --password=PASSWORD --opt DATABASENAME > c:\xampp\htdocs\webman1\backups\database\sql\database_full_backup.sql

That's why it's not working -- redirection on the command-line will not deal with UTF8 properly; use the -r filename option instead.

I tried the '-r' switch as:

 

c:\xampp\mysql\bin\mysqldump.exe -r c:\xampp\htdocs\webman1\backups\database_full_backup2.sql --default-character-set=utf8 --user=USERNAME --password=PASSWORD --opt DATABASE NAME

 

Is this the correct syntax for it?

 

I get a file output for this but it's still ANSI, not UTF8.

I backed up and then restored the database using sqlyog and then reinsert the UTF8 foreign characters and it now creates a UTF-8 backups with the line:

 

c:\xampp\mysql\bin\mysqldump.exe -r c:\xampp\htdocs\webman1\backups\database\sql\database_full_backup2.sql --user=USERNAME --password=PASSWORD --opt DATABASENAME

 

Finally resolved :D.

 

BTW I'm using mysql 5.1 and the latest version of PHP.

 

Thanks to all who helped.

  • 7 months later...

Sorry to bump this old thread. I came across it when I had a similar problem, but came up with a different solution that some might find useful.

 

Tell mysqldump to output latin1 with no SET NAMES (--default-character-set=latin1 and -N parameters).  Tell mysql to also use latin1 when importing (--default-character-set=latin1).  This seems to prevent any double encoding / decoding of UTF8.

 

On linux / unix, that would look like:

mysqldump -u username -p --default-character-set=latin1 -N database > backup.sql
mysql -u username -p --default-character-set=latin1 database < backup.sql

 

See http://docforge.com/wiki/Mysqldump

 

  • 4 years later...

I had the same problem, and tried the above solutions, but not solved it for me. As i think, that this topic is in the top of Google, many people might have the same problem, so i am posting a solution (SO, xx ADMIN, TAKE IN, THAT THIS IS IMPORTANT, AND DONT WARN ME AGAIN!) :

set UTF8 correctly inside your php file, after mysql_connect (or after mysql_select_db) command.



mysql_query("SET NAMES 'utf8'");
Edited by tazosmr
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.