MongoDB copy Set (ii) construction

Source: Internet
Author: User

Preparatory work

Environment: CentOS6.5 64-bit

MongoDB 3.0.1 64-bit

First, start the Mongod service


[Email protected] data]#/usr/local/mongodb3.0.1/bin/mongod--port 40144--dbpath=/data/mongodb3.0.1/data/--logpath =/data/mongodb3.0.1/log/mongodb.log--directoryperdb--maxconns--logappend--replset repl/192.168.66.89:50144-- Fork


[Email protected] data]#/usr/local/mongodb3.0.1/bin/mongod--port 50144--dbpath=/data/mongodb3.0.1/db_50144/-- Logpath=/data/mongodb3.0.1/db_50144/mongodb.log--directoryperdb--maxconns--logappend--replSet repl/ 192.168.66.89:40144--fork


[Email protected] data]#/usr/local/mongodb3.0.1/bin/mongod--port 60144--dbpath=/data/mongodb3.0.1/db_60144/-- Logpath=/data/mongodb3.0.1/db_60144/mongodb.log--directoryperdb--maxconns--logappend--replSet repl/ 192.168.66.89:40144--fork

Parameters:

--oplogsize Log operation file Size (5% of the default disk space remaining)

--dbpath Data File path

--logpath log file path

--port port number, default is 27017

--replset the name of the replica set, the parameter for each node in a replica sets is given a copy set name, here is test.

--replset test/This is followed by the IP and port of the other standard node

--maxconns Maximum number of connections

--fork Background Run

--logappend log files are recycled, and if the log file is full, the new log is overwritten with the longest log.

Select a node to log in to the MongoDB database

[Email protected] data]#/usr/local/mongodb3.0.1/bin/mongo--port 40144
MongoDB Shell version:3.0.1
Connecting To:127.0.0.1:40144/test
Server has startup warnings:
2015-05-08t09:06:42.270+0800 i CONTROL [initandlisten] * * Warning:you is running this process as the root user, which I s not recommended.
2015-05-08t09:06:42.270+0800 I CONTROL [Initandlisten]
2015-05-08t09:06:42.270+0800 I CONTROL [Initandlisten]
2015-05-08t09:06:42.270+0800 I CONTROL [initandlisten] * * WARNING:/sys/kernel/mm/transparent_hugepage/enabled is ' Always '.
2015-05-08t09:06:42.270+0800 I CONTROL [initandlisten] * * We suggest setting it to ' never '
2015-05-08t09:06:42.270+0800 I CONTROL [Initandlisten]
2015-05-08t09:06:42.270+0800 I CONTROL [initandlisten] * * WARNING:/sys/kernel/mm/transparent_hugepage/defrag is ' Always '.
2015-05-08t09:06:42.270+0800 I CONTROL [initandlisten] * * We suggest setting it to ' never '
2015-05-08t09:06:42.270+0800 I CONTROL [Initandlisten]

#使用admin数据库

> Use admin;
Switched to DB admin

#定义副本集配置变量并初始化, here's _id: "Replset" and the above command parameter "--replset repl" to keep the same
> Db.runcommand ({"replsetinitiate": {"_id": "Repl", "members": [{"_id": 1, "host": "192.168.66.89:40144"},{"_id": 2, "Host": "192.168.66.89:50144"},{"_id": 3, "host": "192.168.66.89:60144"}]});
{"OK": 1}

#查看副本集信息
Repl:other> Db._admincommand ("Replsetgetstatus");
{
"Set": "Repl",
"Date": Isodate ("2015-05-08t01:18:55.619z"),
"MyState": 1,
"Members": [
{
"_id": 1,
"Name": "192.168.66.89:40144",
"Health": 1,
"State": 1,
"Statestr": "PRIMARY",
"Uptime": 776,
"Optime": Timestamp (1431047920, 1),
"Optimedate": Isodate ("2015-05-08t01:18:40z"),
"Electiontime": Timestamp (1431047923, 1),
"Electiondate": Isodate ("2015-05-08t01:18:43z"),
"ConfigVersion": 1,
"Self": true
},
{
"_id": 2,
"Name": "192.168.66.89:50144",
"Health": 1,
"State": 2,
"Statestr": "Secondary",
"Uptime": 15,
"Optime": Timestamp (1431047920, 1),
"Optimedate": Isodate ("2015-05-08t01:18:40z"),
"Lastheartbeat": Isodate ("2015-05-08t01:18:53.650z"),
"Lastheartbeatrecv": Isodate ("2015-05-08t01:18:53.650z"),
"Pingms": 0,
"Lastheartbeatmessage": "Could not find member-sync from",
"ConfigVersion": 1
},
{
"_id": 3,
"Name": "192.168.66.89:60144",
"Health": 1,
"State": 2,
"Statestr": "Secondary",
"Uptime": 15,
"Optime": Timestamp (1431047920, 1),
"Optimedate": Isodate ("2015-05-08t01:18:40z"),
"Lastheartbeat": Isodate ("2015-05-08t01:18:53.650z"),
"Lastheartbeatrecv": Isodate ("2015-05-08t01:18:53.649z"),
"Pingms": 0,
"Lastheartbeatmessage": "Could not find member-sync from",
"ConfigVersion": 1
}
],
"OK": 1
}

Second, the test

1, the master node writes the data, the other node queries

repl:primary> use Mtdamai;
Switched to DB Mtdamai
Repl:primary> Db.user.insert ({name: "Wangxiuli"});
Writeresult ({"ninserted": 1})
Repl:primary> Db.user.find ();
{"_id": ObjectId ("554c237123db30ab1a233e4b"), "name": "Wangxiuli"}

#备节点查询发现已有数据
repl:secondary> use Mtdamai;
Switched to DB Mtdamai
Repl:secondary> Db.user.find ();
{"_id": ObjectId ("554c237123db30ab1a233e4b"), "name": "Wangxiuli"}


2. Stop the master node
Repl:secondary> Rs.status ();
{
"Set": "Repl",
"Date": Isodate ("2015-05-08t02:51:25.273z"),
"MyState": 2,
"Members": [
{
"_id": 1,
"Name": "192.168.66.89:40144",
"Health": 0,
"State": 8,
"Statestr": "(not Reachable/healthy)",
"Uptime": 0,
"Optime": Timestamp (0, 0),
"Optimedate": Isodate ("1970-01-01t00:00:00z"),
"Lastheartbeat": Isodate ("2015-05-08t02:51:23.984z"),
"Lastheartbeatrecv": Isodate ("2015-05-08t02:50:55.975z"),
"Pingms": 0,
"Lastheartbeatmessage": "Failed attempt to connect to 192.168.66.89:40144; Couldn ' t connect to server 192.168.66.89:40144 (192.168.66.89), connection attempt failed ",
"ConfigVersion":-1
},
{
"_id": 2,
"Name": "192.168.66.89:50144",
"Health": 1,
"State": 1,
"Statestr": "PRIMARY",
"Uptime": 5563,
"Optime": Timestamp (1431053169, 2),
"Optimedate": Isodate ("2015-05-08t02:46:09z"),
"Lastheartbeat": Isodate ("2015-05-08t02:51:24.227z"),
"Lastheartbeatrecv": Isodate ("2015-05-08t02:51:24.120z"),
"Pingms": 0,
"Electiontime": Timestamp (1431053458, 1),
"Electiondate": Isodate ("2015-05-08t02:50:58z"),
"ConfigVersion": 1
},
{
"_id": 3,
"Name": "192.168.66.89:60144",
"Health": 1,
"State": 2,
"Statestr": "Secondary",
"Uptime": 5733,
"Optime": Timestamp (1431053169, 2),
"Optimedate": Isodate ("2015-05-08t02:46:09z"),
"ConfigVersion": 1,
"Self": true
}
],
"OK": 1
}


#发现主节点已经重新被选出
To insert data validation again
Repl:primary> Db.user.insert ({name: "Chenglong"});
Writeresult ({"ninserted": 1})
Repl:primary> Db.user.find ();
{"_id": ObjectId ("554c237123db30ab1a233e4b"), "name": "Wangxiuli"}
{"_id": ObjectId ("554c25202dcdffb154643e56"), "name": "Chenglong"}


#验证备节点不可写
Repl:secondary> Db.user.insert ({name: "Liudehua"});
Writeresult ({"Writeerror": {"code": Undefined, "errmsg": "Not Master"}})


#查询数据已存在 that the data is synchronized to another node
repl:secondary> use Mtdamai;
Switched to DB Mtdamai
Repl:secondary> Db.user.find ();
Error:error: {"$err": "Not Master and Slaveok=false", "Code": 13435}
Repl:secondary> Rs.slaveok ();
Repl:secondary> Db.user.find ();
{"_id": ObjectId ("554c237123db30ab1a233e4b"), "name": "Wangxiuli"}
{"_id": ObjectId ("554c25202dcdffb154643e56"), "name": "Chenglong"}


#再次启动被关闭的节点, the node becomes secondary
Repl:secondary> Rs.status ();
{
"Set": "Repl",
"Date": Isodate ("2015-05-08t07:54:20.607z"),
"MyState": 2,
"Syncingto": "192.168.66.89:50144",
"Members": [
{
"_id": 1,
"Name": "192.168.66.89:40144",
"Health": 1,
"State": 2,
"Statestr": "Secondary",
"Uptime": 19,
"Optime": Timestamp (1431053600, 1),
"Optimedate": Isodate ("2015-05-08t02:53:20z"),
"Lastheartbeat": Isodate ("2015-05-08t07:54:19.079z"),
"Lastheartbeatrecv": Isodate ("2015-05-08t07:54:20.557z"),
"Pingms": 0,
"Syncingto": "192.168.66.89:60144",
"ConfigVersion": 1
},
{
"_id": 2,
"Name": "192.168.66.89:50144",
"Health": 1,
"State": 1,
"Statestr": "PRIMARY",
"Uptime": 23738,
"Optime": Timestamp (1431053600, 1),
"Optimedate": Isodate ("2015-05-08t02:53:20z"),
"Lastheartbeat": Isodate ("2015-05-08t07:54:20.152z"),
"Lastheartbeatrecv": Isodate ("2015-05-08t07:54:20.105z"),
"Pingms": 0,
"Electiontime": Timestamp (1431053458, 1),
"Electiondate": Isodate ("2015-05-08t02:50:58z"),
"ConfigVersion": 1
},
{
"_id": 3,
"Name": "192.168.66.89:60144",
"Health": 1,
"State": 2,
"Statestr": "Secondary",
"Uptime": 23908,
"Optime": Timestamp (1431053600, 1),
"Optimedate": Isodate ("2015-05-08t02:53:20z"),
"Syncingto": "192.168.66.89:50144",
"ConfigVersion": 1,
"Self": true
}
],
"OK": 1
}


#数据也同步过来了
repl:secondary> use Mtdamai;
Switched to DB Mtdamai
Repl:secondary> Db.user.find ();
{"_id": ObjectId ("554c237123db30ab1a233e4b"), "name": "Wangxiuli"}
{"_id": ObjectId ("554c25202dcdffb154643e56"), "name": "Chenglong"}


MongoDB copy Set (ii) construction

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.