Docker mongo:5.0

 1、拉取

[root@Tseng-HW ~]# docker pull mongo:5.0
5.0: Pulling from library/mongo
7b1a6ab2e44d: Pull complete 
90eb44ebc60b: Pull complete 
5085b59f2efb: Pull complete 
c7499923d022: Pull complete 
019496b6c44a: Pull complete 
c0df4f407f69: Pull complete 
351daa315b6c: Pull complete 
a233e6240acc: Pull complete 
a3f57d6be64f: Pull complete 
dd1b5b345323: Pull complete 
Digest: sha256:5be752bc5f2ac4182252d0f15d74df080923aba39700905cb26d9f70f39e9702
Status: Downloaded newer image for mongo:5.0
docker.io/library/mongo:5.0
[root@Tseng-HW ~]# docker images
REPOSITORY                                    TAG       IMAGE ID       CREATED         SIZE
mongo                                         5.0       dfda7a2cf273   7 months ago    693MB
[root@Tseng-HW ~]#

2、创建数据目录

[root@Tseng-HW docker]# mkdir -p /opt/docker/mongdb/data
[root@Tseng-HW docker]#

3、运行容器命令

[root@Tseng-HW docker]# docker run -d --restart=always -p 20007:27017 --name mongodb -v /opt/docker/mongdb/data:/data/db -e MONGO_INITDB_ROOT_USERNAME=tsengadmin -e MONGO_INITDB_ROOT_PASSWORD=tseng#2022  -d mongo:5.0
9df69ade730bea3b73fba28e364da448f9f5d186150f99c2b3b69e14d6538bc5
[root@Tseng-HW docker]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS          PORTS                                           NAMES
9df69ade730b   mongo:5.0   "docker-entrypoint.s…"   28 seconds ago   Up 27 seconds   0.0.0.0:20007->27017/tcp, :::20007->27017/tcp   mongodb

4、进入容器操作

[root@Tseng-HW docker]# docker exec -it 9df6 mongo
MongoDB shell version v5.0.5
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("ddc933b5-34c8-47bf-a517-0b27661f6b26") }
MongoDB server version: 5.0.5
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
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
> use admin
switched to db admin
> db.auth("tsengadmin","tseng#2022");
1
> show users;
{
	"_id" : "admin.tsengadmin",
	"userId" : UUID("ae877221-06e7-43a0-b4dd-7eddeaeafb75"),
	"user" : "tsengadmin",
	"db" : "admin",
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}
> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB
>

5、创建新的库/表(先退出当前操作,重新进入)

> exit
bye
[root@Tseng-HW docker]# docker exec -it 9df6 mongo
MongoDB shell version v5.0.5
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("92b55e22-d634-4570-a663-fa524b4d00bc") }
MongoDB server version: 5.0.5
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
> use admin
switched to db admin
> db.auth("tsengadmin","tseng#2022");
1
> use tseng
switched to db tseng
> db.createUser({user:"tsengmongo",pwd:"Tseng@2022",roles:[{role: 'readWrite', db:'tseng'},{role: 'dbAdmin', db:'tseng'}]});
Successfully added user: {
	"user" : "tsengmongo",
	"roles" : [
		{
			"role" : "dbAdmin",
			"db" : "tseng"
		}
	]
}
> show users;
{
	"_id" : "tseng.tsengmongo",
	"userId" : UUID("ed69eb0f-66c6-4e33-95d2-b68ab3a28b25"),
	"user" : "tsengmongo",
	"db" : "tseng",
	"roles" : [
		{
			"role" : "dbAdmin",
			"db" : "tseng"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}
> db.createCollection("tseng1");
{ "ok" : 1 }
> db.getCollectionNames();
[ "tseng1" ]
> db
tseng
> db.getName();
tseng
-- 删除用户
> db.dropUser("qcdpre");

6、开放端口、工具连接

7、修改子库/表 连接密码(使用管理员权限用户登录修改密码之后,退出容器,重新进入查库)

> use admin
switched to db admin
> db.auth("tsengadmin","tseng#2022");
1
> use tseng
switched to db tseng
> db.changeUserPassword("tsengmongo", "tseng*2022")
> use tseng
switched to db tseng
> db.auth("tsengmongo","tseng*2022");
1
> db.getCollectionNames();
uncaught exception: Error: listCollections failed: {
	"ok" : 0,
	"errmsg" : "logical sessions can't have multiple authenticated users (for more details see: https://docs.mongodb.com/manual/core/authentication/#authentication-methods)",
	"code" : 13,
	"codeName" : "Unauthorized"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:718:15
DB.prototype._getCollectionNamesInternal@src/mongo/shell/db.js:790:12
DB.prototype.getCollectionNames@src/mongo/shell/db.js:799:12
@(shell):1:1
> exit
bye
[root@Tseng-HW docker]# docker exec -it 9df6 mongo
MongoDB shell version v5.0.5
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("cb916f25-5584-4e97-b432-14740e74d193") }
MongoDB server version: 5.0.5
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
> use tseng
switched to db tseng
> db.auth("tsengmongo","tseng*2022");
1
> db.getCollectionNames();
[ "tseng1" ]
>

密码修改,亲测可用 

[root@iZbp1h97m5g9eit9nhnyb0Z ~]# docker exec -it f3de /bin/bash
root@f3de15488c7c:/# mongo  
MongoDB shell version v5.0.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("13f8efb6-ed63-48a6-9c9c-d110c79f8bd2") }
MongoDB server version: 5.0.6
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
> use admin
switched to db admin
> db.auth("admin","132");
1
> use abc
switched to db abc
> db.getCollectionNames();
[
	"…………",
]
> db.changeUserPassword("abcmongo","AbcBatt!2023")
> db.auth("abcmongo","AbcBatt!2023")
1
> db.getCollectionNames();
-- 密码生效 查询报错
uncaught exception: Error: listCollections failed: {
	"ok" : 0,
	"errmsg" : "logical sessions can't have multiple authenticated users (for more details see: https://docs.mongodb.com/manual/core/authentication/#authentication-methods)",
	"code" : 13,
	"codeName" : "Unauthorized"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:723:15
DB.prototype._getCollectionNamesInternal@src/mongo/shell/db.js:795:12
DB.prototype.getCollectionNames@src/mongo/shell/db.js:804:12
@(shell):1:1
> exit
-- ctrl+c 或 exit 退出重新登录
bye
root@f3de15488c7c:/# mongo
MongoDB shell version v5.0.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("667020d2-56a0-406e-b097-3344ca8d0503") }
MongoDB server version: 5.0.6
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
> use abc
switched to db abc
-- 修改后密码登录查询数据
>  db.auth("abcmongo","AbcBatt!2023")
1
> db.getCollectionNames();
[
	"…………", 
]
>

8、数据库常用命令(操作集合\数据库)

help帮助
db.help()集合所有方法
db.stats()集合当前状态信息
db当前数据库
db.getName()当前数据库
show dbs所有数据库列表
use test切换数据库
db.createCollection('tseng1')数据库创建集合/表
db.getCollectionNames()获取数据库所有集合/表
db.dropDatabase()删除当前数据库
show tables查看当前库中的表/集合
show collections查看当前库中的表/集合
db.tseng1.drop()删除mycollection集合

9、MongoDB的角色

超级用户角色root
数据库用户角色read、readWrite
数据库管理角色dbAdmin、dbOwner、userAdmin
集群管理角色clusterAdmin、clusterManager、4. clusterMonitor、hostManage
备份恢复角色backup、restore
所有数据库角色readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
内部角色__system

10、MongoDB角色说明

root只在admin数据库中可用。超级账号,超级权限
Read允许用户读取指定数据库
readWrite允许用户读写指定数据库
dbAdmin允许用户在指定数据库中执行管理函数,如索引创建、删除,查看统计或访问system.profile
userAdmin允许用户向system.users集合写入,可以在指定数据库里创建、删除和管理用户
clusterAdmin只在admin数据库中可用,赋予用户所有分片和复制集相关函数的管理权限
readAnyDatabase只在admin数据库中可用,赋予用户所有数据库的读权限
readWriteAnyDatabase只在admin数据库中可用,赋予用户所有数据库的读写权限
userAdminAnyDatabase只在admin数据库中可用,赋予用户所有数据库的userAdmin权限
dbAdminAnyDatabase只在admin数据库中可用,赋予用户所有数据库的dbAdmin权限

11、db.stats 

> db.stats()
{
	"db" : "qcd",
	"collections" : 1,
	"views" : 0,
	"objects" : 4,
	"avgObjSize" : 4395.5,
	"dataSize" : 17582,
	"storageSize" : 20480,
	"indexes" : 1,
	"indexSize" : 20480,
	"totalSize" : 40960,
	"scaleFactor" : 1,
	"fsUsedSize" : 93222162432,
	"fsTotalSize" : 527370027008,
	"ok" : 1
}
> db.stats(1024)    # 单位kb
{
	"db" : "qcd",
	"collections" : 1,
	"views" : 0,
	"objects" : 4,
	"avgObjSize" : 4395.5,
	"dataSize" : 17.169921875,
	"storageSize" : 20,
	"indexes" : 1,
	"indexSize" : 20,
	"totalSize" : 40,
	"scaleFactor" : 1024,
	"fsUsedSize" : 91068252,
	"fsTotalSize" : 515009792,
	"ok" : 1
}
> db.stats(1048576)    # 单位M
{
	"db" : "qcd",
	"collections" : 1,
	"views" : 0,
	"objects" : 4,
	"avgObjSize" : 4395.5,
	"dataSize" : 0.016767501831054688,
	"storageSize" : 0.01953125,
	"indexes" : 1,
	"indexSize" : 0.01953125,
	"totalSize" : 0.0390625,
	"scaleFactor" : 1048576,
	"fsUsedSize" : 88939.921875,
	"fsTotalSize" : 502939.25,
	"ok" : 1
}
> 

dbStats.db                        数据库的名称。

dbStats.collections        数据库中的集合数。

dbStats.views                   数量

dbStats.objects               对象数量(具体而言,文件) 在所有集合的数据库中。

dbStats.avgObjSize          每个文档的平均大小(以字节为单位)。这是 dataSize除以文档的数量。这 尺度论据不影响 avgObjSize值。

dbStats.dataSize              数据库中保存的未压缩数据的总大小
dbStats.storageSize   分配给数据库中所有集合的空间总和 文档存储,包括可用空间。

dbStats.freeStorageSize  分配给数据库中所有集合的可用空间总和文档贮存。空闲数据库存储空间分配给集合但不包含数据。

freeStorageSize                  不包括分配给索引的可用空间。看indexFreeStorageSize为总的自由索引大小。将此值包含在dbStats输出,设置 免费存储为 1

dbStats.indexes      数据库中所有集合的索引总数。

dbStats.indexSize             分配给数据库中所有索引的空间总和,包括可用索引空间。

dbStats.indexFreeStorageSize 分配给数据库中所有索引的可用空间总和。空闲数据库存0.6 中更新。

dbStats.totalSize        为数据库中所有集合中的文档和索引分配的空间总和。包括已用和可用存储空间。这是总和storageSize和 indexSize.        4.4版中的新功能。

dbStats.scaleFactor   scale命令使用的值。如果您指定了非整数比例因子,MongoDB 将使用指定因子的整数部分。例如,如果您指定比例因子1023.999,MongoDB 将1023用作比例因子。4.2版中的新功能。

dbStats.fsUsedSize   MongoDB 存储数据的文件系统上正在使用的所有磁盘空间的总大小。

dbStats.fsTotalSize        MongoDB 存储数据的文件系统上所有磁盘容量的总大小。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值