The MongoDB replica set is deployed with no additional configuration (e.g., mongodb-compass). It’s the easiest one

I configured this replica set for mongodb transactions, which are required for transactions that use mongodb.

Let’s get right to the point:

The simplest replica set of MongoDB should have at least three MongoDB instances (tested);

Create the file first (the path of the file is created wherever you like) but it should correspond to the following named path:

Mongo0, mongo1, mongo2 create three folders under root.

Create folders configdb and db respectively. Then create the mongod. Conf file in the configdb folder

In mongod. Conf

net:
  port: 27017                                      # This is the boot port
  bindIp: 0.0.0.0                                  Which IP connections are allowed (bind_ip_all)
systemLog:
  logAppend: true                                 # re-start mongodb to concatenate logs to previous log files
security: 
  keyFile: "/data/configdb/mongodbKeyfile.key"    # this path is important (note that this path is the path mapped by the container below)
  authorization: "enabled"                        # Enable client connection authentication
replication: 
  replSetName: "mongoRs"                          The name of the replica setCopy the code

X.509 certificates are officially recommended for internal cluster validation, but keyFile is simple and is mostly used in test environments.

Then according to the configuration file path of keyFile create mongodbKeyfile. Key (note that each instance to a), the command is:

Openssl rand - 756 > / root/base64 mongo0 configdb/mongodbKeyfile. Key / / key length must be between 6 and 1024 characters, and can only contain base64 centralized character. Chmod 400 / root/mongo0 configdb/mongodbKeyfile. Key / / only for file owner read permissionsCopy the code

If you have client authentication enabled but do not use keyFile internal authentication, an error will be reported when initializing the cluster. Therefore, it is best to enable both authentication methods.

To create an instance, create three instances on the same server (IP: 196.168.0.1).

docker run -di --name=mongo0 -p 27018:27017 -v /root/mongo0/configdb:/data/configdb/ -v /root/mongo0/db/:/data/db/ 785C65F61380 (Mirror name or ID) --replSet"mongoRs" --bind_ip_all -f/data/configdb/mongod.conf docker run -di --name=mongo1 -p 27019:27017 -v /root/mongo1/configdb:/data/configdb/ -v / root/mongo1 / db / : / data/db / 785 c65f61380 (mirror name or id) - replSet"mongoRs" --bind_ip_all -f/data/configdb/mongod.conf docker run -di --name=mongo2 -p 27020:27017 -v /root/mongo2/configdb:/data/configdb/ -v / root/mongo2 / db / : / data/db / 785 c65f61380 (mirror name or id) - replSet"mongoRs" --bind_ip_all -f /data/configdb/mongod.confCopy the code

/ root/mongo0 / configdb: / data/configdb/this is mapping mongo configuration (but no configuration inside after instance is created, it feels like not do, anyway, that’s the way to play it) (write this path with you, but to create a good first)

/ root/mongo0 / db / : / data/db/this is the path of the mapping mongo’s stored data (the path as you write, but to create a good first)

“MongoRs” this is the name of your cluster (be sure to replSet otherwise the machine won’t think you started as a replica set and can’t create the cluster)

The -f/data/configdb mongod. Conf this in the configuration file to start the mongo

Mongo0, mongo0, mongo0, mongo0, mongo0, mongo0, mongo0, mongo0, mongo0

Run the following command:

docker exec -it fb1517df4885 mongo adminCopy the code

This command connects to Mongo as admin. After entering the command, the following page appears:

Then you can initialize the cluster, where the host address can be different, but to connect to the line. Something like Aliyun would have to have an open port

rs.initiate({_id:"mongoRs", members:[{_id:0,host:"192.168.0.1:27018"},{_id:1,host:"192.168.0.1:27019"},{_id:1,host:"192.168.0.1:27020"}]});Copy the code

After successful initialization, “OK” : 1 is displayed, and the following command prefix is changed. The configuration is complete.

You can view the status of the replica set by typing rs.status().

Enter rs.config() to view the configuration of the replica set.

Now that the replica set has been created, you can add user authentication:

Run the command

Db.createuser ({user:'admin'.pwd: 'admin123456', roles: [ { role: "root", db: "admin"}}]); // Create a super administrator // role:"root"// db:"admin"Create on this databaseCopy the code

Db.auth (db.auth)"admin"."admin123456")Copy the code

And there you are! Please like this article if it is helpful to you!!

An aside:

When I started the container with the configuration file, the container failed to start. I checked the log and found that the Keyfile could not be found (the previous path was not the one above, but another one like /root/keyfile.key). At that time, I was upset. And it is a reference to the blog on the Internet, how others on the line, mine is not. After looking up various information for a long time, I suddenly remembered that docker container can not directly access external resources, so we can map the container path to the server path, put the file inside, docker container can access.

< · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · >

References:

MongodbKeyfile create

Official site: docs.mongodb.com/manual/tuto…

Mongodb configuration file:

Official site: docs.mongodb.com/manual/refe…

Blog: www.jianshu.com/p/f9f1454f2…

Replica set management: blog.chinaunix.net/uid-2644609…

Blog.csdn.net/weixin_3403…

A copy of the set configuration: blog.csdn.net/pengjunlee/…

Blog.csdn.net/quanmaoluo5…

www.cnblogs.com/cwp-bg/p/10…

Role Description of replica set:

Blog.csdn.net/ljk168/arti…