MongoDB Backup and Docker Problems - mongodb

I am weeks trying to do backup from my docker without any success.
My Docker Compose:
services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: user
MONGO_INITDB_ROOT_PASSWORD: password
networks:
- internal
volumes:
- /opt/rpg/mongo/data:/data/db
Input: docker exec mongo sh -c 'exec mongodump -d rpg --archive' > /home/rpg/all-collections.archive
Output: Failed: error getting collections for database rpg: error running listCollections. Database: rpg Err: command listCollections requires authentication
Then i tried with password
Input: docker exec mongo sh -c 'exec mongodump -d rpg —uri="mongodb://user:password#mongo:27017/rpg?authSource=admin" --archive' > /home/rpg/all-collections.archive
Output: SASL authentication step: Authentication failed.
After weeks trying i get the connection with:
sudo docker run -it --rm --network internal mongo \
mongo --host mongo \
-u user \
-p password \
--authenticationDatabase admin \
rpg
Now I can see the collections and everything, but i still cant get the backup.
Tried too:
Input:
sudo docker run --rm --network internal mongo \
mongodump --host mongo \
-u user \
-p password \
--authenticationDatabase admin \
--db rpg
> ~/rpg2-collections.archive
Output:
Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed.
Without success, can someone help me ?

Was having the same Authentication problem, and this command worked:
docker exec <docker_container_name> sh -c 'mongodump --uri="mongodb://username:password#localhost:27017/db_name?authSource=admin&readPreference=primary" --archive' > db.dump

I am doing it like this:
mongodump --host='{mongo_server}' --db='{mongo_db}' --collection='{collection_name}' --out={output_folder}
and it works just fine.
though as you can see i am doing collection by collection dump through python script on separate host, because i wasn't able to backup everything at once.
so the first step is to get all collections from container with installed mongo client:
mongo {mongo_server}/{mongo_db} --eval 'db.getCollectionNames()' --quiet
my script also uploads each collection dump to s3 bucket s3cmd, that is why you are seeing only part of it...

After weeks, i get a backup from files (not mongodump).
sudo tar czvf /home/backup.tar.gz /opt/rpg/mongo
And i did a repair and worked at my pc.
mongod --repair ./{{folder}}

Related

How to delete all postgresql data from docker including created users and databses?

I used to create postgresql database for my old project with command:
docker run --name oldpostgresqldb -e POSTGRES_USER=oldadmin -e POSTGRES_PASSWORD=secret -p 5432:5432 -v /data:/var/lib/postgresql/data -d postgres
Then create database with command:
docker exec -i oldpostgresqldb psql -U oldadmin -c "CREATE DATABASE oldDB WITH ENCODING='UTF8' OWNER=oldadmin;"
When I started my new project stoped and removed all containers, images, volumes etc. and even used docker system prune.
Now I am trying to create new container and db with commands:
docker run --name newpostgresqldb -e POSTGRES_USER=newadmin -e POSTGRES_PASSWORD=secret -p 5432:5432 -v /data:/var/lib/postgresql/data -d postgres
and
docker exec -i newpostgresqldb psql -U newadmin -c "CREATE DATABASE newDB WITH ENCODING='UTF8' OWNER=newadmin;"
But I receive psql: error: FATAL: role "newadmin" does not exist.
Furthermore, I still can manage oldDB with oldadmin user in newpostgresqldb container, because user oldadmin is still exists.
How can I delete old data and create new user and database using docker?
Thanks to #Ay0 I've found a solution. I just needed to clear content of /data folder in the host directory manualy.

How to Create MongoDB cluster as Docker Containers

I can able to run single instance on Mongo using the following Docker command
docker run -it --rm -d -p 27017:27017 --user mongodb mongo:3.4
But I can't find out how to configure Config Server and Query Router and also how add shards with Replication
Thanks in Advance
I used this tutorial myself: https://medium.com/#gargar454/deploy-a-mongodb-cluster-in-steps-9-using-docker-49205e231319#.mle6a8wmg
Step 1: create folders
create folders (local on all nodes):
sudo mkdir -p /dockerlocalstorage/data/mongodb
sudo mkdir -p /dockerlocalstorage/backup/mongodb
sudo mkdir -p /dockersharedstorage/config/mongodb/
Step 2: create keyfile
create keyfile as root user and give correct permissions:
sudo su
cd /dockersharedstorage/config/mongodb/
openssl rand -base64 741 > mongodb-keyfile
chmod 600 mongodb-keyfile
sudo chown 999 mongodb-keyfile
Depending on the mount type you might need to use /dockerlocalstorage/ to keep the certs. Mongodb will complain if the permissions are not set correctly (which could be harder to achieve on lets say a cifs mount)
Step 3: setup first node
create mongodb container without auth/keyfile:
docker run --name mongodb \
-v /dockerlocalstorage/data/mongodb:/data/db \
-v /dockersharedstorage/config/mongodb:/opt/keyfile \
--hostname="dock01" \
-p 27017:27017 \
-d mongo
log in to container:
docker exec -it mongodb mongo
create root users, dont forget to change the passwords
use admin
db.createUser( {
user: "admin",
pwd: "PASSWORD",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
});
db.createUser( {
user: "root",
pwd: "PASSWORD",
roles: [ { role: "root", db: "admin" } ]
});
exit and remove the container
exit
docker stop mongodb
docker rm mongodb
Step 4: start nodes
change NODE_NR:
docker run \
-d \
--name mongodb \
-v /dockerlocalstorage/data/mongodb:/data/db \
-v /dockerlocalstorage/backup/mongodb:/data/backup \
-v /dockersharedstore/config/mongodb:/opt/keyfile \
--restart=always \
--hostname="dock01" \
-p 27017:27017 mongo \
--keyFile /opt/keyfile/mongodb-keyfile \
--replSet "SET_NAME"
Step 5: setup replication
connect to node 1:
docker exec -it mongodb mongo
use admin
db.auth("root", "PASSWORD");
initialize the replication set:
rs.initiate()
Check the replica set with: rs.conf() or rs.status()
add others:
rs.add("dock02:27017")
rs.add("dock03:27017")
Step 6: setup mongodump
edit crontab:
sudo su
crontab -l > tempcron
echo new cron into cron file
echo "0 4 * * * docker exec -it mongodb mongodump -u root -p PASSWORD -o /data/backup/daily" >> tempcron
echo "30 4 * * 5 docker exec -it mongodb mongodump -u root -p PASSWORD -o /data/backup/weekly" >> tempcron
install new cron file
crontab tempcron
rm tempcron
exit
You could use docker swarm mode if you want docker native mongodb cluster (Docker >= 1.12 needed).
Have a look at this nice tutorial. This will show you how to get a mongodb cluster with docker, replicated with Config Server. Basically, the steps are:
Create multiple virtual machines (with docker-machine or whatever you use to create a new docker host)
Create the swarm (docker cluster of multiple machines)
Create a swarm overlay network to deal with all your mongodb traffic
Create the services on each swarm node (that will create your mongodb containers on your hosts)
Configure your mongodb replica set
This is a bit of work, but worth it, as when you get there, you'll have tools on docker swarm to orchestrate your mongodb cluster.

Mongorestore command doesn't work as expected

i am trying to import my database to my mongoLab database, but it keep showing the following error:
2016-10-19T21:05:49.183+0800 Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed.
-bash: vd: command not found
This is how I ran my command:
mongorestore -h 243253423.mlab.com:2131242 -d meteor -u <Username> -p <Password> /Users/directory/desktop/mongo/dump
I ran across this error message and I am not sure this was your specific problem but this ended up working for me
mongorestore -d production-db \
-u myusername -p MyPassword \
--authenticationDatabase admin --host mydomain.com \
~/tmp/mongodump/local-production-db/
(be sure to check firewall sudo ufw status and net.bindIp sudo nano /etc/mongod.conf to confirm that you have access and your mongo process is listening on an external port)

How to start a mongodb shell in docker container?

To start the container, I am typing the following command:
sudo docker run -i -t -p 28000:27017 mongo:latest /usr/bin/mongod --smallfiles
But I want to open the shell in this container to type the mongo commands.
What command should I run to do the same?
You can run the interactive mongo shell by running the following command:
docker run -it -p 28000:27017 --name mongoContainer mongo:latest mongo
Otherwise, if your container is already running, you can use the exec command:
docker exec -it mongoContainer mongo
The thing that I struggled too but found a solution:
docker pull mongo
docker run --name CONTAINERNAME --restart=always -d -p 8080:8080 mongo mongod --auth
sudo docker exec -i -t CONTAINERNAME bash
mongo
use admin
db.createUser({user:"user", pwd:"password", roles:[{role:"root", db:"admin"}]})
exit && exit
Now you have created a running Docker container with everything you need. Now if you want to connect from any client with an admin user, just run this
mongo -u "user" -p "password" HOSTIP --authenticationDatabase "admin"
Extending and updating #vladzam answer and if your container is already running in docker, you can use the exec mongosh command with login and pass options like this:
docker exec -it database-dev mongosh -u "myLogin" -p "myPass"
Download the latest MongoDB Docker image from Docker Hub
sudo docker pull mongo
Now set up MongoDB container
docker run --name containername mongo
Interact with the database through the bash shell client
docker exec -it containername bash
Launch the MongoDB shell client
mongosh #now it is mongosh to access shell
It depends which version of MongoDB you're running.
Please see the differences here : The MongoDB Shell versus the Legacy mongo Shell.
For example, with Mongo 3 the executable was mongo :
$ docker run --rm mongo:3 which mongo mongosh mongod
/usr/bin/mongo
/usr/bin/mongod
With Mongo 5 both mongo and mongosh are present :
$ docker run --rm mongo:5 which mongo mongosh mongod
/usr/bin/mongo
/usr/bin/mongosh
/usr/bin/mongod
With Mongo 6 you can only use the newest mongosh :
$ docker run --rm mongo:6 which mongo mongosh mongod
/usr/bin/mongosh
/usr/bin/mongod
Now if you want to try it, run a MongoDB instance :
$ docker run -d -p 29000:27017 --name mongodb-6 mongo:6
Then connect a shell :
$ docker exec -it mongodb-6 mongosh
You'll get something like :
Current Mongosh Log ID: 632456e42dbc88aa0bfe612f
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.5.4
Using MongoDB: 6.0.1
Using Mongosh: 1.5.4
...
You can also pass options like :
$ docker exec -it mongodb-6 mongosh --version
docker exec -it mongoContainer mongosh
i tried mongo, mongod, and mongodb but failed. Then i changed it to mongosh and it works!
Assuming you have mongo installed on your host computer, which was in my case when I asked this question years ago. This is an alternate way I tried: Open a new terminal
mongo 127.0.0.1:28000
Your mongo shell starts in this terminal now.

Access as admin with mongo client ok, fails with mongorestore

Using the mongo client, I can authenticate successfully with my admin account:
$ mongo -u my_admin_username -p my_admin_pass --authenticationDatabase admin
MongoDB shell version: 2.6.3
connecting to: test
>
but when I try to execute mongorestore with the same credentials, it fails:
$ mongorestore -u my_admin_username -p my_admin_pass /backup/20140821/db/myproject/
connected to: 127.0.0.1
assertion: 13 not authorized on admin to execute command { getParameter: 1, authSchemaVersion: 1 }
Why is that? What am I missing? I'd like to execute successfully a mongorestore.
$ mongorestore -u my_admin_username -p my_admin_pass /backup/20140821/db/myproject/ -db myproject
This worked.