尝试从 LAN 网络上的不同电脑获取数据库备份会生成空的 sql 文件
Trying to get a db backup from different pc on LAN network generates empty sql file
当我尝试使用 mysqldump 从与访问计算机(称为 pac 2)服务器连接到同一本地网络的另一台 PC(称为 pac 1)获取数据库备份时
数据库在 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 文件。
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();
}
终于让这段代码起作用了
当我尝试使用 mysqldump 从与访问计算机(称为 pac 2)服务器连接到同一本地网络的另一台 PC(称为 pac 1)获取数据库备份时
数据库在 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 文件。
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();
}
终于让这段代码起作用了