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
Related
I'm trying to connect to a MSSQL database with odbc in php.
My issue is similar to this issue, I can connect with tsql, but not with php.
The answer to the problem not work because I think I don't have SELinux installed (I don't know what it is, but pacman not found this package (or with a similar name) on my computer)
I don't understand why it doesn't work, odbc is installed and detected by php
print_r(PDO::getAvailableDrivers());
Array ( [0] => odbc )
I'm connecting by doing that:
$dsn = 'odbc:Driver=FreeTDS;Server=127.0.0.1;Port:1433;Database=[my base name]';
$pdo = new PDO($dsn, $user, $password);
My base is not in local, I use a ssh tunel because the base in accessible only at my school, and we need an ssh tunnel. And it work, I can connect myself to the base with tsql.
When I was connecting PHP to our MS SQL server, I could never get a connection established using ODBC drivers.
Instead, I downloaded the official PHP > SQL SERVER drivers direct from Microsoft. You can find them here.
Once installed, you must configure your php.ini file to include the new drivers, restart your web server and then use the following to open a new connection:
$conn = new PDO("sqlsrv:Server=SERVER_IP,1433;Database=DATABASE_TO_OPEN;");
So I have written a basic program with PHP and MongoDB and I tested it locally using xampp. This all works fine. Now I want to publish it as a website.
After some research I decided to use Bitnami and I am running an EC2 server on Amazon. I am able to ssh and run commands from the terminal. Now I want to connect to the server and create a Mongo client. I am a bit puzzled and do not know how to do this exactly. I am using the code below. The ssh works fine but I do not know how to set up the mongo client. The '$m = new MongoClient();' works fine locally for localhost, I have also tried to supply to MongoClient my username and password but without success. Thank you very much for any help!
include('Net/SSH2.php');
include('Crypt/RSA.php');
$key = new Crypt_RSA();
$key->loadKey(file_get_contents('../../etc/bitnami.pem'));
$ssh = new Net_SSH2('ip_address');
if (!$ssh->login('bitnami', $key)) {
exit('Login Failed');
}
$ssh->exec('mongo admin --username root -p <myPassword>');
$m = new MongoClient();
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->mycol;
echo "Collection selected succsessfully";
$document = array(
"title" => "MongoDB",
"description" => "database"
);
$collection->insert($document);
Update answer to Jota Martos (unable to write it as comment):
Hi, thank you for your answer. Yes for now I allow access from all ports and IP's for test purposes. I can successfully connect via ssh but now I am stuck. I am running a Mean bitnami application and my website is running on GoDaddy server under PHP. So I want to connect with PHP to my mongoDB bitnami app that is hosted on AWS. So now the problem is that on my GoDaddy server mongoDB is not installed. So running the following code does not work:
$m = new MongoDB\Driver\Manager("mongodb://${bitnami}:${AWSpassword}#hostIp");
Fatal error: Class 'MongoDB\Driver\Manager' not found.
So my question how do I connect with PHP from my GoDaddy server to the EC2 Amazon server and connect to Bitnami mongoDB account. Should I first setup an ssh connection to the server and then try to login to mongoDB server or how should I connect?
Any help is appreciated, thank you in advance!
According to php.net the function that is used is Deprecated, refer this.
Can you try something like:
<?php
$manager = new MongoDB\Driver\Manager("mongodb://myusername:myp%40ss%3Aw%25rd#example.com/mydatabase");
?>
Refer this
Hi Bitnami Engineer here,
Apart from this tip, please note that the database port for the nodes in this solution cannot be accessed over a public IP address by default. For security reasons, we do not recommend making the database port accessible over a public IP address.
https://docs.bitnami.com/aws/infrastructure/mongodb/#how-to-connect-to-mongodb-from-a-different-machine
You can open the MongoDB port in the server by accessing the AWS console and modifying the access group configuration
https://docs.bitnami.com/aws/faq/#how-to-open-the-server-ports-for-remote-access
You will also need to modify the MongoDB configuration file (/opt/bitnami/mongodb/mongodb.conf) to allow the database to not to work only on 127.0.0.1.
Regards,
Jota
If you are looking to connect a tool like Studio 3T to a remote Mongodb instance built using bitnami AMI you can follow the steps below:
1. Open inbound port 27017 on instance security group.
2. Open Instance log of the instance and find the password for user root.
3. Open Studio 3T, server name is instance public ip and port 27017.
4. Got to Authentication and select basic and put in the user as root and password as obtained from instance log.
Click on Test Connection, it should work.
I want to use two database instances in my CodeIgniter application. I have set up db1 and db2 in the application but it always uses the socket of the first database.
$this->db2 = $this->load->database('db2', TRUE);
Message: mysqli::real_connect(): (HY000/2002): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
but the second server runs on '/var/run/mysqld/mysqld2.sock'
I can not see where to tell it to use the second socket.
Please note that I still want to connect to the first database too, I need to merge two replications, so setting defalut socket in php.ini is not an option.
You'll also need to edit your database config file to add the additional settings. Example:
$db['default']['hostname'] = 'localhost';
..
..
..
$db['second_db']['hostname'] = 'localhost';
It seems it was something CodeIgniter was missing, at least for mysqli driver: The socket parameter was always NULL in the driver code. With a modification of the code and the configuration array, it is possible to connect to any given socket for the server(s).
Add this to your config
'socket' => '/tmp/mysql.sock', // if no socked uses set it to NULL
And change this in your /system/database/mysqli/mysqli_driver.php
return $mysqli->real_connect($hostname, $this->username, $this->password, $this->database, $port, $this->socket, $client_flags)
But keep in mind that editing core files could conflict with framework updates, causing errors in your database implementation.
http://forum.codeigniter.com/thread-486.html
I am trying to figure out why I can not connect to MongoClient() hosted on some different IP. I am using below code to connect to another IP and accessing its DB and Collections
<?php
$username = 'root';
$password = '';
$conn = new MongoClient("mongodb://SYSTEMIP:27017", array("username" => $username, "password" => $password));
$database = $conn->testdb; //Creating New Database
$collection = $database->users; //Calling users collection
?>
The exception printed is MongoConnectionException: Failed to connect to: SYSTEM:27017: 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..
Is there anything that I need to change on system. MongoDB is properly installed on the IP that I need to connect to. Pls correct me if I am doing something wrong.
Thanks.
MongoDB 2.6 ships with bindIp config set. Look into your config for it.
http://docs.mongodb.org/manual/reference/configuration-options/#net.bindIp
Also, check whether 27017 is TCP permissible. Check for Firewalls or other restrictions.
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).