Mongo

623 阅读1分钟

MongoDB 入门指南

install with docker

docker pull mongo:4.0.10-xenial
# stack.yml
# Use root/example as user/password credentials
version: '3.1'
services:
  mongo:
    image: mongo:4.0.10-xenial
    restart: always
    ports:
      - 27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
  mongo-express:
    image: mongo-express:0.49.0
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
      ME_CONFIG_MONGODB_SERVER: mongo
      ME_CONFIG_MONGODB_PORT: 27017
      ME_CONFIG_MONGODB_ENABLE_ADMIN: "false"
      ME_CONFIG_MONGODB_AUTH_DATABASE: test
      ME_CONFIG_MONGODB_AUTH_USERNAME: admin
      ME_CONFIG_MONGODB_AUTH_PASSWORD: pass

运行 mongo

docker-compose -f stack.yml up

数据库连接地址:localhost:27017

管理页面访问地址: http://localhost:8081/

命令行登陆数据库

hewenqi$docker ps
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS                      NAMES
aa73346140d7        mongo:4.0.10-xenial    "docker-entrypoint.s…"   16 seconds ago      Up 15 seconds       0.0.0.0:27017->27017/tcp   deploy_mongo_1
d3ab89dce39a        mongo-express:0.49.0   "tini -- /docker-ent…"   16 seconds ago      Up 15 seconds       0.0.0.0:8081->8081/tcp     deploy_mongo-express_1
hewenqi$docker exec -it deploy_mongo_1 bash
root@aa73346140d7:/# mongo -u root -p example --authenticationDatabase admin
MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("d645bf45-9ec8-46ff-be0f-ae8f11719ec4") }
MongoDB server version: 4.0.10
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	http://docs.mongodb.org/
Questions? Try the support group
	http://groups.google.com/group/mongodb-user
Server has startup warnings:
2019-07-30T02:22:08.230+0000 I STORAGE  [initandlisten]
2019-07-30T02:22:08.230+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-07-30T02:22:08.230+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()

命令行创建 test 数据库及 test 用户

> use test
switched to db test
> db.createUser(
...   {
...     user:"test",
...     pwd:"1qaz2wsx",
...     roles:["readWrite", "dbAdmin"]
...   }
... )
Successfully added user: { "user" : "test", "roles" : [ "readWrite", "dbAdmin" ] }

基本指令

# 查看数据库
show dbs;

# 查看集合
show collections;