Connecting Yii2 with remote mongodb database using ssh - mongodb

I'm trying to connect to a mongodb using this configuration in web.php:
'mongodb' => [
'class' => '\yii\mongodb\Connection',
'dsn' => 'mongodb://user:password#10.16.22.72:27017/mobiledata_db',
'options' => [
"username" => "********",
"password" => "*******"
]
],
and it's throwing the following error
MongoDB Exception – yii\mongodb\Exception
No suitable servers found (serverSelectionTryOnce set): [socket timeout calling ismaster on '10.16.22.72:27017']
↵
Caused by: MongoDB\Driver\Exception\ConnectionTimeoutException
No suitable servers found (serverSelectionTryOnce set): [socket timeout calling ismaster on '10.16.22.72:27017']
To mention that i'm able to connect to mongo using compass and ssh.
Do i have to use ssh in connection string? If so how do i do it?

To do that i ha to set up a tunneled ssh:
ssh -fNg -L 27017:127.0.0.1:27017 user#host
And that fixed my problem.

Related

Connecting DigitalOcean Managed MongoDB to Laravel (connection error calling ismaster)

I'm attempting to set up Laravel with the Managed MongoDB provided by DigitalOcean, though for some reason the database is not connecting.
I've hit a wall and I think it's something to do with authSource, but can't replicate it via cli otherwise...
.env file
MONGO_DSN="mongodb+srv://username:password#digitaloceanhostname/databasename?authSource=admin"
MONGO_DATABASE="databasename"
MONGO_USER="username"
MONGO_PASSWORD="password"
MONGO_TLS=true
MONGO_TLS_CERT="./mongo-db-cert.crt"
config/database.php
'connectionmethod' => [
'driver' => 'mongodb',
'dsn' => env('MONGO_DSN'),
'database' => env('MONGO_DATABASE', ''),
'username' => env('MONGO_USER', ''),
'password' => env('MONGO_PASSWORD', ''),
'options' => [
'tls' => (bool) env('MONGO_TLS', false),
'tlsCAFile' => env('MONGO_TLS_CERT', null),
'authSource' => 'admin',
'db' => 'admin',
'database' => 'admin',
],
],
The above causes the following error:
ERROR: No suitable servers found (`serverSelectionTryOnce` set): [connection error calling ismaster on 'digitaloceanhostname'
(MongoDB\\Driver\\Exception\\ConnectionTimeoutException(code: 13053): No suitable servers found (`serverSelectionTryOnce` set): [connection error calling ismaster on 'digitaloceanhostname:27017']
However, when using the CLI to call ismaster, it works:
# mongo "mongodb+srv://MONGO_USER:MONGO_PASSWORD#digitaloceanhostname/MONGO_DATABASE?authSource=admin" --eval 'printjson(db.runCommand({"isMaster": 1}))' --ssl --sslCAFile ./mongo-db-cert.crt
Running it without the ?authSource=admin causes an authentication error, which is making me think the connection error from laravel is the same thing.
Environment:
# mongo --version
MongoDB shell version v3.6.8
# apt list --installed | grep php | grep mongo
php7.4-mongodb/focal,now 1.9.0+1.7.5-6+ubuntu20.04.1+deb.sury.org+1 amd64 [installed]
# php artisan --version
Laravel Framework 5.8.38
I'm probably missing something obvious, but after looking at this all day any input is appreciated!
The solution was to provide the FULL PATH to the certificate.

Unable to obtain the connection string for an Azure Postgres database server using the CLI

I have an Azure database server which I need to obtain the connection string for by way of command line. However, it is telling me that the "az postgres server show-connection-string" is not right, that "show-connection-string" is not recognized. I did a copy and paste from the Microsoft documentation but it gives the error, and I can't seem to find the answer. I could use a hand obtaining the connection string. Here is my CLI command:
az postgres server show-connection-string --server-name myserver1 --admin-user myuser#myserver --admin-password mypassword
Ensure you are running the latest version of the Azure CLI. As of this answer, the latest is 2.26.0 but I got this command to run in CloudShell which is running 2.25.0.
ken#Azure:~$ az postgres server show-connection-string --server-name myserver1 --admin-user myuser#myserver --admin-password mypassword
{
"connectionStrings": {
"C++ (libpq)": "host=myserver1.postgres.database.azure.com port=5432 dbname={database} user=myuser#myserver#myserver1 password=mypassword sslmode=require",
"ado.net": "Server=myserver1.postgres.database.azure.com;Database={database};Port=5432;User Id=myuser#myserver#myserver1;Password=mypassword;",
"jdbc": "jdbc:postgresql://myserver1.postgres.database.azure.com:5432/{database}?user=myuser#myserver#myserver1&password=mypassword",
"node.js": "var client = new pg.Client('postgres://myuser#myserver#myserver1:mypassword#myserver1.postgres.database.azure.com:5432/{database}');",
"php": "host=myserver1.postgres.database.azure.com port=5432 dbname={database} user=myuser#myserver#myserver1 password=mypassword",
"psql_cmd": "postgresql://myuser#myserver#myserver1:mypassword#myserver1.postgres.database.azure.com/{database}?sslmode=require",
"python": "cnx = psycopg2.connect(database='{database}', user='myuser#myserver#myserver1', host='myserver1.postgres.database.azure.com', password='mypassword', port='5432')",
"ruby": "cnx = PG::Connection.new(:host => 'myserver1.postgres.database.azure.com', :user => 'myuser#myserver#myserver1', :dbname => '{database}', :port => '5432', :password => 'mypassword')"
}
}

Mongo::Error::NoServerAvailable: No primary server is available in cluster -- An error occurred when I used logstash to mongodb data in docker

this is my logstash.conf config
input{
stdin{
}
jdbc{
jdbc_connection_string => "jdbc:mysql://ip:3306/database"
jdbc_user => "root"
jdbc_password => ""
jdbc_driver_library => "/app/mysql-connector-java-5.1.46/mysql-connector-java-5.1.46-bin.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
record_last_run => "true"
use_column_value => "false"
tracking_column => "id"
last_run_metadata_path => "/app/info/station_parameter.txt"
clean_run => "false"
jdbc_paging_enabled => "true"
jdbc_page_size => 100
statement => "select * from database where id>=1"
schedule => "* * * * *"
}
}
output{
mongodb{
uri => "mongodb://192.168.1.103:27017"
database => "mydata"
collection => "mycollection"
}
}
the error is
Failed to send event to MongoDB, retrying in 3 seconds {:event=>#, :exception=>#]> with timeout=30, LT=0.015>}
Not sure if you find this useful but we experienced the same issue in two different scenarios and resolved it, which I would like to share with you,
client was not able to resolve the Primary Server address due to network related issues in docker files, it was resulting to this error, the mongodb server was up and running but the client was not able to resolve the address so the error was thrown.
mongodb was restarted (or for some reason connection was lost) and when mongodb was back online, the mongoid client (ruby) was not able to re-connect again, the solution was to restart the client as well, to establish the connection again. After investigation, I would say most likely it was a version incompatibility of the driver and mongodb server.
In general, we were not able to find a permanent solution for it and it appears to be an issue with mongodb drivers, but one thing for sure, please check the compatibility of mongodb driver and the server.

logstash connection to database issue

Using logstash, what I'm trying to do is dump all tagnames from a table in a database to an index. The problem I'm facing here is, the logstash works fine if specify the IP address as 127.0.0.1 for the postgresql connection.
But if I specify my actual IP or some other user's IP i'm not getting the connection and logstash eventually fails.
Here's the configuration :
input {
jdbc {
# Postgres jdbc connection string to our database, empris
jdbc_connection_string => "jdbc:postgresql://127.0.0.1:5432/empris"
# The user we wish to execute our statement as
jdbc_user => "postgres"
#The password for the user
jdbc_password => ""
# The path to our downloaded jdbc driver
jdbc_driver_library => "/home/aravind/Desktop/postgresql-9.4-1206-jdbc4.jar"
# The name of the driver class for Postgresql
jdbc_driver_class => "org.postgresql.Driver"
# our query
statement => "SELECT distinct tagname from filemetadata"
}
}
output {
elasticsearch {
hosts => "192.168.16.238:9200"
index => "arempris"
document_type => "tags"
}
}
And if the jdbc_connection_string is jdbc:postgresql://192.168.16.233:5432,
logstash fails.
Would greatly appreciate your help making me understanding the cause of this. Thanks in advance.
It seems like you ip you are mentioning is not accessible via logstash. Can you try to ping the ip and see if it is able to connect

"FATAL: no pg_hba.conf entry for host" but I can connect trough pgAdmin

I'm having a little trouble with my dev environment for a CakePHP based project.
The thing is, CakePHP can connect in production, but if I run trough XAMPP on my PC, then throws me a
FATAL: no pg_hba.conf entry for host "myexternalip", user "dbuser", database "dbname", SSL off
but using the same parameters I can connect from my PC trough pgAdmin.
I've tried some googling tips that I've found like copy the libpq.dll from c:\xampp\php to c:\xampp\apache\bin but, no luck, had checked pnp.ini and extension=php_pdo_pgsql.dll line is uncommented, had tried reboot apache several times but nothing changes
The project is hosted under heroku platform and my /app/config/database.php looks like
public $production = array(
'datasource' => 'Database/Postgres',
'persistent' => false,
'host' => 'anywhere.at.amazonaws.com',
'login' => 'dbuser',
'password' => 'dbpwd',
'database' => 'dbname',
'encoding' => 'utf8',
);
here's the Stack Trace
- CORE\Cake\Model\Datasource\DboSource.php line 260 → Postgres->connect()
- CORE\Cake\Model\ConnectionManager.php line 105 → DboSource->__construct(array)
- CORE\Cake\Model\Model.php line 3613 → ConnectionManager::getDataSource(string)
- CORE\Cake\Model\Model.php line 1155 → Model->setDataSource(string)
- CORE\Cake\Model\Model.php line 3640 → Model->setSource(string)
- CORE\Cake\Model\Model.php line 827 → Model->getDataSource()
Can anyone give me another hint that could help?
After while I found a solution.
The problem is that's not straightforward.
As the Postgres server is hosted under heroku platform, the default configuration on server is require ssl, so I've came across to this configuration:
public $production = array(
......
'sslmode' => 'require',
);
Now the error has changed to a sslmode value "require" invalid when ssl support is not compiled
then I've googled it and seems that at least under XAMPP v5.5.19, the libpq.dll inside PHP folder wasn't compiled with ssl support
here's the question that helped me solve this problem:
How to installPostgreSQL client library for PHP on Windows with SSL enabled
Seems that pgAdmin's libpq.dll was compiled with ssl support, so that is the cause that I could connect through pgAdmin, but not with php
thanks for help folks!