MongoDB日常维护

日常操作

查询一条数据

db.youcollection.findOne()

查询字段数据类型

typeof db.yourCollectionName.findOne().yourFieldName;

查询多条数据指定显示的字段

find中,第一个大括号为where条件,第二个大括号为要求显示的字段

db.yourcollection.find({},{'S1':1,'_id':0}).limit(2)

S4字段去重

db.getCollection('yourcollection').distinct('S4');

去重后查询总数

db.getCollection("yourcollection").distinct("S2").length

日常维护

时区问题

MongoDB stores all dates and times in UTC. This allows the timezone support and translation to be done application side instead of server side.

The best way to store dates and times is via the Javascript Date object. This will allow you to convert dates to and from Unix timestamps, using getTime() and Date(milliseconds).

It should be noted, that Javascript stores time in milliseconds since UNIX Epoch, whilst the standard UNIX timestamp is in seconds since Epoch.

简单来说就是时区无法在mongodb服务器层面设定,需要应用层面处理

查看负载

mongostat --host=192.168.1.01 --port=27017 --username=admin --password=admin --authenticationDatabase=admin

截断日志

方式一:

kill -SIGUSR1 [pid]

方式二:

db.runCommand({logRotate:1})
定时截断
mkdir -p /home/mongo/scripts/
mkdir -p /home/mongo/logs/

cd /home/mongo/scripts/
vim mlogrotate.sh
#!/bin/sh
source /etc/profile
mongo 127.0.0.1:27017/admin --eval "db.auth('admin','admin1234');db.runCommand({logRotate:1});"
sleep 20s
find /mongodb/log/mongod.log.* -type f -mtime +7 -delete

chmod a+x mlogrotate.sh

arbitor无法登录用户

ps aux | grep mongod | grep -v grep | tr -s " " | cut -d" " -f2 | xargs kill -SIGUSR1

配置定时任务

00 00 * * * /home/mongo/scripts/mlogrotate.sh >> /home/mongo/logs/mlogrotate`date +"\%Y-\%m-\%d"`.log 2>&1 &

查看延迟状态

 db.printSlaveReplicationInfo()

查看datadb库大于3秒的操作

db.currentOp(
	{
		"active":true,
		"secs_running":{"$gt":3},
		"ns":/^datadb\./
	}
)

查看集群集合分片信息

查看某个数据库集合分片信息,key不为空是过滤未分片的表,hashdata库的集合,只显示集合与分片键

use config;
db.collections.find({_id:/^hashdata/,key:{$ne:null}}{"_id":1,"key":1})

查看索引信息

查看索引创建情况

db.adminCommand(
	{
		currentOp:true,
		$or:[
			{op:"command","command.createIndexes":{$exists:true}},
			{op:"none","msg":/^Index Build/}
		]
	}
)

db.currentOp(
	{
		$or:[
			{op:"command","command.createIndexes":{$exists:true}},
			{op:"none","msg":/^Index Build/}
		]
	}
)

查看集合所有索引总大小

db.collectionname.totalIndexSize()

索引使用频率

db.getCollection('collectionname').aggregate([{$indexStats:{}}])

数据迁移

数据压缩导出

cd /mongodb/backup/export/test202106
touch test202106.log
begin_time=`date "+%Y-%m-%d %H:%M:%S"`
echo "begin backup time:" $begin_time>test202106.log
mongodump –h 10.10.10.24:27017 -u admin -p admin1234 --authenticationDatabase=admin -d testdb -c test202106 --numParallelCollections=20 --gzip -o /mongodb/backup/export/test202106
end_time=`date "+%Y-%m-%d %H:%M:%S"`
echo "end backup time:"+ $end_time>>test202106.log

数据导入

. ~/.bash_profile
cd /mongodb/backup/export/test202106
touch restore_test202106.log
begin_time=`date "+%Y-%m-%d %H:%M:%S"`
echo "begin backup time:" $begin_time>restore_test202106.log
mongorestore –h 10.10.10.27:27017 -u admin -p admin1234 --authenticationDatabase=admin -d testhisdb --numInsertionWorkersPerCollection=50 --gzip /mongodb/backup/export/test202106 --noIndexRestore
end_time=`date "+%Y-%m-%d %H:%M:%S"`
echo "end backup time:"+ $end_time>>restore_test202106.log
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值