python实用mysql池-motivation from=>[查询时与mysql服务器失去连接]基于mysql连接器支持fixed动态池
PyMysqlPool的Python项目详细描述
Desc
python实用mysql池说明: 这个包包含一个纯python mysql连接器库。pymysqlpool的目标 是一个mysql池,其动机是基于mysql connector=>;[查询期间与mysql服务器失去连接]。
- 功能
- 使用方便。
- 支持【no、fixed、dynamic pool】。
- 管理【失败/丢失连接】。
- 支持【no、fixed、dynamic pool】=>;django框架。
- 支持【无、固定、动态池】=>;烧瓶框架。
Requirements
- python库–以下之一:
- mysqldb
- Python–以下之一:
- python中的成功测试>;=2.7
- MySQL服务器–以下之一:
- mysql>;=5.5(成功测试,>;=5.5~)
Installation
上一个稳定版本在pypi上可用,可以使用pip:
安装$ pip install PyMysqlPool
您可以使用easy_install:
安装$ easy_install PyMysqlPool
您可以使用manually:
安装$ git clone https://github.com/LuciferJack/python-mysql-pool.git or download ***.tar.gz $ cd PyMysqlPool-*** $ python setup.py install
Example
下面是prototype池示例:
step:1""" file: new a mysql_config.py file and change to your db config """db_config={'local':{'host':"10.95.130.***",'port':8899,'user':"root",'passwd':"****",'db':"marry",'charset':"utf8",'pool':{#use = 0 no pool else use pool"use":1,# size is >=0, 0 is dynamic pool"size":0,#pool name"name":"local",}},'poi':{'host':"10.95.130.***",'port':8787,'user':"lujunxu",'passwd':"****",'db':"poi_relation",'charset':"utf8",'pool':{#use = 0 no pool else use pool"use":0,# size is >=0, 0 is dynamic pool"size":0,#pool name"name":"poi",}},}step:2""" Note:create your own table """step:3(exampleshowbelow)fromPyMysqlPool.db_util.mysql_utilimportquery,query_single,insertOrUpdate,""" pool size special operation """defquery_pool_size():job_status=2_sql="select * from master_job_list j where j.job_status in (%s) "_args=(job_status,)task=query(db_config['local'],_sql,_args)logging.info("query_npool method query_npool result is %s ,input _data is %s ",task,_args)return""" single query """defquery_npool():job_status=2_sql="select * from master_job_list j where j.job_status !=%s "_args=(job_status,)task=query_single(db_config['local'],_sql,_args)logging.info("query_npool method query_npool result is %s ,input _data is %s ",task,_args)return""" insert """definsert(nlp_rank_id,hit_query_word):#add more args_args=(nlp_rank_id,hit_query_word)_sql="""INSERT INTO nlp_rank_poi_online (nlp_rank_id,hit_query_word,rank_type,poi_list,poi_raw_list,article_id,city_id,status,create_time,version,source_from) VALUES (%s,%s,%s, %s, %s,%s, %s,%s, %s,%s,%s)"""affect=insertOrUpdate(db_config['local'],_sql,_args)logging.info("insert method insert result is %s ,input _data is %s ",affect,_args)return""" update """defupdate(query_word,query_id):_args=(query_word,query_id)_sql="""update nlp_rank set query_word = %s WHERE id = %s"""affect=insertOrUpdate(db_config['local'],_sql,_args)logging.info("update method update result is %s ,input _data is %s ",affect,_args)return
Djangouseexample:""" file:settings.py change to your db config """DATABASES={'default':{'ENGINE':'PyMysqlPool.mysql.connector.django','NAME':'django','USER':'root','PASSWORD':'*******','HOST':'10.95.130.***','PORT':'8899','OPTIONS':{'autocommit':True,'pool':{#use = 0 no pool else use pool"use":1,# size is >=0, 0 is dynamic pool"size":0,#pool name"name":"local",}},}}
Flaskuseexample:""" change to your db config """fromPyMysqlPool.mysql.connector.flask.mysqlimportMySQLapp=Flask(__name__,template_folder='flaskPoolShowcase/flask_templates')#mysql configapp.config.update(DEBUG=False,MYSQL_DATABASE_HOST='10.95.130.***',MYSQL_DATABASE_PORT=8899,MYSQL_DATABASE_USER='root',MYSQL_DATABASE_PASSWORD='******',MYSQL_DATABASE_DB='flask',MYSQL_USE_POOL={#use = 0 no pool else use pool"use":0,# size is >=0, 0 is dynamic pool"size":10,#pool name"name":"local",},)mysql=MySQL()mysql.init_app(app)
或者使用连接类型,比如prototype方法。
Resources
python mysql连接器:https://dev.mysql.com/downloads/connector/python/
mysql参考手册:http://dev.mysql.com/doc/
MySQL客户端/服务器协议: http://dev.mysql.com/doc/internals/en/client-server-protocol.html
pymysqlpool邮件列表:https://groups.google.com/forum/#!forum/PyMysqlPool-users
License
pymysqlpool是根据mit许可证发布的。有关详细信息,请参见许可证。
Plan
Dynamic Load Optimization.Minimum number of connections to maximum performance.
Scope
Now use in BaiDu off-line calculation module.Like this project, welcome to use and to enhance together.
Frequency Ask
- django支持–测试以下选项之一: Django 1.11.5日 展示案例:https://github.com/LuciferJack/Django-pool-showcase
- 烧瓶支架-在以下其中一个上进行测试:
- 烧瓶0.12.2 展示案例:https://github.com/LuciferJack/Flask-pool-showcase