我使用Java runtime.exec成功转储了数据。
现在我想处理像" mysqldump这样的异常:收到错误:1045:访问被拒绝用户'错误' @' localhost' (使用密码:是)尝试连接时#34;
当我把错误的用户名或密码或错误的地方
但是我从process.getErrorStream()得到的是"警告:在命令行界面上使用密码可能是不安全的。"并且getInputStream()为null
在终端中,输出如下:
mysqldump -uerror -p123456 erp_sys
警告:在命令行界面上使用密码可能不安全。
mysqldump:收到错误:1045:访问被拒绝用户'错误' @' localhost' (尝试连接时使用密码:是)
我做了一些研究,但仍无法找到解决mysqldump错误的方法。有人有过这方面的经验吗?
答案 0 :(得分:0)
我不知道。它对我有用:
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import org.apache.commons.io.IOUtils;
public class MysqlTest {
public static void main(String[] args) throws Exception {
Process p = new ProcessBuilder("mysqldump", "-u", "error", "-p123456", "erp_sys").start();
ByteArrayOutputStream dmp = new ByteArrayOutputStream();
// Use FileOutputStream dmp = ... in real cases.
Thread t1 = copyStreamsInBackground(p.getInputStream(), dmp);
ByteArrayOutputStream err = new ByteArrayOutputStream();
Thread t2 = copyStreamsInBackground(p.getErrorStream(), err);
t1.join();
t2.join();
int exitCode = p.waitFor();
if (exitCode != 0) {
System.err.println("Exit code: " + exitCode);
String errors = new String(err.toByteArray(), Charset.forName("utf-8"));
System.err.println(errors);
} else {
System.out.println("Exit code: " + exitCode);
String dumps = new String(dmp.toByteArray(), Charset.forName("utf-8"));
System.out.println(dumps);
}
}
private static Thread copyStreamsInBackground(final InputStream is, final OutputStream os) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
IOUtils.copy(is, os);
os.close();
} catch (Exception ex) {
ex.printStackTrace();
throw new IllegalStateException(ex);
}
}
});
t.start();
return t;
}
}
打印:
Exit code: 2
mysqldump: Got error: 1045: Access denied for user 'error'@'localhost' (using password: YES) when trying to connect