Environment Introduction
192.168.1.151 Fedoraserver as the primary library for the replica set, there are already data
192.168.1.152 Fedoraserver from library, empty instance
192.168.1.153 Fedoraserver from library, empty instance
MongoDB replica set and MySQL Gtid based multi-source Three instance loop master-Slave
650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/A7/76/wKioL1nm_E7CCZ3PAACKsl1qQYw193.png-wh_500x0-wm_ 3-wmp_4-s_912918195.png "title=" Untitled. png "alt=" wkiol1nm_e7ccz3paacksl1qqyw193.png-wh_50 "/>
However: compared with MySQL based on keepalive or MHA and other third-party plug-ins to achieve switching or selection of the main, MongoDB self-failover and switch the main function, this product is very high sense
Build steps
MongoDB Installation:
Percona-server-mongodb-34-server
2. MongoDB Configuration:
vim /etc/mongod.conf # mongod.conf, percona Server for MongoDB # for documentation of all options, see: # # There is something special about MONGODB configuration: The colon ":" followed by a space # Where and how to store data. storage: dbpath: /data/mongo/27153/data/ #指定mongodb文件存储位置 journal: enabled: true # engine: mmapv1 # engine: PerconaFT # engine: rocksdb engine: wiredTiger #指定存储引The line Tiger engine and InnoDB are somewhat similar, supporting document locks (row locks) # Storage engine various options # mmapv1: # wiredTiger: # where to write logging data. systemlog: quiet: true destination: file logAppend: true #日志采用追加的方式 path: /data/mongo/27153/log/mongod.log #日志文件位置 processManagement: fork: true #启动线程拉起主线程, Somewhat similar to the Mysqld_safe process pidFilePath: /data/mongo/27153/mongod.pid #PIDFile Location # network interfaces net: port: 27153 #端口, default 27017 bindip: 0.0.0.0 #开放访问的IP范围 security: authorization: enabled #开启验证, keyfile: /data/ mongo/mongodb.key # Use the key file for authentication between replica sets #operationProfiling: #replication: replication: replSetName: zrz #同步集的名称 #sharding: ## Enterprise-only options: #auditLog: #snmp:
3. Construction process:
1. Three off authentication mode
vim/etc/mongod.conf comment out validation-related configuration statements #security: # authorization:enabled #开启验证, # keyfile:/data/mongo/mongodb.key #使用key文件进行副本集之间的认证
2. Start the service:
Mongod-f/etc/mongod.conf
3. Log in on the instance with the data:
MONGO--port 27151mongo>config={"_id": "Zrz", "members": [{"_id": 1,host: "192.168.1.151:27151", priority:100}, {"_id": 2,host: "192.168.1.152:27152", priority:0}]} Define config to variable config in//zrz to group name, priority to select the sovereign weight, the higher the more easily become the main (non-fault case) Mongo>rs.initiate (config)//use Rs.initiate command Initialize replica set configuration Mongo>rs.status//view replica set configuration Mongo>rs.add ({"_id": 3,host: "192.168.1.153:27153", priority:0})//Use a different party Add replica set
4. Turn on authentication:
For security reasons, you need to add authentication to MongoDB's access:
Step 1: Add User Db.createuser () to an instance of the current primary level
Step 2: Close an instance of the secondary level Mongod--shutdown-f/etc/mongod.conf
Step 3: Turn off instances of the primary level
Step 4: Modify the configuration file for each instance to turn on authentication. Configure the account or distribution key,
Step 5: First open the last closed primary instance, then open each secondary instance
There are two ways to authenticate across instances: account and password or use SSL key
Where: Accounts and passwords can only be used on mongos shards of the configuration server, between instances through Config_server authentication
The key file, which is generated by the Base64 string through the OpenSSL program, can be synchronized with the same string
How to generate:
OpenSSL rand-base64 756 >/data/mongo/mongodb.key
Change the properties of the key file to prevent modification:
chmod 400/data/mongo/mongodb.key
Distribute it to your little friends:
Scp/data/mongo/mongodb.key 192.168.1.152:/data/mongo/scp/data/mongo/mongodb.key 192.168.1.153:/data/mongo/
Modify the configuration file to specify the key location
vim/etc/mongod.conf security:authorization:enabled #开启验证, keyfile:/data/mongo/mongodb.key #使用key文件进行副本集之间的认证
Turn on the service in sequence.
Mongod-f/etc/mongo
Common errors and how to resolve them:
1. Build replica set across versions (3.0-3.4), MongoDB does not now support
2. Since the instance failed to join the replica set, some data was accidentally generated, which prevented the primary from joining the replica set
Processing mode: Comment out the copy related configuration file, restart the service, that is, into the standalone mode, using Db.dropdatabase ()
Remove all databases and then re-enter the replica set startup mode to
3. Hint already configured, unable to initialize, use Rs.reconfig (Config{force:true})
4. Use Rs.help () to view commands for the associated replica set
Key file: https://docs.mongodb.com/manual/reference/configuration-options/#security. keyfile
Reconfiguration: https://docs.mongodb.com/manual/reference/method/rs.reconfig/#rs. reconfig
Copy related commands: https://docs.mongodb.com/manual/reference/method/js-replication/
This article is from "Long SQL Road ..." Blog, be sure to keep this source http://l0vesql.blog.51cto.com/4159433/1973850
Setting up a MongoDB replica set