Migrate mongodb native to docker
-
At first you need Backup of your Database.
You can do that with mongodump:
mongodump --db nodebb -u nodebb -p password --gzip --archive=/root/nodebb-backup/mongodb-nodebb-`date +"%d-%m-%y"`.gz
than we create a folder and copy yml to file
mkdir mongodb nodebb-backup vi /root/mongodb/docker-compose.yml services: mongo: image: 'mongo:4.4' restart: unless-stopped ports: - '27017:27017' # environment: # MONGO_INITDB_ROOT_USERNAME: nodebb # MONGO_INITDB_ROOT_PASSWORD: password # MONGO_INITDB_DATABASE: nodebb volumes: - mongo-data:/data/db - /root/nodebb-backup:/root volumes: mongo-data:
Then we start docker Container with your already installed docker and docker-compose file.
docker-compose up -d
Then we have mongo container running without authentication.
We can now import the database backup you made earlier with:docker exec -ti $(docker ps -qf name=mongodb-mongo-1) mongorestore --db nodebb --gzip --archive=/root/mongodb-nodebb-02-04-25_v4-4.gz
Now you can live with no authentication or you need to add root and user to the Database
you need add following to mongodb
docker exec -ti $(docker ps -qf name=mongodb-mongo-1) bash mongo use admin db.getSiblingDB('admin').createUser( { user:"root", pwd:"password", roles: ["root"] }) use nodebb db.createUser( { user: "nodebb2", pwd: "password", // or cleartext password roles: [ { role: "readWrite", db:"nodebb" } ] } ) exit
Now you can enable authentication with enabling it in yml file
services: mongo: image: 'mongo:4.4' restart: unless-stopped ports: - '27018:27017' environment: MONGO_INITDB_ROOT_USERNAME: nodebb MONGO_INITDB_ROOT_PASSWORD: password MONGO_INITDB_DATABASE: nodebb volumes: - mongo-data:/data/db - /root/nodebb-backup:/root volumes: mongo-data:
Then do docker-compose up -d
docker exec -ti $(docker ps -qf name=mongodb-mongo-1) bash mongo use nodebb db.auth('nodebb2','password');
If everything is working correctly you see an ok statement here.
Greetings