- A+
所属分类:mongoDB
1.编写安装脚本
vim mongodb.sh
#!/bin/bash # Check if user is root if [ $(id -u) != "0" ]; then echo "Error: You must be root to run this script, please use root to optimization system" exit 1 fi echo "Select Install 1 YUM MongoDB 3.6 2 YUM MongoDB 4.0 3 Tar MongoDB 3.6 4 Tar MongoDB 4.0 5 don't install is now " sleep 0.1 read -p "Please Input 1,2,3,4,5: " SERVER_ID if [[ $SERVER_ID == 3 ]]; then SERVER="tar3" elif [[ $SERVER_ID == 1 ]]; then SERVER="yum1" elif [[ $SERVER_ID == 2 ]]; then SERVER="yum2" elif [[ $SERVER_ID == 4 ]]; then SERVER="tar4" else exit fi function yum1_ins { tee /etc/yum.repos.d/mongodb-org-3.6.repo <<-'EOF' [mongodb-org-3.6] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc EOF yum install -y mongodb-org } function yum2_ins { tee /etc/yum.repos.d/mongodb-org-4.0.repo <<-'EOF' [mongodb-org-4.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc EOF yum install -y mongodb-org } function tar3_ins { yum install wget numactl -y wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-v3.6-latest.tgz&&tar zxvf mongodb-linux-x86_64-rhel70-v3.6-latest.tgz; mv mongodb-linux-x86_64-rhel70-3.6.6-99-g56cd241/ mongodb && cd mongodb; mkdir -p data/db/ log; echo never >> /sys/kernel/mm/transparent_hugepage/enabled echo never >> /sys/kernel/mm/transparent_hugepage/defrag tee mongo.conf <<-'EOF' systemLog: destination: file path: "./log/mongodb.log" logAppend: true storage: dbPath: "./data/db" directoryPerDB: true journal: enabled: false wiredTiger: engineConfig: cacheSizeGB: 4 directoryForIndexes: true processManagement: fork: true net: #bindIp: 0.0.0.0 port: 27017 # ssl: # mode: allowSSL # PEMKeyFile: /etc/ssl/mongodb.pem # CAFile: /etc/ssl/ca.pem # disabledProtocols: TLS1_0,TLS1_1 setParameter: enableLocalhostAuthBypass: false EOF tee mongo.sh <<-'EOF' #!/bin/bash # #chkconfig: 2345 80 90 #description: Start and stops mongodb #定义命令 CMD=./bin/mongod #定义配置文件 CONF=./mongo.conf #定义数据目录 DBPATH=./data/db #定义日志目录 LOGPATH=./log/mongodb.log start() { #后台运行 numactl --interleave=all $CMD -f $CONF echo "MongoDB is running background..." } stop() { $CMD --shutdown --dbpath=$DBPATH echo "MongoDB is stopped" } case "$1" in start) start ;; stop) stop ;; *) echo $"Usage: $0 {start|stop}" esac EOF chmod +x mongo.sh } function tar4_ins { yum install wget numactl -y wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.1.tgz&&tar zxvf mongodb-linux-x86_64-rhel70-4.0.1.tgz; mv mongodb-linux-x86_64-rhel70-4.0.1 mongodb && cd mongodb; mkdir -p data/db/ log; echo never >> /sys/kernel/mm/transparent_hugepage/enabled echo never >> /sys/kernel/mm/transparent_hugepage/defrag tee mongo.conf <<-'EOF' systemLog: destination: file path: "./log/mongodb.log" logAppend: true storage: dbPath: "./data/db" directoryPerDB: true journal: enabled: false wiredTiger: engineConfig: cacheSizeGB: 4 directoryForIndexes: true processManagement: fork: true net: #bindIp: 0.0.0.0 port: 27017 # ssl: # mode: allowSSL # PEMKeyFile: /etc/ssl/mongodb.pem # CAFile: /etc/ssl/ca.pem # disabledProtocols: TLS1_0,TLS1_1 setParameter: enableLocalhostAuthBypass: false EOF tee mongo.sh <<-'EOF' #!/bin/bash # #chkconfig: 2345 80 90 #description: Start and stops mongodb #定义命令 CMD=./bin/mongod #定义配置文件 CONF=./mongo.conf #定义数据目录 DBPATH=./data/db #定义日志目录 LOGPATH=./log/mongodb.log start() { #后台运行 numactl --interleave=all $CMD -f $CONF echo "MongoDB is running background..." } stop() { $CMD --shutdown --dbpath=$DBPATH echo "MongoDB is stopped" } case "$1" in start) start ;; stop) stop ;; *) echo $"Usage: $0 {start|stop}" esac EOF chmod +x mongo.sh } if [ $SERVER == "yum1" ]; then yum1_ins elif [ $SERVER == "yum2" ];then yum2_ins elif [ $SERVER == "tar3" ];then tar3_ins elif [ $SERVER == "tar4" ];then tar4_ins fi
2.安装mongodb
sh mongodb.sh
3.配置用户
[root@localhost ~]# mongodb/mongo.sh start [root@localhost ~]# mongodb/bin/mongo > use admin; > db.createRole({role:'sysadmin',roles:[],privileges:[{resource:{anyResource:true},actions:['anyAction']}]}); > db.createUser({user:'admin',pwd:'Admin@123',roles:[{role:'sysadmin',db:'admin'}]}); > use history; switched to db history > db.createUser({user:'history',pwd:'history',roles:[{role:'readWrite',db:'history'}]});
4.添加认证启动
[root@localhost ~]# mongodb/mongo.sh stop
修改启动脚本mongo.sh 在启动命令中添加--auth变成如下
numactl --interleave=all $CMD --auth -f $CONF [root@localhost ~]# mongodb/mongo.sh start
5.锁库
db.fsyncLock(); db.fsyncUnlock()
6.导出备份
mongodump --port 27019 -d dspt -u admin -p admin -o /home/long/mongodb/bak --authenticationDatabase admin
7.导入备份
mongorestore --port 27019 -d dspt -u admin -p admin /home/long/mongodb/bak/dspt/ --authenticationDatabase admin
8.测试
mongo 127.0.0.1:27019 >use admin; > db.auth("admin","Admin@123") > show dbs > show collections > db.history_1745.drop() #删除history_1745表名
附:
无mongo.conf,用命令启动
mongodb mongod --fork --dbpath=mongodb/data/db/ --logpath=mongodb/log/mongodb.log --logappend
--fork 后台运行
--logappend 防止日志被删除
--rest 28017 web用户界面
如脚本执行报错,请使用vim -b 编辑脚本,发现脚本中有^M,请使用以下方法修改
sed -i ‘s/^M//g' filename
注意:^M的输入方式是 Ctrl + v ,然后Ctrl + M,使用键盘上的shift+6打出的^是没用的
附: