Trouble with configuring users and connecting to Mongo with PHP - php

I installed mongo 3.0.0 on my ubuntu server.
I tried to use PHP (with appropriate lib installed) to connect to mongo to learn sth new. Unfortunaltey I cannot go any further.
My user configuration looks like this:
> use testdb
switched to db testdb
> show users
{
"_id" : "testdb.testdb",
"user" : "testdb",
"db" : "testdb",
"roles" : [
{
"role" : "readWrite",
"db" : "testdb"
}
]
}
Then I try to execute the following PHP code:
try{
$uri = "mongodb://testdb:password#xxx.xxx.xxx.xxx:27017/testdb";
$options = array("connectTimeoutMS" => 30000);
$client = new MongoClient($uri, $options );
}
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
die();
}
$db = $client->selectDB("testdb");
I get "Message: Failed to connect to: xxx.xxx.xxx.xxx:27017: Authentication failed on database 'testdb' with username 'testdb': auth failed".
In /etc/mongod.conf I have "auth = true" uncommented
I also verified the conncetion with:
> nc -w 3 -v xxx.xxx.xxx.xxx 27017 Connection to xxx.xxx.xxx.xxx 27017
> port [tcp/*] succeeded!
I dig through Internet, i spent few hours already on this, I even re-installed mongo and set everything up again without any success.
Could you point to where to look for solution?

The authentication schema of 3.0 is not compatible with 2.x drivers. One way you should be able to make it work is by downgrading the authentication mechanism:
use "admin"
var schema = db.system.version.findOne({"_id" : "authSchema"})
schema.currentVersion = 3
db.system.version.save(schema)
Then create a user and you should be able to connect with it.

Related

MongoDB PHP Driver "No suitable servers found"

I'm installing a MongoDB on Debian 9 but when I try to connect
"No suitable servers found (serverSelectionTryOnce set): [connection refused calling ismaster on '127.0.0.1:27017']" is raised.
The machine runs
Debian 9
PHP PHP 7.0.27-0+deb9u1
MongoDB Version 3.6.5
MongoDB database driver 1.6.16 (stable) MongoDB driver for PHP 1.4.3 (stable)
I googled all the pages that talked about this thread but nothing.
I also changed the mongod.conf, deleted the ipv6 lookback, but nothing.
The script that I launch in PHP is
use MongoDB\Client;
$user = 'xxxx';
$pwd = 'yyyy';
$host = '127.0.0.1';
$port = '27017';
$db_name = 'my_db';
$mongo = new Client("mongodb://".$user.":".$pwd."#".$host.":".$port);
return $mongo->selectDatabase($db_name);
When I try to
selectDatabase()
the exception was raised
PS: Of course, $user and $pwd are credentials of a user that I correctly create in "admin" Mongo's table
{
"_id" : "admin.root",
"user" : "root",
"db" : "admin",
"credentials" : {
"SCRAM-SHA-1" : {
"iterationCount" : 10000,
"salt" : "<hidden>",
"storedKey" : "<hidden>",
"serverKey" : "<hidden>"
}
},
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
Help please o_O
EDIT: Before deploying the project on live server I tested it on MAMP and everything work properly (in particular the connection with Mongo DB)

Php mongodb not able to connect using username and password

I am able to login to mongo using the following command:
mongo host_name:port/admin -u 'USERNAME' -p 'PASSWORD'
For PHP I tried below code to connect to mongo:
try{
$m = new MongoClient("mongodb://USERNAME:PASSWORD#host_name:27017");
echo "Connection to database successfully";
}
catch(Exception $ex)
{
echo '<pre>'; print_r($ex->getMessage());
}
But it is not connecting. Gives following error:
Failed to connect to: 10.7.0.225:27017: Authentication failed on database 'admin' with username 'USERNAME': auth failed
I have already created user root with role as root.
Any help would be appreciable. Thanks!

Yii 1: Could not find driver error when connecting to microsoft sql server

I successfully tested that my PHP can connect to my MS SQL SERVER.
<?php
$server = 'SRV-MEXAL';
// Connect to MSSQL
$link = mssql_connect($server, 'mexal_db_usr', 'password_changed');
if (!$link) {
die('Something went wrong while connecting to MSSQL');
} else {
echo "Works <br>";
$OK = mssql_select_db ("at6_rp");
echo $OK ? "ok" : "ko";
}
?>
Using this script I got both Works and ok, so connection and db selection are both working.
I tried to configure my db connection into Yii 1 main.php
"mexal_db" => array (
'class' => "CDbConnection",
'connectionString' => 'sqlsrv:Server=SRV-MEXAL; Database=at6_rp',
'enableParamLogging' => false,
'username' => 'mexal_db_usr',
'password' => 'password_changed',
'charset' => 'utf8',
),
But when trying to instantiate it, I got this exception
CDbException' with message 'CDbConnection failed to open the DB connection: could not find driver' in /var/www/httpdocs/test1.phonix.it/yii/framework/db/CDbConnection.php:399
What am I doing wrong?
You will have php.ini inside your project folder. (In yii2, i am having inside web folder. In yii1, I don't know where php.ini file is located.)
So, please find your php.ini file of your project folder.
Inside php.ini, search for pdo_mssql.so.
;extension=pdo.so
;extension=pdo_mssql.so
Remove ; from it. Like
;extension=pdo.so
extension=pdo_mssql.so
Restart your server. It will work.
For more info, check this could not find driver - Yii

cant connect user to google cloud sql database

I am attempting to connect to a database in an instance on Google cloud sql but it is telling me access is denied.
Im just wondering am I missing something here...
I have requested an IP and inserted that into the authorized networks
I have created a database and set a user using SQL prompt (found in original view of Google APIs Console) and granted all privileges to that user for that database.
I have listed the users in that database to make sure "adrian" exists and that I have done that correctly.
I have insured the app being used to connect is authorized in the access control
I have set a root password in the access control section.
Here is my app.yaml file...
application: guestbookadrian12
version: 1
runtime: php
api_version: 1
handlers:
- url: .*
script: main.php
env_variables:
DEVELOPMENT_DB_HOST: ''
DEVELOPMENT_DB_USERNAME: 'root'
DEVELOPMENT_DB_PASSWORD: 'password'
DEVELOPMENT_DB_NAME: 'guestbookadrian12'
PRODUCTION_CLOUD_SQL_INSTANCE: '/cloudsql/guestbookadrian12:my-cloudsql-instance'
PRODUCTION_DB_USERNAME: 'adrian'
PRODUCTION_DB_PASSWORD: 'password'
PRODUCTION_DB_NAME: 'guestbookadrian12'
Here is some php code where it creates the connection...
if (strpos(getenv('SERVER_SOFTWARE'), 'Development') === false) {
$conn = mysqli_connect(null,
getenv('PRODUCTION_DB_USERNAME'),
getenv('PRODUCTION_DB_PASSWORD'),
null,
null,
getenv('PRODUCTION_CLOUD_SQL_INSTANCE'));
$db = getenv('PRODUCTION_DB_NAME');
//echo 'Database = : '.$db;
} else {
$conn = mysqli_connect(getenv('DEVELOPMENT_DB_HOST'),
getenv('DEVELOPMENT_DB_USERNAME'),
getenv('DEVELOPMENT_DB_PASSWORD'));
$db = getenv('DEVELOPMENT_DB_NAME');
}
if ($conn->connect_error) {
die("Could not connect to database: $conn->connect_error " .
"[$conn->connect_errno]");
}
if ($conn->query("CREATE DATABASE IF NOT EXISTS $db") === FALSE) {
die("Could not create database: $conn->error [$conn->errno]");
}
The error Im getting is
Could not create database: Access denied for user 'adrian'#'localhost' to database 'guestbookadrian12' [1044]
Thank you

how to connect to the database in openshift application

I did as following
MySQL 5.1 database added. Please make note of these credentials:
Root User: xxxxxxx
Root Password: xxxxxxx
Database Name: php
Connection URL: mysql://$OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT/
You can manage your new MySQL database by also embedding phpmyadmin-3.4.
The phpmyadmin username and password will be the same as the MySQL credentials above.
phpMyAdmin 3.4 added. Please make note of these MySQL credentials again:
Root User: xxxxxxx
Root Password: xxxxxxx
URL: https://php-doers.rhcloud.com/phpmyadmin/
and i try to connect db using bellow PDO code .but it does not work
$dbh = new PDO('mysql:host=mysql://$OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT/;dbname=php', "xxxxxx, "xxxxxx");
I don't know what is the connection URL mean ?
There is an error in your connection string plus $OPENSHIFT_MYSQL_DB_* are env variables and need to be fetched via getenv php function.
So try the following:
define('DB_HOST', getenv('OPENSHIFT_MYSQL_DB_HOST'));
define('DB_PORT',getenv('OPENSHIFT_MYSQL_DB_PORT'));
define('DB_USER',getenv('OPENSHIFT_MYSQL_DB_USERNAME'));
define('DB_PASS',getenv('OPENSHIFT_MYSQL_DB_PASSWORD'));
define('DB_NAME',getenv('OPENSHIFT_GEAR_NAME'));
$dsn = 'mysql:dbname='.DB_NAME.';host='.DB_HOST.';port='.DB_PORT;
$dbh = new PDO($dsn, DB_USER, DB_PASS);
Open php myadmin from webconsole , and on left corner you will get some Ip
That is like xx.xx.xx.xx:3306 , so your hostname is xx.xx.xx.xx
Ip address wil work , I have tried and worked for me
below code worked for me ,try this it might help you
var mysql = require('mysql');
var mysql = require('mysql');
exports.pool = mysql.createPool({
host: process.env.OPENSHIFT_MYSQL_DB_HOST || 'localhost',
port: process.env.OPENSHIFT_MYSQL_DB_PORT || '3306',
user: process.env.OPENSHIFT_MYSQL_DB_USERNAME || 'root',
password: process.env.OPENSHIFT_MYSQL_DB_PASSWORD || '',
database: 'chichat' || 'chichat',
multipleStatements : true,
charset: 'utf8'
});
I would like to add some information to future reference.
Today I am trying to deploy a Drupal installation to Open Shift (OS) and tried to configure MySql.
I connected to OS via SSH (rhc ssh ), went to app-root/data/sites/default. Following cat settings.php to see how OS configure database.
Here it is:
// When run from Drush, only $_ENV is available. Might be a bug
if (array_key_exists('OPENSHIFT_APP_NAME', $_SERVER)) {
$src = $_SERVER;
} else {
$src = $_ENV;
}
$databases = array (
'default' =>
array (
'default' =>
array (
'database' => $src['OPENSHIFT_APP_NAME'],
'username' => $src['OPENSHIFT_MYSQL_DB_USERNAME'],
'password' => $src['OPENSHIFT_MYSQL_DB_PASSWORD'],
'host' => $src['OPENSHIFT_MYSQL_DB_HOST'],
'port' => $src['OPENSHIFT_MYSQL_DB_PORT'],
'driver' => 'mysql',
'prefix' => '',
),
),
);
$conf['file_private_path'] = $src['OPENSHIFT_DATA_DIR'] . 'private/';
$conf['file_temporary_path'] = $src['OPENSHIFT_TMP_DIR'] . 'drupal/';
So I created a php folder into git repository (see OS documentation about Drupal), copied a clean Drupal install to it and paste code above to settings.php.
After pushing to git, restarted OS app and worked!

Categories