在Ubuntu上实现MongoDB的自动化运维可以通过以下步骤完成:
环境准备确保您的Ubuntu系统满足以下要求:操作系统:Ubuntu 20.04 或更高版本硬件要求:根据您的应用程序需求选择合适的硬件配置网络连接:确保您的服务器可以连接到互联网,以便下载 MongoDB 镜像。安装 MongoDB- 更新系统包列表:
sudo apt update
安装 MongoDB 包管理器:sudo apt install software-properties-common
添加 MongoDB 官方源:sudo add-apt-repository deb [arch=amd64,arm64] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse
更新包列表:sudo apt update
安装 MongoDB:sudo apt install -y mongodb-org
启动 MongoDB 服务:sudo systemctl start mongod
设置 MongoDB 为开机自启:sudo systemctl enable mongod
- 创建配置文件:
sudo vi /etc/mongod.conf
配置副本集:replication:replSetName: "rs0"
重启 MongoDB 服务:sudo systemctl restart mongod
初始化副本集:mongors.initiate({_id: "rs0",members: [{ _id: 0, host: "mongodb-primary:27017" },{ _id: 1, host: "mongodb-secondary:27017" },{ _id: 2, host: "mongodb-arbiter:27017" }]});
- 修改配置文件:
sudo vi /etc/mongod.conf
启用认证:security:authorization: enabled
重启 MongoDB 服务:sudo systemctl restart mongod
创建管理员用户:use admindb.createUser({user: "admin",pwd: "password",roles: [{ role: "userAdminAnyDatabase", db: "admin" }]});
重启 MongoDB 服务:sudo systemctl restart mongod
- 编写启动脚本:
#!/bin/bashsudo systemctl start mongod
编写重启脚本:#!/bin/bashsudo systemctl restart mongod
设置执行权限:chmod +x start_mongodb.shchmod +x restart_mongodb.sh
运行脚本:./start_mongodb.sh./restart_mongodb.sh
通过以上步骤,您可以在 Ubuntu 系统下轻松实现 MongoDB 的安装、配置和自动化运维。希望本文对您有所帮助。