[英]“Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: /tmp/mongodb-27017.sock:0: Connection refused''”
I have the following code: 我有以下代码:
$this->connection = new MongoClient($uri, $options);
$this->database = $database;
However, at line $this->connection
I get: 但是,在$this->connection
我得到:
Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: /tmp/mongodb-27017.sock:0: Connection refused' in...
If I output the following: 如果我输出以下内容:
var_dump($uri, $options); exit;
I get: 我得到:
string(33) "mongodb:///tmp/mongodb-27017.sock" array(1) { ["connect"]=> bool(true) }
This is in my dev environment, I'm not using any username/password for Mongo as I can login to the Mongo shell with mongo reputation
alone. 这是在我的开发环境中,我没有为Mongo使用任何用户名/密码,因为我可以仅凭mongo reputation
登录到Mongo shell。 From there I can view collection and documents. 从那里我可以查看馆藏和文件。
Originally I was getting Permissions denied, but I changed the permissions of my dev /tmp/mongodb-27017.sock to 777. Now I get "connection refused". 最初,我的权限被拒绝,但是我将dev /tmp/mongodb-27017.sock的权限更改为777。现在,我收到“连接被拒绝”的信息。
Anyway below is the file from my ll /tmp/
dir: 无论如何,下面是我的ll /tmp/
目录中的文件:
srwxrwxrwx 1 mongodb mongodb 0 Jan 28 07:47 mongodb-27017.sock=
Anyone know what the issue is, or how I can debug further? 任何人都知道问题是什么,或者我如何进一步调试? Thanks 谢谢
I would recommend in PHP connecting not to the socket which your process might not have permissions to access. 我建议在PHP中不要连接到您的进程可能没有访问权限的套接字。 If you modify the permission of the socket it's only temporary. 如果您修改套接字的权限,那只是暂时的。 When you stop the mongo db it will delete the socket, the next time you start it, the permissions will be back to whatever the default is (700 I think). 当您停止mongo db时,它将删除套接字,下次启动时,权限将恢复为默认值(我认为是700)。
Instead connect to the to the default port (27017) mongo opens on 127.0.0.1. 而是连接到默认端口(27017),然后在127.0.0.1上打开mongo。
Change you connection string in PHP to the following: 将PHP中的连接字符串更改为以下内容:
$uri = "mongodb://127.0.0.1";
$this->connection = new MongoClient($uri, $options);
$connection= new \MongoClient("mongodb://localhost:27017");
return $this->db = $connection->selectDB($config['dbname']);
try this to connect local mongoDB. 试试这个连接本地mongoDB。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.