MongoDB 启动多实例 和 MongoDB创建删除、备份、复制
在单台服务器资源充足的情况下启用多实例
创建数据和日志存放位置
# 数据存放位置
[[email protected] ~] mkdir -p /data/mongodb/mongodb1
#日志存放位置
[[email protected] ~] mkdir /data/logs
[[email protected] ~] touch /data/logs/mongodb1.log
复制mongodb的配置文件
#复制原有的配置文件重新命名为mongod1.conf
[[email protected] ~] cp /etc/mongod.conf /etc/mongod.conf
#修改配置文件
[[email protected] ~] vim /etc/mongod1.conf
systemLog:
destination: file
logAppend: true
path: /data/mongodb/logs/mongodb1.log #修改日志储存位置
storage:
dbPath: /data/mongodb/mongodb1 #修改数据文件存放位置
net:
port: 27018 #修改端口号
重新启动mongod1.conf
[[email protected] ~] mongod -f /etc/mongod1.conf
about to fork child process, waiting until server is ready for connections.
forked process: 7142
child process started successfully, parent exiting
#进入多实例数据库
[[email protected] mongodb1]# mongo --port 27018
MongoDB shell version v3.6.6
connecting to: mongodb://127.0.0.1:27018/
MongoDB server version: 3.6.6
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2018-07-16T16:59:47.637+0800 I CONTROL [initandlisten]
2018-07-16T16:59:47.637+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-07-16T16:59:47.637+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-07-16T16:59:47.637+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-07-16T16:59:47.637+0800 I CONTROL [initandlisten]
2018-07-16T16:59:47.638+0800 I CONTROL [initandlisten]
2018-07-16T16:59:47.638+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is ‘always‘.
2018-07-16T16:59:47.638+0800 I CONTROL [initandlisten] ** We suggest setting it to ‘never‘
2018-07-16T16:59:47.638+0800 I CONTROL [initandlisten]
2018-07-16T16:59:47.638+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is ‘always‘.
2018-07-16T16:59:47.638+0800 I CONTROL [initandlisten] ** We suggest setting it to ‘never‘
2018-07-16T16:59:47.638+0800 I CONTROL [initandlisten]
>
# MongDB关闭命令
[[email protected] ~]# mongod -f /etc/mongod.conf --shutdown
多实例就创建好了每个多实例都是单独分开的
MongoDB的存储结构
MongoDB逻辑存储结构
MongoDB的逻辑结构主要有文档、集合、和数据库三那个部分组成。其中文档储存是MongoDB的核心概念,它是逻辑储存的最小单元,选购党羽关系型数据库中一行的记录
1:数据库
MongoDB可以建立多个数据库。MongoDB的默认数据库是test,该数据库存储在数据文件要显示它需要向数据库插入一些数据
> show dbs; #查看数据库命令,里面是默认的数据库
admin 0.000GB
config 0.000GB
local 0.000GB
> use shcool;
#创建数据库直接use进入在里面创建集合,
# 输入数据会自动生成数据库
switched to db shcool
> db.createCollection(‘info1‘) #创建数据集合
{ "ok" : 1 }
> show collections #查看数据集合
info1
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
shcool 0.000GB
在几何中插入数据命令db.创建的集合.insert({"joect格式"})固有格式
> db.info1.insert ({"name":"zhangsa","id":2,"hobb":"jap"})
WriteResult({ "nInserted" : 1 })
查询集合里面的数据命令
> db.info1.find()
{ "_id" : ObjectId("5b4d46223ee32e205a4122c2"), "name" : "zhangsa", "id" : 2, "hobb" : "jap" }
循环创建数据
> for (var i=2;i<50;i++)db.info1.insert({"id":i,"name":"jccp"+i})
WriteResult({ "nInserted" : 1 })
# 查看创建的信息
> db.info1.find()
{ "_id" : ObjectId("5b4d46223ee32e205a4122c2"), "name" : "zhangsa", "id" : 2, "hobb" : "jap" }
{ "_id" : ObjectId("5b4d48ee3ee32e205a4122c3"), "id" : 2, "name" : "jccp2" }
{ "_id" : ObjectId("5b4d48ee3ee32e205a4122c4"), "id" : 3, "name" : "jccp3" }
{ "_id" : ObjectId("5b4d48ee3ee32e205a4122c5"), "id" : 4, "name" : "jccp4" }
{ "_id" : ObjectId("5b4d48ee3ee32e205a4122c6"), "id" : 5, "name" : "jccp5" }
{ "_id" : ObjectId("5b4d48ee3ee32e205a4122c7"), "id" : 6, "name" : "jccp6" }
{ "_id" : ObjectId("5b4d48ee3ee32e205a4122c8"), "id" : 7, "name" : "jccp7" }
{ "_id" : ObjectId("5b4d48ee3ee32e205a4122c9"), "id" : 8, "name" : "jccp8" }
{ "_id" : ObjectId("5b4d48ee3ee32e205a4122ca"), "id" : 9, "name" : "jccp9" }
{ "_id" : ObjectId("5b4d48ee3ee32e205a4122cb"), "id" : 10, "name" : "jccp10" }
{ "_id" : ObjectId("5b4d48ee3ee32e205a4122cc"), "id" : 11, "name" : "jccp11" }
{ "_id" : ObjectId("5b4d48ee3ee32e205a4122cd"), "id" : 12, "name" : "jccp12" }
{ "_id" : ObjectId("5b4d48ee3ee32e205a4122ce"), "id" : 13, "name" : "jccp13" }
{ "_id" : ObjectId("5b4d48ee3ee32e205a4122cf"), "id" : 14, "name" : "jccp14" }
{ "_id" : ObjectId("5b4d48ee3ee32e205a4122d0"), "id" : 15, "name" : "jccp15" }
{ "_id" : ObjectId("5b4d48ee3ee32e205a4122d1"), "id" : 16, "name" : "jccp16" }
{ "_id" : ObjectId("5b4d48ee3ee32e205a4122d2"), "id" : 17, "name" : "jccp17" }
{ "_id" : ObjectId("5b4d48ee3ee32e205a4122d3"), "id" : 18, "name" : "jccp18" }
{ "_id" : ObjectId("5b4d48ee3ee32e205a4122d4"), "id" : 19, "name" : "jccp19" }
{ "_id" : ObjectId("5b4d48ee3ee32e205a4122d5"), "id" : 20, "name" : "jccp20" }
Type "it" for more #(输入it往下查看)
查找某个指定的数据
> db.info1.find({"id":10})
{ "_id" : ObjectId("5b4d48ee3ee32e205a4122cb"), "id" : 10, "name" : "jccp10" }
修改把第十行的jccp10改为tom
> db.info1.update({"id":10},{$set:{"name":"tom"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.info1.find({"id":10})
{ "_id" : ObjectId("5b4d48ee3ee32e205a4122cb"), "id" : 10, "name" : "tom" }
删除(集合,数据库)
> db.info1.drop()
true
> db.info1.find()
> db.shcool.database
shcool.shcool.database
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
>
备份数据库
下面是我创建的school数据库,集合里面也有信息,把他们备份导出
> use shcool;
switched to db shcool
> db.createCollection(‘info1‘)
{ "ok" : 1 }
> for (var i=2;i<50;i++)db.info1.insert({"id":i,"name":"jccp"+i})
WriteResult({ "nInserted" : 1 })
> db.info1.find()
{ "_id" : ObjectId("5b4d4bf03ee32e205a4122f3"), "id" : 2, "name" : "jccp2" }
{ "_id" : ObjectId("5b4d4bf03ee32e205a4122f4"), "id" : 3, "name" : "jccp3" }
{ "_id" : ObjectId("5b4d4bf03ee32e205a4122f5"), "id" : 4, "name" : "jccp4" }
{ "_id" : ObjectId("5b4d4bf03ee32e205a4122f6"), "id" : 5, "name" : "jccp5" }
{ "_id" : ObjectId("5b4d4bf03ee32e205a4122f7"), "id" : 6, "name" : "jccp6" }
{ "_id" : ObjectId("5b4d4bf03ee32e205a4122f8"), "id" : 7, "name" : "jccp7" }
{ "_id" : ObjectId("5b4d4bf03ee32e205a4122f9"), "id" : 8, "name" : "jccp8" }
{ "_id" : ObjectId("5b4d4bf03ee32e205a4122fa"), "id" : 9, "name" : "jccp9" }
{ "_id" : ObjectId("5b4d4bf03ee32e205a4122fb"), "id" : 10, "name" : "jccp10" }
{ "_id" : ObjectId("5b4d4bf03ee32e205a4122fc"), "id" : 11, "name" : "jccp11" }
{ "_id" : ObjectId("5b4d4bf03ee32e205a4122fd"), "id" : 12, "name" : "jccp12" }
{ "_id" : ObjectId("5b4d4bf03ee32e205a4122fe"), "id" : 13, "name" : "jccp13" }
{ "_id" : ObjectId("5b4d4bf03ee32e205a4122ff"), "id" : 14, "name" : "jccp14" }
{ "_id" : ObjectId("5b4d4bf03ee32e205a412300"), "id" : 15, "name" : "jccp15" }
{ "_id" : ObjectId("5b4d4bf03ee32e205a412301"), "id" : 16, "name" : "jccp16" }
{ "_id" : ObjectId("5b4d4bf03ee32e205a412302"), "id" : 17, "name" : "jccp17" }
{ "_id" : ObjectId("5b4d4bf03ee32e205a412303"), "id" : 18, "name" : "jccp18" }
{ "_id" : ObjectId("5b4d4bf03ee32e205a412304"), "id" : 19, "name" : "jccp19" }
{ "_id" : ObjectId("5b4d4bf03ee32e205a412305"), "id" : 20, "name" : "jccp20" }
{ "_id" : ObjectId("5b4d4bf03ee32e205a412306"), "id" : 21, "name" : "jccp21" }
Type "it" for more
# 导出
[[email protected] ~]# mongoexport -d school -c info1 -o /opt/school.json
2018-07-17T09:55:16.306+0800 connected to: localhost
2018-07-17T09:55:16.306+0800 exported 0 records
[[email protected] ~]# cd /opt/
[[email protected] opt]# ls
rh school.json
# 恢复重命名
[[email protected] opt]# mongoimport -d kfc -c info --file /opt/school.json -h 0.0.0.0:27018
2018-07-17T09:57:02.564+0800 connected to: localhost
2018-07-17T09:57:02.564+0800 imported 0 documents
# 导入那个多实例加入-h ip:端口号
[[email protected] opt]# mongoimport -d school -c info1 --file /opt/school1.json -h 0.0.0.0:27018
2018-07-17T10:27:58.921+0800 connected to: 0.0.0.0:27018
2018-07-17T10:27:58.925+0800 num failures: 48
2018-07-17T10:27:58.925+0800 error inserting documents: multiple errors in bulk operation:
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9914‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9915‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9916‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9917‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9918‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9919‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a991a‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a991b‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a991c‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a991d‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a991e‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a991f‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9920‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9921‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9922‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9923‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9924‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9925‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9926‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9927‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9928‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9929‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a992a‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a992b‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a992c‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a992d‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a992e‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a992f‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9930‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9931‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9932‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9933‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9934‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9935‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9936‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9937‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9938‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9939‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a993a‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a993b‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a993c‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a993d‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a993e‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a993f‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9940‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9941‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9942‘) }
- E11000 duplicate key error collection: school.info1 index: _id_ dup key: { : ObjectId(‘5b4d53622211edb9633a9943‘) }
导入指定一行或者一个范围
# 把school数据库中yo的集合中的id为10的信息导入到/opt/里面命名为school-top10.json
[[email protected] opt]# mongoexport -d school -c yo -q ‘{"id":{"$eq":10}}‘ -o /opt/school-top10.json
2018-07-17T10:57:38.100+0800 connected to: localhost
2018-07-17T10:57:38.101+0800 exported 1 record
[[email protected] opt]# ls
school-top10.json
[[email protected] opt]# cat school-top10.json
{"_id":{"$oid":"5b4d5aa9e2d41b39e5e9cbec"},"id":10.0,"name":"ject10"}
[[email protected] opt]#
备份恢复
创建空目录文件夹
[[email protected] opt]# mongodump -d school -o /backup/
2018-07-17T11:01:07.474+0800 writing school.yo to
2018-07-17T11:01:07.475+0800 done dumping school.yo (19 documents)
[[email protected] opt]# ls
school-top10.json
[[email protected] opt]# ls /
backup boot dev home lib64 mnt proc run srv tmp var
bin data etc lib media opt root sbin sys usr
[[email protected] opt]# cd /b
backup/ bin/ boot/
[[email protected] opt]# cd /backup/
[[email protected] backup]# ls
school
[[email protected] backup]# cd school/
[[email protected] school]# ls
yo.bson yo.metadata.json
[[email protected] school]#
还原
[[email protected] school]# mongorestore -d school2 --dir=/backup/school/
2018-07-17T11:03:31.614+0800 the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2018-07-17T11:03:31.614+0800 building a list of collections to restore from /backup/school dir
2018-07-17T11:03:31.615+0800 reading metadata for school2.yo from /backup/school/yo.metadata.json
2018-07-17T11:03:31.621+0800 restoring school2.yo from /backup/school/yo.bson
2018-07-17T11:03:31.624+0800 no indexes to restore
2018-07-17T11:03:31.624+0800 finished restoring school2.yo (19 documents)
2018-07-17T11:03:31.624+0800 done
[[email protected] school]# mongo
MongoDB shell version v3.6.6
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.6
Server has startup warnings:
2018-07-17T10:45:19.749+0800 I CONTROL [initandlisten]
2018-07-17T10:45:19.749+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-07-17T10:45:19.750+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-07-17T10:45:19.750+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-07-17T10:45:19.750+0800 I CONTROL [initandlisten]
2018-07-17T10:45:19.750+0800 I CONTROL [initandlisten]
2018-07-17T10:45:19.750+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is ‘always‘.
2018-07-17T10:45:19.750+0800 I CONTROL [initandlisten] ** We suggest setting it to ‘never‘
2018-07-17T10:45:19.750+0800 I CONTROL [initandlisten]
2018-07-17T10:45:19.750+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is ‘always‘.
2018-07-17T10:45:19.750+0800 I CONTROL [initandlisten] ** We suggest setting it to ‘never‘
2018-07-17T10:45:19.750+0800 I CONTROL [initandlisten]
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
school 0.000GB
school2 0.000GB
>
复制数据库
> db.copyDatabase("school","school8")
{ "ok" : 1 }
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
school 0.000GB
school2 0.000GB
school8 0.000GB
原文地址:http://blog.51cto.com/13645280/2145270
时间: 2024-10-11 00:54:47