mkdir /usr/local/mongo-4.4.1/rs0
cd /usr/local/mongo-4.4.1/rs0
mkdir rps1 rps1-log rps2 rps2-log rps3 rps3-log
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.1.tgz
wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-rhel70-x86_64-100.1.1.tgz
vim /etc/security/limits.conf
* soft nofile 204800
* hard nofile 204800
* soft nproc 204800
* hard nproc 204800
vim /etc/security/limits.d/20-nproc.conf
* soft nproc 204800
* hard nproc 204800
#增加一行
vim /etc/pam.d/login
session required pam_limits.so
# 查看
ulimit -a
启动三个mongo实例
/usr/local/mongo-4.4.1/bin/mongod --dbpath /usr/local/mongo-4.4.1/rs0/rps1 --bind_ip 0.0.0.0 --maxConns 20000 --logpath /usr/local/mongo-4.4.1/rs0/rps1-log/mongodb-rs0-rps1.log --fork --port 27017 --replSet "rs0"
/usr/local/mongo-4.4.1/bin/mongod --dbpath /usr/local/mongo-4.4.1/rs0/rps2 --bind_ip 0.0.0.0 --maxConns 20000 --logpath /usr/local/mongo-4.4.1/rs0/rps2-log/mongodb-rs0-rps2.log --fork --port 27018 --replSet "rs0"
/usr/local/mongo-4.4.1/bin/mongod --dbpath /usr/local/mongo-4.4.1/rs0/rps3 --bind_ip 0.0.0.0 --maxConns 20000 --logpath /usr/local/mongo-4.4.1/rs0/rps3-log/mongodb-rs0-rps3.log --fork --port 27019 --replSet "rs0"
进入任一实例,初始化rs
#查看最大连接数方法
db.serverStatus().connections
var respconf = {
"_id" : "rs0",
"members" : [
{
"_id" : 0,
"host" : "10.0.0.1:27017"
},
{
"_id" : 1,
"host" : "10.0.0.2:27018"
},
{
"_id" : 2,
"host" : "10.0.0.3:27019"
}
]
}
rs.initiate(respconf);
#!/bin/bash
DUMP=/nfsdata/mongoBak/bin/mongodump #mongodump命令路径
OUT_DIR=/nfsdata/mongoBak/mongodb_bak_now #临时备份目录
TAR_DIR=/nfsdata/mongoBak/mongodb_bak_list #备份存放路径
DATE=`date +%Y_%m_%d` #获取当前系统时间
DB_USER=sdbuser #数据库账号
DB_PASS=*** #数据库密码
DAYS=20 #DAYS=20代表删除20天前的备份,即只保留近20天的备份
TAR_BAK="mongodb_bak_$DATE.tar.gz" #最终保存的数据库备份文件
cd $OUT_DIR
rm -rf $OUT_DIR/*
mkdir -p $OUT_DIR/$DATE
$DUMP -h 10.0.0.1:27017 -u $DB_USER -p $DB_PASS -o $OUT_DIR/$DATE #备份全部数据库
tar -zcvf $TAR_DIR/$TAR_BAK $OUT_DIR/$DATE #压缩为.tar.gz格式
find $TAR_DIR/ -mtime +$DAYS -delete #删除20天前的备份文件
exit
crontab -e
0 1 * * * /nfsdata/mongoBak/bin/backup.sh #每天1点执行
use admin
db.createUser({user:"aaa",pwd:"xxx",roles:[{role:"readWrite",db:"test"}]})
#数据连接uri
mongodb://sdb:bigdata@10.0.0.1:27017,10.0.0.2:27018,10.0.0.3:27019/test?authSource=admin&replicaSet=rs0&slaveOk=true&readPreference=secondaryPreferred