This article describes the MongoDB replica set, which is configured for the first 3 nodes: A (primary), B (secondary), C (arbiter)
1. Install a MongoDB on each node of the cluster;
2. Configure the replica set;
3. Configure the user name and password of the replica set;
4. Configure the KeyFile security authentication of the replica set;
5. Configure boot start;
6. Golang connect the MongoDB replica set and perform CRUD operations;
IP per node:
A node: 11.11.11.11
b Node: 22.22.22.22
C Node: 33.33.33.33
First step: Install MongoDB
Each node installs MongoDB
Vi/etc/yum.repos.d/mongodb-enterprise.repo
Add the following to the Mongodb-enterprise.repo file:
[Mongodb-enterprise]
Name=mongodb Enterprise Repository
baseurl=https://repo.mongodb.com/yum/redhat/$releasever/mongodb-enterprise/3.6/$basearch/
Gpgcheck=1
Enabled=1
Gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
Then run the following command
sudo yuminstall-y mongodb-enterprise
Configure mongod.conf
Create the following directory first
/test/db/mongodb |--logs |--keyfiles |--data
Re-configuration log file path, data directory, replica set name
> vi/etc/mongod.conf
Systemlog:
Path:/test/db/mongodb/logs/mongod.log #日志文件的路径
Storage
DbPath:/test/db/mongodb/data #数据目录
Net
port:27017
bindip:11.11.11.11 #客户端通过此ip连接
Replication
Replsetname: "Rs0" #副本集名称
Note : The data directory, log file directory to change the user to Mongod
Chown Mongod:mongod-r/test/db/mongodb/data
Chown Mongod:mongod-r/test/db/mongodb/logs
Start each node separately: A, B, c node
Mongod--config/etc/mongod.conf
Step two: Build a replica set
Suppose we want to use a node as primary, b node as secondary, and C node as arbiter.
Run on the A-node (11.11.11.11) machine.
Mongo
After the connection is successful, start configuring the replica set:
> config = {
"_id": "Rs0",
"Members": [
{
"_id": 0,
"Host": "11.11.11.11:27017"
}
]
}
> rs.initiate (config) #初始化副本集 {"OK": 1}
> Rs.add ({host: ' 22.22.22.22:27017 '})
> Rs.addarb (' 33.33.33.33:27017 ')
> Rs.status () #查看副本集状态, locate the IP of the primary node
Step three: Create an account password
After the replica set is successfully built, you need to create an account, password for the entire replica set
On a node (primary), use client connection, create user rights (master node, can be viewed with rs.status ())
[Root@servera ~]# MONGO
rs0:primary> Use admin
Switched to DB admin
#创建超级管理员: Root
Replset:primary> Db.createuser ({
"User": "Root",
"pwd": "Rootpwd",
"Roles": [
{"Role": "Root", "db": "Admin"}
]
})
Successfully added User: {
"User": "Root",
"Roles": [{"Role": "Root", "db": "admin"}]
}
Rs0:primary> Use MyTest
#创建mytest数据库
Switched to DB mytest
#创建mytest数据库管理员
Replset:primary> Db.createuser ({
"User": "MyTest",
"pwd": "Mytestpwd",
"Roles": [{"Role": "Dbowner", "db": "MyTest"}]
})
Successfully added User: {
"User": "MyTest",
"Roles": [{"Role": "Dbowner", "db": "MyTest"}]
}
Fourth Step: Create KeyFile
To create a key file, three nodes must be generated in one machine, copied to the other two, and modified to 400 file attributes using the same copy of the keyfile.
Create the/test/db/mongodb/keyfiles directory on Node A, node B, and Node C, and then enter the following command on Node A.
OpenSSL rand-base64 756 >/test/db/mongodb/keyfiles/keyfile
chmod 400/test/db/mongodb/keyfiles/keyfile
And the KeyFile synchronization to the other two nodes of the /test/db/mongodb/keyfiles directory;
Close all members of the replica set
Close the Mongod on Node B (secondary), close the Mongod on node C (arbiter), and finally close primary on Node A (mongod)
Run the MONGO shell on B, C, A, respectively, and then enter the following command to close the Mongod
Use admin
Db.shutdownserver ()
Modify the Security configuration item in the configuration file mongo.conf for each node:
Security
KeyFile:/test/db/mongodb/keyfiles/keyfile
Authorization:enabled
Note : Three machines have to be modified mongod.conf. In addition, the KeyFiles directory will change the user to Mongod
Chown Mongod:mongod-r/test/db/mongodb/keyfiles
Restart replica set
[Root@servera ~]# mongod-f/etc/mongod.conf #机器A
[Root@serverb ~]# mongod-f/etc/mongod.conf #机器B
[Root@serverc ~]# mongod-f/etc/mongod.conf #机器C
Fifth step: Set up MongoDB boot start
[Root@servera ~]# Systemctl Enable Mongod.service
[Root@serverb ~]# Systemctl Enable Mongod.service
[Root@serverc ~]# Systemctl Enable Mongod.service
Sixth step: Golang Connect MongoDB
Package main
Import (
"OS"
"FMT"
"github.com/ Globalsign/mgo "
)
Func main () {
//user, password, database, Rs0 are user name, password, database, replica set, please modify the
& nbsp URL: = "Mongodb://user:password@11.11.11.11,22.22.22.22/database?replicaset=rs0"
Session, ERR: = MgO. Dial (URL)
If err! = Nil {
FMT. Println (Err. Error ())
OS. Exit ( -1)
}
session. SetMode (MgO. Monotonic, True)
Conn: = Session. DB ("")
//get a MongoDB connection, then you can make a variety of crud
}