天天看點

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資料庫

轉載請注明出處,謝謝!