我在使用MysqlDump在java中创建MySQL数据库备份时遇到了一个奇怪的问题。当我在Windows的命令提示符中运行命令时,它可以轻松执行而没有任何问题并创建备份,但是当我从java应用程序执行命令时,我得到错误。 我的命令是
c:/ xampp / mysql / bin / mysqldump -h localhost --user root --password = abc --add-drop-database -B db_WebBe -r db_test.sql
我得到MySQL用户的这个错误
mysqldump:收到错误:1044:选择数据库时,用户''@ localhost'拒绝访问数据库'? - user'
我尝试过所有可以做的蠢事,包括
1) allow all the rights to ''@localhost
2) deleted user ''@localhost and then got error with 'jdbc'@localhost
3) Tried to Run Java Application with Administartor Rights (Stupid)
4) Some other codes from StackOverflow guides but all returned same error
5) Changed root password
6) Reinstalled, Updated ODBC and JDBC Drivers
6) and many others don't remember :-/
我的理解代码是
public boolean databaseBackup()
{
try
{
CreateConnection();
Process runtimeProcess;
runtimeProcess = Runtime.getRuntime().exec("c:/xampp/mysql/bin/mysqldump -h localhost –-user root –-password=abc --add-drop-database -B db_poonawala -r db_test.sql");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(runtimeProcess.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(runtimeProcess.getErrorStream()));
String s=null;
try {
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
} catch (IOException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
try {
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
} catch (IOException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {
System.out.println("5");
System.out.println("Backed up");
return true;
} else {
System.out.println("Not Backed up");
}
conn.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
return false;
}
答案 0 :(得分:0)
我终于想通了,现在我的工作代码就是这个。也许未来有人可以从中受益。除了mysqldump
命令行之外,我只是在没有任何连接的情况下执行了我的代码。这可能是因为我在另一个仍然打开的连接中使用mysqldump
。
try
{
int processComplete;
Process runtimeProcess = Runtime.getRuntime().exec("C:\\xampp\\mysql\\bin\\mysqldump -u root -pabc db_Name -r D:/backup.sql");
processComplete = runtimeProcess.waitFor();
if(processComplete==1)
{
JOptionPane.showMessageDialog(null, "Backup Failed");
}
else if(processComplete==0)
{
JOptionPane.showMessageDialog(null,"\n Backup created Successfully.");
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e);
}