简体   繁体   English

连接到Amazon EC2上托管的mongoDb Bitnami服务器

[英]Connect to mongoDb Bitnami server hosted on Amazon EC2

So I have written a basic program with PHP and MongoDB and I tested it locally using xampp. 因此,我已经用PHP和MongoDB编写了一个基本程序,并使用xampp在本地对其进行了测试。 This all works fine. 这一切都很好。 Now I want to publish it as a website. 现在,我想将其发布为网站。

After some research I decided to use Bitnami and I am running an EC2 server on Amazon. 经过研究后,我决定使用Bitnami,并且在Amazon上运行EC2服务器。 I am able to ssh and run commands from the terminal. 我可以通过ssh并从终端运行命令。 Now I want to connect to the server and create a Mongo client. 现在,我想连接到服务器并创建一个Mongo客户端。 I am a bit puzzled and do not know how to do this exactly. 我有点困惑,不知道该怎么做。 I am using the code below. 我正在使用下面的代码。 The ssh works fine but I do not know how to set up the mongo client. ssh可以正常工作,但是我不知道如何设置mongo客户端。 The '$m = new MongoClient();' '$ m = new MongoClient();' works fine locally for localhost, I have also tried to supply to MongoClient my username and password but without success. 对于本地主机在本地工作正常,我也尝试向MongoClient提供我的用户名和密码,但没有成功。 Thank you very much for any help! 非常感谢您的帮助!

include('Net/SSH2.php');
include('Crypt/RSA.php');

$key = new Crypt_RSA();
$key->loadKey(file_get_contents('../../etc/bitnami.pem'));


$ssh = new Net_SSH2('ip_address');
if (!$ssh->login('bitnami', $key)) {
    exit('Login Failed');
}

$ssh->exec('mongo admin --username root -p <myPassword>');

$m = new MongoClient();
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->mycol;
echo "Collection selected succsessfully";

$document = array( 
   "title" => "MongoDB", 
   "description" => "database"
);

$collection->insert($document);

Update answer to Jota Martos (unable to write it as comment): Hi, thank you for your answer. 更新对Jota Martos的回答(无法写为评论):嗨,谢谢您的回答。 Yes for now I allow access from all ports and IP's for test purposes. 是的,目前,出于测试目的,我允许从所有端口和IP进行访问。 I can successfully connect via ssh but now I am stuck. 我可以通过ssh成功连接,但是现在卡住了。 I am running a Mean bitnami application and my website is running on GoDaddy server under PHP. 我正在运行Mean Meannn应用程序,并且我的网站在PHP下的GoDaddy服务器上运行。 So I want to connect with PHP to my mongoDB bitnami app that is hosted on AWS. 因此,我想用PHP连接到托管在AWS上的mongoDB bitnami应用。 So now the problem is that on my GoDaddy server mongoDB is not installed. 所以现在的问题是,在我的GoDaddy服务器上未安装mongoDB。 So running the following code does not work: 因此,运行以下代码不起作用:

$m = new MongoDB\Driver\Manager("mongodb://${bitnami}:${AWSpassword}@hostIp");

Fatal error: Class 'MongoDB\\Driver\\Manager' not found. 致命错误:找不到类'MongoDB \\ Driver \\ Manager'。

So my question how do I connect with PHP from my GoDaddy server to the EC2 Amazon server and connect to Bitnami mongoDB account. 因此,我的问题是如何从我的GoDaddy服务器使用PHP连接到EC2 Amazon服务器并连接到Bitnami mongoDB帐户。 Should I first setup an ssh connection to the server and then try to login to mongoDB server or how should I connect? 我应该首先建立与服务器的ssh连接,然后尝试登录mongoDB服务器,还是应该如何连接?

Any help is appreciated, thank you in advance! 任何帮助表示赞赏,在此先感谢您!

According to php.net the function that is used is Deprecated, refer this . 根据php.net,已使用的功能已弃用, 请参阅此

Can you try something like: 您可以尝试以下方法吗:

<?php

$manager = new MongoDB\Driver\Manager("mongodb://myusername:myp%40ss%3Aw%25rd@example.com/mydatabase");

?>

Refer this 推荐这个

Hi Bitnami Engineer here, 您好Bitnami工程师在这里,

Apart from this tip , please note that the database port for the nodes in this solution cannot be accessed over a public IP address by default. 除本技巧外 ,请注意,默认情况下,无法通过公共IP地址访问此解决方案中节点的数据库端口。 For security reasons, we do not recommend making the database port accessible over a public IP address. 出于安全原因,我们不建议您通过公共IP地址访问数据库端口。

https://docs.bitnami.com/aws/infrastructure/mongodb/#how-to-connect-to-mongodb-from-a-different-machine https://docs.bitnami.com/aws/infrastructure/mongodb/#how-to-connect-to-mongodb-from-a-different-machine

You can open the MongoDB port in the server by accessing the AWS console and modifying the access group configuration 您可以通过访问AWS控制台并修改访问组配置来打开服务器中的MongoDB端口

https://docs.bitnami.com/aws/faq/#how-to-open-the-server-ports-for-remote-access https://docs.bitnami.com/aws/faq/#how-to-open-the-server-ports-for-remote-access

You will also need to modify the MongoDB configuration file ( /opt/bitnami/mongodb/mongodb.conf ) to allow the database to not to work only on 127.0.0.1. 您还需要修改MongoDB配置文件( /opt/bitnami/mongodb/mongodb.conf ),以允许该数据库仅在127.0.0.1上不起作用。

Regards, Jota 此致,Jota

If you are looking to connect a tool like Studio 3T to a remote Mongodb instance built using bitnami AMI you can follow the steps below: 如果您想将Studio 3T之类的工具连接到使用bitnami AMI构建的远程Mongodb实例,则可以执行以下步骤:

1. Open inbound port 27017 on instance security group. 1.打开实例安全组上的入站端口27017。
2. Open Instance log of the instance and find the password for user root. 2.打开实例的实例日志,然后找到root用户的密码。
3. Open Studio 3T, server name is instance public ip and port 27017. 3.打开Studio 3T,服务器名称为实例public ip,端口为27017。
4. Got to Authentication and select basic and put in the user as root and password as obtained from instance log. 4.转到身份验证,然后选择基本,然后以从实例日志中获取的根用户名和密码输入用户。

Click on Test Connection, it should work. 单击测试连接,它应该可以工作。

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

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