In the learning process, the use of MongoDB is generally direct local connection without user password, but the database used in production is certainly not like this. I learn to use some points to record in my spare time.

The installation process

Pull the mirror

docker pull mongo:latest

Start the container

docker run -itd --name mongo -p 27017:27017 mongo --auth

-p 27017:27017: maps port 27017 of the container service to port 27017 of the host. External users can access mongo services directly from host IP :27017.

–auth: requires a password to access container services.

Then use the following command to add a user and set a password, and try to connect.

Docker exec -it mongo mongo admin # create a user named admin with password 123456. > db.createUser({ user:'admin',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},"readWriteAnyDatabase"]}); # try to connect using the user information created above. > db.auth('admin', '123456')Copy the code

As follows:

Client connection

I’m using a Robo 3T and this is the kind of connection you would normally have if you didn’t have a password

With password:

Create a link,

Due to their initial learning to find a lot of blogs and so on are no password links, and finally see the official documents just know the link way

Mongodb.github. IO /node-mongod…

Convenient for others to learn, but also convenient for their own records.