如何使用Java驱动程序连接到MongoDB中的EC2连接到、如何使用、驱动程序、Java

2023-09-11 09:25:43 作者:别嗱无知当个性ゝ

我也跟着教程 http://www.jcraft.com/jsch/examples /PortForwardingL.java.html 和 HTTP://www.jcraft。 COM / jsch /例子/ UserAuthPubKey.java.html ,我知道如何使用PEM文件作为重点通过SSH连接到EC2 Ubuntu的实例。我可以在控制台的IntelliJ EC2实例以及在腻子互动。但我想连接到MongoDB的这里。我试图用新的MongoClient与本地主机和EC2地址与端口22和27017,但每一个组合的失败。

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. I can interact with EC2 instance in IntelliJ console as well as in putty. But I want to connect to MongoDB and use command described here. I tried to use new MongoClient with localhost and ec2 address with port 22 and 27017, but every combination failed.

这是从控制台输出:

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

这是我的code:

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?

推荐答案

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

You can set up SSH tunneling on your local computer to connect through port 22 like any SSH-enabled client e.g. Robomongo or IntelliJ do. But it is a hassle.

除非你的网络的安全性明确禁止打开端口27017,或任何你正在运行蒙戈,就在EC2(安全组)将其打开。所有基本的数据库安全precautions申请,即应为有限的权限来连接应用程序 - 而不是根级用户,当然蒙戈应该有权威性启动=真。

Unless your network security specifically prohibits opening port 27017 or whatever you are running Mongo on, just open it in EC2 (Security Groups). All basic database security precautions apply i.e. you should be connecting as a limited-permission application-, not "root"-level user, and of course Mongo should be started with auth=true.