05月20, 2022

通过shell创建mongodb用户

通过shell创建用户

#!/bin/bash
DB_NAME=mydb
DB_USER=mydb
DB_PASSWORD=psw123
MONGO_ADMIN_USER=root
MONGO_ADMIN_PASSWORD=pwd123
MONGO_HOST=192.168.1.3:27017
#
MONGO_CMD="db.createUser({user: '${DB_USER}',pwd: '${DB_PASSWORD}',roles: [{role: 'dbOwner', db: '${DB_NAME}'}]});"

# 本地存在mongo
mongo mongodb://${MONGO_ADMIN_USER}:${MONGO_ADMIN_PASSWORD}@${MONGO_HOST}/${DB_NAME}?authSource=admin --quiet --eval "${MONGO_CMD}"

# 没安装mongo使用docker
docker run --rm -it mongo:4.4 mongo mongodb://${MONGO_ADMIN_USER}:${MONGO_ADMIN_PASSWORD}@${MONGO_HOST}/${DB_NAME}?authSource=admin --quiet --eval "${MONGO_CMD}"

本文链接:https://blog.jnliok.com/post/tong-guo-shell-chuang-jian-mongodb-yong-hu.html

-- EOF --

Comments