【问题标题】:Trying to get a db backup from different pc on LAN network generates empty sql file尝试从 LAN 网络上的不同 pc 获取 db 备份会生成空 sql 文件
【发布时间】:2018-11-06 11:50:50
【问题描述】:

当我尝试从另一台 PC(称为 pac 1)获取数据库备份时,该 PC 使用 mysqldump 连接到与访问计算机(称为 pac 2)服务器到本地计算机的同一本地网络

数据库在 PC 1 上,我想通过执行 java 程序在 PC 上生成数据库备份。目前我实现了这一点,但是有一个错误,所以创建了一个空的 SQL 文件。

  • userPathForDbBackup 是一个字符串,它通过文件选择器从用户获取路径

  • mySqlPath 是一个变量,它是 pc2 上的文件路径

String mySqlPath="C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump.exe";
String name="name";
String pathFinal=userPathForDbBackup + "\\Backup_" + name + ".sql";
String[] command = {mySqlPath,"mysqldump", "-h"+dbh, "-u "+dbUser, "-p" + dbPass, dbName,">"+pathFinal};
try{
  connection = create_connection.jdbc.getCon();
  String executeCmd = "";
  if(connection!=null){
    executeCmd = "mysqldump -h"+dbh+"-u"+dbUser+" -p"+dbPass+" "+dbName+"file.sql" ;
    System.out.println();
    ProcessBuilder pb = new ProcessBuilder(command);

    Process runtimeProcess =pb.start();
    // Process runtimeProcess =Runtime.getRuntime().exec(command);
    int processComplete = runtimeProcess.waitFor();
    if(processComplete == 0){
      System.out.println("Backup taken successfully");
    } else {
      System.out.println("Could not take mysql backup");
    }
  } else{
    System.out.println("connection not sucess");
  }
}catch (Exception e) {
  e.printStackTrace();
}

这不会引发任何异常,但会在用户选择的目录中创建一个空的 SQL 文件。

【问题讨论】:

  • “有一个错误”,据我们所知,就是代码找到的数据库
  • @Stultuske 对不起,我没有得到你..
  • 据我们所知,代码运行良好,但找不到任何/数据库
  • @Stultuske 但是程序退出代码是-1..你有什么修复建议吗..谢谢
  • 你调试过你的代码吗?即使在此处格式化也行不通,因此我不会对您未显示的代码或此代码的详细信息做进一步的假设

标签: java mysql database-backups


【解决方案1】:
 String dbName = "";
            String dbUser = "";
            String dbPass = "";
            String dbh = "";
            String dbport = "";
            Connection connection;


//userPathForDbBackup is user selected path from file chooser


            String pathFinal=userPathForDbBackup + "\\Backup_" + name + ".sql";
            System.out.println(userPathForDbBackup+"before");
            userPathForDbBackup= userPathForDbBackup+"\\Backup_name.sql";
            System.out.println(userPathForDbBackup+"after");

            String folderPath = userPathForDbBackup.substring(0, userPathForDbBackup.lastIndexOf("\\")) ;


            File path =new File(userPathForDbBackup);

            String[] executeCmd = new String[]{"C:/Program Files/MySQL/MySQL Server 5.6/bin/mysqldump.exe", "--user=" + dbUser, "--password=" + dbPass,""+ dbName,"-r"+path};

            try{

                connection = create_connection.jdbc.getCon();

                if(connection!=null){
                    Process runtimeProcess =Runtime.getRuntime().exec(executeCmd);
                    int processComplete = runtimeProcess.waitFor();
                    if (processComplete != 0) { //4
                        // something went wrong
                        InputStream errorStream = runtimeProcess.getErrorStream();
                        byte[] buffer = new byte[errorStream.available()];
                        errorStream.read(buffer);

                        System.out.println(new String(buffer));
                    }
                } else{
                    System.out.println("connection not sucess");
                }
            }catch (Exception e) {
                e.printStackTrace();
            }

终于让这段代码工作了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2012-05-05
    • 1970-01-01
    • 2011-11-02
    • 1970-01-01
    相关资源
    最近更新 更多