MongoDB 3.0.6的主,从,仲裁节点搭建

在MongoDB所在路径创建log和data目录
mkdir log
mkdir data

在data目录下 创建master、slaver、arbiter路径

mkdir master

mkdir slaver

mkdir arbiter

新建日志文件
在log下执行 touch mongodb.log 创建log文件

在MongoDB根目录下创建master.pid  slaver.pid  arbiter.conf.pid (用来记录启动的进程号)

daemon方式启动的fork参数也可以配置配置文件中 在bin下创建master.conf  slaver.conf  arbiter.conf文件:配置如下 (主,备,仲裁节点)

创建master.conf

#master.conf
dbpath=/usr/local/mongodb-linux-x86_64-3.0.6/data/master
logpath=/usr/local/mongodb-linux-x86_64-3.0.6/log/master.log
pidfilepath=/usr/local/mongodb-linux-x86_64-3.0.6/log/master.pid
directoryperdb=true
logappend=true
replSet=testrs
bind_ip=192.168.77.130
port=27017
oplogSize=10000
fork=true
noprealloc=true


创建
slaver.conf
dbpath=/usr/local/mongodb-linux-x86_64-3.0.6/data/slaver
logpath=/usr/local/mongodb-linux-x86_64-3.0.6/log/slaver.log
pidfilepath=/usr/local/mongodb-linux-x86_64-3.0.6/log/slaver.pid
directoryperdb=true
logappend=true 
replSet=testrs
bind_ip=192.168.77.130
port=27018
oplogSize=10000 
fork=true
noprealloc=true

 

创建
#arbiter.conf
dbpath=/usr/local/mongodb-linux-x86_64-3.0.6/data/arbiter
logpath=/usr/local/mongodb-linux-x86_64-3.0.6/log/arbiter.log
pidfilepath=/usr/local/mongodb-linux-x86_64-3.0.6/arbiter.pid
directoryperdb=true
logappend=true 
replSet=testrs
bind_ip=192.168.77.130
port=27019
oplogSize=10000 
fork=true
noprealloc=true

主从节点启动
./mongod -f master.conf
./mongod -f slaver.conf

./mongod -f arbiter.conf

 

连接相应节点

 

./mongo 10.1.235.62:27017

./mongo 10.1.235.61:27018

......

参数含义:

dbpath:数据存放的目录

logpath:日志存放路径

pidfilepath:用于记录进程号的文件

logappend: 记录日志

relSet:replica set的名字

bind_ip:mongodb的ip地址

port:端口号

oplogSize:mongodb操作日志文件的最大大小

noprealloc:不预先分配存储

 

启动客户端连接
./mongodb
退出
在shell中输入exit

相关命令:
show dbs;  show collections;  show users;  show profile;  show logs


如果想创建一个数据库名称 <coc>
use mydb

要检查当前选择的数据库使用命令:
db

创建的数据库mydb 列表中是不存在的。要显示的数据库,需要把它插入至少一个文件。
db.movie.insert({"name":"tutorials yiibai"})

 

 配置主、从、仲裁节点:

首先连接一个mongdb地址

./mongo 192.168.77.130:27017

执行初始化配置,这里的priority的值越高,初始化完后,该节点就会成为主节点,arbiterOnly:true 代表该节点为仲裁节点。

 cfg={ _id:"testrs", members:[ {_id:0,host:"192.168.77.130:27017",priority:2}, {_id:1,host:"192.168.77.130:27018",priority:1},{_id:2,host:"192.168.77.130:27019",arbiterOnly:true}] };

执行初始化

rs.initiate(cfg)

通过rs.status()查看状态。  这里在自己的机子上初始化可能会报   这个该死的问题折磨死我了,最后发现是磁盘空间不足导致的。所以在自己的机子上做集群可能会无法初始化

具体还在想办法。。。反正原因大概是这个原因~

如果你不想用这种模式,毕竟有时候虚拟机磁盘会不够初始化,可以就搭个单节点自己玩,那么要删除master.conf文件中的replSet=testrs

然后重启

./mongod -f master.conf

随后连接自己~

./mongo 192.168.77.129:27017

OK了~自己玩吧~

 

停止mongodb时 千万不要Kill -9 否则会比较麻烦~  用kill -15就可以啦~

MongoDB与传统SQL:

db.users.find() select * from users

db.users.find({"age" : 27}) select * from users where age = 27

db.users.find({"username" : "joe", "age" : 27}) select * from users where "username" = "joe" and age = 27

db.users.find({}, {"username" : 1, "email" : 1}) select username, email from users

db.users.find({}, {"username" : 1, "_id" : 0}) // no case // 即时加上了列筛选,_id也会返回;必须显式的阻止_id返回

db.users.find({"age" : {"$gte" : 18, "$lte" : 30}}) select * from users where age >=18 and age <= 30 // $lt(<) $lte(<=) $gt(>) $gte(>=)

db.users.find({"username" : {"$ne" : "joe"}}) select * from users where username <> "joe"

db.users.find({"ticket_no" : {"$in" : [725, 542, 390]}}) select * from users where ticket_no in (725, 542, 390)

db.users.find({"ticket_no" : {"$nin" : [725, 542, 390]}}) select * from users where ticket_no not in (725, 542, 390)

db.users.find({"$or" : [{"ticket_no" : 725}, {"winner" : true}]}) select * form users where ticket_no = 725 or winner = true

db.users.find({"id_num" : {"$mod" : [5, 1]}}) select * from users where (id_num mod 5) = 1

db.users.find({"$not": {"age" : 27}}) select * from users where not (age = 27)

db.users.find({"username" : {"$in" : [null], "$exists" : true}}) select * from users where username is null // 如果直接通过find({"username" : null})进行查询,那么连带"没有username"的纪录一并筛选出来

db.users.find({"name" : /joey?/i}) // 正则查询,value是符合PCRE的表达式

db.food.find({fruit : {$all : ["apple", "banana"]}}) // 对数组的查询, 字段fruit中,既包含"apple",又包含"banana"的纪录

db.food.find({"fruit.2" : "peach"}) // 对数组的查询, 字段fruit中,第3个(从0开始)元素是peach的纪录

db.food.find({"fruit" : {"$size" : 3}}) // 对数组的查询, 查询数组元素个数是3的记录,$size前面无法和其他的操作符复合使用

db.users.findOne(criteria, {"comments" : {"$slice" : 10}}) // 对数组的查询,只返回数组comments中的前十条,还可以{"$slice" : -10}, {"$slice" : [23, 10]}; 分别返回最后10条,和中间10条

db.people.find({"name.first" : "Joe", "name.last" : "Schmoe"}) // 嵌套查询

db.blog.find({"comments" : {"$elemMatch" : {"author" : "joe", "score" : {"$gte" : 5}}}}) // 嵌套查询,仅当嵌套的元素是数组时使用,

db.foo.find({"$where" : "this.x + this.y == 10"}) // 复杂的查询,$where当然是非常方便的,但效率低下。对于复杂查询,考虑的顺序应当是 正则 -> MapReduce -> $where

db.foo.find({"$where" : "function() { return this.x + this.y == 10; }"}) // $where可以支持javascript函数作为查询条件

db.foo.find().sort({"x" : 1}).limit(1).skip(10); // 返回第(10, 11]条,按"x"进行排序; 三个limit的顺序是任意的,应该尽量避免skip中使用large-number、

 

https://www.cnblogs.com/yangsy0915/p/4867310.html

posted @ 2021-02-10 18:12  seasonzone  阅读(153)  评论(0编辑  收藏  举报