如何使用Java驱动程序在EC2上连接MongoDB

时间:2015-06-06 22:18:59

标签: java mongodb ssh amazon-ec2

我遵循了教程http://www.jcraft.com/jsch/examples/PortForwardingL.java.htmlhttp://www.jcraft.com/jsch/examples/UserAuthPubKey.java.html,我知道如何使用pem文件作为密钥通过SSH连接到EC2 Ubuntu实例。我可以在IntelliJ控制台和putty中与EC2实例进行交互。但我想连接到MongoDB并使用描述here的命令。我尝试使用带有localhost的新MongoClient和带端口22和27017的ec2地址,但每个组合都失败了。

这是从控制台输出的:

INFO: Cluster created with settings {hosts=[ec2Instance:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
com.mongodb.diagnostics.logging.JULLogger log
INFO: Exception in monitor thread while connecting to server ec2Instance:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.connection.SocketStream.open(SocketStream.java:63)
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:114)
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50)
    at com.mongodb.connection.SocketStream.open(SocketStream.java:58)
    ... 3 more

这是我的代码:

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;
import com.mongodb.MongoClient;


public class Connection {

    private String pathToKey = "path to pem file";
    private String user = "ubuntu";
    private String hostname;

    private int tunnelLocalPort = 22;
    private int tunnelRemotePort = 27017;


    public Connection(String hostname) {
        this.hostname = hostname;
        createConnection();
    }


    private void createConnection() {
        JSch JavaSecureChannel = new JSch();

        try {
            Session session = JavaSecureChannel.getSession(user, hostname, tunnelLocalPort);
            UserInfo userInfo = new OwnUserInfo();

            JavaSecureChannel.addIdentity(pathToKey);
            session.setUserInfo(userInfo);

            session.connect();
            session.setPortForwardingL(tunnelLocalPort, "host", tunnelRemotePort);

            MongoClient client = new MongoClient("ec2Instance");  

            com.jcraft.jsch.Channel channel = session.openChannel("shell");
            channel.setInputStream(System.in);
            channel.setOutputStream(System.out);
            channel.connect();
            // these four lines connect to terminal and I can write commands into IntelliJ console

        } catch (JSchException e) {
            e.printStackTrace();
        }
    }
}

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

您可以在本地计算机上设置SSH隧道,以便像任何支持SSH的客户端一样通过端口22连接,例如Robomongo或IntelliJ做。但这很麻烦。

除非您的网络安全明确禁止打开端口27017或您正在运行Mongo的任何内容,否则只需在EC2(安全组)中打开它即可。所有基本的数据库安全预防措施都适用,即您应该作为有限权限应用程序进行连接,而不是“root”级用户,当然Mongo应该以auth = true启动。