您的位置:首页 > 数据库 > Mongodb

mongodban安装和配置

jerrism 2020-05-11 04:13 239 查看 https://blog.csdn.net/jerrism/

Ubuntu16.04安装:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
echo "deb [ arch=amd64,arm64,ppc64el,s390x ] http://repo.mongodb.com/apt/ubuntu xenial/mongodb-enterprise/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-enterprise.list
sudo apt-get update
sudo apt-get install -y mongodb-enterprise
  1. vim /etc/mongod.conf修改bind-ip
  2. 修改内存限制:
storage:
wiredTiger:
engineConfig:
cacheSizeGB: 10
  1. 设置登录验证:
security:
authorization: enabled

设置用户密码:

use admin
db.createUser(
{
user:"***",
pwd:"***",
roles:[{role:"root",db:"admin"}]
}
)

启动

  • service mongod start
  • 或通过配置文件运行:
    mongod -f /etc/mongod.conf --fork
  • 无法读取配置文件时:
    mongod --dbpath /var/lib/mongodb --logpath /var/log/mongodb/mongod.log --bind_ip_all --fork

重复主键批量数据插入测试:

import pymongo
import random
import time
from pymongo.errors import BulkWriteError

db = pymongo.MongoClient()
collection = db['test']['test']
data = []
for i in range(100000):
data.extend([{'_id': str(i)}] * random.randint(1, 2))
a = time.time()
print('original data counts:', len(data))
try:
collection.insert_many(data, ordered=False)
except BulkWriteError:
print('Batch op errors!')
finally:
print('time cost:', time.time() - a)
print('inserted data counts:', collection.count_documents({}))

测试结果:

original data counts: 149954
Batch op errors!
time cost: 2.9790291786193848
inserted data counts: 100000

Inserted_many中的ordered参数设为false即乱序插入,当前一条文档出现插入错误时不会影响其他文档的插入。

与mysql插入性能比较:
https://blog.csdn.net/chenhuajie123/article/details/9326777
Mongodb在指定主键的情况下插入性能下降极大,考虑以后所有数据先用ssdb去重,然后使用无主键插入。

jerrism 原创文章 16获赞 1访问量 454 关注 私信
标签: