I'm trying to copy my db for testing purposes. I'm using the docs for that, but it still fails:
from pymongo import MongoClient
client = MongoClient(username='root', password='pass')
client.admin.command('copydb', fromdb='src', todb='dst')
OperationFailure: no such command: 'copydb', full error: {'ok': 0.0, 'errmsg': "no such command: 'copydb'", 'code': 59, 'codeName': 'CommandNotFound'}
When trying another command, from another section of the doc, it worked:
from pymongo import MongoClient
client = MongoClient(username='root', password='pass')
db = client.src
db.admin.command('copydb', fromdb='src', todb='dst')
The objects used here are different, but this is what the docs say... I still tried using the db object for the copydb - and failed again:
from pymongo import MongoClient
client = MongoClient(username='root', password='pass')
db = client.src
db.admin.command('buildinfo')
TypeError: 'Collection' object is not callable. If you meant to call the 'command' method on a 'Collection' object it is failing because no such method exists.
which means I use a bad object (makes sense, but I don't get how to make it work)
pymongo.version: '3.11.0'
mongodb version: 4.4.1 (running on docker)
Using the help from #AnuragWagh and this nice answer I managed to have my own script to handle dump & restore:
from bson.json_util import dumps, loads
import os
import pymongo
def backup_db(client, db_name, backup_dir):
if not os.path.isdir(backup_dir):
os.makedirs(backup_dir)
database = client[db_name]
collections = database.list_collection_names()
for i, collection_name in enumerate(collections):
col = getattr(database,collections[i])
collection = col.find()
jsonpath = collection_name + '.json'
jsonpath = os.path.join(backup_dir, jsonpath)
with open(jsonpath, 'wb') as jsonfile:
jsonfile.write(dumps(collection).encode())
backup_dir = './bckp'
client = pymongo.MongoClient(host='localhost', username='root', password='pass')
src_db_name = 'Jenkins'
dst_db_name = 'Test'
backup_db(client, src_db_name, backup_dir)
for backup_collection in filter(lambda path: path.endswith('.json'), os.listdir(backup_dir)):
collection_name = backup_collection.rstrip('.json')
with open(os.path.join(backup_dir, backup_collection), 'rb') as backup_file:
data = loads(backup_file.read())
client[dst_db_name][collection_name].insert_many(data)
This is not the ideal solution. If you can, use mongodump & mongorestore. For my case (pymongo on a host without mongodb installed, this seems like the best solution)
I am using pymongo to connect with mongodb, but I unable to access the data.
import pymongo
client = pymongo.MongoClient()
db = client['db_name']
for i in db.<collection>.find():
print(i)
Error.
pymongo.errors.OperationFailure: database error: not authorized for query on <collection>
I am connecting to an Aurora database using python.Everything works when I have a static userid and password;
I am trying to use IAM Authenticated user and the user has been setup and it works well using MySQL Workbench
I am trying to use SQLAlchemy to connect using python - this is supposed to be a no brainier but it does not work
from _collections import OrderedDict
import datetime
from sqlalchemy import create_engine
from boto3 import client
# import pymysql
db_end_point = "yyyyyyyyyyyy.cluster-xxxxxxxxxxxx.eu-west-1.rds.amazonaws.com"
def get_db_password(cluster_end_point, user_name):
rds_client = client('rds', region_name='eu-west-1')
rds_token = rds_client.generate_db_auth_token(DBHostname=cluster_end_point, Port=3306, DBUsername=user_name, Region='eu-west-1')
return rds_token
user_password = get_db_password(db_end_point, "svc_payment_data_write")
print(user_password)
url = "mysql://{}:3306/payment_data_store".format(db_end_point)
rds_credentials = {
'user' : 'svc_payment_data_write', 'passwd' : user_password
}
kw = dict()
kw.update(rds_credentials)
print(kw)
engine = create_engine(url, connect_args=kw)
connection = engine.connect()
I have also tried creating the url with the password string quoted properly. The same thing works in JAVA properly
I am trying to connect to mongodb atlas using python using the following code:
import pymongo
myclient = pymongo.MongoClient("mongodb+srv://username:p%40ssword#cluster0-t9lf6.mongodb.net/test?retryWrites=true&w=majority")
mydb = myclient["test"]
mycol = mydb["somecol"]
dblist = myclient.list_database_names() #trial-1
x = mycol.find_one() #trail-2
I am getting error for both trail-1 and trail-2 as :
OperationFailure: bad auth Authentication failed.
Consider my username to be username and password to be p%40ssword with # replaced with %40. I checked my credentials, they are correct. But still i get this error. What to do ?
I instantiate Mongo Client as below. It works fine. However I am trying to read the DB name (primer here) from the configuration. How do I do that?
from pymongo import MongoClient
client = MongoClient()
db = client.primer # want to read "primer" string from a variable
coll = db.dataset
You could do:
db_name = 'primer'
db = getattr(client, db_name)
if you are trying to connect to only one database you can specify the dbname while creating the db object itself
dbname = "primer"
db = MongoClient()[dbname]