Can connect locally but not remotely - php

I am facing an issue in connecting my php client to my mongodb server.
Basically, I installed MongoDB on my server (hosted in the US), I can connect to it locally when I am connected to it via ssh.
But I can't connect to this server on remote from my php client, here is the error that I get :
Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: xxx.xxx.xxx.xxx:27017: Connection refused' in /var/www/pages/news.php:9 Stack trace: #0 /var/www/pages/news.php(9): MongoClient->__construct('mongodb://xxx.xxx...') #1 {main} thrown in /var/www/pages/news.php on line 9
I am using this in order to create a connection from my php client :
$m = new MongoClient("mongodb://xxx.xxx.xxx.xxx");
My remote server is Ubuntu 12.04 and I already added the INPUT and OUTPUT rules in iptables to allow traffic for the port 27017.
I don't really know where the issue comes from ? (I did not add any authentication credentials to my mongodb instance, its a new one for testing purposes).
If anyone had the same issue and found a solution, it could be helpful, thank you !

Ok guys, found the issue :
I typed this command in order to display the listening services and their corresponding ports:
netstat -anltp | grep "LISTEN"
I saw that SSH was listening with the IP : 0.0.0.0
But mongod was listening on the local interface 127.0.0.1
I just had to update the mongodb configuration file here :
/etc/mongodb.conf and set the parameter "bind_ip" to : 0.0.0.0
and uncommented the "port" field to use the default one : 27017.
As a result, now I can connect from my client to my mongod instance on my server.
Hope this helps !

Related

disable symfony 5 database connection

I have the default config in my symfonu project I have this line at .env file
DATABASE_URL="postgresql://db_user:db_password#127.0.0.1:5432/db_name?serverVersion=13&charset=utf8"
and I get this error
An exception occurred in driver: SQLSTATE[08006] [7] could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
Of course I dont have not isntalled postgres, and I dont need a database for this project, How I cant fix this error I tried to comment the .env file line but I get another error
Commenting the line as L3xpert answer suggests doesn't seem to do the job.
It will throw the error :
Environment variable not found: "DATABASE_URL".
If you don't need Doctrine but you have Website-Skeleton installed, you can remove Doctrine by running this command :
composer remove doctrine/doctrine-bundle doctrine/doctrine-migrations-bundle

Mongodb:Failed to connect

I am new to MongoDB.I installed mongodb and used with Laravel framework.Its had been worked for a long time without any issues.But currently while i try to acess my website it shows:
Failed to connect to: localhost:27017: Connection refused
when i try to acces mongodb via command line,it shows:
warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146 exception: connect failed
when i try to access it via rock tool,it shows:
Unable to connect MongoDB, please check your configurations. MongoDB said:Failed to connect to: 127.0.0.1:27017: Connection refused.
what i will do? I can't figure what's wrong from my part since its working after the past months.I do nothing since the last days.Thanks in advance..
After a short break, I can find out the solution.
The origin of that issue is the unexpected shutdown of the system.So i run the below code in command prompt:
sudo rm /var/lib/mongodb/mongod.lock
mongod --repair
sudo service mongodb start
then after the above code run, its working fine :)

Failed to connect to: localhost:27017: Connection refused

I have installed MongoDB in xampp on Ubuntu using Laravel 5.1. But I always get this error
MongoConnectionException in TestController.php line 19: Failed to
connect to: localhost:27017: Connection refused
Nor I'm able
I have gone through a lot of questions, but all proved to be trivial solution.
Please help me with this as I'm stuck on it.
I think that first thing to do is to ssh on that server and check that mongod process is alive, then try open mongo shell using command - mongo . If all works try telnet on this server and port from your machine ,if there is no errors , read mongo logs

Zend Framework error [database connection]

Today trying to run my project coded under Zend Framework, and connected to a remote database, i got those 2 errors:
PDOException: SQLSTATE[HY000] [2002] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\wamp\www\trunk\library\Zend\Db\Adapter\Pdo\Abstract.php on line 129
Zend_Db_Adapter_Exception: SQLSTATE[HY000] [2002] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\wamp\www\trunk\library\Zend\Db\Adapter\Pdo\Abstract.php on line 144
Before it was working normally, and i don't know what going on with it today.
It seems it's a connection problem to the database but the question, where the problem come from, from the database server, or my project and where excatly.
It's the first time, i got this error, and i really don't know what that mean.
It sounds like a network problem to me, try a telnet from the app server:
telnet <remote db name/IP> 3306
If that fails, you have a network/firewall problem. Or the DB is is not listening on that port or on the network entirely. Do a netstat on the remote DB to confirm:
Linux
netstat -an | grep LISTENING
Or windows
netstat -an | find "LISTENING"
You should see :3306 if it's listening on the default port.

php failed to connect to mongodb socket

just set up a fresh development ubuntu 13.04 server with nginx, php-fpm, and mongodb. The following line of code kills all of my scripts that were running great on a production ubuntu 12.10 server
$m = new MongoClient("mongodb://user:pass#/tmp/mongodb-27017.sock/database");
error
Warning: MongoClient::__construct(): php_network_getaddresses:
getaddrinfo failed: Name or service not known in
/var/www/app/php/db.php on line 4
Fatal error: Uncaught exception 'MongoConnectionException'
with message 'Failed to connect to: /tmp/mongodb-27017.sock:0:
php_network_getaddresses: getaddrinfo failed: Name or service not
known' in /var/www/app/php/db.php:4 Stack trace:
0 /var/www/app/php/db.php(4): MongoClient->__construct('mongodb://app...')
1 /var/www/app/admin/loggedin.php(3): require('/var/www/app...')
2 /var/www/app/admin/index.php(1): require('/var/www/app...')
3 {main} thrown in /var/www/app/php/db.php on line 4
I've been toying with this for a day and half and can't figure it out. It works when I connect to localhost but I'd really prefer sockets. any ideas?
The PHP driver does not support passing in the credentials as part of the URI when connecting over Unix Domain Sockets.
You need to pass in the username and password in the $options array (2nd argument).
Ubuntu 14.04, Php 5.59, MongoDB shell version: 2.4.9
I got mine to work by passing the following:
I had to change my connection from $m = new Mongo(); // connect to $m = new Mongo("mongodb://127.0.0.1"); // connect
For some reason it appears that the /etc/hosts file stopped being called localhost for 127.0.0.1 which was throwing off the default mongodb connector so I had to be explicit. Sigh.

Categories