1. 下载MongoDB
cd /opt
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon-3.4.9.tgz
tar xf /opt/mongodb-linux-x86_64-amazon-3.4.9.tgz -C /opt/
rm -rf /opt/mongodb-linux-x86_64-amazon-3.4.9.tgz
mv mongodb-linux-x86_64-amazon-3.4.9 mongodb
2. 配置参数
2.1 通用配置
# 环境变量
vim /etc/profile source /etc/profile
=======================
#mongodb
export PATH=$PATH:/opt/mongodb/bin
=======================
# 创建目录
mkdir -p /opt/mongodb/log && mkdir -p /opt/mongodb/db && chown -R 777 /opt/mongodb/log && chown -R 777 /opt/mongodb/log && mkdir -p /opt/mongodb/conf && chown -R 777 /opt/mongodb/conf
2.2 节点配置
cd /opt/mongodb/conf
touch mongodb.conf
vim mongodb.conf
dbpath=/opt/mongodb/db
logpath=/opt/mongodb/log/mongodb.log
port=27017
fork=true
初次启动添加admin帐号
[root@DB01 conf]# mongod -f /opt/mongodb/conf/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 100629
child process started successfully, parent exiting
[root@DB01 conf]# mongo
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.9
Server has startup warnings:
2022-08-22T16:10:08.865+0800 I CONTROL [initandlisten]
2022-08-22T16:10:08.865+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2022-08-22T16:10:08.865+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2022-08-22T16:10:08.865+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2022-08-22T16:10:08.865+0800 I CONTROL [initandlisten]
> db.createUser({user:'admin',pwd:'PASSWORD',roles:[{role:'root',db:'admin'}]})
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
> exit
bye
2.3 设置节点
关闭进程
[root@DB01 conf]# mongod -f /opt/mongodb/conf/mongodb.conf --shutdown
2022-08-22T16:13:20.986+0800 I CONTROL [main] log file "/opt/mongodb/log/mongodb.log" exists; moved to "/opt/mongodb/log/mongodb.log.2022-08-22T08-13-20".
# 配置文件修改
echo replSet=rs0 >> /opt/mongodb/conf/mongodb.conf
[root@DB01 conf]# mongod -f /opt/mongodb/conf/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 100077
child process started successfully, parent exiting
[root@DB01 conf]# mongo 10.161.53.10:27017/admin -u test -p
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.9
Server has startup warnings:
2022-08-22T16:01:22.773+0800 I CONTROL [initandlisten]
2022-08-22T16:01:22.773+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2022-08-22T16:01:22.773+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2022-08-22T16:01:22.773+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2022-08-22T16:01:22.773+0800 I CONTROL [initandlisten]
> use admin
switched to db admin
> config_rs={_id : 'mongoTest',members : [{_id : 0, host : '10.161.53.10:27017'},{_id : 1, host : '10.161.53.11:27017'}]}
{
"_id" : "mongoTest",
"members" : [
{
"_id" : 0,
"host" : "10.161.53.10:27017"
},
{
"_id" : 1,
"host" : "10.161.53.11:27017"
}
]
}
> rs.initiate(config_rs);
{
"ok" : 0,
"errmsg" : "Attempting to initiate a replica set with name mongoTest, but command line reports rs0; rejecting",
"code" : 93,
"codeName" : "InvalidReplicaSetConfig"
}
> config_rs={_id : 'rs0',members : [{_id : 0, host : '10.161.53.10:27017'},{_id : 1, host : '10.161.53.11:27017'}]}
{
"_id" : "rs0",
"members" : [
{
"_id" : 0,
"host" : "10.161.53.10:27017"
},
{
"_id" : 1,
"host" : "10.161.53.11:27017"
}
]
}
> rs.initiate(config_rs);
{ "ok" : 1 }
查看副本状态
[scode type=“share” size=“”]
health表示副本集中该节点是否正常,0表示不正常,1表示正常;
state表示节点的身份,0表示非主节点,1表示主节点;
stateStr用于对节点身份进行字符描述,PRIMARY表示主节点,SECONDARY表示副节点;
name是副本集节点的ip和端口信息
[/scode]
查看副本同步状态
db.printSlaveReplicationInfo();
3. 注册服务
vim /etc/init.d/mongod
#!/bin/sh
#
#chkconfig: 2345 80 90
#author: qwq.ro
#description: mongodb
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
start() {
/opt/mongodb/bin/mongod -f /opt/mongodb/conf/mongodb.conf
}
stop() {
/opt/mongodb/bin/mongod -f /opt/mongodb/conf/mongodb.conf --shutdown
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac
# 修改权限
chmod -R 777 /etc/init.d/mongod
# 设置开机自启
chkconfig --add mongod
chkconfig mongod on
# 重启服务
service mongod restart
4. 配置副本集的读写分离
[root@DB02 init.d]# sudo find / -name .mongorc.js
/root/.mongorc.js
[root@DB02 init.d]# echo 'rs.slaveOk();' >> /root/.mongorc.js
[scode type=“yellow” size=“”]
一般这个文件都是空的,直接加上去。保存退出。之后退出mongo在进去就可以了。
尽量每台服务器都添加上去。
[/scode]