搭建MongoDB服务器
时间:2021-07-01 10:21:17
帮助过:20人阅读
[root@mongodb51 mongodb]# ls
mongodb-linux-x86_64-rhel70-3.6.3.tgz
[root@mongodb51 mongodb]# tar -xf mongodb-linux-x86_64-rhel70-3.6.3.tgz
[root@mongodb51 mongodb]# mkdir /usr/local/mongodb
[root@mongodb51 mongodb]# cd /usr/local/mongodb/
[root@mongodb51 mongodb]# cp -r \
/root/mongodb/mongodb-linux-x86_64-rhel70-3.6.3/bin/ .
[root@mongodb51 mongodb]# ls
bin
[root@mongodb51 mongodb]# mkdir etc
[root@mongodb51 mongodb]# mkdir log
[root@mongodb51 mongodb]# mkdir -p data/db
[root@mongodb51 mongodb]# vim etc/mongodb.conf
dbpath=/usr/local/mongodb/data/db/
logpath=/usr/local/mongodb/log/mongodb.log
logappend=true
fork=true
2)设置PATH变量
- [root@mongodb51 mongodb]# vim /etc/profile
- export PATH=/usr/local/mongodb/bin:$PATH
- [root@mongodb51 mongodb]# source /etc/profile
3)启动服务
- [root@mongodb51 mongodb]# mongod -f /usr/local/mongodb/etc/mongodb.conf
- about to fork child process, waiting until server is ready for connections.
- forked process: 28001
- child process started successfully, parent exiting
4)验证配置,默认端口为27017
- [root@mongodb51 mongodb]# ls /usr/local/mongodb/data/db/
- [root@mongodb51 mongodb]# ps -C mongod
- PID TTY TIME CMD
- 28001 ? 00:00:01 mongod
- [root@mongodb51 mongodb]# netstat -antup | grep mongod
- tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 28001/mongod
5)连接服务
- [root@mongodb51 mongodb]# mongo
- MongoDB shell version v3.6.3
- connecting to: mongodb:
- MongoDB server version: 3.6.3
- ...
- ...
- > show dbs
- admin 0.000GB
- config 0.000GB
- local 0.000GB
- > db
- test
- > exit
- bye
6)停止服务
- [root@mongodb51 mongodb]# mongod --dbpath=/usr/local/mongodb/data/db/ --shutdown
- killing process with pid: 28001
7)由于启动和停止服务名字太长,可以起一个别名
给停止服务起一个别名
- [root@mongodb51 mongodb]# alias cmdb=‘mongod --dbpath=/usr/local/mongodb/data/db/ --shutdown‘
给启动服务起一个别名
- [root@mongodb51 mongodb]# alias smdb=‘mongod -f /usr/local/mongodb/etc/mongodb.conf‘
8)修改配置文件,使用ip和端口连接服务
- [root@mongodb51 mongodb]# vim /usr/local/mongodb/etc/mongodb.conf
- bind_ip=192.168.4.51
- port=27077
- [root@mongodb51 mongodb]# smdb
- about to fork child process, waiting until server is ready for connections.
- forked process: 28240
- child process started successfully, parent exiting
- [root@mongodb51 mongodb]# ps -C mongod
- PID TTY TIME CMD
- 28240 ? 00:00:01 mongod
- [root@mongodb51 mongodb]# netstat -antup | grep mongod
- tcp 0 0 192.168.4.51:27077 0.0.0.0:* LISTEN 28240/mongod
9)连接服务
- [root@mongodb51 mongodb]# mongo --host 192.168.4.51 --port 27077
2 案例2:MongoDB常用管理操作
2.1 问题
- 要求如下:
- 练习库的创建、查看、切换、删除
- 练习集合的创建、查看、删除
- 练习文档的查看、插入、删除
2.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:MongoDB常用管理操作
1)数据库管理
命令格式:
show dbs 查看已有的库
db 显示当前所在的库
use 库名 切换库,若库不存在延时创建库
show collections 或 show tables 查看库下已有集合
db.dropDatabase() 删除当前所在的库
查看
- > show dbs
- admin 0.000GB
- config 0.000GB
- local 0.000GB
创建,切换
- > db
- test
- > use ddsdb
- switched to db ddsdb
- > db
- ddsdb
2)集合管理
命令格式:
show collections 或 show tables 查看集合
db.集合名.drop() 删除集合
db.集合名.save({‘‘,‘‘}) 创建集合,集合不存在时,创建并添加文档
- > db.t1.save({name:"yaya",age:60,addr:"hebei",email:"yaya@163.com"})
- WriteResult({ "nInserted" : 1 })
- > db.t1.save({name:"yaya",age:70})
- WriteResult({ "nInserted" : 1 })
- > show tables
- t1
查看集合里的所有内容
- > db.t1.save({name:"yaya",age:70})
- WriteResult({ "nInserted" : 1 })
- > db.t1.find()
- { "_id" : ObjectId("5badf1b420cdd1574b851f12"), "name" : "yaya", "age" : 60, "addr" : "hebei", "email" : "yaya@163.com" }
- { "_id" : ObjectId("5badf21820cdd1574b851f13"), "name" : "yaya", "age" : 70 }
- >
查看第一行的内容
- > db.t1.findOne()
- {
- "_id" : ObjectId("5badf1b420cdd1574b851f12"),
- "name" : "yaya",
- "age" : 60,
- "addr" : "hebei",
- "email" : "yaya@163.com"
- }
- >
3)文档管理
命令格式:
db.集合名.find()
db.集合名.count()
db.集合名.insert({“name”:”jim”})
db.集合名.find(条件)
db.集合名.findOne() 返回查询一条文档
db.集合名.remove({}) 删除所有文档
db.集合名.remove({条件}) 删除与条件匹配的所有文档
- > db.t1.save({name:"xm",age:18,"addr":"hn","email":"xm.qq.com","like":"nicai"})
- WriteResult({ "nInserted" : 1 })
- > db.t1.count()
- 3
- > db.t1.find({name:"xm"})
- { "_id" : ObjectId("5badf4bc20cdd1574b851f14"), "name" : "xm", "age" : 18, "addr" : "hn", "email" : "xm.qq.com", "like" : "nicai" }
删除与条件匹配的所有文档
- > db.t1.remove({name:"yaya"})
- WriteResult({ "nRemoved" : 2 })
删除所有文档
- > db.t1.remove({})
- WriteResult({ "nRemoved" : 1 })
4)插入记录
- > db.col.insert(
- { title: ‘MongoDB 教程‘,
- description: ‘MongoDB 是一个 Nosql 数据库‘,
- by: ‘MongoDB中文网‘,
- url: ‘http://www.mongodb.org.cn‘,
- tags: [‘mongodb‘, ‘database‘, ‘NoSQL‘],
- likes: 100
- }
- )
- WriteResult({ "nInserted" : 1 })
删除记录
- > db.col.remove({‘title‘:‘MongoDB 教程‘})
- WriteResult({ "nRemoved" : 1 })
步骤二:基本数据类型
1)null:用于表示空值或者不存在的字段,{“x”:null}
- > db.t1.save({name:"bob",work:null})
- WriteResult({ "nInserted" : 1 })
- > db.t1.find()
- { "_id" : ObjectId("5badf71520cdd1574b851f16"), "name" : "bob", "work" : null }
- > db.t1.save({_id:9,name:"jerry",work:null})
- WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 9 })
- > db.t1.find()
- { "_id" : ObjectId("5badf71520cdd1574b851f16"), "name" : "bob", "work" : null }
- { "_id" : 9, "name" : "jerry", "work" : null }
2)布尔值:布尔类型有两个值true和false,{“x”:true}
- > db.t1.save({name:"zhangsan",addr:"shanghai",marry:"true"})
- WriteResult({ "nInserted" : 1 })
- > db.t1.save({name:"lisi",age:35,addr:"beijing",marry:"false"})
- WriteResult({ "nInserted" : 1 })
- > db.t1.find()
- { "_id" : ObjectId("5badf71520cdd1574b851f16"), "name" : "bob", "work" : null }
- { "_id" : 9, "name" : "jerry", "work" : null }
- { "_id" : ObjectId("5badf7b020cdd1574b851f17"), "name" : "zhangsan", "addr" : "shanghai", "marry" : "true" }
- { "_id" : ObjectId("5badf7b720cdd1574b851f18"), "name" : "lisi", "age" : 35, "addr" : "beijing", "marry" : "false" }
- >
3)数值:shell默认使用64为浮点型数值,{“x”:3.14}或{“x”:3},对于整型值,可以使用NumberInt(4字节符号整数)或NumberLong(8字节符号整数),{“x”:NumberInt(“3”)}{“x”:NumberLong(“3”)}
- > db.t1.save({name:"alice",woker:"gcs",pay:28888.88})
- WriteResult({ "nInserted" : 1 })
- > db.t1.find({pay:28888.88})
- { "_id" : ObjectId("5badf80f20cdd1574b851f19"), "name" : "alice", "woker" : "gcs", "pay" : 28888.88 }
- > db.t1.save({name:"lilei",woker:"cxy",pay:28000})
- WriteResult({ "nInserted" : 1 })
- > db.t1.find({pay:28000})
- { "_id" : ObjectId("5badf81f20cdd1574b851f1a"), "name" : "lilei", "woker" : "cxy", "pay" : 28000 }
4)字符串:UTF-8字符串都可以表示为字符串类型的数据,{“x”:“呵呵”}
- > db.t1.save({name:"hehe",woker:null,pay:null})
- WriteResult({ "nInserted" : 1 })
- > db.t1.save({name:"呵呵",woker:"没有",pay:"没有"})
- WriteResult({ "nInserted" : 1 })
5)日期:日期被存储为自新纪元依赖经过的毫秒数,不存储时区,{“x”:new Date()}
- > db.t1.save({name:"bobo",wokertime:new Date()})
- WriteResult({ "nInserted" : 1 })
- > db.t1.find({name:"bobo"})
- { "_id" : ObjectId("5badf8ff6827555e3fd8680f"), "name" : "bobo", "wokertime" : ISODate("2018-09-28T09:48:47.437Z") }
- >
6)正则表达式:查询时,使用正则表达式作为限定条件,语法与JavaScript的正则表达式相同,{“x”:/[abc]/}
- > db.t1.save({procname:"php",code:/abc$/})
- WriteResult({ "nInserted" : 1 })
- > db.t1.save({procname:"php",code:/[a-z]/})
- WriteResult({ "nInserted" : 1 })
- > db.t1.save({procname:"java",code:/[a-z]/})
- WriteResult({ "nInserted" : 1 })
- > db.t1.find()
- ...
- ...
- { "_id" : ObjectId("5badf93b6827555e3fd86810"), "procname" : "php", "code" : /abc$/ }
- { "_id" : ObjectId("5badf9426827555e3fd86811"), "procname" : "php", "code" : /[a-z]/ }
- { "_id" : ObjectId("5badf9496827555e3fd86812"), "procname" : "java", "code" : /[a-z]/ }
7)数组:数据列表或数据集可以表示为数组,{“x”: [“a“,“b”,”c”]},一个字段有多个值
- > db.t1.save({name:"jerry",email:["plj@163.com","lij@yahoo.net","lij@tedu"]})
- WriteResult({ "nInserted" : 1 })
- > db.t1.find({name:"jerry"})
- { "_id" : 9, "name" : "jerry", "work" : null }
- { "_id" : ObjectId("5badf9976827555e3fd86813"), "name" : "jerry", "email" : [ "plj@163.com", "lij@yahoo.net", "lij@tedu" ] }
- >
8)内嵌文档:文档可以嵌套其他文档,被嵌套的文档作为值来处理,{“x”:{“y”:3 }}
- > db.t1.save({book: {zuozhe:"dmy",bname:"yuweizhidao",jiaqian:69,version:2.0 }})
- WriteResult({ "nInserted" : 1 })
- > db.t1.find()
- ...
- ...
- { "_id" : ObjectId("5badfa4a6827555e3fd86814"), "book" : { "zuozhe" : "dmy", "bname" : "yuweizhidao", "jiaqian" : 69, "version" : 2 } }
9) 对象id:对象id是一个12字节的字符串,是文档的唯一标识,{“x”: objectId() }
- > db.t1.save({stunum:ObjectId(),name:"yaya",class:"nsd"})
- WriteResult({ "nInserted" : 1 })
- > db.t1.find({name:"yaya"})
- { "_id" : ObjectId("5badfa966827555e3fd86816"), "stunum" : ObjectId("5badfa966827555e3fd86815"), "name" : "yaya", "class" : "nsd" }
10)二进制数据:二进制数据是一个任意字节的字符串。它不能直接在shell中使用。如果要将非utf-字符保存到数据库中,二进制数据是唯一的方式。
11)代码:查询和文档中可以包括任何JavaScript代码,{“x”:function(){/*…*/}}
- > db.t1.save({lname:"html",codecript:function(){
- WriteResult({ "nInserted" : 1 })
- > db.t1.save({lname:"html",codecript:function(){
- WriteResult({ "nInserted" : 1 })
- > db.t1.find()
- ...
- ...
- { "_id" : ObjectId("5badfd626827555e3fd86817"), "lname" : "html", "codecript" : { "code" : "function (){/*...*/}" } }
- { "_id" : ObjectId("5badfd6a6827555e3fd86818"), "lname" : "html", "codecript" : { "code" : "function (){/*<html><h1>abc</h1></html>*/}" } }
- >
3 案例3:数据导入导出/备份/恢复
3.1 问题
3.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:数据备份与恢复
1)数据备份
- [root@mongodb51 ~]# mongodump --host 192.168.4.51 --port 27077
- 2018-09-28T18:14:12.585+0800 writing admin.system.version to
- 2018-09-28T18:14:12.586+0800 done dumping admin.system.version (1 document)
- 2018-09-28T18:14:12.586+0800 writing ddsdb.t1 to
- 2018-09-28T18:14:12.586+0800 writing test.t1 to
- 2018-09-28T18:14:12.586+0800 writing ddsdb.col to
- 2018-09-28T18:14:12.587+0800 done dumping ddsdb.t1 (17 documents)
- 2018-09-28T18:14:12.588+0800 done dumping test.t1 (1 document)
- 2018-09-28T18:14:12.588+0800 done dumping ddsdb.col (0 documents)
- [root@mongodb51 ~]# ls
- dump
- [root@mongodb51 ~]# bsondump dump/ddsdb/t1.bson
- {"_id":{"$oi