Docker安装MongoDB

604 阅读4分钟

安装

docker pull mongo

Using default tag: latest
latest: Pulling from library/mongo
f22ccc0b8772: Pull complete 
3cf8fb62ba5f: Pull complete 
e80c964ece6a: Pull complete 
329e632c35b3: Pull complete 
3e1bd1325a3d: Pull complete 
4aa6e3d64a4a: Pull complete 
035bca87b778: Pull complete 
874e4e43cb00: Pull complete 
0e50e71d834e: Pull complete 
27768a0d0c67: Pull complete 
be4e0bd8b992: Pull complete 
c887415d0643: Pull complete 
Digest: sha256:7722bd2778a299b6f4a62b93a0d2741c734ba7332a090131030ca28261a9a198
Status: Downloaded newer image for mongo:latest
docker.io/library/mongo:latest

运行

#  docker run -itd --name my_mongo -p 27017:27017 mongo --auth

c698673adbbb0779233d66804d0a756ce1735664adb5c2ff521a375f109a5b7c


# docker logs my_mongo


{"t":{"$date":"2021-01-08T10:56:34.714+00:00"},"s":"I",  "c":"CONTROL",  "id":23285,   "ctx":"main","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}
{"t":{"$date":"2021-01-08T10:56:34.725+00:00"},"s":"W",  "c":"ASIO",     "id":22601,   "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}
{"t":{"$date":"2021-01-08T10:56:34.750+00:00"},"s":"I",  "c":"NETWORK",  "id":4648601, "ctx":"main","msg":"Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize."}
{"t":{"$date":"2021-01-08T10:56:34.751+00:00"},"s":"I",  "c":"STORAGE",  "id":4615611, "ctx":"initandlisten","msg":"MongoDB starting","attr":{"pid":1,"port":27017,"dbPath":"/data/db","architecture":"64-bit","host":"c698673adbbb"}}
{"t":{"$date":"2021-01-08T10:56:34.751+00:00"},"s":"I",  "c":"CONTROL",  "id":23403,   "ctx":"initandlisten","msg":"Build Info","attr":{"buildInfo":{"version":"4.4.3","gitVersion":"913d6b62acfbb344dde1b116f4161360acd8fd13","openSSLVersion":"OpenSSL 1.1.1  11 Sep 2018","modules":[],"allocator":"tcmalloc","environment":{"distmod":"ubuntu1804","distarch":"x86_64","target_arch":"x86_64"}}}}
{"t":{"$date":"2021-01-08T10:56:34.751+00:00"},"s":"I",  "c":"CONTROL",  "id":51765,   "ctx":"initandlisten","msg":"Operating System","attr":{"os":{"name":"Ubuntu","version":"18.04"}}}
{"t":{"$date":"2021-01-08T10:56:34.751+00:00"},"s":"I",  "c":"CONTROL",  "id":21951,   "ctx":"initandlisten","msg":"Options set by command line","attr":{"options":{"net":{"bindIp":"*"},"security":{"authorization":"enabled"}}}}
{"t":{"$date":"2021-01-08T10:56:34.760+00:00"},"s":"I",  "c":"STORAGE",  "id":22315,   "ctx":"initandlisten","msg":"Opening WiredTiger","attr":{"config":"create,cache_size=397M,session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress,compact_progress],"}}
{"t":{"$date":"2021-01-08T10:56:35.488+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1610103395:488202][1:0x7f4ef0469ac0], txn-recover: [WT_VERB_RECOVERY | WT_VERB_RECOVERY_PROGRESS] Set global recovery timestamp: (0, 0)"}}
{"t":{"$date":"2021-01-08T10:56:35.488+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1610103395:488284][1:0x7f4ef0469ac0], txn-recover: [WT_VERB_RECOVERY | WT_VERB_RECOVERY_PROGRESS] Set global oldest timestamp: (0, 0)"}}
{"t":{"$date":"2021-01-08T10:56:35.494+00:00"},"s":"I",  "c":"STORAGE",  "id":4795906, "ctx":"initandlisten","msg":"WiredTiger opened","attr":{"durationMillis":734}}
{"t":{"$date":"2021-01-08T10:56:35.494+00:00"},"s":"I",  "c":"RECOVERY", "id":23987,   "ctx":"initandlisten","msg":"WiredTiger recoveryTimestamp","attr":{"recoveryTimestamp":{"$timestamp":{"t":0,"i":0}}}}
{"t":{"$date":"2021-01-08T10:56:35.502+00:00"},"s":"I",  "c":"STORAGE",  "id":4366408, "ctx":"initandlisten","msg":"No table logging settings modifications are required for existing WiredTiger tables","attr":{"loggingEnabled":true}}
{"t":{"$date":"2021-01-08T10:56:35.503+00:00"},"s":"I",  "c":"STORAGE",  "id":22262,   "ctx":"initandlisten","msg":"Timestamp monitor starting"}
{"t":{"$date":"2021-01-08T10:56:35.506+00:00"},"s":"W",  "c":"CONTROL",  "id":22178,   "ctx":"initandlisten","msg":"/sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'","tags":["startupWarnings"]}
{"t":{"$date":"2021-01-08T10:56:35.506+00:00"},"s":"W",  "c":"CONTROL",  "id":22181,   "ctx":"initandlisten","msg":"/sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'","tags":["startupWarnings"]}
{"t":{"$date":"2021-01-08T10:56:35.508+00:00"},"s":"I",  "c":"STORAGE",  "id":20320,   "ctx":"initandlisten","msg":"createCollection","attr":{"namespace":"admin.system.version","uuidDisposition":"provided","uuid":{"uuid":{"$uuid":"a9c1f10c-b93a-463a-b99b-971c39acfabe"}},"options":{"uuid":{"$uuid":"a9c1f10c-b93a-463a-b99b-971c39acfabe"}}}}
{"t":{"$date":"2021-01-08T10:56:35.517+00:00"},"s":"I",  "c":"INDEX",    "id":20345,   "ctx":"initandlisten","msg":"Index build: done building","attr":{"buildUUID":null,"namespace":"admin.system.version","index":"_id_","commitTimestamp":{"$timestamp":{"t":0,"i":0}}}}
{"t":{"$date":"2021-01-08T10:56:35.517+00:00"},"s":"I",  "c":"COMMAND",  "id":20459,   "ctx":"initandlisten","msg":"Setting featureCompatibilityVersion","attr":{"newVersion":"4.4"}}
{"t":{"$date":"2021-01-08T10:56:35.519+00:00"},"s":"I",  "c":"STORAGE",  "id":20536,   "ctx":"initandlisten","msg":"Flow Control is enabled on this deployment"}
{"t":{"$date":"2021-01-08T10:56:35.521+00:00"},"s":"I",  "c":"STORAGE",  "id":20320,   "ctx":"initandlisten","msg":"createCollection","attr":{"namespace":"local.startup_log","uuidDisposition":"generated","uuid":{"uuid":{"$uuid":"e0ffeb0e-4923-4c6a-90f7-05817790231f"}},"options":{"capped":true,"size":10485760}}}
{"t":{"$date":"2021-01-08T10:56:35.530+00:00"},"s":"I",  "c":"INDEX",    "id":20345,   "ctx":"initandlisten","msg":"Index build: done building","attr":{"buildUUID":null,"namespace":"local.startup_log","index":"_id_","commitTimestamp":{"$timestamp":{"t":0,"i":0}}}}
{"t":{"$date":"2021-01-08T10:56:35.531+00:00"},"s":"I",  "c":"FTDC",     "id":20625,   "ctx":"initandlisten","msg":"Initializing full-time diagnostic data capture","attr":{"dataDirectory":"/data/db/diagnostic.data"}}
{"t":{"$date":"2021-01-08T10:56:35.570+00:00"},"s":"I",  "c":"NETWORK",  "id":23015,   "ctx":"listener","msg":"Listening on","attr":{"address":"/tmp/mongodb-27017.sock"}}
{"t":{"$date":"2021-01-08T10:56:35.570+00:00"},"s":"I",  "c":"NETWORK",  "id":23015,   "ctx":"listener","msg":"Listening on","attr":{"address":"0.0.0.0"}}
{"t":{"$date":"2021-01-08T10:56:35.570+00:00"},"s":"I",  "c":"NETWORK",  "id":23016,   "ctx":"listener","msg":"Waiting for connections","attr":{"port":27017,"ssl":"off"}}
{"t":{"$date":"2021-01-08T10:56:35.571+00:00"},"s":"I",  "c":"STORAGE",  "id":20320,   "ctx":"LogicalSessionCacheRefresh","msg":"createCollection","attr":{"namespace":"config.system.sessions","uuidDisposition":"generated","uuid":{"uuid":{"$uuid":"db13618f-c814-47c9-a572-372575e72853"}},"options":{}}}
{"t":{"$date":"2021-01-08T10:56:35.572+00:00"},"s":"I",  "c":"CONTROL",  "id":20712,   "ctx":"LogicalSessionCacheReap","msg":"Sessions collection is not set up; waiting until next sessions reap interval","attr":{"error":"NamespaceNotFound: config.system.sessions does not exist"}}
{"t":{"$date":"2021-01-08T10:56:35.585+00:00"},"s":"I",  "c":"INDEX",    "id":20345,   "ctx":"LogicalSessionCacheRefresh","msg":"Index build: done building","attr":{"buildUUID":null,"namespace":"config.system.sessions","index":"_id_","commitTimestamp":{"$timestamp":{"t":0,"i":0}}}}
{"t":{"$date":"2021-01-08T10:56:35.585+00:00"},"s":"I",  "c":"INDEX",    "id":20345,   "ctx":"LogicalSessionCacheRefresh","msg":"Index build: done building","attr":{"buildUUID":null,"namespace":"config.system.sessions","index":"lsidTTLIndex","commitTimestamp":{"$timestamp":{"t":0,"i":0}}}}
{"t":{"$date":"2021-01-08T10:56:36.023+00:00"},"s":"E",  "c":"STORAGE",  "id":22435,   "ctx":"thread1","msg":"WiredTiger error","attr":{"error":28,"message":"[1610103396:23251][1:0x7f4ee675f700], log-server: __posix_std_fallocate, 58: /data/db/journal/WiredTigerTmplog.0000000003: fallocate:: No space left on device"}}
{"t":{"$date":"2021-01-08T10:56:36.526+00:00"},"s":"E",  "c":"STORAGE",  "id":22435,   "ctx":"thread1","msg":"WiredTiger error","attr":{"error":28,"message":"[1610103396:526786][1:0x7f4ee675f700], log-server: __posix_sys_fallocate, 75: /data/db/journal/WiredTigerTmplog.0000000003: fallocate:: No space left on device"}}
{"t":{"$date":"2021-01-08T10:56:37.031+00:00"},"s":"E",  "c":"STORAGE",  "id":22435,   "ctx":"thread1","msg":"WiredTiger error","attr":{"error":28,"message":"[1610103397:31194][1:0x7f4ee675f700], log-server: __posix_posix_fallocate, 92: /data/db/journal/WiredTigerTmplog.0000000003: fallocate:: No space left on device"}}

创建用户

  • 进入容器
# docker exec -it my_mongo bash
root@c698673adbbb:/#

root@c698673adbbb:/# mongo

MongoDB shell version v4.4.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("cc46c98f-10e6-45bd-a456-7fd6a3f8aba0") }
MongoDB server version: 4.4.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
    https://community.mongodb.com
>
  • 创建admin管理用户

连上数据库后,我们先创建一个admin用户,用以管理其它用户:

> use admin

switched to db admin

> db.createUser({ user:'admin',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'}]});


Successfully added user: {
	"user" : "admin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	]
}

  • 创建具有读写权限的用户

如下面创建拥有读写权限的用户user:

> db.auth('admin','123456');
1

> db.createUser({ user:'user',pwd:'123456',roles:[ { role:'readWrite', db: 'testdb'}]});


Successfully added user: {
	"user" : "user",
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "testdb"
		}
	]
}