Linux MongoDB Replica set deployment steps detailed

Source: Internet
Author: User
Tags auth base64 eval mongodb version openssl versions file permissions node server
Explanation:
There are three servers, and the MongoDB database has been installed. The specific information is as follows:
MongoDB version: mongodb-linux-x86_64-2.6.11
MongoDB installation directory: / usr / local / mongodb
MongoDB database directory: / home / data / mongodb / mongodb_data
MongoDB log directory: / home / data / mongodb / mongodb_log
MongoDB configuration file: /usr/local/mongodb/mongodb.conf
Three server IP addresses:
192.168.0.8
192.168.0.9
192.168.0.10
achieved goals:
Deploy three servers to implement the replica set function.
Specific operation:
First, determine the replica set name, master and slave nodes, configuration files, startup script information
1. Define the replica set name as: osyunwei
2. The master node is: 192.168.0.8
3. The two slave nodes are:
192.168.0.9
192.168.0.10
4. Ensure that the configuration files on the three replica set servers are exactly the same, as follows:
The first way of writing: officially recommended, suitable for versions after 2.6
vi /usr/local/mongodb/mongodb.conf #edit
systemLog:
destination: "file"
path: "/home/data/mongodb/mongodb_log/mongodb.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "/ home / data / mongodb / mongodb_data /"
directoryPerDB: true
processManagement:
fork: true
pidFilePath: "/usr/local/mongodb/mongo.pid"
net:
port: 27017
http:
enabled: true
RESTInterfaceEnabled: true
#security:
# keyFile: "/ usr / local / mongodb / keyfile"
# clusterAuthMode: "keyFile"
# authorization: "enabled"
replication:
replSetName: "osyunwei"
: wq! #Save and exit
The second way of writing: applicable to all versions
vi /usr/local/mongodb/mongodb.conf #edit
port = 27017
dbpath = / home / data / mongodb / mongodb_data /
logpath = / home / data / mongodb / mongodb_log / mongodb.log
pidfilepath = / usr / local / mongodb / mongo.pid
fork = true
logappend = true
shardsvr = true
directoryperdb = true
# auth = true
#keyFile = / usr / local / mongodb / keyfile
replSet = osyunwei
: wq! #Save and exit
Note: The above two configuration files can be written.
5. Make sure that the startup scripts on the three replica set servers are exactly the same, as follows:
vi /etc/init.d/mongod
ulimit -SHn 655350
#! / bin / sh
# chkconfig:-64 36
# description: mongod
case $ 1 in
start)
/ usr / local / mongodb / bin / mongod --maxConns 20000 --config /usr/local/mongodb/mongodb.conf
;;
stop)
/ usr / local / mongodb / bin / mongo 127.0.0.1:27017/admin --eval "db.shutdownServer ()"
# / usr / local / mongodb / bin / mongo 127.0.0.1:27017/admin --eval "db.auth ('system', '123456'); db.shutdownServer ()"
;;
status)
/ usr / local / mongodb / bin / mongo 127.0.0.1:27017/admin --eval "db.stats ()"
# / usr / local / mongodb / bin / mongo 127.0.0.1:27017/admin --eval "db.auth ('system', '123456'); db.stats ()"
;;
esac
: wq! #Save and exit
Second, configure the master node
1./usr/local/mongodb/bin/mongo # Enter the master node 192.168.0.8
Detailed deployment steps for MongoDB replica set under Linux-mongodb distributed deployment
rs.initiate ()
rs.conf ()
#Initialize the replica set and set this machine as the primary node PRIMARY
2.Add replica set slave node
rs.add ("192.168.0.9:27017")
rs.add ("192.168.0.10:27017")
3.Set node priority
cfg = rs.conf () #View node order
cfg.members [0] .priority = 1
cfg.members [1] .priority = 1
cfg.members [2] .priority = 2 #Set the node with _ID 2 as the master node
rs.reconfig (cfg) #Make the configuration take effect
exit #Exit the console
Explanation:
The MongoDB replica set determines the priority by setting the priority. The default priority is 1 and the value is a number between 1 and 1000. The priority value is a number between 0 and 100. The larger the number, the higher the priority. This node can never become the primary node primay.
cfg.members [0] .priority = 1 parameter, the number in brackets is the order of nodes viewed by executing rs.conf (), the first node is 0, the second node is 1, and the third node is 2 And so on.
Third, configure the two slave nodes separately
/ usr / local / mongodb / bin / mongo #Enter the slave node 192.168.0.9/10
Detailed deployment steps for MongoDB replica set under Linux-mongodb cluster deployment
db.getMongo (). setSlaveOk () #Set the slave node as a read-only database
exit #Exit the console
Set up a database account and enable login verification
1.Set up a database account
Operate on the master node server 192.168.0.8
/ usr / local / mongodb / bin / mongo #Enter the master node 192.168.0.8
show dbs #List all databases
use admin #Switch to the admin database, the added account is the administrator account.
show collections
db.system.users.find ()
#Add two administrator accounts, one system administrator: system and one database administrator: administrator
db.createUser ({user: "system", pwd: "123456", roles: [{role: "root", db: "admin"}]}) #Add a system administrator account to manage users
db.auth ('system', '123456') #Add administrator user authentication, you can manage all databases after authentication
db.createUser ({user: "administrator", pwd: "123456", roles: [{role: "userAdminAnyDatabase", db: "admin"}]}) #Add a database administrator to manage all databases
db.auth ('administrator', '123456') #Add administrator user authentication, you can manage all databases after authentication
exit #Exit
#Login with the account you just created
/ usr / local / mongodb / bin / mongo -u system -p 123456 --authenticationDatabase admin
exit #Exit
/ usr / local / mongodb / bin / mongo -u administrator -p 123456 --authenticationDatabase admin
exit #Exit
2.Enable login authentication
Operate on the master node server 192.168.0.8
cd / usr / local / mongodb #Enter the mongodb installation directory
openssl rand -base64 21> keyfile #Create a keyfile (use openssl to generate a 21-bit base64 encrypted string)
chmod 600 / usr / local / mongodb / keyfile #Modify the file permissions to 600
cat / usr / local / mongodb / keyfile #View the string just generated, make a record, and use it later.
Note: The above number 21 is preferably a multiple of 3, otherwise the generated string may contain illegal characters and authentication fails.
3.Set up a configuration file
Operate on all nodes separately
192.168.0.8/9/10
Configuration file /usr/local/mongodb/mongodb.conf
The first way of writing:
security:
keyFile: "/ usr / local / mongodb / keyfile"
clusterAuthMode: "keyFile"
authorization: "enabled"
The second way of writing:
auth = true
keyFile = / usr / local / mongodb / keyfile
The startup script uses the following code (comment the original, comment out before enabling)
/ usr / local / mongodb / bin / mongo 127.0.0.1:27017/admin --eval "db.auth ('system', '123456'); db.shutdownServer ()"
/ usr / local / mongodb / bin / mongo 127.0.0.1:27017/admin --eval "db.auth ('system', '123456'); db.stats ()"
4. Set permissions verification file
First enter the master node server 192.168.0.8, check cat / usr / local / mongodb / keyfile # check the string just generated and make a record
Then enter two slave node servers respectively 192.168.0.9/10
vi / usr / local / mongodb / keyfile #Enter the string viewed in the previous step
: wq! #Save and exit
chmod 600 / usr / local / mongodb / keyfile #Modify the file permissions to 600
Five, verify the replica set
Restart each of the three replica set servers
/ usr / local / mongodb / bin / mongo -u system -p 123456 --authenticationDatabase admin #Log in to the console
rs.status () #View the status of the replica set
Detailed deployment steps for MongoDB replica set under Linux-mongodb deployment
exit #Exit
When the master node fails, a new master node is elected on the two slave nodes. After the fault is recovered, the previous master node becomes a slave node.
At this point, the deployment of the MongoDB replica set under Linux is complete.
Further reading:
Official documentation:
http://docs.mongodb.org/manual/reference/configuration-options/
rs.remove ("ip: port"); #Remove copy
/ usr / local / mongodb / bin / mongo --host 192.168.0.9 #Remotely connect to the server
By default, mongodb does not enable authentication.
Single server, add --auth parameter at startup to enable authentication.
Replica set server. When the --auth parameter is enabled, the keyfile parameter must be specified. The communication between nodes is based on the keyfile. The key length must be between 6 and 1024 characters, preferably a multiple of 3. It cannot contain illegal characters .
Reset replica set
rs.stepDown ()
cfg = rs.conf ()
cfg.members [n] .host = 'new_host_name: prot'
rs.reconfig (cfg)
The total number of servers in all nodes of the replica set must be odd. When the number of servers is even, you need to add an arbitration node. The arbitration node does not parameterize the replica set and only has the right to vote.
rs.addArb ("192.168.0.11:27017") #Add arbitration node

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.