MongoDB安装
官方下载地址
RPM包列表
包名称 | 命令集 | 说明 |
---|
mongodb-org-server-4.0.4-1.el6.x86_64.rpm | mongod | 守护进程 |
mongodb-org-mongos-4.0.4-1.el6.x86_64.rpm | mongos | 分片群集的控制器和查询路由器 |
mongodb-org-shell-4.0.4-1.el6.x86_64.rpm | mongo | 交互式的MongoDB Shell |
mongodb-org-tools-4.0.4-1.el6.x86_64.rpm | mongodump,mongoexport,mongofiles,mongoimport,mongorestore,mongostat,mongotop | 运维工具 |
命令 | 说明 |
---|
mongodump | 数据备份 |
mongorestore | 数据恢复 |
mongoexport | 导出工具 |
mongoimport | 导入工具 |
mongofiles | GridFS文件系统 |
mongostat | mongodb监控-整体 |
mongotop | mongodb监控-实时 |
配置yum源-安装
[root@c61 yum.repos.d]
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://mirrors.aliyun.com/mongodb/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
[root@c61 yum.repos.d]
[root@c61 yum.repos.d]
[root@c61 mongodb]
使用
[root@i-jq96wmgl ~]
MongoDB shell version v3.6.19
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("fb0fcfff-e743-42e2-aeca-a8627e047af1") }
MongoDB server version: 3.6.19
Server has startup warnings:
2020-08-31T10:46:58.523+0800 I STORAGE [initandlisten]
2020-08-31T10:46:58.523+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-08-31T10:46:58.523+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2020-08-31T10:46:59.249+0800 I CONTROL [initandlisten]
2020-08-31T10:46:59.249+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-08-31T10:46:59.249+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2020-08-31T10:46:59.249+0800 I CONTROL [initandlisten]
2020-08-31T10:46:59.249+0800 I CONTROL [initandlisten]
2020-08-31T10:46:59.249+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-08-31T10:46:59.249+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2020-08-31T10:46:59.249+0800 I CONTROL [initandlisten]
2020-08-31T10:46:59.249+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-08-31T10:46:59.249+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2020-08-31T10:46:59.249+0800 I CONTROL [initandlisten]
> db.help()
....
....
....
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> use webapp
switched to db webapp
>db.dropDatabase()
{ "ok" : 1 }
> db.stats()
{
"db" : "webapp",
"collections" : 1,
"views" : 0,
"objects" : 3,
"avgObjSize" : 52,
"dataSize" : 156,
"storageSize" : 16384,
"numExtents" : 0,
"indexes" : 1,
"indexSize" : 16384,
"fsUsedSize" : 3638845440,
"fsTotalSize" : 21002534912,
"ok" : 1
}
> db
webapp
> db.user.insert( {"name" : "zhangsan", "age" : 23})
WriteResult({ "nInserted" : 1 })
> db.user.save({ "name" : "lisi", "age" : 18})
WriteResult({ "nInserted" : 1 })
> db.user.save({ "name" : "wangwu", "age" : 20})
WriteResult({ "nInserted" : 1 })
> db.user.find()
{ "_id" : ObjectId("5f4cbfa3638d80132cc0fe87"), "name" : "zhangsan", "age" : 23 }
{ "_id" : ObjectId("5f4cbfb9638d80132cc0fe88"), "name" : "lisi", "age" : 18 }
{ "_id" : ObjectId("5f4cbfc7638d80132cc0fe89"), "name" : "wangwu", "age" : 20 }
> show tables
user
> show collections
user
> db.user.help()
....
....
....
> db.user.remove({})
WriteResult({ "nRemoved" : 3 })
> db.user.find()
{ "_id" : ObjectId("5f4cbfa3638d80132cc0fe87"), "name" : "zhangsan", "age" : 23 }
{ "_id" : ObjectId("5f4cbfb9638d80132cc0fe88"), "name" : "lisi", "age" : 18 }
{ "_id" : ObjectId("5f4cbfc7638d80132cc0fe89"), "name" : "wangwu", "age" : 20 }
> db.user.update({},{$set:{age:30}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : ObjectId("5f4cbfa3638d80132cc0fe87"), "name" : "zhangsan", "age" : 30 }
{ "_id" : ObjectId("5f4cbfb9638d80132cc0fe88"), "name" : "lisi", "age" : 18 }
{ "_id" : ObjectId("5f4cbfc7638d80132cc0fe89"), "name" : "wangwu", "age" : 20 }
> db.user.update({age:20},{$set:{age:30}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : ObjectId("5f4cbfa3638d80132cc0fe87"), "name" : "zhangsan", "age" : 30 }
{ "_id" : ObjectId("5f4cbfb9638d80132cc0fe88"), "name" : "lisi", "age" : 18 }
{ "_id" : ObjectId("5f4cbfc7638d80132cc0fe89"), "name" : "wangwu", "age" : 30 }
> db.user.update({},{$set:{age:18}},{multi:true})
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 2 })
> db.user.find()
{ "_id" : ObjectId("5f4cbfa3638d80132cc0fe87"), "name" : "zhangsan", "age" : 18 }
{ "_id" : ObjectId("5f4cbfb9638d80132cc0fe88"), "name" : "lisi", "age" : 18 }
{ "_id" : ObjectId("5f4cbfc7638d80132cc0fe89"), "name" : "wangwu", "age" : 18 }
> db.user.find().pretty()
{
"_id" : ObjectId("5f4cbfa3638d80132cc0fe87"),
"name" : "test",
"age" : 18
}
{
"_id" : ObjectId("5f4cbfb9638d80132cc0fe88"),
"name" : "lisi",
"age" : 18
}
{
"_id" : ObjectId("5f4cbfc7638d80132cc0fe89"),
"name" : "wangwu",
"age" : 18
}
> db.user.find().limit(10)
> db.cloneDatabase("127.0.0.1")
> db.copyDatabase("mydb", "temp", "127.0.0.1")
> db.repairDatabase()
> db.getName()
> db.stats()
> db.version()
> db.getMongo()
> db.serverStatus()
参考文档:https://www.jianshu.com/p/24e88fbb0574
推荐阅读:https://www.cnblogs.com/pyedu/p/10313148.html