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

时间:2022-04-24 14:55:03

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

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

转载请注明出处,谢谢!