mongodb基础操作(陆续更新)

来源:互联网 发布:淘宝产品参数怎么设置 编辑:程序博客网 时间:2024/06/06 01:04
首先系统要修改的配置文件
[root@yw-2 ~]# echo never >>  /sys/kernel/mm/transparent_hugepage/enabled
[root@yw-2 ~]# echo never >>  /sys/kernel/mm/transparent_hugepage/defrag

mongod参数:
--fork:指定后台运行
--port:指定运行端口
--logpath:指定日志存放的目录
--logappend:指定日志是追加形式
--dbpath:指定数据存放位置,默认是/data/db



指定一些参数:
后台运行,端口30000,数据目录,日志目录,日志已追加模式
[root@1ae768c7bca3 mongodb]# ./bin/mongod --fork --port 30000 --dbpath /mongo/mongodb/data --logpath /mongo/mongodb/log/mongodb.log  --logappend
about to fork child process, waiting until server is ready for connections.
forked process: 102
child process started successfully, parent exiting
注:mongodb默认启动数据路径需要在/data/db,或者指定数据放在哪里。



进入mongo:
[root@1ae768c7bca3 mongodb]# ./bin/mongo 127.0.0.1:30000
MongoDB shell version v3.4.2
connecting to: 127.0.0.1:30000
MongoDB server version: 3.4.2
Server has startup warnings:
2017-03-28T05:59:04.883+0000 I CONTROL  [initandlisten]
2017-03-28T05:59:04.883+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-03-28T05:59:04.883+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-03-28T05:59:04.883+0000 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-03-28T05:59:04.883+0000 I CONTROL  [initandlisten]
注:这个告警意思是没有启动用户认证



查看mongo所有的库:
> show dbs
admin  0.000GB
local  0.000GB

进入一个库,如果这个库没有,在进入这个空库后创建一个集合,新库也就被创建了。
> use wangzz
switched to db wangzz

查看在哪个库
> db
crr

创建一个集合(表)
> db.createCollection("crr322")
{ "ok" : 1 }

查看所在库里的所有表
> show tables;
crr322

查看所在库里的所有集合(表)
> show collections
crr322


查看当前数据库中有那些个集合(表)
> db.getCollectionNames()
[ "crr322", "wzz623" ]

插入文档时创建集合,如果集合不存在就创建集合
> db.wzz623.insert({"name":"wangzhengzhong","age":28})  
WriteResult({ "nInserted" : 1 })
> show tables
crr322
wzz623

删除当前所在的库
> db.dropDatabase()
{ "dropped" : "crr", "ok" : 1 }

查看mongodb版本
> db.version()
3.4.2

查看当前数据库的链接机器地址
> db.getMongo()
connection to 127.0.0.1:30000


查看当前库的状态
> db.stats()
{
    "db" : "wangzz",
    "collections" : 2,
    "views" : 0,
    "objects" : 2,
    "avgObjSize" : 33,
    "dataSize" : 66,
    "storageSize" : 32768,
    "numExtents" : 0,
    "indexes" : 2,
    "indexSize" : 32768,
    "ok" : 1
}
> use test
switched to db test
> db.stats()
{
    "db" : "test",
    "collections" : 3,
    "views" : 0,
    "objects" : 3,
    "avgObjSize" : 33,
    "dataSize" : 99,
    "storageSize" : 36864,
    "numExtents" : 0,
    "indexes" : 4,
    "indexSize" : 40960,
    "ok" : 1
}

删除当前库的表
> show tables;
crr322
test
wzz623
> db.crr322.drop()
true
> show tables;
test
wzz623


查看表里的内容
> use admin
switched to db admin
> show tables
system.version
> use local
switched to db local
> show tables
startup_log
> db.startup_log.find()
{ "_id" : "1ae768c7bca3-1490680744884", "hostname" : "1ae768c7bca3"

查看表里的内容(整齐排列方法)
> db.startup_log.find().pretty()
{
    "_id" : "1ae768c7bca3-1490680744884",
    "hostname" : "1ae768c7bca3",
    "startTime" : ISODate("2017-03-28T05:59:04Z"),
    "startTimeLocal" : "Tue Mar 28 05:59:04.884",
    "cmdLine" : {
        "net" : {
            "port" : 30000
        },
        "processManagement" : {
            "fork" : true
        },
        "storage" : {
            "dbPath" : "/mongo/mongodb/data"
        },
        "systemLog" : {
            "destination" : "file",
            "logAppend" : true,
            "path" : "/mongo/mongodb/log/mongodb.log"


查看所在库的用户
rs1:PRIMARY> db.system.users.find()
{ "_id" : ObjectId("55273d079111ccc287f89b7b"), "user" : "root", "readOnly" : false, "pwd" : "34e5772aa66b703a319641d42a47d696" }
{ "_id" : ObjectId("57872d92eb6de83cdbe4cc66"), "user" : "obd_readonly", "readOnly" : true, "pwd" : "c882fcd71ae94df84c9e1a4c33dad3e0" }
rs1:PRIMARY> use obd_base
switched to db obd_base
rs1:PRIMARY> db.system.users.find()
{ "_id" : ObjectId("564d62113e82bc13e81763d5"), "user" : "root", "readOnly" : false, "pwd" : "34e5772aa66b703a319641d42a47d696" }
rs1:PRIMARY> use obd_info
switched to db obd_info
rs1:PRIMARY> db.system.users.find()
{ "_id" : ObjectId("564d61f73e82bc13e81763d3"), "user" : "root", "readOnly" : false, "pwd" : "34e5772aa66b703a319641d42a47d696" }
{ "_id" : ObjectId("578732e0d6192a674432d9d5"), "user" : "obd_readonly", "readOnly" : true, "pwd" : "c882fcd71ae94df84c9e1a4c33dad3e0" }
0 0