专业编程基础技术教程

网站首页 > 基础教程 正文

mongodb远程链接以及用户授权以及备份恢复

ccvgpt 2024-08-14 15:34:13 基础教程 9 ℃

使用场景

a. mongodb命令行模式数据查询

b.mongodb数据库创建以及用户授权

mongodb远程链接以及用户授权以及备份恢复

c.mongodb数据备份

d.mongodb数据恢复

mongodb的远程链接命令详解

远程链接命令(命令行)

1.基于ubuntu系统
安装 mongo客户端
apt install mongodb-clients

2.测试远程链接命令
mongo --host mongo-rs0.uatapps.hw-k8s.efssc.assistfc.com --port 27017  --username efssc --password efssc@123456 --authenticationDatabase efssc

参数解释: --username  用户名称 --password 用户密码 --authenticationDatabase 认证的库 --host 链接地址 --port 链接的端口

mongodb数据库创建以及用户授权

1. 以超级管理员身份(root@admin)登陆mongoDB 

root@redis-cluster:/data/mongo_prod/prod_mongo/efssc# mongo --host mongo-rs0.uatapps.hw-k8s.efssc.assistfc.com --port 27017  --username root --password test@mongo123456 --authenticationDatabase admin
MongoDB shell version v3.6.3
connecting to: mongodb://mongo-rs0.uatapps.hw-k8s.efssc.assistfc.com:27017/
MongoDB server version: 4.4.10
WARNING: shell and server versions do not match
Server has startup warnings: 
{"t":{"$date":"2023-05-26T03:16:41.791+00:00"},"s":"I",  "c":"REPL",     "id":21315,   "ctx":"ReplCoord-0","msg":"","tags":["startupWarnings"]}
{"t":{"$date":"2023-05-26T03:16:41.791+00:00"},"s":"I",  "c":"REPL",     "id":21316,   "ctx":"ReplCoord-0","msg":"** WARNING: This replica set has a Primary-Secondary-Arbiter architecture, but readConcern:majority is enabled ","tags":["startupWarnings"]}
{"t":{"$date":"2023-05-26T03:16:41.791+00:00"},"s":"I",  "c":"REPL",     "id":21317,   "ctx":"ReplCoord-0","msg":"**          for this node. This is not a recommended configuration. Please see ","tags":["startupWarnings"]}
{"t":{"$date":"2023-05-26T03:16:41.791+00:00"},"s":"I",  "c":"REPL",     "id":21318,   "ctx":"ReplCoord-0","msg":"**          https://dochub.mongodb.org/core/psa-disable-rc-majority","tags":["startupWarnings"]}
{"t":{"$date":"2023-05-26T03:16:41.791+00:00"},"s":"I",  "c":"REPL",     "id":21319,   "ctx":"ReplCoord-0","msg":"","tags":["startupWarnings"]}
rs0:PRIMARY> 
  2. 没有wade库,use wade 会自动建库
  rs0:PRIMARY> use wade
switched to db wade
rs0:PRIMARY> 
  3.在 wade库里建一个 wade业务用户,并授权
rs0:PRIMARY> db.createUser({user : "wade_test",pwd : "wade@123",roles: [ { role : "readWrite", db : "wade" } ,{ role : "dbAdmin", db : "wade" } ,{ role : "userAdmin", db : "wade" } ],mechanisms: ["SCRAM-SHA-1"]})
Successfully added user: {
        "user" : "wade_test",
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "wade"
                },
                {
                        "role" : "dbAdmin",
                        "db" : "wade"
                },
                {
                        "role" : "userAdmin",
                        "db" : "wade"
                }
        ],
        "mechanisms" : [
                "SCRAM-SHA-1"
        ]
}

mongodb的数据备份

mongodump --host mongo服务IP地址 -u root -p admin密码 --authenticationDatabase  admin --forceTableScan -d efssc_main -o ./

mongodb的数据恢复

mongorestore --host mongo-rs0.uatapps.hw-k8s.efssc.assistfc.com --port 27017  --username root --password efssc@mongo123456 --authenticationDatabase admin -d efssc efssc
参数详解 :-d 是数据库名称  后面是备份文件

Tags:

最近发表
标签列表