install

pip install mysql-connector-python-rf   报错

从https://dev.mysql.com/downloads/connector/python/ 下载

[root@mhc 下载]# rpm -ivh mysql-connector-python-2.1.6-1.el7.x86_64.rpm
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-connector-python-2.1.6-1.el################################# [100%]

test

client.py:

import osimport reimport subprocessfrom collections import OrderedDictimport stringimport socketimport logging as logimport mysql.connector as mysqlconnimport timeimport pwdfrom mysql.connector import Error as MySQLError

class Error(Exception):pass

class MySQL(object):""" MySQL represents the connection to and configuration of the MySQL process and its clients. """ def __init__(self, db=None,user="test",password="test.123",root_password="root.123",repl_user="repluser",repl_password="repl.123"):self.mysql_db = dbself.mysql_user = userself.mysql_password = passwordself.mysql_root_password = root_passwordself.mysql_random_root_password = False self.mysql_onetime_password = None self.repl_user = repl_userself.repl_password = repl_passwordself.datadir = '/var/lib/mysql' self.pool_size = None

 # state self.ip = "127.0.0.1" self._conn = None self._query_buffer = OrderedDict()

def render(self, src='/etc/my.cnf.tmpl', dest='/etc/my.cnf'):""" Writes-out config files, even if we've previously initialized the DB, so that we can account for changed hostnames, resized containers, etc. """ pool_size = self._get_innodb_buffer_pool_size()with open(src, 'r') as f: template = string.Template(f.read()) rendered = template.substitute(buffer=pool_size,server_id=self.server_id,hostname=self.ip)with open(dest, 'w') as f: f.write(rendered)

@property def server_id(self):""" replace server-id with ID derived from hostname """ _hostname = socket.gethostname()return int(str(_hostname)[:4], 16)

def _get_innodb_buffer_pool_size(self):""" replace innodb_buffer_pool_size value from environment or use a sensible default (70% of available physical memory) """ if not self.pool_size:with open('/proc/meminfo', 'r') as memInfoFile: memInfo = memInfoFile.read() base = re.search(r'^MemTotal: *(\d+)', memInfo).group(1)self.pool_size = int((int(base) / 1024) * 0.7)return self.pool_size

@property def conn(self):""" Convenience method for setting up a cached connection with the replication manager user. """ if self._conn:return self._conn ctx = dict(user=self.repl_user,password=self.repl_password,timeout=25) # derived from ContainerPilot config ttl self._conn = self.wait_for_connection(**ctx)return self._conn

def wait_for_connection(self, user='root', password=None, database=None, timeout=10):""" Polls mysqld socket until we get a connection or the timeout expires (raise WaitTimeoutError). Defaults to root empty/password. """ while timeout > 0:try: sock = '/var/lib/mysql/mysql.sock' return mysqlconn.connect(unix_socket=sock,user=user,password=password,database=database,charset='utf8',connection_timeout=timeout)except MySQLError as ex: timeout = timeout - 1 if timeout == 0:raise Error(ex) time.sleep(1)

def add(self, stmt, params=()):""" Adds a new SQL statement to an internal query buffer """ self._query_buffer[stmt] = params

def execute(self, sql, params=(), conn=None):""" Execute and commit a SQL statement with parameters """ self.add(sql, params)self._execute(conn, discard_results=True)

def execute_many(self, conn=None):""" Execute and commit all previously `add`ed statements in the query buffer """ self._execute(conn, discard_results=True)

def query(self, sql, params=(), conn=None):""" Execute a SQL query with params and return results. """ self.add(sql, params)return self._execute(conn=conn)

def _execute(self, conn=None, discard_results=False):""" Execute and commit all composed statements and flushes the buffer """ try:if not conn: conn = self.connexcept (Error, MySQLError):raise # unrecoverable

 try: cur = conn.cursor(dictionary=True, buffered=True)for stmt, params in self._query_buffer.items(): log.debug('%s %s', stmt, params) cur.execute(stmt, params=params)if not discard_results:return cur.fetchall()

转载于:https://www.cnblogs.com/mhc-fly/p/7227904.html

python mysql connector相关推荐

  1. python从云端数据库获取数据失败_使用%s的Python MySQL Connector数据库查询失败

    使用%s的Python MySQL Connector数据库查询失败 我有一个基本程序,应该查询包含用户信息的数据库.我正在尝试为特定用户选择信息并将其打印到控制台. 这是我的代码:import my ...

  2. python mysql connector update_Python(Mysql Connector)如何刷新curs上的结果

    在开始提问之前,我应该告诉你我是新手.在 我的问题是我有两个游标(在不同的sql连接上),第一个游标从sql数据库获取结果,而第二个游标对结果中的行进行更改.问题是,一旦第二个游标发生更改,我想刷新第 ...

  3. Python MySQL示例教程

    Welcome to Python MySQL example tutorial. MySQL is one of the most widely used database and python p ...

  4. mysql connector python linux_MySQL Connector/Python 安装、测试

    安装Connector/Python: # wget http://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-1. ...

  5. mysql connector python linux_Python使用mysql.connector链接mysql数据库

    之前一直使用pythond mysqldb链接数据库,随着mysql被oracle收购之后,我发现mysqldb就不怎么更新了. 现在开始使用oracle提供的mysql.connector来操作my ...

  6. django mysql connector,MySQL Connector / Python作为Django引擎?

    即使经过数小时和数小时的谷歌搜索,也无法找到答案.搜索堆栈溢出.我向你们保证,我已经看到了所有可能被视为相关的答案,但这些答案都没有解决我所面临的问题.无需再费周折 – 目前在shell中我可以这样做 ...

  7. MySQL Connector / Python

    MySQL Connector / Python允许Python程序使用符合Python数据库API规范v2.0(PEP 249)的API访问MySQL数据库 . MySQL Connector / ...

  8. django mysql connector,MySQL Connector / python在Django中不起作用

    我正在学习以MySQL为后端的Django. 我安装了Oracle的mysql连接器以与mysql连接. 但是,当我运行python manage.py时,出现此错误 Traceback (most ...

  9. python 连接mysql报错:mysql.connector.errors.NotSupportedError: Authentication plugin ‘caching_sha2_passw

    python 连接mysql报错:mysql.connector.errors.NotSupportedError: Authentication plugin 'caching_sha2_passw ...

  10. 安装 Python MySQL 驱动(mysql-connector-python、MySQL-python)

    1. 安装 由于 MySQL 服务器以独立的进程运行,并通过网络对外服务,所以,需要支持 Python 的MySQL 驱动来连接到 MySQL 服务器. 目前,有两个MySQL驱动: mysql-co ...

最新文章

  1. python判断变量不为空_Python简洁方法
  2. 非root用户加入docker用户组省去sudo
  3. mysql mgr故障恢复实现_MGR实现分析 - 成员管理与故障恢复实现
  4. 如何用软件工程消灭软件危机?
  5. 最全总结!聊聊 Python 发送邮件的几种方式
  6. 统计MOOC证书 (15 分)
  7. python set去重 字典 计算求和_python字典set方法的特殊方法
  8. mysql5.5.35编译安装_CentOS 6.5最小化编译安装mysql 5.5.35
  9. [biztalk笔记]-1.Hello World!
  10. GAMIT/GLOBK处理流程
  11. 计算机打印东西怎么横向打印机,打印怎么设置横向打印(打印机怎么调整打印方向)...
  12. C语言----小游戏
  13. 01 hbuilder账号的注册
  14. 2022广东省安全员A证第三批(主要负责人)试题及模拟考试
  15. 【HTML + CSS】如何引入icon图标
  16. .mkv格式的视频播放不了?看这里_
  17. 每日一学—text-decoration 属性
  18. Visual Studio Code的下载和安装
  19. 对抗样本(论文解读八):Towards More Robust Adversarial Attack Against Real World Object Detectors
  20. 【JavaSE】基础语法练习---减肥计划(流程控制)

热门文章

  1. OSPF区域间路由计算规则与实验
  2. ansible 第一次练习
  3. 《腾讯iOS测试实践》一一1.8 小结
  4. 分布式搜索Elasticsearch——QueryBuilders.matchPhrasePrefixQuery
  5. 关于拉格朗日乘子法和KKT条件
  6. ANDROID 中设计模式的採用--结构型模式
  7. Spring 的 ApplicationEvent and ApplicationListener
  8. 基于注解的 Spring MVC 简单入门
  9. 品鉴B500:艺术对技术无乐不作的反击
  10. AD数据库备份[为企业部署Windows Server 2008系列十五]