python连接mognodb和mysql
下载python mysql接口模块MySQLdb
wget http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz --no-check-certificate
--no-check-certificate 可以不添加,若下载出现异常,可尝试添加该参数重试一次
安装MySQLdb
tar zxvf MySQL-python-1.2.3.tar.gz MySQL-python-1.2.3 python setup.py build python setup.py install
在/etc/profile中添加path,并执行source
安装pymongo
pip2 install pymongo
简单使用
mongodb简单使用
#!/usr/bin/python # -*- coding: UTF-8 -*- import logging import pymongo import datetime import time logging.basicConfig( level=logging.INFO, format='%(asctime)s %(filename)s : %(levelname)s %(message)s', datefmt='%Y-%m-%d %A %H:%M:%S', filename='tool.log', filemode='a') myclient = pymongo.MongoClient("mongodb://root:abc123@127.0.0.1:27017/") mydb = myclient["mydb"] start=datetime.datetime.now() mycol=mydb["tr069_param_values"] mycol.update_many({},{"$set":{"cs":"mycall123"}}) x=mycol.find_one() ## 测试,休眠1s time.sleep(1) end=datetime.datetime.now() logging.info(x) logging.info(x["sn"]) logging.info((end-start).seconds)
mysql简单使用
#!/usr/bin/python # -*- coding: UTF-8 -*- import MySQLdb conn=MySQLdb.Connect(host='127.0.0.1',user='root',passwd='12345',db='mydb',charset='utf8') ### update this value cursor=conn.cursor() cursor.execute("select count(*) from mydb.mytable") hbResult=cursor.fetchall() hbCount=hbResult[0][0] cursor.close() print "hbCount:",hbCount