Blogger: haitianis good
The original blog website:
http://blog.csdn.net/haitianisgood/article/details/73682739
mongodb Cluster in Centos
Summary
A mongod service can have multiple databases, and each database can have multiple tables. The table name here is collection. Each collection can store multiple document. Each document can be stored in the form of BSON (binary json) on the hard disk, so it can store more complex data types. It is stored as a single document. You can add or delete fields to one or a batch of documents at will without affecting other documents. This is called schema-free, which is also the main advantage of document database. Unlike the general key-value database, it stores structural information in its value, so you can read, write and count some domains like a relational database. Mongo's greatest feature is that the query language he supports is very powerful. Its grammar is somewhat similar to object-oriented query language. It can almost achieve most functions similar to single-table query in relational databases, and it also supports data indexing. Mongo can also solve the query efficiency of massive data. According to official documents, when the amount of data reaches more than 50GB, the access speed of Mongo database is 10 times faster than MySQL.
BSON
BSON is the abbreviation of Binary JSON. It is a binary encoding format of JSON document object. BSON, like JSON, supports inserting document objects and arrays into other document objects and arrays, and extends the data type of JSON. For example, BSON has Date type and BinDate type.
BSON has three characteristics: lightweight, cross-platform, efficient Mongo
1. Prerequisite preparation
Prepare three servers
10.8.6.2 10.8.6.3 10.8.6.4
Create directories
mkdir /data
mkdir /data/log/mongodb/
Download binary packages
Enter directory / data
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.4.5.tgz
tar -zxvf mongodb-linux-x86_64-rhel62-3.4.5.tgz
mv mongodb-linux-x86_64-rhel62-3.4.5.tgz mongodb
2. Create and write mongodb.conf
cd /data/mongodb
The three machines have the same configuration files. Create the following configuration files:
vim mongodb.conf
fork = true
port = 27017
dbpath = /data/mongodb/data
logpath = /data/log/mongodb/mongodb.log
logappend = true
replSet = Jackie
directoryperdb = true
journal = true
Save out
Configuration parameters reference website:
https://docs.mongodb.com/manual/reference/program/mongod/
3. Start mongodb
cd /data/mongodb
bin/mongod --config mongodb.conf
4. Initialize the mongodb cluster
Any mongodb landing:
/data/mongodb/bin/mongo
Here _id:"Jackie" is the same as "replSet = Jackie" in the configuration file above.
After connecting to mongdo, enter the following:
config = {"_id" : " Jackie ",
"members" : [
{"_id" : 0, "host" : "10.8.6.2:27017"},
{"_id" : 1, "host" : "10.8.6.3:27017"},
{"_id" : 2, "host" : "10.8.6.4:27017"},
]}
Then return and execute the initialization replica set configuration command:
rs.initiate(config);
Output Success
{ "ok" : 1 }
View the status of cluster nodes
PRIMARY>rs.status();
{
"set" : "Jackie",
"date" : ISODate("2017-06-07T05:48:02.177Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "10.8.6.2:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 66,
"optime" : {
"ts" : Timestamp(1478497627, 2),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-06-07T05:47:07Z"),
"lastHeartbeat" : ISODate("2017-06-07T05:48:01.274Z"),
"lastHeartbeatRecv" : ISODate("2017-06-07T05:48:00.450Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.8.64.54:27017",
"configVersion" : 1
},
{
"_id" : 1,
"name" : "10.8.6.3:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 135,
"optime" : {
"ts" : Timestamp(1478497627, 2),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-06-07T05:47:07Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1478497627, 1),
"electionDate" : ISODate("2017-06-07T05:47:07Z"),
"configVersion" : 1,
"self" : true
}
],
"_id" : 3,
"name" : "10.8.6.3:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 66,
"optime" : {
"ts" : Timestamp(1478497627, 2),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-06-07T05:47:07Z"),
"lastHeartbeat" : ISODate("2017-06-07T05:48:01.274Z"),
"lastHeartbeatRecv" : ISODate("2017-06-07T05:48:00.450Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.8.64.54:27017",
"configVersion" : 1
},
"ok" : 1
}
To complete the building!!!
Reference Web Site
Official website: