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"`.gzthan 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 -dThen 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.gzNow 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" } ] } ) exitNow 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
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login