自动运维.利用PYTHON DBUtils+MySQLdb实现数据库连接池的自动管理?

文件结构:

代码实现:

/xm-workspace/xm-pyss/auto_python/sqlconf.py:

# -*- coding: utf-8 -*-
"""
#
# Authors: limanman
# OsChina: http://my.oschina.net/pydevops/
# Purpose:
#
"""

MysqlConfig = {
    'port': 3306,
    # 启动时连接池中创建的的连接数
    'mincached': 5,
    # 连接池中最大允许创建的连接数
    'maxcached': 20,
    'user': '...',
    'charset': 'utf8',
    'db': '.........',
    # 所有允许的最大连接数上限设置
    'maxconnections': 62,
    'passwd': '...............',
    'host': '..................'
}


if __name__ == '__main__':
    print 'Found Errors: the module can only be imported, not run!'

/xm-workspace/xm-pyss/auto_python/sqlpool.py:

# -*- coding: utf-8 -*-
"""
#
# Authors: limanman
# OsChina: http://my.oschina.net/pydevops/
# Purpose:
#
"""
# 数据库配置文件
import sqlconf
import MySQLdb
from DBUtils.PooledDB import PooledDB
# 为返回字典格式推荐将连接池的cursorclass设置为它
from MySQLdb.cursors import DictCursor


class Mysql(object):
    """
    Attributes:
       None
    """
    __mysql_pool = None

    def __init__(self):
        # 获取连接对象
        self._mysql_conn = Mysql.__get_connection()
        # 创建游标对象
        self._mysql_cursor = self._mysql_conn.cursor()

    @staticmethod
    def __get_connection():
        """Get mysql connection from connection pool.

        Args:
            None
        Returns:
            connection
        """
        # 根据配置文件创建连接池
        if not Mysql.__mysql_pool:
            Mysql.__mysql_pool = PooledDB(
                creator=MySQLdb,
                use_unicode=False,
                cursorclass=DictCursor,
                db=sqlconf.MysqlConfig['db'],
                host=sqlconf.MysqlConfig['host'],
                port=sqlconf.MysqlConfig['port'],
                user=sqlconf.MysqlConfig['user'],
                passwd=sqlconf.MysqlConfig['passwd'],
                charset=sqlconf.MysqlConfig['charset'],
                mincached=sqlconf.MysqlConfig['mincached'],
                maxcached=sqlconf.MysqlConfig['maxcached'],
                maxconnections=sqlconf.MysqlConfig['maxconnections'])
        # 返回连接池中连接对象
        return Mysql.__mysql_pool.connection()

    def get_all(self, sql_command, cmd_param=None):
        """"Get all sql command execute result.

        Args:
            sql_command: sql command
            cmd_param  : sql command paramaters
        Returns:
            tuple
        """
        if cmd_param:
            count = self._mysql_cursor.execute(sql_command, cmd_param)
        else:
            count = self._mysql_cursor.execute(sql_command)

        if count:
            sql_result = self._mysql_cursor.fetchall()
        else:
            sql_result = None

        return sql_result

    def get_one(self, sql_command, cmd_param=None):
        """"Get one sql command execute result.

        Args:
            sql_command: sql command
            cmd_param  : sql command paramaters
        Returns:
            dict
        """
        if cmd_param:
            count = self._mysql_cursor.execute(sql_command, cmd_param)
        else:
            count = self._mysql_cursor.execute(sql_command)

        if count:
            sql_result = self._mysql_cursor.fetchoneDict()
        else:
            sql_result = None

        return sql_result

    def get_money(self, sql_command, res_num, cmd_param=None):
        """"Get one sql command execute result.

        Args:
            sql_command: sql command
            cmd_param  : sql command paramaters
        Returns:
            dict
        """
        if cmd_param:
            count = self._mysql_cursor.execute(sql_command, cmd_param)
        else:
            count = self._mysql_cursor.execute(sql_command)

        if count:
            sql_result = self._mysql_cursor.fetchmoney(size=res_num)
        else:
            sql_result = None

        return sql_result

    # 删查改 (略)
    
    def sta_commit(self):
        # 不对修改立即提交.
        self._mysql_conn.autocommit(0)

    def con_release(self, is_commit=False):
        # 释放连接提交回滚?
        # if is_is_commit:
            # self._mysql_conn.commit()
        # else:
            # self._mysql_conn.rollback()

        # 释放刚获取的连接
        self._mysql_cursor.close()
        self._mysql_conn.close()

/xm-workspace/xm-pyss/auto_python/main.py:

# -*- coding: utf-8 -*-
"""
#
# Authors: limanman
# OsChina: http://my.oschina.net/pydevops/
# Purpose:
#
"""
import time
import pprint
from sqlpool import Mysql


def main():
    """Main function."""
    # 简单测试(watch -n 1 "netstat -ant|grep 3306")
    sql_command = r"select mac from device_my where userid=(select id from `user` where username='ytest');"
    while True:
        mysql = Mysql()
        pprint.pprint(mysql.get_one(sql_command ))
        mysql.con_release()

        time.sleep(1)

if __name__ == '__main__':
    main()

有图有相:


你可能感兴趣的:(自动运维.利用PYTHON DBUtils+MySQLdb实现数据库连接池的自动管理?)