Cannot connect mongoDB and PHP - php

I am successfully running mongoDB v3.2.10 in terminal on macOS 10.11.6 and MAMP server running php 7.0.8.
When I try to reach mongo via php code like:
<?php
// connect to mongodb
$m = new MongoClient();
?>
at url: http://localhost:8888/Mongo/login.php
I get:
This site can’t be reached
localhost refused to connect.
When I reach mongo on the native port
at url: http://localhost:27017/Mongo/login.php
I get:
It looks like you are trying to access MongoDB over HTTP on the native
driver port.
Any idea what am I missing here?

Looks like you are using the built-in PHP Mongo DB clients. There is a new MongoDB driver that replace that. You will have to install it.
http://php.net/manual/en/set.mongodb.php
The newer MongoDB extension replaces the built-in PHP Mongo DB client. You'll also want to use the composer PHPLIB MongoDB client from here: http://php.net/manual/en/mongodb.tutorial.library.php
use MongoDB\Client;
try {
$mongoDbClient = new Client('mongodb://localhost:27017');
} catch (Exception $error) {
echo $error->getMessage(); die(1);
}

Related

PHP can't connect to SQL Server

I have been thrown into the world of PHP, and I'm having a bit of an issue connecting to my SQL server. I'm using the following code to try to connect. When it hits the SQLsrv_connect command, it just seems to stop processing. No error, it just stops loading.
function testConnection()
{
$returnable = "TEST";
echo 'Current PHP version: ' . phpversion();
$connectionInfo = array("Database"=>"MyDatabase", "UID"=>"MyLogin", "PWD"=>"MyPassword");
$conn = sqlsrv_connect("MyServer",$connectionInfo);
if ($conn) {
echo "Connection Established.<br />";
} else {
echo "Something went wrong while connecting to MSSQL.<br />";
}
return $returnable;
}
Any idea on what I might be missing? I tried some very old syntax for version 5, and I got the same issue. I am trying to connect to sql server 2008.
Thanks
First, check if you have installed the sql_srv extension for PHP.
Probably this extension is not installed/loaded by you php.ini file.
For Windows
Download proper driver from SQL Driver for MSSQL extract and copy into you php installation directory. Then edit php.ini file and add in extensions path your extension. (For 99% you should copy NTS sql_srv version) also don't forget add sql_srv_pdo extension.
For Ubuntu/Linux
You can try install sql_srv and pdo_sql_srv by pecl (if is installed)
pecl install sqlsrv pdo_sqlsrv.

Timeout while connecting to mongo database with mongo php ext

Timeout while connecting to MongoDB database with mongo php ext under Ubuntu 12.04 and php 5.6
I'm using
latest Apache 2.4
Ubuntu 12.04
php 5.6 with mongo ext Version 1.6.14
MongoDB v3.4 running in an lxc container
And the problem is when I connect to the mongo database.
I'm using this connection string:
mongodb://myDbUser:passW0rT#0.0.0.0:27020/myDb
(password, database and addr have been changed).
I have verified that php mongo ext is installed, with this code:
echo extension_loaded("mongo") ? "loaded\n" : "not loaded\n"; //prints loaded.
if (class_exists('\MongoClient')) {
echo 'is Mongo Client'."\n";//this is printed
}
else {
echo 'no Mongo Client'."\n";
}
try {
$client = new MongoClient('mongodb://myDbUser:passW0rT#0.0.0.0:27020/myDb', []);
} catch (Exception $ex) {
echo $ex->getMessage();//prints Failed to connect to: 0.0.0.0:27020: Connection timed out
}
What is more strange I can connect to this database using MongoClient and in addition to this I can connect to this database by my local server running on my computer (Win 10).
The problem appears also when I use security.authorization set to enabled and without difference set to disabled – then I use dsn mongodb://0.0.0.0:27020
Unfortunately server's response always gives timeout error when I enter web addr using www (domain which is on the server where mongo database is installed).
It could be one clue that server where mongo is installed is server which runs as container which means that I have to add port number to connect eg. via ssh.
I even changed the server from ngix to apache and problem didn't disappear.

Using PHP MSSQL PDO with Plesk

My current Website Control Panel is PLESK Parallels, and i'm trying to use PDO MSSQL on my website.
I've searched the internet and i'm aware that the PDO dll needs to be in my php.ini file and such, but I keep reading different ways to do it all the time, using commands like YUM and i'm getting confused.
Can someone possibly, in plain, simple black and white instructions, explain the process so I can simply run code like this
try {
$hostname = "myhostname";
$port = myportnumber;
$dbname = "databasename";
$username = "user";
$pw = "password";
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
When I load my page where this command is located i get the error message
'Failed to get DB handle: could not find driver'
Thanks
Example of installing pdo_dblib on CentOS-like OS for Plesk PHP 5.6:
# yum install plesk-php56-devel
# /opt/plesk/php/5.6/bin/pecl download pdo_dblib
# tar -xzf PDO_DBLIB-1.0.tgz
# cd PDO_DBLIB-1.0/
# /opt/plesk/php/5.6/bin/phpize
# ./configure --with-php-config=/opt/plesk/php/5.6/bin/php-config --with-pdo-dblib=./freetds
# vim pdo_dblib.c
On line #37 replace:
function_entry pdo_dblib_functions[] = {
with:
zend_function_entry pdo_dblib_functions[] = {
Save file and:
# make
# make install
you should see something like
Installing shared extensions: /opt/plesk/php/5.6/lib64/php/modules/
Enable extension:
# echo "extension=pdo_dblib.so" >> /opt/plesk/php/5.6/etc/php.d/pdo_dblib.ini
Verify:
# /opt/plesk/php/5.6/bin/php -m | grep pdo_dblib
pdo_dblib
Now sites in Plesk with PHP handler 5.6 will work with:
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
Pay attention for :
PHP DBlib PDO Issue
How to connect MSSQL from PHP 7, Plesk 12.5 installed on CentOS 7
PHP PDO works in OOPS .
Hence in PHP PDO first you must create a database handler using PDO class which is already defined in PHP Library. which will work as object for your database queries.
When you connect your database using PDO Class than you are ready to play with your queries with the database handler which you got during PDO connection.
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
Here $dbh will works as database handler object for MySQL Queries Operations.
I have a personal blog for PHP PDO here . Just go to reference and take a look how queries are working . Finally i would like to say it's very simple and secure.

Mongodb installation

Mongodb installed in linux CentOS server (ssh) with steps mentioned in this url
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-red-hat/
Started MongoDB server using the command and the status is ok.
sudo service mongod start
When connecting mongodb with PHP (Yii app), it shows error like this.
include(MongoClient.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory
PHP code
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->health;
echo "Database mydb selected";
$collection = $db->medical;
echo "Collection selected succsessfully";
?>
Yii2 provide class yii\mongodb\Connection for mongodb connection.
Check below link.
http://www.yiiframework.com/doc-2.0/yii-mongodb-connection.html
MongoDb Extension for Yii 2
http://www.yiiframework.com/doc-2.0/ext-mongodb-index.html
It will resolve your issue.

MSSQL VIA FreeTDS, ODBC, and Cpanel Unknown host machine name (severity 2)

I have installed FreeTDS 0.91, ODBC, on a Cpanel server running Centos 6.5x64. Everything appears to be running fine and I can connect to the remote MSSQL 2012 server using:
/usr/local/freetds/bin/tsql -S sqlserver -U test -P mypassword
and succesfully execute queries in the database.
I can also connect through:
isql -v sqlserverdatasource test mypasswordhere
But for some reason /usr/local/freetds/bin/tsql -LH server.ip.here
returns no information or errors which doesn't make much sense when it is proven I can connect with the other methods above.
So now when running a test script from a cpanel account on the machine I get:
Unknown host machine name (severity 2)
Here is the test script:
//Database connection function.
function getConnection() {
try {
//$dbconnect = new PDO("sqlserver:Server=server.ip.here,1433;Database=dbname", "user", "password");
$dbconnect = new PDO("dblib:host=server.ip.here,1433;dbname=dbname", 'user', 'password');
} catch (PDOException $e) {
echo "CONNECTION ERROR.<br>Error message:<br><br>" . $e->getMessage();
die();
}
if (!$dbconnect) {
die('Cant connect to database. Please try again later!');
}
else{
echo "i'm in!";
return $dbconnect;
}
}
The first commented line is the old one using sqlserv which I found did not work at all from what i can tell because of the x64 OS. I have also tried with "" around user and pass as well as no marks at all.
php -m does show PDO and pdo-dblib.
Any ideas where I can look next?
Update: This was fixed. I missed in freetds.conf:
[global]
# TDS protocol version
tds version = 8.0
It was originally set to 4.5 instead of 8.
The fix for me was with three steps:
First, I edited /etc/freetds/freetds.conf and changed the tds version like this:
tds version = 8.0
The second step was not entering port number. The port was already 1433, and not specifying it fixed the exact same issue on my case.
Lastly, to connect properly, I had to restart networking as #user1054844 mentioned as this:
/etc/init.d/networking restart
After all these steps, I was able to connect and work with the SQL Server database.
You actually did not need ODBC at all since your connect script is using pdo_dblib not odbc. You can just install FreeTDS than enable pdo_dblib via the compile time flag in rawopts and rebuild via EasyApache. Of course cPanel specifics for this are a bit different.
I just did this for a friend and decided to document it since it is hard to find accurate clear information for FreeTds and pdo_dblib on cPanel.
Guide is here: FreeTDS And pDO_dblib On cPanel

Categories