#初始化复制配置#创建数据目录
sudo mkdir -p /app/mongodb6-0/db
sudo mkdir -p /app/mongodb6-0/configdb
sudo chmod -R 777 /app/mongodb6-0
#生成keyfile
sudo openssl rand -base64 128 > /app/mongodb6-0/configdb/keyFile
sudo chmod 600 /app/mongodb6-0/configdb/keyFile
#初始化配置文件
sudo cat > /app/mongodb6-0/configdb/mongod.conf <<EOF
net:
port: 27017
bindIpAll: true
# compression:
# compressors: zstd
security:
keyFile: /data/configdb/keyFile
authorization: enabled
replication:
replSetName: rs0
storage:
wiredTiger:
engineConfig:
cacheSizeGB: 0.5
# journalCompressor: zstd
# collectionConfig:
# blockCompressor: zstd
EOF
#安装mongodb-6.0
sudo docker run -d \
--network host \
--restart=always \
-v /app/mongodb6-0/db:/data/db \
-v /app/mongodb6-0/configdb:/data/configdb \
-v /etc/localtime:/etc/localtime:ro \
--log-opt max-size=1g \
--log-opt max-file=2 \
--name mongodb6-0 mongo:6 mongod \
-f /data/configdb/mongod.conf
#进入容器
docker exec -it mongodb6-0 /bin/bash
#登录数据库
mongosh --port=27017
#切换到admin数据库
use admin
#初始化集群信息
rs.initiate(
{
_id: "rs0",
members: [
{ _id : 0, host : "192.168.1.9:27017"}
]
}
)
#创建管理员账号
db.createUser(
{
user: "admin",
pwd: "123456",
roles: [ { role: "root", db: "admin" } ]
}
);
#登录
db.auth('admin', '123456')