How to clone/copy a database in Azure Cosmos DB - mongodb

How do I clone a database in Azure Cosmos DB to a mongod running on localhost:27001?
I've tried the following, but I can't get it working:
db.cloneDatabase("mycosmosdb:mypassword#username.documents.azure.com:10255/MyDatabase?ssl=true&replicaSet=globaldb")
This returns the following when running the command from my local machine where mongod is running :
{
"clonedColls" : [ ],
"ok" : 0,
"errmsg" : "connect failed to replica set mycosmosdb:mypassword#username.documents.azure.com:10255/Mydatabase?ssl=true&replicaSet=globaldb:27017",
"code" : 6,
"codeName" : "HostUnreachable"
}
Trying this variant also fails:
db.copyDatabase("NameOfAzureDB", "NameOfLocalDB", "username.documents.azure.com:10255", "username", "password")
{
"ok" : 0,
"errmsg" : "couldn't connect to server username.documents.azure.com:27017, connection attempt failed"
}

cloneDatabase is not supported on Cosmos DB as clone command that it wraps around is not flexible enough to allow SSL connection and the name of the replica set. It only accepts the hostname and port number and nothing else, so the connection string used in the question is not supported (and there's no such moniker as "mycosmosdb" either).

Related

Installing Mongodb in Kubernetes using Helm

I installed mongodb using Helm in my Kubernetes cluster.
https://github.com/kubernetes/charts/tree/master/stable/mongodb
It works fine and lets me connect to the db. But I cannot do any operation once I log into the mongo server. Here is the error I get when I try to see the collections.
> show collections;
2018-04-19T18:03:59.818+0000 E QUERY [js] Error: listCollections failed: {
"ok" : 0,
"errmsg" : "not authorized on test to execute command { listCollections: 1.0, filter: {}, $db: \"test\" }",
"code" : 13,
"codeName" : "Unauthorized"
} :
_getErrorWithCode#src/mongo/shell/utils.js:25:13
DB.prototype._getCollectionInfosCommand#src/mongo/shell/db.js:942:1
DB.prototype.getCollectionInfos#src/mongo/shell/db.js:954:19
DB.prototype.getCollectionNames#src/mongo/shell/db.js:965:16
shellHelper.show#src/mongo/shell/utils.js:836:9
shellHelper#src/mongo/shell/utils.js:733:15
#(shellhelp2):1:1
I am logging in as the root user using
mongo -p password
I don't know why even the root user has no authorization to do anything.
I found the issue. By default, MongoDB uses the admin DB to authenticate but in the helm chart, the authentication DB is the same as the DB that you create with it. So If I create a DB called test, the authentication DB will also be test

Mongo java driver authentication not working properly

I am trying to access mongodb from java using mongo java driver 3.4. I wrote below code to access mongodb and even though I entered wrong credentials while connecting I am able to access my database.
MongoCredential credential = MongoCredential.createScramSha1Credential("rupesh_user", "rupesh_db",
"admin123".toCharArray());
MongoClient mongoClient= new MongoClient( new ServerAddress( "localhost", 27017 ), Arrays.asList(credential));
When I am trying to access mongo database from terminal in ubuntu. Its asking for authentication.
> use rupesh_db
switched to db rupesh_db
> show collections
2017-08-03T13:07:52.970+0530 E QUERY [thread1] Error: listCollections failed: {
"ok" : 0,
"errmsg" : "not authorized on rupesh_db to execute command { listCollections: 1.0, filter: {} }",
"code" : 13,
"codeName" : "Unauthorized"
} :
From Console you will have to authorize
> use rupesh_db
switched to db rupesh_db
> db.auth("rupesh_user","admin123")
> show collections
More details

Mongo Copy Database Collect Exist Error

I am trying to copy a database from a remote host over to the current database. I am using this command:
rs0:PRIMARY> db.copyDatabase("olddb", "newdb", "xx.xx.xx.137", "user1", "abc123");
But I am getting this error:
{
"done" : true,
"ok" : 0,
"errmsg" : "failed to create collection \"newdb.email_batches\": collection already exists"
}
Except the collect does not exist. What could I be doing wrong in this command?
The problem was different database versions. The DB I was on was Mongo 3.0 and the database I was trying to transfer from was 3.2
What is very strange about that is the command copyDatabase as functionaliy for transferring data from 2.6 to 3.2, yet for some reason does not work with 3.0.

Cannot copydb from a remote MongoDB machine. "namespace query failed remote-db-name.system.namespaces"

I have a remote mongodb database, and I want to make a copy to my local machine. I have a read-only user on the remote machine, and I must connect to a slave instance from a replica set.
I am trying to run the copydb command, but this is what happens:
> mongo localhost
MongoDB shell version: 2.4.6
connecting to: localhost
> use admin
switched to db admin
> db.runCommand(
{
"copydb" : 1,
"fromhost" : "thisismyurl_blhblbhlblh:31337",
"fromdb" : "remote-db-name",
"todb" : "local-db-name",
"username" : "myusername",
"key" : "543827989734kjjkf3997834897",
"slaveOK" : true
}
)
{
"ok" : 0,
"errmsg" : "namespace query failed remote-db-name.system.namespaces"
}
>
By the way, I can connect the mongo client to the remote machine with the same username and password, and perform queries with no problems.
Any thoughts?

Can't add member into MongoDB replica-set

I am using the MongoDB 2.4.3, and following the wizard:
http://docs.mongodb.org/manual/tutorial/deploy-replica-set/
But when adding the other members into replica-set, get the following error:
root#vm3:~# mongo
MongoDB shell version: 2.4.3
connecting to: test
rs1:PRIMARY> rs.add("vm1")
{
"errmsg" : "exception: set name does not match the set name host vm1:27017 expects",
"code" : 13145,
"ok" : 0
}
rs1:PRIMARY> rs.add("vm4")
{
"errmsg" : "exception: set name does not match the set name host vm4:27017 expects",
"code" : 13145,
"ok" : 0
}
vm1, vm3 and vm4 know each other because I configured their /etc/hosts files correctly.
Any idea? I don't understand what does this error message mean!
After restarting all vms, it works now.
root#vm3:~# mongo
MongoDB shell version: 2.4.3
connecting to: test
rs1:PRIMARY> rs.add("vm4")
{ "ok" : 1 }
rs1:PRIMARY> rs.add("vm1")
{ "ok" : 1 }
In my case, just restart virtual machines, every thing is fine.
If you are re-installing a MongoDB instance, the replSet may be living in your data file on the drive. I had the same problem setting up a new replica set. The problem was from changing the replica set name after bringing up instances with an older replSet name. I deleted the data files, ran my install scripts again and it worked just fine.