php failed to connect to mongodb socket - php

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.

Related

Warning: mysqli::__construct(): (HY000/2002): No such file or directory in [duplicate]

This question already has answers here:
Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in
(17 answers)
Closed 2 years ago.
I'm totally stuck having spent a couple of days trying to figure this out...
I have set up an Apache web server (version 2.4.34) on my Mac OS running Mojave (10.14.3) and have installed the current version of MySQL Community Server (8.0.15). Both running perfectly as I can determine (from command line). I have PHP 7.1.23 installed. When I create a .php file with the following code and put it in my Sites directory and then in Safari go to http://localhost/~dave/hello.php I get this error:
"Warning: mysqli::__construct(): (HY000/2002): No such file or
directory in /Users/dave/Sites/hello.php on line 7 Connection failed:
No such file or directory".
Code in hello.php:
<?php
$servername = "localhost";
$username = "dave";
$password = "*****";
//Create connection
$conn = new mysqli($servername, $username, $password);
//Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
I have tried everything I can think of, including those suggestions in the other two similar posts (this one: My database user exists, but I still get an (HY000/2002): No such file or directory and this one: PHP Warning: mysqli::__construct(): (HY000/2002): No such file or directory (Debian GNU/Linux 9)), and I cannot connect to MySQl with php. In particular, I checked with the php info page and the socket for mysqli is given as var/mysql/mysql.sock. In the relevant table in mysql the value for socket is given as /tmp/mysql.sock. I tried changing the php.ini.default file by adding each of these sockets (in place of the empty value) and neither worked (still generates the same error). I thought that the 2002 MySQL socket error might be the problem, so I made a symlink by doing this:
sudo mkdir /var/mysql
sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock
but all that did was generate a new error:
Warning: mysqli::__construct(): The server requested authentication
method unknown to the client [caching_sha2_password] in
/Users/dave/Sites/hello.php on line 7
Warning: mysqli::__construct(): (HY000/2054): The server requested
authentication method unknown to the client in
/Users/dave/Sites/hello.php on line 7 Connection failed: The server
requested authentication method unknown to the client
So I removed the symlink...and returned to the original error.
I also tried replacing "localhost" with the IP address 127.0.0.1 as well as "localhost:3306" as other question threads have suggested. Nothing worked here either.
Please help, I am new to this kind of use of the computer/web design and I'm out of obvious (googleable/understandable) options.
Use 127.0.0.1 instead of localhost.
I got some help from some local tech guys in my area and it turns out that fixing the socket error was the first step, like I tried to do above. I eventually did it this way, using the solution by noun:
Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in
The second error message was then generated (same one as in my question, here again):
mysqli::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in /Users/dave/Sites/hello.php on line 7
Warning: mysqli::__construct(): (HY000/2054): The server requested authentication method unknown to the client in /Users/dave/Sites/hello.php on line 7
This was fixed by the SQL-based solution (June 9 2018 answer) to this question:
php mysqli_connect: authentication method unknown to the client [caching_sha2_password]
This SQL statement was simply run in MySQL from the terminal. After restarting the Apache server I reloaded the .php file in Safari and this resulted in a successful mysqli connection to MySQL with PHP. I also tried a PDO connection and this also worked.

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 :)

Can connect locally but not remotely

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 !

MongoConnectionException in local, not in remote

I am trying to connect to MongoDB from local (a Azure VM). I can connect when I try in my localhost workspace with the URL of the VM. But, when I try to connect in localhost there is a error:
At localhost:
$m = new Mongo("mongodb://xxx.cloudapp.net:27017");
At local VM:
$m = new Mongo("mongodb://localhost:27017");
I also try with: 127.0.0.1 and xxx.cloudapp.net.
This is the error:
Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: localhost:27017: Permission denied' in /var/www/html/test.php:8 Stack trace: #0 /var/www/html/test.php(8): Mongo->__construct('mongodb://local...') #1 {main} thrown in /var/www/html/test.php on line 8
This is not a new issue. This is a regular issue, the problem is with the instalation.
Please check it out: http://www.php.net/manual/en/mongo.installation.php#mongo.installation.fedora
You have to run
$ /usr/sbin/setsebool -P httpd_can_network_connect 1 and then restart apache service to fix the problem.
Thank you all

Memcache connect throws error in cron job

When i try to connect to memcached server from a php page, it works without any problem.
Using this code
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
But when it tries to connect to server from a php script which is fired by cron job it throws this error
Warning: Memcache::connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/...../cron/acts_cron.php on line 3
Warning: Memcache::connect(): Can't connect to localhost:11211, php_network_getaddresses: getaddrinfo failed: Name or service not known (0) in /home/...../cron/acts_cron.php on line 3
Could not connect
What can cause this problem ?
Seems like you're missing an entry for localhost inside your hosts file. Try updating /etc/hosts and make sure that you got a line like the following in there:
127.0.0.1 localhost.localdomain localhost
using 127.0.0.1 instead of localhost fixed the issue.

Categories