36_部署MongoDB服务 MongoDB基本使用
Posted luwei0915
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了36_部署MongoDB服务 MongoDB基本使用相关的知识,希望对你有一定的参考价值。
版本:3.6.3
1. 部署MongoDB服务 192.168.4.50
创建服务工作目录
]# mkdir /usr/local/mongodb
]# cd /usr/local/mongodb/
]# mkdir etc
]# mkdir log
]# mkdir -p data/db
安装软件
]# tar -zxvf mongodb-linux-x86_64-rhel70-3.6.3.tgz
]# cd mongodb-linux-x86_64-rhel70-3.6.3/
]# cp -r bin /usr/local/mongodb/
编写配置文件
]# vim /usr/local/mongodb/etc/mongodb.conf
logpath=/usr/local/mongodb/log/mongodb.log
logappend=true
dbpath=/usr/local/mongodb/data/db
fork=true
:wq
启动服务
]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/etc/mongodb.conf
bout to fork child process, waiting until server is ready for connections.
forked process: 18252
child process started successfully, parent exiting
]# alias startmdb=\'/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/etc/mongodb.conf\'
查看服务信息
]# netstat -utnlp | grep :27017
]# ps -C mongod
PID TTY TIME CMD
18252 ? 00:00:00 mongod
都有东西:
]# ls /usr/local/mongodb/log/
mongodb.log
]# ls /usr/local/mongodb/data/db
collection-0-4733909942938376078.wt index-1-4733909942938376078.wt _mdb_catalog.wt storage.bson WiredTiger.lock
collection-2-4733909942938376078.wt index-3-4733909942938376078.wt mongod.lock WiredTiger WiredTiger.turtle
diagnostic.data journal sizeStorer.wt WiredTigerLAS.wt WiredTiger.wt
停止服务
]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/etc/mongodb.conf --shutdown
killing process with pid: 18252
]# alias stopmdb=\'/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/etc/mongodb.conf --shutdown\'
使用命令别名启动服务
]# startmdb
bout to fork child process, waiting until server is ready for connections.
forked process: 18409
child process started successfully, parent exiting
]# netstat -utnlp | grep :27017
连接mongodb服务
]# /usr/local/mongodb/bin/mongo
> help
db.help() help on db methods
db.mycoll.help() help on collection methods
sh.help() sharding helpers
rs.help() replica set helpers
help admin administrative help
help connect connecting to a db help
help keys key shortcuts
help misc misc things to know
help mr mapreduce
show dbs show database names
show collections show collections in current database
show users show users in current database
show profile show most recent system.profile entries with time >= 1ms
show logs show the accessible logger names
show log [name] prints out the last segment of log in memory, \'global\' is default
use <db_name> set current database
db.foo.find() list objects in collection foo
db.foo.find( { a : 1 } ) list objects in foo where a == 1
it result of the last line evaluated; use to further iterate
DBQuery.shellBatchSize = x set default number of items to display on shell
exit quit the mongo shell
> exit
启动mongodb服务定义服务使用ip 地址及端口号
]# stopmdb
]# vim /usr/local/mongodb/etc/mongodb.conf
bind_ip=192.168.4.50
port=27050
:wq
]#startmdb
]# netstat -utnlp | grep :27050
tcp 0 0 192.168.4.50:27050 0.0.0.0:* LISTEN 18556/mongod
连接时指定指ip地址和端口号
]# /usr/local/mongodb/bin/mongo --host 192.168.4.50 --port 27050
> exit
2. MongoDB服务基本使用
2.1 常用管理命令
> show dbs
admin 0.000GB
local 0.000GB
> use gamedb(没有则直接创建)
switched to db gamedb
> db //查看当前库
gamedb
> db.t1.save({name:"bob",age:19,sex:"boy",school:"xxx"}) //插入数据
WriteResult({ "nInserted" : 1 })
> db.t1.save({class:"999"})
WriteResult({ "nInserted" : 1 })
> show tables
t1
> db.t1.find() //查看数据
{ "_id" : ObjectId("5e39737e7db4055d1b2e7a5f"), "name" : "bob", "age" : 19, "sex" : "boy", "school" : "xxx" }
{ "_id" : ObjectId("5e3973957db4055d1b2e7a60"), "class" : "999" }
> db.t2.save({name:"tom"})
> db.t2.find()
{ "_id" : ObjectId("5e39740c7db4055d1b2e7a61"), "name" : "tom" }
> db.t1.count() //查询表中一共有多少条数据
2
> db.t1.find({name:"jim"}) (不存在为空)
> db.t1.find({name:"bob"}) //查看bob的数据
{ "_id" : ObjectId("5e39737e7db4055d1b2e7a5f"), "name" : "bob", "age" : 19, "sex" : "boy", "school" : "xxx" }
> db.t1.save({name:"tom",age:20,sex:"girl",school:"yyy"})
> db.t1.findOne() //查询第一条数据
db.t1.findOne()
{
"_id" : ObjectId("5e39737e7db4055d1b2e7a5f"),
"name" : "bob",
"age" : 19,
"sex" : "boy",
"school" : "xxx"
}
> db.t1.remove({name:"tom"}) //删除表数据(tom所有的数据)
WriteResult({ "nRemoved" : 1 })
> db.t2.drop() // 删除表
true
> show tables
t1
> db.t1.remove({}) //清空表数据
WriteResult({ "nRemoved" : 2 })
> db.t1.find() //查看数据
> show tables
t1
2.2 mongodb基本数据类型
字符类型 "abc" "中国"
布尔 true 或false
空 null
数值 64位浮点型 8 NumberLong() NumberInt()
数组 ["","",""]
字符
> db.t1.save({name:"tom"})
布尔
> db.t1.save({name:"jerry",card:true,marry:false})
> db.t1.save({name:"tom",card:true})
空
> db.t1.save({name:"lilei",ruslt:null})
> db.t1.find()
{ "_id" : ObjectId("5e3976217db4055d1b2e7a63"), "name" : "tom" }
{ "_id" : ObjectId("5e39762c7db4055d1b2e7a64"), "name" : "jerry", "card" : true, "marry" : false }
{ "_id" : ObjectId("5e3976337db4055d1b2e7a65"), "name" : "tom", "card" : true }
{ "_id" : ObjectId("5e3976417db4055d1b2e7a66"), "name" : "lilei", "ruslt" : null }
数值
> db.t1.save({name:"mack3",pay:NumberLong(300000)})
> db.t1.save({name:"mack3",pay:NumberInt(300.56)})
> db.t1.save({name:"mack3",pay:300.56})
{ "_id" : ObjectId("5e3976667db4055d1b2e7a67"), "name" : "mack3", "pay" : NumberLong(300000) }
{ "_id" : ObjectId("5e39766f7db4055d1b2e7a68"), "name" : "mack3", "pay" : 300 }
{ "_id" : ObjectId("5e3976757db4055d1b2e7a69"), "name" : "mack3", "pay" : 300.56 }
数组
> db.t1.save({name:"mack4",like:["a","b","c"]})
> db.t1.find()
{ "_id" : ObjectId("5e39769b7db4055d1b2e7a6a"), "name" : "mack4", "like" : [ "a", "b", "c" ] }
代码
> db.t1.save({ lname:"php",dm:function(){/* <?php echo "abc"; ?>*/}})
> db.t1.find({lname:"php"})
{ "_id" : ObjectId("5e3976c47db4055d1b2e7a6b"), "lname" : "php", "dm" : { "code" : "function (){/* <?php echo \\"abc\\"; ?>*/}" } }
对象
> db.t1.save({name:"009", num:ObjectId() })
> db.t1.find()
{ "_id" : ObjectId("5e3976e87db4055d1b2e7a6d"), "name" : "009", "num" : ObjectId("5e3976e87db4055d1b2e7a6c") }
日期
> db.t1.save({ name:"jerry",birthday:new Date() })
{ "_id" : ObjectId("5e3977477db4055d1b2e7a6e"), "name" : "jerry", "birthday" : ISODate("2020-02-04T13:53:11.819Z") }
内嵌
> db.t3.save({
birdbook: { worker:"birdboy" ,pay:99 , ver:3.0},
ttt: { addr:"bg" ,tel:"12306",per:"shy"}
})
> db.t3.find()
{ "_id" : ObjectId("5e39777e7db4055d1b2e7a6f"), "birdbook" : { "worker" : "birdboy", "pay" : 99, "ver" : 3 }, "ttt" : { "addr" : "bg", "tel" : "12306", "per" : "shy" } }
正则表达式
> db.t3.save({name:"cc",bds:/.*a.*/})
> db.t3.save({name:"dd",bds:/^..$/})
> db.t3.save({name:"dd",bds:/^a/})
> db.t3.find()
{ "_id" : ObjectId("5c6a5bf86f800ed6eb2a848b"), "name" : "cc", "bds" : /.*a.*/ }
{ "_id" : ObjectId("5c6a5bfe6f800ed6eb2a848c"), "name" : "dd", "bds" : /^..$/ }
{ "_id" : ObjectId("5c6a5c036f800ed6eb2a848d"), "name" : "dd", "bds" : /^a/ }
2.3 数据导入导出
数据导出: 把集合的文档存储到系统文件里
创建存储文件的目录
]# mkdir /mdb
> db.t1.find() (gamedb库,t1表)
{ "_id" : ObjectId("5e3978812c1cf44a97db51ae"), "name" : "bob", "age" : 19 }
{ "_id" : ObjectId("5e3978842c1cf44a97db51af"), "name" : "tom", "age" : 20 }
导出为json格式
]# /usr/local/mongodb/bin/mongoexport \\
--host 192.168.4.50 --port 27050 \\
-d gamedb -c t1 --type=json > /mdb/gamedb_t1.json(必须有gamedb库,t1表)
2020-02-04T21:59:48.914+0800 connected to: 192.168.4.50:27050
2020-02-04T21:59:48.914+0800 exported 2 records
]# ls /mdb/*.json
/mdb/gamedb_t1.json
]# cat /mdb/gamedb_t1.json
{"_id":{"$oid":"5e3978812c1cf44a97db51ae"},"name":"bob","age":19.0}
{"_id":{"$oid":"5e3978842c1cf44a97db51af"},"name":"tom","age":20.0}
导出为csv格式
]# /usr/local/mongodb/bin/mongoexport \\
--host 192.168.4.50 --port 27050 \\
-d gamedb -c t1 -f name,age --type=csv > /mdb/gamedb_t1.csv
2020-02-04T22:01:14.481+0800 connected to: 192.168.4.50:27050
2020-02-04T22:01:14.481+0800 exported 2 records
]# ls /mdb/*.csv
/mdb/gamedb_t1.csv
]# cat /mdb/gamedb_t1.csv
name,age
bob,19
tom,20
数据导入: 把系统文件的内容存储到集合里
使用json文件导入数据 (库表不存在,则直接创建)
]# /usr/local/mongodb/bin/mongoimport \\
--host 192.168.4.50 --port 27050 \\
-d bbsdb -c user --type=json /mdb/gamedb_t1.json
2020-02-04T22:02:27.167+0800 connected to: 192.168.4.50:27050
2020-02-04T22:02:27.244+0800 imported 2 documents
查看导入数据
]# /usr/local/mongodb/bin/mongo --host 192.168.4.50 --port 27050
> show dbs
> use bbsdb
> show tables
user
> db.user.count()
2
> db.user.find()
{ "_id" : ObjectId("5e3978812c1cf44a97db51ae"), "name" : "bob", "age" : 19 }
{ "_id" : ObjectId("5e3978842c1cf44a97db51af"), "name" : "tom", "age" : 20 }
使用csv文件导入数据
]# /usr/local/mongodb/bin/mongoimport \\
--host 192.168.4.50 --port 27050 \\
-d bbsdb -c user2 -f user,old \\
--type=csv /mdb/gamedb_t1.csv
2020-02-04T22:05:06.264+0800 connected to: 192.168.4.50:27050
2020-02-04T22:05:06.366+0800 imported 3 documents
> use bbsdb
> db.user2.count()
3
> db.user2.find()
{ "_id" : ObjectId("5e397a12727c50e033c960b7"), "user" : "name", "old" : "age" }
{ "_id" : ObjectId("5e397a12727c50e033c960b8"), "user" : "bob", "old" : 19 }
{ "_id" : ObjectId("5e397a12727c50e033c960b9"), "user" : "tom", "old" : 20 }
不加 --drop 选项 是追加方式导入数据 反之 删除原表后再导入数据
]# /usr/local/mongodb/bin/mongoimport \\
--host 192.168.4.50 --port 27050 \\
-d bbsdb -c user5 --headerline --drop \\
--type=csv /mdb/gamedb_t1.csv
2020-02-04T22:07:04.222+0800 connected to: 192.168.4.50:27050
2020-02-04T22:07:04.222+0800 dropping: bbsdb.user5
2020-02-04T22:07:04.353+0800 imported 2 documents
> use bbsdb
> db.user5.count()
2
> db.user5.find()
{ "_id" : ObjectId("5e397a88727c50e033c960ce"), "name" : "bob", "age" : 19 }
{ "_id" : ObjectId("5e397a88727c50e033c960cf"), "name" : "tom", "age" : 20 }
把/etc/passwd文件的内容存储到 bbsdb库里user6集合里。
]# cp /etc/passwd /mdb/
]# sed -i \'s/:/,/g\' /mdb/passwd //逗号替换
]# sed -i \'1iname,password,uid,gid,comment,homedir,shell\' /mdb/passwd //第一行前插入
]# /usr/local/mongodb/bin/mongoimport --host 192.168.4.50 \\
--port 27050 -d bbsdb -c user6 \\
--headerline --type=csv /mdb/passwd
2020-02-04T22:11:32.686+0800 connected to: 192.168.4.50:27050
2020-02-04T22:11:32.818+0800 imported 23 documents
> use bbsdb
> db.user6.count()
23
> db.user6.find()
2.4 数据备份恢复
备份全部
]# cd /mdb
]# ls
]# /usr/local/mongodb/bin/mongodump --host 192.168.4.50 --port 27050
2020-02-04T22:14:17.020+0800 writing admin.system.version to
2020-02-04T22:14:17.021+0800 done dumping admin.system.version (1 document)
2020-02-04T22:14:17.021+0800 writing bbsdb.user6 to
2020-02-04T22:14:17.021+0800 writing gamedb.t3 to
2020-02-04T22:14:17.022+0800 writing bbsdb.user2 to
2020-02-04T22:14:17.022+0800 writing bbsdb.user to
2020-02-04T22:14:17.023+0800 done dumping bbsdb.user6 (23 documents)
2020-02-04T22:14:17.023+0800 writing bbsdb.user5 to
2020-02-04T22:14:17.024+0800 done dumping gamedb.t3 (4 documents)
2020-02-04T22:14:17.024+0800 writing gamedb.t1 to
2020-02-04T22:14:17.025+0800 done dumping bbsdb.user5 (2 documents)
2020-02-04T22:14:17.025+0800 done dumping gamedb.t1 (2 documents)
2020-02-04T22:14:17.026+0800 done dumping bbsdb.user (2 documents)
2020-02-04T22:14:17.026+0800 done dumping bbsdb.user2 (3 documents)
]# ls
]# cd dump/
]# ls
admin bbsdb gamedb
]# ls admin/
system.version.bson system.version.metadata.json
]# cat admin/system.version.bson 报错
]# /usr/local/mongodb/bin/bsondump /mdb/dump/bbsdb/user6.bson
单独备份一个表
]# mkdir /mdbbak
]# /usr/local/mongodb/bin/mongodump --host 192.168.4.50 \\
--port 27050 -d bbsdb -c user6 -o /mdbbak/
2020-02-04T22:17:58.368+0800 writing bbsdb.user6 to
2020-02-04T22:17:58.369+0800 done dumping bbsdb.user6 (23 documents)
]# ls /mdbbak/
]# ls /mdbbak/bbsdb/
user6.bson user6.metadata.json
]# /usr/local/mongodb/bin/bsondump /mdbbak/bbsdb/user6.bson(查看数据)
数据恢复 恢复一个表
]# /usr/local/mongodb/bin/mongorestore \\
--host 192.168.4.50 --port 27050 \\
-d testdb -c testab /mdbbak/bbsdb/user6.bson
2020-02-04T22:21:08.297+0800 checking for collection data in /mdbbak/bbsdb/user6.bson
2020-02-04T22:21:08.298+0800 reading metadata for testdb.testab from /mdbbak/bbsdb/user6.metadata.json
2020-02-04T22:21:08.434+0800 restoring testdb.testab from /mdbbak/bbsdb/user6.bson
2020-02-04T22:21:08.495+0800 no indexes to restore
2020-02-04T22:21:08.495+0800 finished restoring testdb.testab (23 documents)
2020-02-04T22:21:08.495+0800 done
恢复所有数据,删除要恢复的库 再执行恢复
]# /usr/local/mongodb/bin/mongorestore \\
--host 192.168.4.50 --port 27050 /mdb/dump/
以上是关于36_部署MongoDB服务 MongoDB基本使用的主要内容,如果未能解决你的问题,请参考以下文章