The mongodb version of Ubuntu installed by apt-get is older than 2.6.10. Simply delete it all and install it by Docker, which is lightweight, simple and convenient. The process of pitting is as follows.

Host version

Ubuntu14.04

View mongo related images

docker search mongo
Copy the code

Pull Required image

# pull your own image (mongo, Node, or DockerHub)
docker pull mongo

# View an existing mirror
docker images -a
Copy the code

Boot image

The startup mode can be create/run or docker-comemage. yml

  • Basic steps
docker run -p 27017:27017 -v <LocalDirectoryPath>:/data/db --name docker_mongodb -d mongo:latest
The default mongodb port is 27017
# -v mapping directory, native directory: container
# --name sets the name of the container
# -d Set the container to run as a daemon
Copy the code
# 1. Create folders locally
# mkdir /var/docker/mongo/data/
# 2. Map the database
# /var/docker/mongo/data:/data/db
docker run -p 27017:27017 -v /var/docker/mongo/data:/data/db --name docker_mongodb_1 -d mongo:latest
# 3. Check the startup status of docker
docker ps -a
# 4. Test whether the connection is normal (Robo 3T)(whether local docker or server docker can be connected)
# address: IP 27017


You can also start a new Docker process, as follows
# docker run -p 3000:27017 -v /var/docker/mongo/data2:/data/db --name docker_mongodb_2 -d mongo:latest
Copy the code
  • add--authAccessing the database (docker-compose.ymlAdd Auth mode click here to view)

    To be more secure, the default port has also been changed

Delete the previous test container
Start a new container (ECS opens port 27018, host)
docker run -p 27018:27017 -v /var/docker/mongo/data:/data/db --name docker_mongodb -d mongo:latest --auth
# --auth: access the database as authentication

Enter the container using bash (or mongo)
# docker exec -it <id> bash
# check mongo version: mongo --version
# Enter mongo-shell: mongo

# exit exit

Run the mongo command to enter the container
docker exec -it <id> mongo
show dbs
User password authentication
use admin
# create a super administrator.
db.createUser({user:"root".pwd:"123456",roles:[{role:'root',db:'admin'}]})
# db.createUser({user:"admin",pwd:"123456",roles:["userAdminAnyDatabase",db:"admin"]})

# auth validation
use admin
db.auth('root'.'123456')
Copy the code
  • Creating a Common User
# create superuser
# Log in to the container
docker exec -it <id> bash
Enter mongo shell
mongo
# query database
show dbs
Switch # admin
use admin
# auth certification
db.auth('root'.'123456')
[role: assign privileges to roles, db: assign privileges to admin databases]
db.createUser({user:"wang".pwd:"123456",roles:[{ role: "readWrite", db: "car" }]})


# Authenticate ordinary users
db.auth('wang'.'123456')
Switch to the CAR library
use car
#!!!!! Insert test data (!! Note that robo3t will always fail to authenticate if it is inserted first.
db.car.save({name:'rolling',price:1988})

# Robo3t Remote access

Copy the code
  • User view and delete
# check
db.system.users.find()
# remove
db.system.users.remove({user:"wang"})
Copy the code
  • Common docker commands
# container_id | name will do
docker rm <container_id>
docker stop <container_id>
docker start <container_id>
docker restart <container_id>
docker describe <container_id>
Copy the code
  • Docker database import
# above have made the machine with the docker database mapping/var/docker/mongo/data: / data/db
/var/docker/mongo/data internal exec into docker image can also be accessed normally
# to create temporary file is used to store the backup of the database/var/docker/mongo/data/db - dump
# exec enter docker image
Mongorestore --help/mongorestore --version
mongorestore -h localhost:27017 -d car --dir ./db-dump/car -u wang -p 123456
Copy the code

An error is as follows

Failed: can't create session: could not connect to server: connection() : auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": (AuthenticationFailed) Authentication failed.

Note the addition: –authenticationDatabase=admin

The complete import is as follows
mongorestore -h localhost:27017 -d car --dir ./db-dump/car -u wang -p 123456 --authenticationDatabase=admin
Copy the code

Exec export is the same after entering docker image. Data under corresponding folder will be shared (also called data volume) after mapping is done in advance.

Nodejs connects to the database

Due to setting up authentication connection,!! Note: and there are special characters in the password (@,%,:, etc.), it is best not to join the address

The practical scheme is as follows:

? authSource=admin

Indicates that the admin database is used for login authentication, and then the corresponding account database is operated

Connect to the options

The password contains special characters

An example of this code is as follows

Had better not splicing url / / connection way, "mongo: / / username: [email protected]: 27017 / db"
mongoose.connect(` mongo: / / 110.120.119.114:27018 / car? authSource=admin`, {
  useCreateIndex: true.useNewUrlParser: true.useUnifiedTopology: true.useFindAndModify: false.user: 'wang'.// username
  pass: '123456 @ 120 $%' // password
})
Copy the code

The remote connection

tool

Robo 3T

Complete the following steps

Note ali Cloud ECS enable security group 27018

  • IP and port
  • Auth certification
  • Connect the test

The resources

  • Ubuntu Docker build mongodb, enable authorization access to Redis, mysql MSSQL backup restore
  • Get started with Docker
  • mongodb auth