First, installation environment:
Version: MongoDB Server version:3.4.4
Install Source package: mongodb-linux-x86_64-enterprise-rhel62-3.4.4.tgz
System environment: CentOS Release 6.6 (Final)
Node ip1:172.16.88.195
Node ip2:172.16.88.196
Node ip3:172.16.88.190
Second, node configuration
Operate on three nodes, respectively:
1. Into the installation directory/usr/local/decompression source package:
TAR–ZXVF MONGODB-LINUX-X86_64-ENTERPRISE-RHEL62-3.4.4.TGZMV mongodb-linux-x86_64-enterprise-rhel62-3.4.4 MongoDB
#mkdir-P/home/mongodb/data/
#mkdir –p/home/mongodb/logs
Node 1 configuration:
Vi/etc/mongodb.conf
#数据文件存放目录
DBPath =/opt/mongodb/data/
#日志文件存放目录
LogPath =/opt/mongodb/logs/mongodb.logs
#端口
#bind_ip = 172.16.88.195 Default Listener local, comment listens to all
Port = 27027
#以守护程序的方式启用, which runs in the background
Fork = True
Pidfilepath =/opt/mongodb/27027.pid
#是否禁止http接口, which is the 28017 port-enabled service. False by default, supports
#nohttpinterface = True
Logappend = True
Oplogsize = 200
#directoryperdb = True
Replset = Test
Httpinterface = True
Rest = True
Replindexprefetch = _id_only
Node 2 configuration:
Vi/etc/mongodb.conf
#数据文件存放目录
DBPath =/home/mongodb/data/
#日志文件存放目录
LogPath =/home/mongodb/logs/mongodb.logs
#端口
Port = 27027
#bind_ip = 172.16.88.196 Default Listener local, comment listens to all
#以守护程序的方式启用, which runs in the background
Fork = True
Pidfilepath =/home/mongodb/27027.pid
#是否禁止http接口, which is the 28017 port-enabled service. False by default, supports
#nohttpinterface = True
Logappend = True
Oplogsize = 200
#directoryperdb = True
Replset = Test
Httpinterface = True
Node 3 configuration:
Vi/etc/mongodb.conf
#数据文件存放目录
DBPath =/home/mongodb/data/
#日志文件存放目录
LogPath =/home/mongodb/logs/mongodb.logs
#端口
#bind_ip = 172.16.88.190 Default Listener local, comment listens to all
Port = 27027
#以守护程序的方式启用, which runs in the background
Fork = True
#是否禁止http接口, which is the 28017 port-enabled service. False by default, supports
#nohttpinterface = True
Httpinterface = True
Pidfilepath =/home/mongodb/27027.pid
Logappend = True
Oplogsize = 200
#directoryperdb = True
Replset = Test
Rest = True
Replindexprefetch = _id_only
Start Mongodb:/usr/localmongodb/bin/mongod-f/etc/mongodb.conf on each node
Login:/usr/localmongodb/bin/mongo 172.16.88.190:27027/admin
Config = {
"_id": "Test",
members:[
{"_id": 0,host: "172.16.88.196:27027"},
{"_id": 1,host: "172.16.88.190:27027"},
{"_id": 2,host: "172.16.88.195:27027"}]
}
Rs.initiate (config); # # #初始化集群
--View Replica set members
Config.members
Master node Create library table:
Test:primary> Db.stu.insert ({"Name": "Zhangsan", "Age": 23})
Secondary node synchronization:
Test:secondary> Rs.slaveok ()
test:secondary> Use test
Switched to DB test
Test:secondary> Show Collections
Stu
Stop a MongoDB and view the election situation
Rs.status ()
Since the default from the node is not allowed to read and write, to solve this problem, there are two ways
- Executing Rs.slaveok () on the slave node
- or execute Db.getmongo () on the master node. Setslaveok ()
Third, user creation
1. Create Admin user admin:
Db.createuser ({
User: "Admin",
PWD: "123456",
Roles
[
{
Role: "Useradminanydatabase",
DB: "Admin"
},
{
Role: "Readwriteanydatabase",
DB: "Admin"
},
{
Role: "Dbadminanydatabase",
DB: "Admin"
},
{
Role: "Root",
DB: "Admin"
}
]})
2. Create a DevOps user (read and write access):
Db.createuser (
{
User: "DevOps",
PWD: "2wsx#edc",
Roles: [{role: ' Readwriteanydatabase ', db: ' admin '}]
}
);
Third, AUTH certification:
1. Create an authentication file
OpenSSL rand-base64 >/home/mongodb/keyfile.data
Copy the/home/mongodb/keyfile.data to the other two-node directory:
Scp/home/mongodb/keyfile.data [email protected] $ip:/home/mongodb/
Modify its permissions to: chmod 600/home/mongodb/keyfile.data
2. Modify the configuration
In the configuration file for each node, add:
KeyFile =/home/mongodb/keyfile.data
Auth = True
Restart MongoDB
/usr/localmongodb/bin/mongod-f/etc/mongodb.conf
3. Login Authorization
Login to MongoDB
/usr/localmongodb/bin/mongo 172.16.88.190:27027/admin
Authorization: Db.auth ("admin", "123456")
Db.auth ("DevOps", "2wsx#edc")
Quit Quit ();
Login with Admin User:
/usr/localmongodb/bin/mongo 172.16.88.190:27027/admin-u admin–p 123456
Sign in with a DevOps user:
/usr/localmongodb/bin/mongo 172.16.88.190:27027/admin-u devops–p 2wsx#edc
Replication Set Authentication Complete
Note: You need to perform the operation from the node first: Rs.slaveok ()
4. Configure the hosts:
Vi/etc/hosts
172.16.88.195 mongo.slave0.56xyl.com
172.16.88.196 mongo.slave1.56xyl.com
172.16.88.190 mongo.slave2.56xyl.com
MongoDB replication Set Deployment documentation