mongodb开启副本集功能

2 篇文章 0 订阅
2 篇文章 0 订阅
#--------------------------------------------
# 此脚本用于开启Mongodb数据库的副本集功能
# author:cch
# date:2022-09-13
#--------------------------------------------

#!/bin/bash

#admin用户的密码
admin_pwd="admin"

#mongodb服务端口
server_port=27017

#需要操作的数据库
database="inventory"

#新建用户的用户名
new_user_name="newUser"
#新建用户的密码
new_user_pwd="123456"

#开启副本集
mongo localhost:${server_port}/${database} <<-EOF
    rs.initiate({
        _id: "rs0",
        members: [ { _id: 0, host: "localhost:27017" } ]
    });
EOF

#是否存在admin账户,不存在会创建一个,密码是上面指定的密码
#如果已经存在了admin账户,可以把这3行注释掉
mongo localhost:${server_port}/admin <<-EOF
    db.createUser({ user: 'admin', pwd: '${admin_pwd}', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });
EOF

# 配置mongodb事件监听角色
mongo -u admin -p ${admin_pwd} localhost:${server_port}/admin <<-EOF
    db.runCommand({
        createRole: "listDatabases",
        privileges: [
            { resource: { cluster : true }, actions: ["listDatabases"]}
        ],
        roles: []
    });

    db.runCommand({
        createRole: "readChangeStream",
        privileges: [
            { resource: { db: "", collection: ""}, actions: [ "find", "changeStream" ] }
        ],
        roles: []
    });
EOF


sleep 1
#创建用户
mongo -u admin -p ${admin_pwd} localhost:${server_port}/admin <<-EOF
    use ${database}
    
    db.createUser({
        user: '${new_user_name}',
        pwd: '${new_user_pwd}',
        roles: [
            { role: "readWrite", db: "${database}" },
            { role: "read", db: "local" },
            { role: "listDatabases", db: "admin" },
            { role: "readChangeStream", db: "admin" },
            { role: "read", db: "config" },
            { role: "read", db: "admin" }
        ]
    });
EOF
echo "Created users"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值