天天看点

Java在线备份和还原MySQL数据库。

2018年6月29日14:00:48

阅读数:1534

  今天整了整整一整天,终于使用Java在线备份和还原MySQL数据库了,哎,备份倒是很快,就是在还原的时候遇到了一个问题,也不报错,结果将sql语句放到cmd中一运行才知道是编码的问题,下面代码。

首先将将Mysql的环境变量配上:你的mysql安装目录\bin;,放到系统变量中path变量中,最好是开始Mysql的远程连接的功能,将mysql数据库中的user表中的你连接mysql的用户的host字段设置为%号。

  1. package com.hancai.base.admin.action;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.io.OutputStream;
  10. import java.io.OutputStreamWriter;
  11. public class Test {
  12. public static void main(String[] args) throws IOException{
  13. backup("d:\\d.sql");
  14. recover("d:\\d.sql");
  15. }
  16. public static void backup(String path) throws IOException{
  17. Runtime runtime = Runtime.getRuntime();
  18. //-u后面是用户名,-p是密码-p后面最好不要有空格,-family是数据库的名字
  19. Process process = runtime.exec("mysqldump -u root -p123456 family");
  20. InputStream inputStream = process.getInputStream();//得到输入流,写成.sql文件
  21. InputStreamReader reader = new InputStreamReader(inputStream);
  22. BufferedReader br = new BufferedReader(reader);
  23. String s = null;
  24. StringBuffer sb = new StringBuffer();
  25. while((s = br.readLine()) != null){
  26. sb.append(s+"\r\n");
  27. }
  28. s = sb.toString();
  29. System.out.println(s);
  30. File file = new File(path);
  31. file.getParentFile().mkdirs();
  32. FileOutputStream fileOutputStream = new FileOutputStream(file);
  33. fileOutputStream.write(s.getBytes());
  34. fileOutputStream.close();
  35. br.close();
  36. reader.close();
  37. inputStream.close();
  38. }
  39. public static void recover(String path) throws IOException{
  40. Runtime runtime = Runtime.getRuntime();
  41. //-u后面是用户名,-p是密码-p后面最好不要有空格,-family是数据库的名字,--default-character-set=utf8,这句话一定的加
  42. //我就是因为这句话没加导致程序运行成功,但是数据库里面的内容还是以前的内容,最好写上完成的sql放到cmd中一运行才知道报错了
  43. //错误信息:
  44. //mysql: Character set \'utf-8\' is not a compiled character set and is not specified in the \'
  45. //C:\Program Files\MySQL\MySQL Server 5.5\share\charsets\Index.xml\' file ERROR 2019 (HY000): Can\'t
  46. // initialize character set utf-8 (path: C:\Program Files\MySQL\MySQL Server 5.5\share\charsets\),
  47. //又是讨人厌的编码问题,在恢复的时候设置一下默认的编码就可以了。
  48. Process process = runtime.exec("mysql -u root -p123456 --default-character-set=utf8 family");
  49. OutputStream outputStream = process.getOutputStream();
  50. BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
  51. String str = null;
  52. StringBuffer sb = new StringBuffer();
  53. while((str = br.readLine()) != null){
  54. sb.append(str+"\r\n");
  55. }
  56. str = sb.toString();
  57. System.out.println(str);
  58. OutputStreamWriter writer = new OutputStreamWriter(outputStream,"utf-8");
  59. writer.write(str);
  60. writer.flush();
  61. outputStream.close();
  62. br.close();
  63. writer.close();
  64. }
  65. }

下载地址:Java在线备份和还原MySQL数据库

转载请注明出处,谢谢!