简体   繁体   English

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

[英]How to connect to MongoDB on EC2 using Java driver

I followed tutorial http://www.jcraft.com/jsch/examples/PortForwardingL.java.html and http://www.jcraft.com/jsch/examples/UserAuthPubKey.java.html and I know how to connect to EC2 Ubuntu instance via SSH using pem file as a key. 我遵循了http://www.jcraft.com/jsch/examples/PortForwardingL.java.htmlhttp://www.jcraft.com/jsch/examples/UserAuthPubKey.java.html的教程,我知道如何连接到EC2使用pem文件作为密钥通过SSH的Ubuntu实例。 I can interact with EC2 instance in IntelliJ console as well as in putty. 我可以在IntelliJ控制台以及腻子中与EC2实例进行交互。 But I want to connect to MongoDB and use command described here . 但是我想连接到MongoDB并使用此处描述的命令。 I tried to use new MongoClient with localhost and ec2 address with port 22 and 27017, but every combination failed. 我尝试将新的MongoClient与localhost和ec2地址(端口22和27017)一起使用,但是每种组合均失败。

This is output from console: 这是从控制台输出的:

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

And this is my code: 这是我的代码:

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();
        }
    }
}

Can someone help me? 有人能帮我吗?

You can set up SSH tunneling on your local computer to connect through port 22 like any SSH-enabled client eg Robomongo or IntelliJ do. 您可以在本地计算机上设置SSH隧道以像任何启用SSH的客户端(例如Robomongo或IntelliJ)一样通过端口22连接。 But it is a hassle. 但这是一个麻烦。

Unless your network security specifically prohibits opening port 27017 or whatever you are running Mongo on, just open it in EC2 (Security Groups). 除非您的网络安全明确禁止打开端口27017或您在其上运行Mongo的任何端口,否则请在EC2(安全组)中将其打开。 All basic database security precautions apply ie you should be connecting as a limited-permission application-, not "root"-level user, and of course Mongo should be started with auth=true. 所有基本的数据库安全预防措施均适用,即您应该以受限权限的应用程序级别而非“ root”级别的用户身份进行连接,并且当然,Mongo应该以auth = true开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM