准备环境

系统版本IP地址MongoDB版本角色
Ubuntu 20.0410.84.3.1234.2.25PRIMARY
Ubuntu 20.0410.84.3.1244.2.25SECONDARY
Ubuntu 20.0410.84.3.1254.2.25SECONDARY

配置文件官方说明
https://www.mongodb.com/docs/manual/reference/configuration-options/

安装MongoDB

1. 创建所需用户、组和mongodb所需目录结构

useradd -m -s /bin/bash mongod && mkdir -p /apps/mongodb/{conf,data,log}

2. 创建YAML格式的配置文件

cat > /apps/mongodb/conf/mongo.conf <<EOF
systemLog:
  destination: file
  path: "/apps/mongodb/log/mongodb.log"
  logAppend: true
storage:
  dbPath: "/apps/mongodb/data/"
processManagement:
  fork: true
net:
  port: 27017
  bindIp: 0.0.0.0
replication:
  replSetName: myrepl  ##指定复制集名称,所有复制集成员此名称要一致
EOF

3. 对数据库文件和目录进行授权

chown -R mongod.mongod /apps/mongodb/

4. 下载、解压并软链

官方地址:MongoDB Community Downloads
因官方下载地址中未找到MongoDB 4.2.25针对Ubuntu 20.04的二进制安装包,所以下载Ubuntu 18.04进行安装,测试无恙

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1804-4.2.25.tgz && tar zxvf mongodb-linux-x86_64-ubuntu1804-4.2.25.tgz -C /usr/local/ && ln -sf /usr/local/mongodb-linux-x86_64-ubuntu1804-4.2.25/ /usr/local/mongodb

5. 验证

root@mongodb02:~# ll /usr/local/
total 48
drwxr-xr-x 12 root root 4096 May 23 13:41 ./
drwxr-xr-x 14 root root 4096 Aug 31  2022 ../
drwxr-xr-x  2 root root 4096 May 20 14:46 bin/
drwxr-xr-x  2 root root 4096 Aug 31  2022 etc/
drwxr-xr-x  2 root root 4096 Aug 31  2022 games/
drwxr-xr-x  2 root root 4096 Aug 31  2022 include/
drwxr-xr-x  3 root root 4096 Aug 31  2022 lib/
lrwxrwxrwx  1 root root    9 Aug 31  2022 man -> share/man/
lrwxrwxrwx  1 root root   50 May 23 13:41 mongodb -> /usr/local/mongodb-linux-x86_64-ubuntu1804-4.2.25//
drwxr-xr-x  3 root root 4096 May 23 13:39 mongodb-linux-x86_64-ubuntu1804-4.2.25/
drwxr-xr-x  2 root root 4096 Aug 31  2022 sbin/
drwxr-xr-x  5 root root 4096 Apr 10 08:52 share/
drwxr-xr-x  2 root root 4096 Aug 31  2022 src/

6. 验证设置PATH变量

echo PATH=/usr/local/mongodb/bin/:'$PATH' > /etc/profile && source /etc/profile

7. 创建systemd管理文件

cat > /etc/systemd/system/mongod.service <<EOF
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
User=mongod
Group=mongod
ExecStart=/usr/local/mongodb/bin/mongod --config /apps/mongodb/conf/mongo.conf
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/usr/local/mongodb/bin/mongod --config /apps/mongodb/conf/mongo.conf --shutdown
PrivateTmp=true
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for mongod as specified in
# https://docs.mongodb.com/manual/reference/ulimit/#recommended-ulimit-settings

[Install]
WantedBy=multi-user.target
EOF

8. 启动mongo服务

systemctl start mongod.service

9. 验证mongo服务

systemctl status mongod.service

10. 设置开机自启

systemctl enable mongod.service

注意:以上操作是安装MongoDB,因此三台都需要执行

配置复制集

1. 登录mongo

root@mongodb02:~#mongo --port 27017 admin

2. 指定复制集的所有成员信息

config = { _id: 'myrepl', members: [
{_id: 0, host: '10.84.3.123:27017'},
{_id: 1, host: '10.84.3.124:27017'} ,
{_id: 2, host: '10.84.3.125:27017'}]
}

3. 初始化并启动复制集

rs.initiate(config)

4. 查询复制集状态

rs.status()

注意:初始化后MongoDB需要选举PRIMARY,因此需要等待片刻才能看到PRIMARY

设置MongoDB登录密码

1. 登录PRIMARY节点

root@mongodb02:~#mongo -host 10.84.3.124 --port 27017 admin

2. 创建admin密码

db.createUser(
  {
    user: "admin",
    pwd: "Seca@2024...",
    roles: [ { role: "root", db: "admin" } ]
  }
)

3. 使用密码登录

root@mongodb02:~# mongo -host 10.84.3.124 --port 27017 -u admin -p
MongoDB shell version v4.2.25
Enter password: #输入密码Seca@2024...
connecting to: mongodb://10.84.3.124:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("92c79a7c-b249-4b11-80ae-d865d1ada8c7") }
MongoDB server version: 4.2.24
Server has startup warnings: 
2024-05-23T07:25:33.868+0000 I  STORAGE  [initandlisten] 
2024-05-23T07:25:33.868+0000 I  STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2024-05-23T07:25:33.868+0000 I  STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2024-05-23T07:25:34.670+0000 I  CONTROL  [initandlisten] 
2024-05-23T07:25:34.670+0000 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2024-05-23T07:25:34.670+0000 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2024-05-23T07:25:34.670+0000 I  CONTROL  [initandlisten] 
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

myrepl:PRIMARY> 
最后修改:2024 年 05 月 25 日
如果觉得我的文章对你有用,请随意赞赏