MongoDB

单节点启动

wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.4/x86_64/RPMS/mongodb-org-server-4.4.2-1.el7.x86_64.rpm

rpm -i mongodb-org-server
#此时已自动创建mongod用户, 创建默认 /etc/mongod.conf

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.2.tgz
#解压这个压缩包之后包含, mongod(服务程序,上面已经安装), mongos(路由节点), mongo(客户端,用于连接mongod服务进程)



# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /mongodb/data
  journal:
    enabled: true
#  engine:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.


#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

启动与关闭MongoDB
#不需要认证启动方式
/usr/bin/mongod -f /etc/mongod.conf
#需要认证启动
mongod -f /etc/mongod.conf --auth
db.shutdownServer()
./mongod  --shutdown -f /etc/mongod.conf
	//命令行可以使用help查看命令帮助
	help
	db.help();
	//查看数据库
	show dbs
	//查看数据库版本
	db.version();
	//查看集合
	show collections
	db.getCollectionNames();
	//查看服务状态
	db.serverStatus()
	//查看数据库状态
	db.stats();
	//插入数据
	db.students.insert({"name":"小花", "age":12, "sex":"女"});
	//查询数据
	db.students.findOne({name:"小花"});
	db.students.findOne({age:{$in:[5, 12]}});
	db.students.findOne({age:{$gte:12}});
	//$in 大于等于和小于等于之间的某个数
	//$gt 大于
	//$gte 大于等于
	//$lt 小于
	//$lte 小于等于
	//$ne 不等于 相反的eq是等于直接给出就是等于
	//$nin not in 不在某个范围内
	db.students.findOne({$or:[{age:{$in:[2,15]}},{age:{$gt:2}}]});
	// $or 条件或查询
	db.students.findOne({$or:[{age:{$in:[2,15]}},{age:{$gt:2}}]});
	// $not 条件取反
	db.students.findOne({name:{$not:{$eq:"小草"}}});
	// $nor 返回所有不符合条件的 
	db.students.findOne({$nor:[{name:"小蓝"}]});
	// $exists 是否存在某个字段
	db.students.find({age:{$exists:true}});
	//.count()  .sort()  .skip()  .limit()
	
	
	//$set 更新某一字段数据, 如果没有该字段则直接添加
	db.students.update({name:"小花"},{$set:{age:13}});
	db.students.update({name:"小花"}, {$set:{"age.birthday":"2020.01.02"}});
	//"age.number"更新对象元素
	db.students.update({name:"小花"}, {$set:{"age.number":15}});
	//对象元素为数组
	db.students.update({name:"小花"}, {$set:{"age.history":[1,2,3,4,5,6,7]}});
	//$set更新数组
	db.students.update({name:"小花"}, {$set:{"age.history.0":9}});
	//$push向数组添加元素
	db.students.update({name:"小花"}, {$push:{"age.history":8}});
	//$unset用下标删除数组元素,删除之后该元素为null并没有消失
	db.students.update({name:"小花"}, {$unset:{"age.history.1":""}});
	//$pull 删除值为null的元素, 按值删除数组中的某个元素类似redis中的集合元素删除
db.students.update({name:"小花"}, {$pull:{"age.history":null}});
	//$rename 更改字段名称
	db.students.update({name:"小花"},{$rename:{"color":"colors"}});
	//$inc 某个字段上加数字
	db.students.update({name:"小花"},{$inc:{"age": 1}});
	//$unset 删除某个字段
	db.students.update({name:"小花"},{$unset:{"colors":true}});
	
	//删除文档
	db.students.remove({age:{$gte:18}});

	//创建索引
	db.students.createIndex({"age.number": 1},{unique:true, dropDups:true});
replication
# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /mongodb/data
  journal:
    enabled: true
#  engine:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.


#security:

#operationProfiling:

replication:
  replSetName: MyMongo

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:
//mongo shell 内执行初始化副本集脚本
config = {
... _id: "MyMongo",
... members: [
... 	{_id:91, host:"192.168.1.91:27017"},
... 	{_id:92, host:"192.168.1.92:27017"},
... 	{_id:93, host:"192.168.1.93:27017"},
... 	]
... }

rs.initiate(config);
//查看副本集状态
re.status();
//secondary节点上执行此命令
rs.secondaryOk();
//js变量保存配置信息
conf = rs.conf();
//修改配置
conf.members[1].priority = 2;
//重新配置
rs.reconfig(conf);
sharding
# config server 存储索引的节点
# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /mongodb/data
  journal:
    enabled: true
#  engine:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  #port: 27017
  bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.


#security:

#operationProfiling:

replication:
  replSetName: ForConfig

sharding:
  clusterRole: configsvr

## Enterprise-Only Options

#auditLog:

#snmp:
#最后正常启动 mongod -f /etc/mongod.conf 默认监听在27019端口
#mongs 路由节点
./mongos --configdb ForConfig/192.168.1.94:27019 --fork --logpath /var/log/mongodb/mongod.log
#mongs 路由节点 指定keyFile 指定 bind_ip_all
./mongos --configdb ForConfig/192.168.1.94:27017,192.168.1.94:27018,192.168.1.94:27019 --fork --logpath /var/log/mongodb/mongod.log --keyFile /mongodb/data/mongo.keyfile --bind_ip_all
//测试选项
db.setting.save({_id: "chunksize", value: 1});
// 加入一个副本集
sh.addShard("MyMongo/192.168.1.91:27017,192.168.1.91:27020,192.168.1.93:27018");
// 加入单个shardsvr节点
sh.addShard("192.168.1.92:27017");
// 数据库开启shared, 并指明主shared
sh.enableSharding("school");
//集合开启shared 并指明索引
sh.shardCollection("school.students", {"age": 1});
#shard 节点

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /mongodb/data
  journal:
    enabled: true
#  engine:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  #port: 27017
  bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.


#security:

#operationProfiling:

replication:
  replSetName: MyMongo

sharding:
  clusterRole: shardsvr

## Enterprise-Only Options

#auditLog:
#snmp:
#最后正常启动 mongod -f /etc/mongod.conf 默认监听在27019端口



权限
	use admin;
	//创建超级管理员
	db.createUser({user:"myroot", pwd:"xxxxx", roles:[{"role":"root", "db":"admin"}]});
	//添加一个管理其他账户的账户
	db.createUser({user:"myadmin", pwd:"xxxxx", roles:[{role:"userAdminAnyDatabase", db:"admin"}]});
	//登陆	
	use admin;
	db.auth("myroot","xxxxx");
	//在两个个数据库上创建读写权限用户
	db.createUser({user:"your_world_2",pwd:"xxxxx", roles:[{role:"readWrite", db: "school"},{role:"readWrite", db:"school_2"}]});
	
#生成keyFile
openssl rand 90 -base64 -out /usr/local/mongo.keyFile
配置文件增加认证并指明集群间节点认证文件

#每个replication节点, shard节点, configsvr节点都必须在配置文件中指明keyFile
# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /mongodb/data
  journal:
    enabled: true
#  engine:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.


security:
  keyFile: /mongodb/data/mongo.keyfile
  authorization: enabled

#operationProfiling:

replication:
  replSetName: MyMongo

sharding:
  clusterRole: shardsvr

## Enterprise-Only Options

#auditLog:

#snmp:

集群部署步骤:
部署所有数据副本集, 指明副本集名称, 指明shard节点身份,开启认证, 指明keyFile.
部署configsvr副本集
部署mongos, 加入shard节点
mongos 创建认证账号.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值