Garbled characters for Database Backup Using mysqldump

Source: Internet
Author: User

Recently, in the project, because the system needs to provide the database backup function, after some online searches, I think the simple mysqldump is used.

Using mysqldump in Linux to back up a MySQL database as an SQL File

Use mysqldump in Linux to regularly back up MySQL Databases

(1) java code

String backupSQL = "cmd/c mysqldump-urootdd -- extended-insert = false-hlocalhost fts>"
+ DB_BACKUPFILE_PATH + "";
Runtime runtime = Runtime. getRuntime ();
System.out.println(runtime.exe c (backupSQL ));

The Chinese part of the file generated by executing the above Code segment has garbled characters. At first, it is suspected that it is related to the database encoding. Therefore, you can query the database encoding in the following ways:

(2)> show variables like 'character % ';

After the above steps, we can confirm that the encoding of the database is correct. After the online search again, we can find the following conclusion:

"If the character set on the MySQL server is latin1 or another one, Chinese characters from mysqldump are garbled! A simple method is to add the default character set options, such:

Mysqldump [-h 127.0.0.1-P 3307]-u username -- default-character-set = gbk-p databasename> dumpfile.txt,

-- When the default-character-set knows that the data content is Chinese, it can be specified as gbk, so that even if the database itself sets the character set to the Chinese language of the file latin1, it can be normal! ", And finally change the code to the following form:

(3) Final java code

String backupSQL = "cmd/c mysqldump-urootdd -- default-character-set = gbk -- opt -- extended-insert = false-hlocalhost fts>"
+ DB_BACKUPFILE_PATH + "";
Runtime runtime = Runtime. getRuntime ();
System.out.println(runtime.exe c (backupSQL ));

After the above modification, the exported files are displayed normally in Chinese.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.