我们如何使用带有身份验证的ProcessBuilder(java)启动mongo作为进程。
我的mongo服务器启用了身份验证。 早期的代码是。
String osName = System.getProperty("os.name");
String executableName = (osName != null && osName.contains("indows")) ? "mongo.exe" : "mongo";
String[] commandLine = new String[]{
PathUtil.findExecutable(executableName),
"localhost" + ":" + "27017" + "/" + "test", getScriptPath(path)
};
ProcessBuilder processBuilder = new ProcessBuilder(commandLine);
processBuilder.directory(new File(getScriptPath(path)).getParentFile());
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();
如何在身份验证中添加用户名/密码?
答案 0 :(得分:0)
将用户/密码添加到mongodb网址
String[] commandLine = new String[]{
PathUtil.findExecutable(executableName), "user:password@localhost:27017/test", getScriptPath(path)
};