博客详情

centos7中安装mongod-4.2 (原创)

作者: 朝如青丝暮成雪
发布时间:2019-12-09 14:24:25  文章分类:centos   阅读(932)  评论(0)

centos7中安装mongod-4.2


1、tgz安装方式(采用)

tar -zxvf  mongodb-linux-x86_64-rhel70-4.2.1.tgz -C /usr/local/
cd /usr/local/
mv  mongodb-linux-x86_64-rhel70-4.2.1 mongodb 

cd mongodb 
mkdir data log 
mkdir  -p /var/run/mongodb

vi mongod.conf,内容如下


# 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: /usr/local/mongodb/log/mongod.log

# Where and how to store data.
storage:
  dbPath: /usr/local/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:



启动服务端
cd  /usr/local/mongod/bin
./mongod  -f ../mongod.conf  

启动客户端cli
./mongo --host 127.0.0.1 --port 27017


关闭服务端
use admin
db.shutdownServer() 

ps -ef |grep mongod | grep -v grep|awk '{print $2}'
killall mongod
kill -2 <pid>
kill <pid>



2、mongo常用命令

#查看所有db
show dbs

#查看当前db
db

#查看表
show tables
show collections

#创建/切换数据库(没有则新建)
use  mydb

#创建表
db.createCollection("t_student")

#插入一些数据 
db.t_student.insert({"name":"张三","score":78,"sex":1,"birthday":"1993-03-23","create_time":"2019-11-02 10:20:20"});
db.t_student.insert({name:"李四","score":85,"sex":1,"birthday":"1993-04-23","create_time":"2019-11-06 10:20:20"});
db.t_student.insert({name:"王五","score":90,"sex":1,"birthday":"1992-04-20","create_time":"2019-11-10 10:20:20"});
db.t_student.insert({name:"陈六","score":92,"sex":2,"birthday":"1994-04-22","create_time":"2019-12-02 10:20:20"});
db.t_student.insert({name:"张三丰","score":99,"sex":1,"birthday":"1989-05-20","create_time":"2019-10-02 10:20:20"});

#查询
db.t_student.find()
db.t_student.find().pretty()

#删除表
db.t_student.drop()

#删除数据库 
use  mydb
db.dropDatabase()

#模糊查询  like
db.getCollection('t_student').find({'name':{$regex:/张/}})
db.getCollection('t_student').find({'name':/张/})
db.getCollection('t_student').find({'name':/zhang/i})

#模糊查询  not like
db.getCollection('t_student').find({'name':{$not:/张/}})



#按时间范围段查询
db.getCollection('t_student').find({
    'create_time': {$gte: '2019-11-01 00:00:00',$lte:'2019-11-30 23:59:59'}
    })

#按时间范围段查询, 分页查询
db.getCollection('t_student').find({
    'create_time': {$gte: '2019-11-01 00:00:00',$lte:'2019-11-30 23:59:59'}
    }).skip(0).limit(10)

#按时间范围段查询,按时间倒序排序,分页查询
    db.getCollection('t_student').find({
    'create_time': {$gte: '2019-11-01 00:00:00',$lte:'2019-11-30 23:59:59'}
    }).sort({create_time:-1}).skip(0).limit(10)



#删除一条
db.t_student.deleteOne({name:'张三'})
#删除多条
db.t_student.deleteMany({sex:1})
#删除所有
db.t_student.deleteMany()



#更新数据
db.t_student.update({'name':'张三'},{$set:{'score':79}},{'multi':true})

#save指定_id字段覆盖更新
db.t_student.save({"_id" : ObjectId("5de77e7ce57719ec0a93212a"),"name":"张三","score":77,"sex":1,"birthday":"1993-03-23"});




关键字:  mongodb  centos
评论信息
暂无评论
发表评论

亲,您还没有登陆,暂不能评论哦! 去 登陆 | 注册

博主信息
   
数据加载中,请稍候...
文章分类
   
数据加载中,请稍候...
阅读排行
 
数据加载中,请稍候...
评论排行
 
数据加载中,请稍候...

Copyright © 叮叮声的奶酪 版权所有
备案号:鄂ICP备17018671号-1

鄂公网安备 42011102000739号