Mongodb的实践一初始包括安装

27次阅读

共计 2442 个字符,预计需要花费 7 分钟才能阅读完成。

Mongodb 系列教程

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

序言

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

简介

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

官网

https://www.mongodb.com/downl…

单机安装

三种安装支持

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 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 }

正文完
 0