Since we started using mongoDB a while back, a large part of our PHP codebase looks like this:
$mongo = new Mongo();
$coll = $mongo->mydb->mycoll;
The default is to connect via TCP to localhost:27017 which has worked just fine for us for a few years now. Due to speed considerations, I would like to switch to using Unix domain sockets which are supported like this:
$client = new MongoClient("mongodb:///tmp/mongodb-27017.sock");
Is there a way to make this work without having to specify the socket file in the code? The docs list mongo.default_host and mongo.default_port which can be set in php.ini so you can write:
$client = new MongoClient();
but that seems to be possible only for TCP connections. Or can I use these parameters to specify a socket file? If so, how?
Related
I have found glimpses of the fact that it is possible to connect PHP to MySQL via pipes in their documentation of MySQLi, but I cannot, for the life of me, find anyone explaining what is needed.
The host parameter claims:
When possible, pipes will be used instead of the TCP/IP protocol.
But when is it "possible"? I have my own machine, and I definitely have the necessary privileges to achieve this, I just don't know how. Connecting to the host localhost reports "Localhost via UNIX socket" when examining the host_info.
Trying to follow one (downvoted) comment from that page, and connecting to host ., with socket parameter set to mysql, causes a 2002 connection error.
How do I tell it to (always) connect via a pipe instead?
Today I had the same issue and it required much time to solve this on Windows.
I can establish a named pipe connection with the following code:
$connection = new mysqli('.', 'user', 'pass', 'database', null, '\\\\.\\pipe\\name_of_pipe');
The server, where I want to connect to, has the following configuration:
[mysqld]
skip-networking
enable-named-pipe
socket=name_of_pipe
Using '127.0.0.1', 'localhost' or NULL as hostname doesn't work. Also you must specify a path to the named pipe and not just the name of the pipe.
Unfortunately the PHP documentation is a little bit weak here...
Named Pipes only work under Windows.
Also
Whenever you specify "localhost" or "localhost:port" as server, the MySQL client library will override this and try to connect to a local socket (named pipe on Windows). If you want to use TCP/IP, use "127.0.0.1" instead of "localhost". If the MySQL client library tries to connect to the wrong local socket, you should set the correct path as in your PHP configuration and leave the server field blank.
It is not mentioned in the actual PHP documentation, but it should be still valid.
I'm using laravel 5.2 and MongoDB 3.2.
I want to test if connection with is ok before my app starts (i can't use DB facade), in the monolog configuration. If connection is not ok, i will use logging in file.
By recommendation, i'm testing MongoClient, Mongo and MongoDB\Client, and using whatever is enabled.
I'm trying to test mongo connect as the following:
$mongoClient = new \MongoDB\Client('mongodb://localhost:27017');
$mongoClient->selectCollection('mydb', 'mycollection');
That's the return:
Client {
+manager: Manager {#21}
+uri: "mongodb://localhost:27017"
+typeMap: [
array => "MongoDB\Model\BSONArray",
document => "MongoDB\Model\BSONDocument",
root => "MongoDB\Model\BSONDocument"
]
}
Finnaly, my questions:
Exists a way to use DB facade before app starts?
How and what is the right way to test MongoDB connection with PHP?
If you has another suggestion, i will be thankful.
According to PHP document, the driver connects to the database lazily (http://php.net/manual/en/mongodb-driver-manager.getservers.php), the only way to test connection should be actually execute commands like findOne() stated in yours comment.
In addition, if the name of DB or Collection is uncertain at the point, you can use listDatabases() method which also throws exceptions if connection fails.
using this way you can check MongoDB connection with PHP:
$connection = new MongoClient(); // connects to localhost:27017
$connection = new MongoClient( "mongodb://example.com" ); // connect to a remote host (default port: 27017)
$connection = new MongoClient( "mongodb://example.com:65432" ); // connect to a remote host at a given port
Okay, this is something I've been banging my head on for a few weeks now, so just bear with me. When I set up my postgres database, I wanted to connect to it only through the local Unix socket. Just like how I set up my MySQL database, and subsequent PHP scripts I wrote for it. Thus, the bottom of my pg_hba.conf file looked like this:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
#host all all 127.0.0.1/32 md5
# IPv6 local connections:
#host all all ::1/128 md5
I was, and still am, able to interface with my db via the psql command-line utility, and do anything I need to do without issue. However, when I tried to write a simple PHP script to connect to the db, it would always fail:
<?php
$dbhost = "localhost";
$dbname = "postgres";
$dbuser = "postgres";
$dbpassword = "####";
$conn_string = "host=$dbhost dbname=$dbname user=$dbuser password=$dbpassword";
$dbconn = pg_connect($conn_string);
if($dbconn)
{
echo "<p>Win!</p>";
}
else
{
echo "<p>Connect failed!</p>";
exit();
}
pg_close($dbconn);
?>
I was absolutely dumbstruck for weeks as to why PHP wouldn't establish a simple Unix socket connection, especially after writing quite a few scripts with the mysqli library for my MySQL db that used local connections! However, I finally un-commented the line associated with the loop-back address, and like magic it suddenly worked! This indicates to me that when 'localhost' is passed as the host parameter of pg_connect, it uses the loop-back interface, rather than just the local Unix socket.
It's not the end of the world, but is there a way to use PHP in a way so that it connects via a local Unix socket, or does it not support this type of connection with postgresql?
Set host in the connection string to the value of the directory that holds the unix socket.
So if the socket is at /var/run/postgresql/psql.sock then try:
pg_connect("host=/var/run/postgresql dbname=..etc...");
Other comments on the PHP docs page for pg_connect indicate that you can also leave out the host key/value completely and it will work - but I haven't tried.:
http://php.net/manual/en/function.pg-connect.php
I need to connect to a remote database server with PHP to query and return data.
So far, I have tried this:
$connection = mssql_connect('[redacted]:1433\SQLEXPRESS', '[redacted]', '[redacted]');
and
$connection = mssql_connect('[redacted]', '[redacted]', '[redacted]');
Both result in FALSE, but no error thrown. What am I missing? It doesn't even seem to attempt to connect (fails very quick).
Usually this isn't so much about the connection code as the setup of the external DB server. First try this:
$connection = mssql_connect('[redacted]\SQLEXPRESS', '[redacted]', '[redacted]');
But you are connecting to SQL Express which by default doensn't accept any incoming TCP/IP requests, so if that doesn't work out, you'll need to check the configuration (or if it's truly external, have the DBA check it) and make sure:
it's set up to accept TCP/IP requests,
that the firewall is allowing that IP and port through, and
that the specific IP address your request is coming in on is accepting requests
Good answer by AlexC, but what I wound up doing was converting the MSSQL database over to MySQL and performing the migration with the Migrate framework (Drupal).
Hi
My proplem is that I have a open socket in my server side code (written php) and I need to trasform this socket to use TLS, with negotiation and decription of the stream before passing it to the already existing code that work with a clear stream without encryption.
What is the simplest solution to archive this?
PHP supports TLS/SSL listening using a stream context. Something like this should work:
$listenstr = "tls://0.0.0.0:1234";
$ctx=stream_context_create(array('ssl'=>array(
"local_cert"=>"mycertandkey.pem",
"passphrase"=>"badpassphrase"
)));
$s = stream_socket_server($listenstr,$errno,$errstr,STREAM_SERVER_BIND|STREAM_SERVER_LISTEN,$ctx);
$conn = stream_socket_accept($s);
// do stuff with your new connection here