I used foloowing code to back up my database from my sql which was run fine and got my back up in backup.sql.
我使用以下代码从我的sql备份我的数据库,运行正常并在backup.sql备份。
import java.io.*;
import java.lang.*;
import java.util.*;
class Backup
{
public static void main(String[] args)
{
String command = "D:\\MySQL\\bin\\mysqldump.exe -u root -proot yatin -r D:/backup.sql";
System.out.println(command);
Process runtimeProcess;
try
{
runtimeProcess = Runtime.getRuntime().exec(command);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0)
{
System.out.println("Backup created successfully");
//return true;
}
else
{
System.out.println("Could not create the backup");
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
But when I executed below code it holds me in waiting state. formarly my database name was yatin and now I want to restore it to mybackup database so I created database with name mybackup. What's going wrong here
但是当我执行下面的代码时,它让我处于等待状态。 formarly我的数据库名称是yatin,现在我想将它恢复到mybackup数据库,所以我创建了名为mybackup的数据库。这里出了什么问题
import java.io.*;
import java.lang.*;
import java.util.*;
class Restore
{
public static void main(String[] args)
{
String command = "D:\\MySQL\\bin\\mysql.exe -u root -proot mybackup< D:/backup.sql";
//String [] command = new String[]{"mysql",mybackup,"D:\\MySQL\\bin\\mysql.exe -u root -proot < D:/backup.sql";
//String[] command = new String[]{"mysql", [database], "--user=" + [username], "--password=" + [password], "-e", " source " + [absolute path to the sql file]};
//String[] command = new String[]{"mysql",mybackup, "-u=" +root, "-p="+root, "-e", " source " +D:/backup.sql};
System.out.println(command);
Process runtimeProcess;
try
{
runtimeProcess = Runtime.getRuntime().exec(command);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0)
{
System.out.println("Restore created successfully");
//return true;
}
else
{
System.out.println("Could not restore");
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
UPDATE 1
mysql> mysql -u root -proot mybackup < D:/backup.sql;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'mysql
-u root -proot mybackup < D:/backup.sql' at line 1
1 个解决方案
#1
0
This command should be outside of mysql console. You don't need to login in mysql and run this command, as mysql -u root -p
already will do it for you.
此命令应该在mysql控制台之外。你不需要登录mysql并运行这个命令,因为mysql -u root -p已经为你做了。
cmd > mysql -u root -proot mybackup < D:\backup.sql;
cmd> mysql -u root -proot mybackup
#1
0
This command should be outside of mysql console. You don't need to login in mysql and run this command, as mysql -u root -p
already will do it for you.
此命令应该在mysql控制台之外。你不需要登录mysql并运行这个命令,因为mysql -u root -p已经为你做了。
cmd > mysql -u root -proot mybackup < D:\backup.sql;
cmd> mysql -u root -proot mybackup