我在使用 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;
}