Initially set the password of the database : root/123456 , Table name is :testDB
1. query docker Operation of docker ps 2. get into mongodb Container for docker exec -it [ container ID] bash 3.
Create a directory for dump mkdir dump 4. get into db catalogue cd data/db 5. Backup data (path:data/db) mongodump -h
127.0.0.1 --port 27017 -u='root' -p='123456' -d testDB -o /dump 6.
Delete data manually or delete testDB whole db 7. Recover data (path:data/db) mongorestore -h 127.0.0.1:27017
-u='root' -p='123456' -d testDB /dump/testDB
The specific operation is as follows :
This operation is inside the container , If necessary, move the data locally , The operation is as follows .( This action is recommended when creating mongodb The container can be reserved before )
docker-compose.yml
version: '2' services: mongo-container: image: mongo:3.4 container_name:
mongodb environment: - MONGO_INITDB_ROOT_USERNAME=root -
MONGO_INITDB_ROOT_PASSWORD=123456 ports: - "27017:27017" volumes: -
"./data/mongo:/data/db" -
"./data/mongo-entrypoint/:/docker-entrypoint-initdb.d/" - "./copydata:/dump/"
command: mongod
In the file ./copydata:/dump/ namely , local : container Mapping relationship of .
If you just started There is no mapping relationship , It doesn't matter . restart docker that will do
1. close docker docker-compose down 2. restart docker docker-compose up -d Local dump as well as
Inside container dump This file is automatically generated .
Technology