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.