I am using mongoDB with PHP. on local system it works fine. Now I am trying to access mongoDB cluster on AWS which is secured by SSH. I am using below code
$conn = new MongoClient('mongodb://SSH Hostname', [
'username' => 'username',
'password' => '',
'db' => 'DBname'
]);
print_r($conn);
It gives below error
Type: MongoConnectionException
Message: Failed to connect to: SSH Hostname :27017: Connection refused
I can connect DB from MongoDB compass or Studio 3T. How I can use SSH while connecting using PHP
Related
I'm trying to connect from PHP PDO to Amazon's PostgreSQL DB instance using SSL. I'm using the following DSN:
$db = new PDO('pgsql:host=[host];port=[port];dbname=[db];options=\'--client_encoding=utf8\';sslmode=require;sslcert=/path/domain.crt;sslkey=/path/private.key;sslrootcert=/path/intermediate.ca-bundle;', '[user]', '[pass]');
I'm using the same sslcert, sslkey and sslrootcert that are used by Apache on the same client server - they are in PEM format. I can connect using SSL to MySQL with them using following PDO options.
$options = [
PDO::MYSQL_ATTR_SSL_KEY => '/path/private.key',
PDO::MYSQL_ATTR_SSL_CERT => '/path/domain.crt',
PDO::MYSQL_ATTR_SSL_CA => '/path/intermediate.ca-bundle',
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => true,
];
But for PG, I'm getting:
SQLSTATE[08006] [7] SSL error: certificate verify failed
Without SSL, I can connect with no problem.
Any help would be appreciated!
I am using Predis PHP library to connect to redis server running on AWS EC2 server. When I try to connect to redis installed on my local system, it works fine. Same code does not work when I try to connect to Redis on AWS EC2. I receive below error.
php_network_getaddresses: getaddrinfo failed: No such host is known. [tcp://my-server-address:6379]
I tried to check connect server on redis-cli using below command and it works fine.
redis-cli -h my-server-address -p 6379
below is the code PHP that I use to connect to Redis.
function config() {
$client = new Predis\Client([
'scheme' => 'tcp',
'host' => 'my-server-address',
'port' => 6379,
'database' => 1,
]);
return $client;
}
I made sure that there is nothing wrong with my server address.
I am using two Docker container, one having ubuntu and an apache webserver running, the other one a mysql server. The containers are linked and i can connect from the ubuntu container onto the mysql server. For the connection I use in the ubuntu container:
mysql -u root -h mysql
where the second 'mysql' is the name of the container. I can connect to it through the container id as well, so the connection works as well as connecting onto the database from the windows environment.
What doesnt work is the connection from doctrine to the database within the PHP application which is in the ubuntu container.
The config looks like this:
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => 'mysql',
'port' => '3306',
'user' => 'root',
'password' => '',
'dbname' => 'db_name',
'charset' => 'utf8',
)
)
),
)
But I get the Error message
Uncaught PDOException: could not find driver in /var/www/vendor/zendframework/zend-servicemanager/src/ServiceManager.php
and
Zend\ServiceManager\Exception\ServiceNotCreatedException: An abstract factory could not create an instance of doctrine.entitymanager.ormdefault(alias: doctrine.entitymanager.orm_default). in /var/www/vendor/zendframework/zend-servicemanager/src/ServiceManager.php
Does anyone have any idea how to solve this and where exactly the error comes from?
I have already tried to put in the container ID as 'host' and commented out the 'password' field as it is not used.
Thanks in advance
Jonathan
You need to have pdo_mysql allowed on your system, it doesn't seem a connectivity issue but a php configuration problem.
Can you try to do this command inside your php container
php -i | grep pdo_mysql
Just to understand if php has this module
This is how I am attempting to connect to the database on heroku:
//db.php
$config = array(
'username' => 'username',
'password' => 'password',
'connection_string'=> sprintf('mongodb://%s:%d','EXAMPLE.herokuapp.com','27017')
);
$connection = new \MongoClient($config['connection_string']);
When I pushed to the repo, and go to the app URL the follow error message is shown:
Failed to connect to: EXAMPLE.herokuapp.com:27017 Connection refused in db.php.
How should I format the connection_string so that it will not give the error message?
There is no locally running MongoDB available on Heroku.
You need to use one of the add-ons at https://addons.heroku.com/#data-stores such as MongoLab.
When you run
$ heroku addons:add mongolab
it's provisioned for you, and there will be a config variable in the environment with the connection information:
$ heroku config | grep MONGOLAB_URI
MONGOLAB_URI => mongodb://heroku_app1234:random_password#ds029017.mongolab.com:29017/heroku_app1234
You can just read that from $_ENV['MONGOLAB_URI'], or using getenv('MONGOLAB_URI'):
$connection = new \MongoClient(getenv('MONGOLAB_URI'));
I'm trying to connect to pgsql via laravel and finally got everything setup (pgsql server running, pdo installed, all libs installed). I'm running on a VPS (CentOS) managed via CPanel/WHM.
Here's what I'm doing:
I'm trying to create a user database via artisan (Laravel's command line) using migrate:install.
For those that don't use Laravel, artisan uses php's PDO for pgsql to connect. Here are my settings:
'pgsql' => array(
'driver' => 'pgsql',
'host' => 'localhost',
'database' => 'dbname',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'prefix' => '',
),
I've also setup a test.php file:
$dbconn = pg_connect("host=localhost port=5432 dbname=dbname user=username password=password");
which also fails. I used phpPgAdmin to see what's up and all of the permissions are set correctly, the database shows up, the username is correct, same with password. I checked where postgre (version 8.4.12 btw) was running and phpPgAdmin tells me "localhost:5432".
The error I get via the command line is the following:
SQLSTATE[08006] [7] FATAL: no pg_hba.conf entry for host "::1", user "myusername", database "my_database", SSL OFF
Now, I tried to find the pg_hba.conf file but I'm not entirely sure where to look for it. Same goes for the error/access logs for pg and google hasn't been any help as far as this goes.
Any idea on what I can do?
localhost points to IPV6 ::1 address on your system. Postgresql makes the difference between ipv6 and ipv4 addresses when dealing with access list.
I was able to install/configure everything correctly. I changed the "host" to 127.0.0.1, not sure why that made a difference but it did :)