python中的pymongo连接脚本分析

无情 阅读:229 2020-04-10 13:36:45 评论:0

author: headsen chen

date: 2019-04-12  17:39:12

 

先安装python3,pymongo

[[email protected] mnt]# cat /root/python-install.sh  
#!/bin/bash 
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel 
mkdir -p /usr/local/python3 
cd /mnt 
wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz 
tar -zxvf Python-3.6.1.tgz 
cd Python-3.6.1 
./configure --prefix=/usr/local/python3 
make && make install 
ln -s /usr/local/python3/bin/python3 /usr/bin/python3 
echo "PATH=$PATH:/usr/local/python3/bin"  >>/etc/profile 
echo "export PATH " >>/etc/profile 
source /etc/profile 
python3 -V

安装 pip

/usr/local/python3/bin/pip3 install --upgrade pip 
/usr/local/python3/bin/pip3 install pymongo

安装mongodb

[[email protected] mnt]# cat mongo-install.sh  
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz 
tar fx mongodb-linux-x86_64-3.0.6.tgz  
mv mongodb-linux-x86_64-3.0.6 /usr/local/mongodb 
export PATH=/usr/local/mongodb/bin:$PATH

[[email protected] mnt]# echo "export PATH=/usr/local/mongodb/bin:$PATH" >>/etc/profile;source /etc/profile # 让mongo命令生效到环境变量里边,

pymongo插入多行数据脚本

import pymongo 
import datetime 
def main():
myclient = pymongo.MongoClient('mongodb://abc.dddd.com:27017') # 连接地址 myclient.admin.authenticate("useraaaaa","passwordaaaa",mechanism='SCRAM-SHA-1') # 用户名,密码,连接机制 mydb = myclient["db_value"] # 数据库名 mycol = mydb["Collection_name"] # 表名 for i in range(98): invite_record = {"Code":"Event008","Name":"邀请好友注册", "Target":"","UserId":169203, "Value":10, "CreateTime": datetime.datetime.now().utcnow()} x = mycol.insert_one(invite_record) # insert_one :pymongo 自带的一次插入一行记录的方法 print(x.inserted_id) main()
标签:mongodbPython
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
关注我们

扫一扫关注我们,了解最新精彩内容