Mongodb series
- Mongodb practice 1: initial (including installation)
- Practice 2 of Mongodb:
- Practice 3 of Mongodb
- Practice 4 of Mongodb
- Practice 5 of Mongodb
- Practice 6 of Mongodb
- Practice 7 of Mongodb
- Practice 8 of Mongodb
Preface
Mongodb actually wanted to write a tutorial as early as a year ago. Because of the busy work, I didn't really use a lot of practice in real projects, which has been delayed.
brief introduction
MongoDB is a database based on distributed file storage.
Official website
https://www.mongodb.com/downl...
Single machine installation
Three installation supports
1. For window installation, please refer to
mongodb stand-alone installation test under Windows
2. Installation under unix like system
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 installation
# -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 directory structure
├── conf.d │ └── mongod.conf ├── data └── logs
mongod.conf
/mongo configuration file under data/mongodb/docker/4.1.5/conf.d
# 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:
Connection test
mongo --host 127.0.0.1 --port 27077 > db.auth("root", "123456") 1 > show dbs; admin 0.000GB config 0.000GB local 0.000GB
Create new data and account
> 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;
Test insert and query
# Need reconnection 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 }