Docker安装MongoDB

  1. 安装docker、配置环境 略…
  2. 用命令:docker search mongo 先查找mongo镜像
sai:~ ws$ docker search mongo
NAME                                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mongo                               MongoDB document databases provide high avai…   5026                [OK]                
mongo-express                       Web-based MongoDB admin interface, written w…   300                 [OK]                
tutum/mongodb                       MongoDB Docker image – listens in port 27017…   224                                     [OK]
mvertes/alpine-mongo                light MongoDB container                         82                                      [OK]
mongoclient/mongoclient             Official docker image for Mongoclient, featu…   55                                      [OK]
bitnami/mongodb                     Bitnami MongoDB Docker Image                    53                                      [OK]
frodenas/mongodb                    A Docker Image for MongoDB                      17                                      [OK]
mongooseim/mongooseim               Small docker image for MongooseIM - robust a…   16                                      
mongooseim/mongooseim-docker        MongooseIM server the latest stable version     11                                      [OK]
cvallance/mongo-k8s-sidecar         Kubernetes side car to setup and maintain a …   8                                       [OK]
centos/mongodb-32-centos7           MongoDB NoSQL database server                   5                                       
istepanov/mongodump                 Docker image with mongodump running as a cro…   5                                       [OK]
centos/mongodb-26-centos7           MongoDB NoSQL database server                   5                                       
eses/mongodb_exporter               mongodb exporter for prometheus                 4                                       [OK]
khezen/mongo                        MongoDB Docker image supporting RocksDB stor…   4                                       [OK]
centos/mongodb-36-centos7           MongoDB NoSQL database server                   2                                       
neowaylabs/mongodb-mms-agent        This Docker image with MongoDB Monitoring Ag…   2                                       [OK]
openshift/mongodb-24-centos7        DEPRECATED: A Centos7 based MongoDB v2.4 ima…   1                                       
ekesken/mongo                       docker image for mongo that is configurable …   1                                       [OK]
centos/mongodb-34-centos7           MongoDB NoSQL database server                   1                                       
webhippie/mongodb                   Docker images for mongodb                       1                                       [OK]
circleci/mongo                      CircleCI images for MongoDB                     1                                       [OK]
amd64/mongo                         MongoDB document databases provide high avai…   0                                       
ansibleplaybookbundle/mongodb-apb   An APB to deploy MongoDB.                       0                                       [OK]
quilt/mongo                         MongoDB container for quilt.io                  0                                       [OK]
  1. 下载镜像:docker pull mongo
sai:~ ws$ docker pull mongo
Using default tag: latest
latest: Pulling from library/mongo
3b37166ec614: Pull complete 
504facff238f: Pull complete 
ebbcacd28e10: Pull complete 
c7fb3351ecad: Pull complete 
2e3debadcbf7: Pull complete 
004c7a04feb1: Pull complete 
897284d7f640: Pull complete 
af4d2dae1422: Pull complete 
5e988d91970a: Pull complete 
aebe46e3fb07: Pull complete 
fc1d0b190eb9: Pull complete 
a419c9ea1312: Pull complete 
eabf5629c48a: Pull complete 
77af0cb42d89: Pull complete 
Digest: sha256:8fd980fa17d0f1b455d6329249501bd922d0e8d1e3691bc19edf656923942fb0
Status: Downloaded newer image for mongo:latest

  1. 运行镜像:
sai:~ ws$ docker run  --name some-mongo   -p 27017:27017   -d mongo   --auth
8401fe40e632c0ba79fec43cf5feff4ba887f7a764d6253595ca6068071b7f54
  1. 查看运行的镜像:
sai:~ ws$ docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                      NAMES
8401fe40e632        mongo               "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:27017->27017/tcp   some-mongo
  1. 进入容器,查看 mongo版本,并创建用户:
    1进入容器:docker exec -it 8401fe40e632 /bin/bash
    2运行mongo命令查看是否正常启动:mongo
    3创建用户和密码:root/root
sai:~ ws$ docker    exec  -it 8401fe40e632 /bin/bash
root@8401fe40e632:/# ls
bin  boot  data  dev  docker-entrypoint-initdb.d  etc  home  js-yaml.js  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@8401fe40e632:/# mongo
MongoDB shell version v4.0.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.2
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
> use admin
switched to db admin
> db.createUser({user:"root",pwd:"root",roles:[{role:'root',db:'admin'}]})
Successfully added user: {
	"user" : "root",
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}
> exit
bye

  1. 登录校验是否创建成功:
    mongo 机器IP/admin -u root -p
root@8401fe40e632:/# mongo 31.16.6.49/admin  -u root -p
MongoDB shell version v4.0.2
Enter password: 
connecting to: mongodb://31.16.6.49:27017/admin
MongoDB server version: 4.0.2
Server has startup warnings: 
2018-09-27T03:05:02.788+0000 I STORAGE  [initandlisten] 
2018-09-27T03:05:02.789+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-09-27T03:05:02.789+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()
---

> show databases;
admin   0.000GB
config  0.000GB
local   0.000GB
> use admin
switched to db admin
  1. 安装,测试成功。可以耍了

你可能感兴趣的:(DB)