MongoDB与PHP的远程连接 [英] MongoDB Remote connection with php

查看:61
本文介绍了MongoDB与PHP的远程连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

部署了一个快速的云服务器,只是为了托管MongoDB出于好奇而对其进行修补.它已安装并且可以工作.做了这样的测试数据库/表:

Deployed a quick cloud server just to host a MongoDB to tinker with it out of curiosity. It's installed and works. Made a test DB/Table like this:

 db.items.insert({ name: 'eggs', quantity: 10, price: 1.50 })
 db.items.insert({ name: 'bacon', quantity: 3, price: 3.50 })
 db.items.insert({ name: 'tomatoes', quantity: 30, price: 0.50 })

当我运行db.items.find({})时,所有项目都会出现并且一切都很好.

When I run db.items.find({}) all the items appear and all is well.

现在在PHP中,当我从其他服务器连接到该数据库时,我会这样做:

Now in PHP when I connect to that database from a different server I do this:

  // open connection to MongoDB server
  $conn = new Mongo('mongodb://theAdmin:Gold1234@165.225.130.252:27017');

  // access database
  $db = $conn->test;

  // access collection
  $collection = $db->items;

  // execute query
  // retrieve all documents
  $cursor = $collection->find();

  // iterate through the result set
  // print each document
  echo $cursor->count() . ' document(s) found. <br/>';  
  foreach ($cursor as $obj) {
    echo 'Name: ' . $obj['name'] . '<br/>';
    echo 'Quantity: ' . $obj['quantity'] . '<br/>';
    echo 'Price: ' . $obj['price'] . '<br/>';
    echo '<br/>';
  }

我得到这个错误:

致命错误:未捕获的异常'MongoConnectionException'与 消息'无法连接到:165.225.130.252:27017:传输 端点未连接",位于/home/moosex/public_html/info.php:4 堆栈跟踪:#0/home/moosex/public_html/info.php(4): Mongo-> __construct('mongodb://[theA ...')#1 {main}被抛出 /home/moosex/public_html/info.php,第4行

Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: 165.225.130.252:27017: Transport endpoint is not connected' in /home/moosex/public_html/info.php:4 Stack trace: #0 /home/moosex/public_html/info.php(4): Mongo->__construct('mongodb://[theA...') #1 {main} thrown in /home/moosex/public_html/info.php on line 4

我查了一下,尝试了几种不同的连接方式,但仍然无法接通.我应该如何远程连接?

I've looked up and tried several different ways to connect and still can't get it. How am I supposed to connect remotely?

顺便说一句,这是该服务器的实际用户名密码和地址(除了鸡蛋培根和西红柿之外,那里什么都没有),如果您可以连接到该服务器,请上帝保佑您.

BTW, that is the actual username password and address to that server(there's nothing on there except for eggs bacon and tomatoes), if you can connect to it, god bless you lol.

推荐答案

要能够像这样调试随机怪异",打开内部驱动程序日志记录非常有用. 该驱动程序在后台做很多事情,并且可以吐出各种重要的调试信息.

To be able to debug "random weirdness" like this, it is very useful to turn on the internal driver logging. The driver does whole lot of things behind the scenes, and can spit out all sort of important debug information.

在脚本顶部添加以下内容:

Add the following at the top of your script:

<?php
MongoLog::setLevel(MongoLog::ALL);
MongoLog::setModule(MongoLog::ALL);
?>

默认情况下,记录器将发出"php错误消息"(E_NOTICE/E_WARNING),如果启用了error_log,请确保检查该文件的结果.

By default the logger will spew out "php error messages" (E_NOTICE/E_WARNING), if you have error_log enabled, make sure to check that file for the results.

对于您的(稍作修改的)连接字符串,我得到以下结果

For your (slightly modified) connection string, I get the following results

Notice: PARSE   INFO: Parsing mongodb://theAdmin:Gold1234@localhost:27027 in Command line code on line 1
Notice: PARSE   INFO: - Found user 'theAdmin' and a password in Command line code on line 1
Notice: PARSE   INFO: - Found node: localhost:27027 in Command line code on line 1
Notice: PARSE   INFO: - Connection type: STANDALONE in Command line code on line 1
Notice: PARSE   INFO: - No database name found for an authenticated connection. Using 'admin' as default database in Command line code on line 1
Notice: CON     INFO: mongo_get_read_write_connection: finding a STANDALONE connection in Command line code on line 1
Notice: CON     INFO: connection_create: creating new connection for localhost:27027 in Command line code on line 1
Notice: CON     WARN: connection_create: error while creating connection for localhost:27027: Invalid argument in Command line code on line 1
Notice: CON     WARN: Couldn't connect to 'localhost:27027': Invalid argument in Command line code on line 1

我怀疑两端都有防火墙问题.您可以使用mongo shell连接到服务器吗?

I suspect a firewall issue at either end.. Can you connect to the server using the mongo shell?

这篇关于MongoDB与PHP的远程连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆