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