• mysql备份数据库


    package com.pdsu.mysql.backup;
    import java.io.BufferedReader;

    import java.io.File;

    import java.io.FileInputStream;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.InputStream;

    import java.io.InputStreamReader;

    import java.io.OutputStream;

    import java.io.OutputStreamWriter;


    public class BackupTest {

    public static void main(String[] args) throws IOException{

    //backup("G:\backup\d.sql");

    recover("G:\backup\d.sql");

    }

    public static void backup(String path) throws IOException{

    Runtime runtime = Runtime.getRuntime();

    //-u后面是用户名,-p是密码-p后面最好不要有空格,-family是数据库的名字

    Process process = runtime.exec("mysqldump -u root -proot tt");

    InputStream inputStream = process.getInputStream();//得到输入流,写成.sql文件

    InputStreamReader reader = new InputStreamReader(inputStream);

    BufferedReader br = new BufferedReader(reader);

    String s = null;

    StringBuffer sb = new StringBuffer();

    while((s = br.readLine()) != null){

    sb.append(s+" ");

    }

    s = sb.toString();

    // System.out.println(s);

    File file = new File(path);

    file.getParentFile().mkdirs();

    FileOutputStream fileOutputStream = new FileOutputStream(file);

    fileOutputStream.write(s.getBytes());

    fileOutputStream.close();

    br.close();

    reader.close();

    inputStream.close();

    }

    public static void recover(String path) throws IOException{

    Runtime runtime = Runtime.getRuntime();

    //-u后面是用户名,-p是密码-p后面最好不要有空格,-family是数据库的名字,--default-character-set=utf8,这句话一定的加

    //我就是因为这句话没加导致程序运行成功,但是数据库里面的内容还是以前的内容,最好写上完成的sql放到cmd中一运行才知道报错了

    //错误信息:

    //mysql: Character set 'utf-8' is not a compiled character set and is not specified in the '

    //C:Program FilesMySQLMySQL Server 5.5sharecharsetsIndex.xml' file ERROR 2019 (HY000): Can't
    // initialize character set utf-8 (path: C:Program FilesMySQLMySQL Server 5.5sharecharsets),

    //又是讨人厌的编码问题,在恢复的时候设置一下默认的编码就可以了。

    Process process = runtime.exec("mysql -u root -proot --default-character-set=utf8 tt");

    OutputStream outputStream = process.getOutputStream();

    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));

    String str = null;

    StringBuffer sb = new StringBuffer();
    while((str = br.readLine()) != null){

    sb.append(str+" ");

    }

    str = sb.toString();

    // System.out.println(str);

    OutputStreamWriter writer = new OutputStreamWriter(outputStream,"utf-8");

    writer.write(str);

    writer.flush();

    outputStream.close();

    br.close();

    writer.close();

    }

    }

  • 相关阅读:
    Centos7上安装docker
    docker部署mysql5.6.40
    centos7上部署spring boot并保存日志
    [转载]Ocelot简易教程(一)Ocelot是什么
    浅谈Surging服务引擎中的RabbitMQ组件和容器化部署
    [转载]Surging教学视频资源汇总
    [转载]netcore 使用surging框架发布到docker
    [转载]Surging 分布式微服务框架使用入门
    [转载]Surging Demo 项目之一
    [转载]剥析surging的架构思想
  • 原文地址:https://www.cnblogs.com/shenjun/p/3301146.html
Copyright © 2020-2023  润新知