how to connect mongo read replica from python - python

I can connect with my mongo access with read preference on replica by following command:
mongo DBHOST:27017/db
db.auth('username','password')
rs.slaveOk()
I tried with python, but didnt worked.
Python
from pymongo import Connection
from pymongo import MongoClient
client = MongoClient('mongodb://username:password#DBHOST:27017/DB',readPreference='secondaryPreferred')
Any help?

Related

PyMongo - can't connect to localhost?

I have tried running the following:
import pymongo
from pymongo import MongoClient
client = MongoClient('mongodb://localhost:27017/')
db = client.test_database
collection = db.test_collection
collection.find_one()
but I get
ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused
Any suggestions on how to fix this? I am running this behind a corporate proxy, but have already set environment variables for that in .bashrc.
EDIT
If I run mongo from the terminal, I get
$ mongo
MongoDB shell version v4.0.13
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
2019-10-16T10:30:23.269+0100 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect#src/mongo/shell/mongo.js:344:17
#(connect):2:6
exception: connect failed
Its look like you didn't run any mongo instance on your machine
run the command mongod -f <config_file> and then test it again (this may needs a sudo permissions)

How to connect mongodb database in python on a server ubuntu?

I have the IP address of my apache server, running on ubuntu 18.04. I have installed MongoDB on the server. I have a script in python to connect the database but it doesn't work. I have shh connection without authentication for the database
I have already tried SSHTunnelForwarder but unsuccessfully.
from sshtunnel import SSHTunnelForwarder
from pymongo import MongoClient
MONGO_HOST = 'MY_IP_ADDRESS'
server = SSHTunnelForwarder(
MONGO_HOST,
remote_bind_address=('127.0.0.1', 27017)
)
server.start()
client = MongoClient('127.0.0.1', server.local_bind_port)
db=client.myDatabaseName
I have also tried
client = MongoClient('mongodb://MY_IP_ADDRESS/')

ServerSelectionTimeoutError Pymongo

I'm trying out pymongo for the first time and I keep getting a ServerSelectionTimeoutError. When using mongo commandline to login I run a command as follows
$ mongo-3.0 --ssl test.net:27080/db_qa --sslAllowInvalidCertificates -u content -p
MongoDB shell version: 3.0.12
Enter password:
and I'm able to connect fine but with pymongo I get the error
pymongo.errors.ServerSelectionTimeoutError: test.net:27080: [Errno 60] Operation timed out
My code is as follows
from pymongo import MongoClient
client = MongoClient('mongodb://content:<password>#test.net:27080/db_qa')
client.server_info()
Your connection string is missing the options that your shell command line provides, namely ssl and option to allow invalid certificate.
You could add ?ssl=true&ssl_cert_reqs=CERT_NONE after the database name in the string you are passing to MongoClient or see other options for certificate handling on MongoClient page (scroll to "SSL configuration" section)
So what worked for me was my refreshing my current IP which changed under the "setup connection security" tab

PyMongo MongoClient SSH Connection

I'm trying to establish a connection with a MongoDB database via an SSH connection programmatically from python. I can create a MongoClient object to connect locally but I can't see how to establish an SSH connection for my MongoClient to use.
How would I do this?
First make sure you have no local MongoDB running on your machine, then ssh to the server where MongoDB is running:
ssh -L 27017:MYHOST:27017 MYUSER#MYHOST
Replace MYUSER and MYHOST with your username and host. Then, in another terminal window, run the "mongo" shell from your local computer. By default it connects to localhost:27017, which you've port-forwarded to the remote host. The "mongo" shell should connect correctly.
Then, create a PyMongo connection normally in Python:
>>> from pymongo import MongoClient
>>> c = MongoClient()
>>> c.test.command('buildinfo')
...
More info about SSH tunneling here:
https://help.ubuntu.com/community/SSH/OpenSSH/PortForwarding
This is not MongoDB-specific at all, any network protocol can be tunneled with SSH port-forwarding.

mongodb refusing connection in python

I am using windows8, for writing code I use IDLE. I tried to connect python to mongodb. But when trying to get collection name than it gives an error.
ServerSelectionTimeoutError: localhost:20101: [Errno 10061] No connection could be made because the target machine actively refused it
This is code for which i am getting an error.
from pymongo import MongoClient
connection = MongoClient('localhost',20101)
db = connection['Bhautik']
collection = db['Student']
db.collection_names(include_system_collections=True)
By the output message you probably didn't set your mongo bind_ip or didn't set the dbpath. Try this:
mongod --dbpath <database_path> --bind_ip 127.0.0.1 --port 20101
It would be more helpful to put alongside with your code some information regarding the mongodb configuration, like the server port, if you are using authentication or not, which dbpath you are using and so on.
So put in your question your mongodb.conf (if you are using one) or the command you are using to start the mongo server.
If you are starting to use mongoDB after installation, make C:/data/db because it is a default database directory which mongoDB uses.
To change the database directory, do type below:
C:\Program Files\MongoDB\Server\3.x\bin> mongod --dbpath "c:\custom_folder"
You can try
run mongo like that:
"C:\\Program Files\\MongoDB\\Server\\3.6\\bin\\mongod.exe" --dbpath E:\\data\\db --port 27017 --bind_ip 127.0.0.1
E:\data\db should be your location path
then in you code
it will lok like
client = MongoClient("127.0.0.1", 27017)
db = client['addsome']
datas = db.follow_up
and if you want to access from a distant machine make sure you open the port "27017" in the firewall
Some times it's gives this error when you forgot to run the local server (if it's run with local server).
To run it you need to write on your terminal:
mongod
or, if MongoDB not in PATH, you can find it via this link in your computer:
C:\Program Files\MongoDB\Server\4.0\bin\mongod.exe
In order to run MongoDB,
You should have installed MongoDB into your OS, download it from https://www.mongodb.com/download-center/community?tck=docs_server
Add the installation's bin folder to your system environment variables.
Openup the terminal and check 'mongod' and 'mongo' commands are working.
Then try to rerun your python script.

Categories