Docker安装MongoDB
Posted 阳光__小好
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker安装MongoDB相关的知识,希望对你有一定的参考价值。
1、创建主机挂载配置目录
mkdir -p /docker/mongodb/data && cd /docker/mongodb
data目录存放mongodb数据库文件,删除重启容器不会丢失
2、生成启动文件
2.1 无账户密码,不需要认证
cat <<EOF> start.sh
#!/bin/bash
MONGODB_DIR=`pwd`
docker stop mongodb
docker rm mongodb
docker run -d \\\\
--name mongodb \\\\
--restart always \\\\
--privileged \\\\
-p 27017:27017 \\\\
-v \\$MONGODB_DIR/data:/data/db \\\\
mongo:4.2.2
EOF
2.2 有账户密码,需要认证(推荐)
cat <<EOF> start.sh
#!/bin/bash
MONGODB_DIR=`pwd`
docker stop mongodb
docker rm mongodb
docker run -d \\\\
--name mongodb \\\\
--restart always \\\\
--privileged \\\\
-p 27017:27017 \\\\
-v \\$MONGODB_DIR/data:/data/db \\\\
-e MONGO_INITDB_ROOT_USERNAME=admin \\\\
-e MONGO_INITDB_ROOT_PASSWORD=123456 \\\\
mongo:4.2.2 mongod --auth
EOF
说明:
-d: 后台运行容器;
--name: 指定容器名;
-p: 指定服务运行的端口;
-v: 映射目录或文件;
--privileged 拥有真正的root权限
--restart=always Docker服务重启容器也启动
-e MONGO_INITDB_ROOT_USERNAME=admin 指定用户名
-e MONGO_INITDB_ROOT_PASSWORD=123456 指定密码
mongod --auth :容器默认启动命令是mongod,我们认证需要修改启动命为mongod --auth开启认证
3、运行start.sh
sh start.sh
停止和删除容器
docker stop mongodb && docker rm mongodb
4. 使用Robo连接
启动脚本配置不同,账号密码输入也不一样
无账户密码
:用户名
和密码
为空就能登录有账户密码
:账号密码是上面设置的admin/123456
5、进入容器
[root@localhost mongodb]# docker exec -it mongodb bash
root@ce90018683a8:/# mongo --version
MongoDB shell version v4.2.2
git version: a0bbbff6ada159e19298d37946ac8dc4b497eadf
OpenSSL version: OpenSSL 1.1.1 11 Sep 2018
allocator: tcmalloc
modules: none
build environment:
distmod: ubuntu1804
distarch: x86_64
target_arch: x86_64
root@ce90018683a8:/#
6、进入mongodb
6.1 无密码进入数据库
root@ce90018683a8:/# mongo
MongoDB shell version v4.2.2
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session "id" : UUID("e35fba4a-fddf-4e87-b5d4-cce3d6769d63")
MongoDB server version: 4.2.2
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, seehttp://docs.mongodb.org/
Questions? Try the support grouphttp://groups.google.com/group/mongodb-user
# 查看命令
> help
db.help() help on db methods
db.mycoll.help() help on collection methods
sh.help() sharding helpers
rs.help() replica set helpers
help admin administrative help
help connect connecting to a db help
help keys key shortcuts
help misc misc things to know
help mr mapreduce
show dbs show database names
show collections show collections in current database
show users show users in current database
show profile show most recent system.profile entries with time >= 1ms
show logs show the accessible logger names
show log [name] prints out the last segment of log in memory, 'global' is default
use <db_name> set current database
db.foo.find() list objects in collection foo
db.foo.find( a : 1 ) list objects in foo where a == 1
it result of the last line evaluated; use to further iterate
DBQuery.shellBatchSize = x set default number of items to display on shell
exit quit the mongo shell
>
6.2 需要密码认证
在连接期间进行身份验证,使用-u <username>,-p <password>和--authenticationDatabase <database>命令行选项启动一个mongo shell
root@ce90018683a8:/# mongo --port 27017 -u "admin" -p "123456" --authenticationDatabase "admin"
MongoDB shell version v4.2.2
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session "id" : UUID("275dac83-d84c-4147-bb4c-9516cedc534a")
MongoDB server version: 4.2.2
Server has startup warnings:
2022-08-29T03:39:16.657+0000 I CONTROL [initandlisten]
2022-08-29T03:39:16.657+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2022-08-29T03:39:16.657+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2022-08-29T03:39:16.657+0000 I CONTROL [initandlisten]
2022-08-29T03:39:16.657+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2022-08-29T03:39:16.657+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2022-08-29T03:39:16.657+0000 I CONTROL [initandlisten]
---
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 dbs
admin 0.000GB
config 0.000GB
local 0.000GB
>
6.3 以 admin 用户身份进入,先连接后验证
将 mongo shell
连接到 mongodb
,也就是先连接,后验证用户身份
root@ce90018683a8:/# mongo
MongoDB shell version v4.2.2
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session "id" : UUID("6e560085-2f03-4b6a-81a6-b42cd5b03f96")
MongoDB server version: 4.2.2
> use admin
switched to db admin
> db.auth("admin","123456")
1
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
>
7、使用命令创建数据库
# 进入admin数据库
[root@localhost mongodb]# docker exec -it mongodb mongo admin
MongoDB shell version v4.2.2
connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session "id" : UUID("91508ef9-195d-4a87-a8ae-c36ee3dfcd12")
MongoDB server version: 4.2.2
# 输入账号密码认证,返回1说明认证成功
> db.auth("admin", "123456")
1
# 查看所有数据库
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
# 创建新数据库
> use oyz
switched to db oyz
# 创建 和新创建的数据库 绑定的用户
> db.createUser( user: 'haolb', pwd: 'haolb123456', roles: [ role: "readWrite", db: "oyz" ] );
Successfully added user:
"user" : "haolb",
"roles" : [
"role" : "readWrite",
"db" : "oyz"
]
# exit退出当前用户,否则继续认证新的用户会报错 too many users are authenticated
> exit
bye
# 进入 oyz 数据库
[root@localhost mongodb]# docker exec -it mongodb mongo oyz
MongoDB shell version v4.2.2
connecting to: mongodb://127.0.0.1:27017/oyz?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session "id" : UUID("b9a9ea61-a15f-43d8-9e4a-dc2bfa0bdb05")
MongoDB server version: 4.2.2
# 重新认证新的用户
> db.auth("haolb","haolb123456")
1
# 随便添加一条信息才算创建成功
> db.oyz.insert("name":"abc1111");
WriteResult( "nInserted" : 1 )
> show dbs
oyz 0.000GB
>
8、使用命令删除用户
# 切换 admin 库
> use admin
switched to db admin
> db.auth("admin","123456")
1
# 创建一个 myuser 用户
> db.createUser(user: "myuser",pwd: "my123",roles: [ role: "root", db: "admin" ])
Successfully added user:
"user" : "myuser",
"roles" : [
"role" : "root",
"db" : "admin"
]
> db.auth("myuser","my123")
1
> show users
"_id" : "admin.admin",
"userId" : UUID("c19fdb6d-efe1-4398-b32a-77ef8c12bac3"),
"user" : "admin",
"db" : "admin",
"roles" : [
"role" : "root",
"db" : "admin"
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
"_id" : "admin.myuser",
"userId" : UUID("ec82e9f7-9f81-4a2a-b10b-8368d4750e6c"),
"user" : "myuser",
"db" : "admin",
"roles" : [
"role" : "root",
"db" : "admin"
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
# 删除单个用户:myuser
> db.system.users.remove(user:"myuser")
WriteResult( "nRemoved" : 1 )
# 切换 admin 用户登录
> db.auth("admin","123456")
1
# 显示用户信息
> show users
"_id" : "admin.admin",
"userId" : UUID("c19fdb6d-efe1-4398-b32a-77ef8c12bac3"),
"user" : "admin",
"db" : "admin",
"roles" : [
"role" : "root",
"db" : "admin"
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
>
以上是关于Docker安装MongoDB的主要内容,如果未能解决你的问题,请参考以下文章