MongoDB Foundation 2 Copy Set Chapter

Source: Internet
Author: User
Tags mongodb

Then the last chapter
在单台服务器资源充分的情况下,可以使用多实例,以便充分使用服务器资源
由于我在虚拟机中做的所以恢复了快照开启服务systemctl start mongod.servicenetstat -ntap>show dbs   //查看数据库admin  0.000GBlocal  0.000GBconfig  0.000GB>use school  //进入school 虽然但是有集合了后会自动创建> for(var i=1;i<=100;i++)db.users.insert({"id":i,"name":"jack"+i})  //先创建100个玩玩> show dbsadmin   0.000GBconfig  0.000GBlocal   0.000GBschool  0.000GB  //就会自动创建> show collections  //查看集合users> show tables   //也是查看集合users> db.users.find()  //查看集合内容{ "_id" : ObjectId("5b49ab2a0bd68f9074b77bb4"), "id" : 1, "name" : "jack1" }此处省略··········db.copyDatabase("school","sheare")  //把school 复制成shearer //内容一样> show dbsadmin   0.000GBconfig  0.000GBlocal   0.000GBschool  0.000GBsheare  0.000GB克隆对方的集合创建多实例mkdir -p /data/mongodb/mongodb2    //创建数据目录cd /data/mongodb/ mkdir logstouch logs/mongodb2.log    //创建日志文件cd logs/chmod 777 *.log     //赋予权限和前面一样
Cloning data
进入到 mongo --port 27018> show dbs  //此时完全是独立的数据admin   0.000GBconfig  0.000GBlocal   0.000GB创建多实例-----------vim mongod2.confsystemLog:省略···path: /data/mongodb/logs/mongodb2.log //手动添加日志文件路径省略····storage:  dbPath: /data/mongodb/mongodb2 //定义数据文件目录net:  port: 27018  //修改端口号mongod -f /etc/mongod2.conf //开启多实例2netstat -ntap | grep 27018tcp        0      0 0.0.0.0:27018           0.0.0.0:*               LISTEN      9870/mongod > db.runCommand({"cloneCollection":"school.user","from":"192.168.100.20.27017"}) //复制 school(数据库中的)uesr(集合)的数据。来自192.168.100.20 27017 里面的数据--------------进程管理-----------> db.currentOp()  //当前进程省略·····"currentOpTime" : "2018-07-14T23:01:50.596+0800",            "opid" : 2206,   //进程编号> db.killOp(2206)  




Copy set
mkdir -p /data/mongodb/mongodb{2,3,4}  //同时创建多个文件数据存放目录mkdir -p /data/mongodb/logs  //为其创建数据日志文件touch /data/mongodb/logs/mongodb{2,3,4}.log //同时创建多个数据日志文件chmod 777 /data/mongodb/logs/*.log  //开放权限vim /etc/mongod.conf  //修改配置文件开启用户replication: //开启用户        replSetName: abc mongod -f /etc/mongod.conf --shutdown  //关闭服务  //正对于莫一个实例 在有多实例的情况下 mongod -f /etc/mongod.conf  //开启服务 cp -p /etc/mongod.conf /etc/mongod2.conf   //创建多端口实现复制集 mongod -f /etc/mongod4.conf  //挨个开启实例 netstat -nata  //查看有没有开启 若有错则检查配置文件日志存放路径以及数据出存放路径 mongo --port 27018  //挨个测试一下是否能近入 mongo //进入默认 27017 定义复制集
Replica Set Configuration
 cfg={"_id":"abc","members":[{"_id":0,"host":"192.168.10.5:27017"},{"_id":1,"host":"192.168.10.5:27018"},{"_id":2,"host":"192.168.10.5:27019"}]}  //定义复制集 节点数据 #初始化配置时保证从节点没有数据> rs.initiate(cfg) #添加节点abc:PRIMARY> rs.add("192.168.10.5:27020")#删除节点abc:PRIMARY> rs.remove("192.168.10.5:27019")

------------------Failover Switching-----------------------

ps aux | grep  mongod  //查看当前进程 就有4个#手动切换abc:PRIMARY> rs.freeze(30)      //暂停30s不参与选举abc:PRIMARY> rs.stepDown(60,30)   //交出主节点位置,维持从节点状态不少于60秒,等待30秒使主节点和从节点日志同步










MongoDB Foundation 2 Copy Set Chapter

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.