Mongodb 系列教程

  1. Mongodb的实践一:安装
  2. Mongodb的实践二:初识
  3. Mongodb的实践三:
  4. Mongodb的实践四
  5. Mongodb的实践五
  6. Mongodb的实践六
  7. Mongodb的实践七
  8. Mongodb的实践八

序言

Mongodb 教程其实早在一年前就一直想写一写,因为工作比较忙,自己也没有真的大量用于真实项目里的实践,一直耽搁了

简介

MongoDB是一个基于分布式文件存储的数据库.

官网

https://www.mongodb.com/

单机安装

三种安装支持

1. window安装可参考

mongodb单机 在window下安装测试

2. 类unix系下安装

wget https://fastdl.mongodb.org/osx/mongodb-osx-ssl-x86_64-4.0.6.tgz
tar zxvf mongodb-osx-ssl-x86_64-4.0.6.tgz
cd mongodb-osx-ssl-x86_64-4.0.6/bin
mongod --config /xx/xx.config --fork

3. docker安装

# -p local_port:docker_port
docker pull mongo:4.1.5
docker run -d --name mongodb-4.1.5 -e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=123456 -v /data/mongodb/docker/4.1.5:/mongodb -v /data/mongodb/docker/4.1.5/conf.d:/etc/mongo -p 27077:27017 mongo:4.1.5 --config /etc/mongo/mongod.conf --auth

docker 目录结构

├── conf.d
│   └── mongod.conf
├── data
└── logs

mongod.conf

/data/mongodb/docker/4.1.5/conf.d下的mongo配置文件

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /mongodb/data
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /mongodb/logs/mongod.log

# network interfaces
net:
  port: 27017
#  bindIp: 127.0.0.1
#  bindIp: 0.0.0.0


# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

security:
  authorization: enabled

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

连接测试

mongo --host 127.0.0.1 --port 27077
> db.auth("root", "123456")
1
> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB
> use admin
switched to db admin
# 创建可以创建用户权限的角色超管账号
# 可参考了[Mongodb的实践二:初识]解角色组权限
> db.createUser({
... user : "qkl",
... pwd: "123456",
... roles : [
... {
... "role" : "userAdminAnyDatabase",
... "db" : "admin"
... }
... ],
... "mechanisms" : [
... "SCRAM-SHA-1",
... "SCRAM-SHA-256"
... ]
... });
Successfully added user: {
    "user" : "qkl",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ],
    "mechanisms" : [
        "SCRAM-SHA-1",
        "SCRAM-SHA-256"
    ]
}

创建新数据和账号

> use demo;
switched to db demo
> db.createUser({
... user : "demo",
... pwd: "123456",
... roles : [
... {
... "role" : "dbAdmin",
... "db" : "demo"
... },
... {
... "role" : "dbOwner",
... "db" : "demo"
... }
... ],
... "mechanisms" : [
... "SCRAM-SHA-1",
... "SCRAM-SHA-256"
... ]
... });
Successfully added user: {
    "user" : "demo",
    "roles" : [
        {
            "role" : "dbAdmin",
            "db" : "demo"
        },
        {
            "role" : "dbOwner",
            "db" : "demo"
        }
    ],
    "mechanisms" : [
        "SCRAM-SHA-1",
        "SCRAM-SHA-256"
    ]
}
> exit;

测试插入和查询

# 需要重连
mongo --host 127.0.0.1 --port 27077
>db.auth("demo","123456");
1
> db
demo
> db.foo.insert({"name":"qkl",age:"18",sex:1});
WriteResult({ "nInserted" : 1 })
> show dbs;
demo  0.000GB
> show collections;
foo
> db.foo.find({})
{ "_id" : ObjectId("5d22db4bbcbb35d902a31a92"), "name" : "qkl", "age" : "18", "sex" : 1 }
> db.foo.insert({"name":"hax",age:"16",sex:0});
WriteResult({ "nInserted" : 1 })
> db.foo.find({})
{ "_id" : ObjectId("5d22db4bbcbb35d902a31a92"), "name" : "qkl", "age" : "18", "sex" : 1 }
{ "_id" : ObjectId("5d22dbafbcbb35d902a31a93"), "name" : "hax", "age" : "16", "sex" : 0 }
03-05 21:38