apicloud mongodb数据库运维
1. 用户数据库查询慢的问题解决:
慢查询日志: A6066413327427.picture query: { $query: { topic(uz*R*id): "5a73c6a7bc2592b52bcd217d" }, orderby: { _id: 1 } } planSummary: IXSCAN { _id: 1 }Exhausted:1 keyUpdates:0 writeConflicts:0 numYields:1050 nreturned:6 reslen:2036 locks:{ Global: { acquireCount: { r: 2102 }, acquireWaitCou} } }
解决办法:创建索引
shell> mongo --port 27017
mongo> use A6066413327427;
mongo> show collections
mongo> db.picture.ensureIndex({"topic(uz*R*id)":1},{ background: true }); //{ background: true } 不锁库方法 等同于这种执行方式db.getCollection("abworkrecord").createIndex({"date":-1,"bossname":-1},background=True);
mongo>db.abworkrecord.getIndexKeys() //获取该表索引
mongo>db.abworkrecord.dropIndex({"date":-1,"bossname":-1},background=True); //删除指定索引 dropIndexes() 则是删除当前表的所有索引
后台运行添加索引:db.jiagouworkrecord.ensureIndex({"username":1},{ background: true });
2.mongodb使用认证的方式进行备份和恢复
更新账户信息:db.updateUser("admin",{pwd:test",roles:[ {role:"root",db:"admin"} ]})
mongo -u admin -p test --port 27018 --authenticationDatabase admin
dT=`date +'%m%d'`
/usr/local/mongodb-3.4.7/bin/mongodump --host="10.0.3.5" --port=27018 -d test -o /opt/intel.$dT.dump
/usr/local/mongodb-3.4.7/bin/mongorestore --host="127.0.0.1" --port=27018 -d intel --dir=/opt/intel.$dT.dump/intel -u admin -p test --authenticationDatabase admin --drop
/usr/local/mongodb3.4.7/bin/mongoexport --host 127.0.0.1 --port 27018 --username apicloud --password chunqiu2admin --authenticationDatabase "admin" --db A6021502052260 --collection file --type json -o /root/test.json
3. 批量更新指定表字段值 (mcm 又拍云域名问题修复)
db.getCollection('file').find({"url":/b0.upaiyun.com/}).forEach(
function(item){
item.url = item.url.replace("b0.upaiyun.com","upyuncdn.apicloud-system.com");
db.getCollection('file').update({"_id":item._id},{$set:{"url":item.url }},true);
}
)
mongodb不能只修改字段数组中单一值,只能通过全量更新的方式。
//A6056276978322
//video
db.getCollection('ershouche').find({"video":/b0.upaiyun.com/}).forEach(
function(item){
item.url = item.video.replace("b0.upaiyun.com","upyuncdn.apicloud-system.com");
db.getCollection('ershouche').update({"_id":item._id},{$set:{"video":item.url }},true);
}
)
//pic_array
db.getCollection('ershouche').find({"pic_array.url":/b0.upaiyun.com/}).forEach(
function(item){
num=item.pic_array.length;
arr=[];
for(i=0;i<num;i++){
url=item.pic_array[i].url.replace("b0.upaiyun.com","upyuncdn.apicloud-system.com");
arr.push({"url":url});
}
db.getCollection('ershouche').update({"_id":item._id},{$set:{"pic_array":arr }},true);
}
)
4. mongodb数据库备份脚本备份
#!/bin/bash
#备份路径
path=/mnt/mongodb
today=`date +'%Y.%m.%d'`
day_5=`date -d "-5 day" +'%Y.%m.%d'`
if [[ ! -d $path/$today ]]; then
mkdir -p $path/$today
fi
#获取备份数据库名
host="127.0.0.1"
port=27018
user="apicloud"
password="chunqiu2admin"
id=`echo "show dbs;"|/usr/local/mongodb3.4.7/bin/mongo $host:$port --username=$user --password=$password --authenticationDatabase=admin --shell|grep 'A[0-9]\{13\}'|awk -F ' ' '{print $1}'`
for i in $id
do
/usr/local/mongodb3.4.7/bin/mongodump --host=$host --port=$port --username=$user --password=$password --authenticationDatabase=admin -d $i -o $path/$today/ >> $path/$today/bak.log
cd $path/$today && tar -czf $i.tar.gz $i --remove-files
done
rm -fr $path/$day_5
5. 修改mongodb最大连接数方法
https://blog.csdn.net/weixin_42451611/article/details/112813832
system 启动时会重置默认limit值,需要在启动服务中增加 LimitNOFILE=655350
/etc/security/limits.conf * soft nofile 655350 * hard nofile 655350 * soft fsize unlimited * hard fsize unlimited * soft nproc 655350 * hard nproc 655350 /etc/systemd/system/mongodb.service [Unit] Description=mongodb After=network.target remote-fs.target nss-lookup.target [Service] Type=forking RuntimeDirectory=mongodb RuntimeDirectoryMode=0751 PIDFile=/home/mongodb/mongod.pid ExecStart=/usr/local/mongodb3.2/bin/mongod --config /etc/mongodb.conf ExecStop=/usr/local/mongodb3.2/bin/mongod --shutdown --config /etc/mongodb.conf PrivateTmp=false LimitNOFILE=655350 [Install] WantedBy=multi-user.target

浙公网安备 33010602011771号