MongoDB start multi-instance and MongoDB create Delete, backup, copy enable multi-instance creation data and log storage locations with sufficient single server resources
# 数据存放位置[[email protected] ~] mkdir -p /data/mongodb/mongodb1#日志存放位置[[email protected] ~] mkdir /data/logs[[email protected] ~] touch /data/logs/mongodb1.log
Copy the MongoDB configuration file
#复制原有的配置文件重新命名为mongod1.conf[[email protected] ~] cp /etc/mongod.conf /etc/mongod.conf#修改配置文件[[email protected] ~] vim /etc/mongod1.confsystemLog: destination: file logAppend: true path: /data/mongodb/logs/mongodb1.log #修改日志储存位置 storage: dbPath: /data/mongodb/mongodb1 #修改数据文件存放位置 net: port: 27018 #修改端口号
Restart mongod1.conf
[[email protected] ~] mongod-f/etc/mongod1.confabout to fork child process, waiting until server is a ready for Conne Ctions.forked Process:7142child Process started successfully, parent exiting# into multi-instance database [[email protected] Mongodb1 ]# MONGO--port 27018MongoDB Shell version v3.6.6connecting to:mongodb://127.0.0.1:27018/mongodb server version:3.6.6we Lcome 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 have 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 is running thi S 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/transpa Rent_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] > # Mo Ngdb Close command [[email protected] ~]# mongod-f/etc/mongod.conf--shutdown
Multi-instance creates a separate MongoDB storage structure for each multi-instance MongoDB logical storage structure
The logical structure of MONGODB consists of three parts: Document, collection, and database. The document storage is the core concept of MongoDB, it is the smallest unit of logical storage, the purchase of a row in the partisan relational database
1: Database MongoDB can build multiple databases. The default database for MongoDB is test, which is stored in a data file to show that it needs to insert some data into the database
> 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
Insert Data command in Geometry DB. The Created collection. Insert ({"Joect format"}) native format
> db.info1.insert ({"name":"zhangsa","id":2,"hobb":"jap"}) WriteResult({ "nInserted" : 1 })
Querying data commands within a collection
> db.info1.find() { "_id" : ObjectId("5b4d46223ee32e205a4122c2"), "name" : "zhangsa", "id" : 2, "hobb" : "jap" }
Looping through the creation of data
> for (var i=2;i<50;i++) Db.info1.insert ({"id": I, "name": "JCCP" +i}) Writeresult ({"ninserted": 1}) # View the information created > Db.info1.find () {"_id": ObjectId ("5B4D46223EE32E205A4122C2"), "name": "Zhangsa", "id": 2, "Hobb": "Jap"} {"_id": OBJ Ectid ("5b4d48ee3ee32e205a4122c3"), "id": 2, "name": "Jccp2"} {"_id": ObjectId ("5b4d48ee3ee32e205a4122c4"), "id": 3, " Name ":" JCCP3 "} {" _id ": ObjectId (" 5b4d48ee3ee32e205a4122c5 ")," id ": 4," name ":" JCCP4 "} {" _id ": ObjectId (" 5b4d48ee3 Ee32e205a4122c6 ")," 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": objec TId ("5B4D48EE3EE32E205A4122CB"), "id": Ten, "Name": "Jccp10"} {"_id": ObjectId ("5b4d48ee3ee32e205a4122cc"), "id": 11, "Name": "Jccp11"} {"_id":ObjectId ("5B4D48EE3EE32E205A4122CD"), "id": "Name": "Jccp12"} {"_id": ObjectId ("5b4d48ee3ee32e205a4122ce"), "id": "Name": "Jccp13"} {"_id": ObjectId ("5B4D48EE3EE32E205A4122CF"), "id": +, "name": "Jccp14"} {"_id": ObjectId (" 5b4d48ee3ee32e205a4122d0 ")," id ": +," name ":" Jccp15 "} {" _id ": ObjectId (" 5b4d48ee3ee32e205a4122d1 ")," id ": +," name ":" Jccp16 "} {" _id ": ObjectId (" 5B4D48EE3EE32E205A4122D2 ")," id ": +," name ":" Jccp17 "} {" _id ": ObjectId (" 5b4d48ee3e E32e205a4122d3 ")," id ": +," name ":" Jccp18 "} {" _id ": ObjectId (" 5b4d48ee3ee32e205a4122d4 ")," id ": +," name ":" JCCP1 9 "} {" _id ": ObjectId (" 5b4d48ee3ee32e205a4122d5 ")," id ": +," name ":" Jccp20 "}type" It "for more # (enter it to view down)
To find a specified data
> db.info1.find({"id":10}){ "_id" : ObjectId("5b4d48ee3ee32e205a4122cb"), "id" : 10, "name" : "jccp10" }
Change the jccp10 of line tenth to 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" }
Delete (collection, database)
> db.info1.drop()true> db.info1.find()> db.shcool.databaseshcool.shcool.database> show dbsadmin 0.000GBconfig 0.000GBlocal
Back up the database below is the school database I created, and the collection contains information that will be backed up and exported
> Use shcool;switched to DB shcool> db.createcollection (' Info1 ') {"OK": 1}> for (var i=2;i<50;i++) db.info1.i Nsert ({"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 ("5b4d4bf03ee32e20 5a4122f6 ")," 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 ("5b 4D4BF03EE32E205A4122FB ")," id ": Ten," Name ":" Jccp10 "} {" _id ": ObjectId (" 5B4D4BF03EE32E205A4122FC ")," id ": one," name " : "Jccp11"} {"_id": ObjectId ("5b4d4bf03ee32E205A4122FD ")," id ": +," name ":" Jccp12 "} {" _id ": ObjectId (" 5b4d4bf03ee32e205a4122fe ")," id ": +," name ":" Jccp13 "} {" _id ": ObjectId (" 5b4d4bf03ee32e205a4122ff ")," id ": +," name ":" Jccp14 "} {" _id ": ObjectId (" 5b4d4bf03ee32e205a412 "id": "Name": "Jccp15"} {"_id": ObjectId ("5b4d4bf03ee32e205a412301"), "id": +, "name": "Jccp16"} {"_id" : ObjectId ("5b4d4bf03ee32e205a412302"), "id": +, "name": "Jccp17"} {"_id": ObjectId ("5b4d4bf03ee32e205a412303"), "id ":", "name": "Jccp18"} {"_id": ObjectId ("5b4d4bf03ee32e205a412304"), "id": +, "name": "Jccp19"} {"_id": objecti D ("5b4d4bf03ee32e205a412305"), "id": "Name": "Jccp20"} {"_id": ObjectId ("5b4d4bf03ee32e205a412306"), "id": +, "n Ame ":" Jccp21 "}type" it "for more# export [[email protected] ~]# 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[[email protected ] ~]# CD/OPT/[[EMAIL&NBSp;protected] opt]# lsrh school.json# Restore rename [[email protected] 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 doc uments# import that multi-instance join-H IP: port number [[email protected] 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-1 7t10:27:58.925+0800 error inserting documents:multiple errors in bulk operation:-E11000 duplicate key error collect ION: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:s CHOOL.INFO1 index: _id_ dup key: {: ObjectId (' 5b4d53622211edb9633a9916 ')}-E11000 duplicate key error Collection:scho OL.INFO1 Index: _id_ DUP Key: {: ObjectId (' 5b4d53622211edb9633a9917 ')}-E11000 duplicate key error COLLECTION:SCHOOL.INFO1 index: _id_ dup ke Y: {: 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: {: Ob Jectid (' 5b4d53622211edb9633a991b ')}-E11000 duplicate key error COLLECTION:SCHOOL.INFO1 index: _id_ dup key: {: Objec TId (' 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 ')}-E 11000 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 ')}-E1 Duplicate key error COLLECTION:SCHOOL.INFO1 index: _id_ dup key: {: ObjectId (' 5b4d53622211edb9633a9922 ')}-E1100 0 Duplicate key Error COLLECTION:SCHOOL.INFO1 index: _id_ dup key: {: ObjectId (' 5b4d53622211edb9633a9923 ')}-E11000 D Uplicate key Error COLLECTION:SCHOOL.INFO1 index: _id_ dup key: {: ObjectId (' 5b4d53622211edb9633a9924 ')}-E11000 DUPL Icate key Error COLLECTION:SCHOOL.INFO1 index: _id_ dup key: {: ObjectId (' 5b4d53622211edb9633a9925 ')}-E11000 Duplica Te 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.i NFO1 index: _id_ dup key: {: ObjectId (' 5b4d53622211edb9633a9929 ')}-E11000 duplicate key error collection:school.info 1 index: _id_ dup key: {: ObjectId (' 5b4d53622211edb9633a992a ')}-E11000 duplicate key error collection:school.info1 I Ndex: _id_ dup key: {: ObjectId (' 5b4d53622211edb9633a992b ')}-E11000 duplicate key error collection:school.info1 Inde x: _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_ D Up 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 (' 5b4d5362 2211edb9633a9931 ')}-E11000 duplicate key error COLLECTION:SCHOOL.INFO1 index: _id_ dup key: {: ObjectId (' 5b4d5362221 1edb9633a9932 ')}-E11000 duplicate key error COLLECTION:SCHOOL.INFO1 index: _id_ dup key: {: ObjectId (' 5b4d53622211ed b9633a9933 ')}-E11000 duplicate key error COLLECTION:SCHOOL.INFO1 index: _id_ dup key: {: ObjectId (' 5b4d53622211edb96 33a9934 ')}-E11000 duplicate key error COLLECTION:SCHOOL.INFO1 index: _id_ dup key: {: ObjectId (' 5b4d53622211edb9633a 9935 ')}-E11000 duplicate key error COLLECTION:SCHOOL.INFO1 index: _id_ dup key: {: ObjectId (' 5b4d53622211edb9633a993 6 ')}-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 keyError COLLECTION:SCHOOL.INFO1 index: _id_ dup key: {: ObjectId (' 5b4d53622211edb9633a9939 ')}-E11000 Duplicate key err or 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 col LECTION:SCHOOL.INFO1 index: _id_ dup key: {: ObjectId (' 5b4d53622211edb9633a993c ')}-E11000 duplicate key error Collec TION:SCHOOL.INFO1 index: _id_ dup key: {: ObjectId (' 5b4d53622211edb9633a993d ')}-E11000 duplicate key error Collectio N: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:sch OOL.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 ')}
Import a specified row or range
# 把school数据库中yo的集合中的id为10的信息导入到/opt/里面命名为school-top10.json[[email protected] 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
Backup recovery Create an empty directory folder
[[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]# lsschool-top10.json[[email protected] opt]# ls /backup boot dev home lib64 mnt proc run srv tmp varbin data etc lib media opt root sbin sys usr[[email protected] opt]# cd /bbackup/ bin/ boot/ [[email protected] opt]# cd /backup/[[email protected] backup]# lsschool[[email protected] backup]# cd school/[[email protected] school]# lsyo.bson
Restores
[[email protected] school]# mongorestore-d school2--dir=/backup/school/2018-07-17t11:03:31.614+0800 the--db an D--collection args should only is used when restoring from a BSON file. Other uses is deprecated and would not be exist in the future; Use--nsinclude instead2018-07-17t11:03:31.614+0800 building a list of collections to restore From/backup/school DIR20 18-07-17t11:03:31.615+0800 reading metadata for School2.yo from/backup/school/yo.metadata.json2018-07-17t11:0 3: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 (documents) 2018-07-17t11:03:31.624+0800 Done[[email p Rotected] 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 was running this process as the root user, which was 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>
Copy Database
> db.copyDatabase("school","school8"){ "ok" : 1 }> show dbsadmin 0.000GBconfig 0.000GBlocal 0.000GBschool 0.000GBschool2 0.000GBschool8 0.000GB
MongoDB launches multi-instance and MongoDB create Delete, backup, copy