博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MongoDB 启动多实例 和 MongoDB创建删除、备份、复制
阅读量:6269 次
发布时间:2019-06-22

本文共 16915 字,大约阅读时间需要 56 分钟。

MongoDB 启动多实例 和 MongoDB创建删除、备份、复制

在单台服务器资源充足的情况下启用多实例

创建数据和日志存放位置

# 数据存放位置[root@localhost ~] mkdir -p /data/mongodb/mongodb1#日志存放位置[root@localhost ~] mkdir /data/logs[root@localhost ~] touch /data/logs/mongodb1.log

复制mongodb的配置文件

#复制原有的配置文件重新命名为mongod1.conf[root@localhost ~] cp /etc/mongod.conf /etc/mongod.conf#修改配置文件[root@localhost ~] vim /etc/mongod1.confsystemLog:  destination: file  logAppend: true  path: /data/mongodb/logs/mongodb1.log #修改日志储存位置  storage:  dbPath: /data/mongodb/mongodb1 #修改数据文件存放位置  net:  port: 27018 #修改端口号

重新启动mongod1.conf

[root@localhost ~] mongod -f /etc/mongod1.confabout to fork child process, waiting until server is ready for connections.forked process: 7142child process started successfully, parent exiting#进入多实例数据库[root@localhost mongodb1]# mongo --port 27018MongoDB shell version v3.6.6connecting to: mongodb://127.0.0.1:27018/MongoDB server version: 3.6.6Welcome 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-userServer 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关闭命令[root@localhost ~]# mongod -f /etc/mongod.conf --shutdown

多实例就创建好了每个多实例都是单独分开的

MongoDB的存储结构

MongoDB逻辑存储结构

MongoDB的逻辑结构主要有文档、集合、和数据库三那个部分组成。其中文档储存是MongoDB的核心概念,它是逻辑储存的最小单元,选购党羽关系型数据库中一行的记录

1:数据库

MongoDB可以建立多个数据库。MongoDB的默认数据库是test,该数据库存储在数据文件要显示它需要向数据库插入一些数据

> show dbs;      #查看数据库命令,里面是默认的数据库admin   0.000GBconfig  0.000GBlocal   0.000GB> use shcool;  #创建数据库直接use进入在里面创建集合,# 输入数据会自动生成数据库switched to db shcool> db.createCollection('info1')   #创建数据集合{ "ok" : 1 }> show collections             #查看数据集合info1> show dbsadmin   0.000GBconfig  0.000GBlocal   0.000GBshcool  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.databaseshcool.shcool.database> show dbsadmin   0.000GBconfig  0.000GBlocal   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# 导出[root@localhost ~]# mongoexport -d school -c info1 -o /opt/school.json2018-07-17T09:55:16.306+0800    connected to: localhost2018-07-17T09:55:16.306+0800    exported 0 records[root@localhost ~]# cd /opt/[root@localhost opt]# lsrh  school.json# 恢复重命名[root@localhost opt]# mongoimport -d kfc -c   info --file /opt/school.json -h 0.0.0.0:270182018-07-17T09:57:02.564+0800    connected to: localhost2018-07-17T09:57:02.564+0800    imported 0 documents# 导入那个多实例加入-h ip:端口号[root@localhost opt]# mongoimport -d school -c info1 --file /opt/school1.json -h 0.0.0.0:270182018-07-17T10:27:58.921+0800    connected to: 0.0.0.0:270182018-07-17T10:27:58.925+0800    num failures: 482018-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[root@localhost opt]# mongoexport -d school -c yo -q '{"id":{"$eq":10}}' -o /opt/school-top10.json2018-07-17T10:57:38.100+0800    connected to: localhost2018-07-17T10:57:38.101+0800    exported 1 record[root@localhost opt]# lsschool-top10.json[root@localhost opt]# cat school-top10.json {"_id":{"$oid":"5b4d5aa9e2d41b39e5e9cbec"},"id":10.0,"name":"ject10"}[root@localhost opt]#

备份恢复

创建空目录文件夹
[root@localhost 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)[root@localhost opt]# lsschool-top10.json[root@localhost opt]#  ls /backup  boot  dev  home  lib64  mnt  proc  run   srv  tmp  varbin     data  etc  lib   media  opt  root  sbin  sys  usr[root@localhost opt]# cd /bbackup/ bin/    boot/   [root@localhost opt]# cd /backup/[root@localhost backup]# lsschool[root@localhost backup]# cd school/[root@localhost school]# lsyo.bson  yo.metadata.json[root@localhost school]#

还原

[root@localhost 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 instead2018-07-17T11:03:31.614+0800    building a list of collections to restore from /backup/school dir2018-07-17T11:03:31.615+0800    reading metadata for school2.yo from /backup/school/yo.metadata.json2018-07-17T11:03:31.621+0800    restoring school2.yo from /backup/school/yo.bson2018-07-17T11:03:31.624+0800    no indexes to restore2018-07-17T11:03:31.624+0800    finished restoring school2.yo (19 documents)2018-07-17T11:03:31.624+0800    done[root@localhost school]# mongoMongoDB shell version v3.6.6connecting to: mongodb://127.0.0.1:27017MongoDB server version: 3.6.6Server 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 dbsadmin    0.000GBconfig   0.000GBlocal    0.000GBschool   0.000GBschool2  0.000GB>

复制数据库

> db.copyDatabase("school","school8"){ "ok" : 1 }> show dbsadmin    0.000GBconfig   0.000GBlocal    0.000GBschool   0.000GBschool2  0.000GBschool8  0.000GB

转载于:https://blog.51cto.com/13645280/2145270

你可能感兴趣的文章
Atitit.Java exe bat 作为windows系统服务程序运行
查看>>
session的生命周期
查看>>
数据库的本质、概念及其应用实践(二)
查看>>
iOS开发多线程--(NSOperation/Queue)
查看>>
php的ajax简单实例
查看>>
maven常用构建命令
查看>>
C#:关联程序和文件
查看>>
推荐科研软件
查看>>
gradle
查看>>
如何取消未知类型文件默认用记事本打开
查看>>
[Javascript] Immute Object
查看>>
Java 关于finally、static
查看>>
Posix mq和SystemV mq区别
查看>>
P6 EPPM Manual Installation Guide (Oracle Database)
查看>>
XMPP协议、IM、客户端互联详解
查看>>
PHP写文件函数
查看>>
mysql的sql_mode合理设置
查看>>
函数连续性与可导性
查看>>
linux下libevent安装
查看>>
用ip来获得用户所在地区信息
查看>>