python mysql connector
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 os
import re
import subprocess
from collections import OrderedDict
import string
import socket
import logging as log
import mysql.connector as mysqlconn
import time
import pwd
from 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 = db
self.mysql_user = user
self.mysql_password = password
self.mysql_root_password = root_password
self.mysql_random_root_password = False
self.mysql_onetime_password = None
self.repl_user = repl_user
self.repl_password = repl_password
self.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.conn
except (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() # we discard results from writes
conn.commit()
try:
cur.fetchall()
except MySQLError:
# Will get "InternalError: No result set to fetch from."
# for SET statements. We can safely let this slide if the
# `execute` call passes
pass
finally:
# exceptions are an unrecoverable situation
self._query_buffer.clear()
cur.close() def initialize_db(self):
"""
post-installation run to set up data directories
and install mysql.user tables
"""
self.make_datadir()
log.info('Initializing database...')
try:
subprocess.check_call(['/usr/bin/mysql_install_db',
'--user=mysql',
'--datadir={}'.format(self.datadir)])
log.info('Database initialized.')
return True
except subprocess.CalledProcessError:
log.warn('Database was previously initialized.')
return False def make_datadir(self):
""" Create the data dir if it doesn't already exist"""
try:
os.mkdir(self.datadir, 0770)
self.take_ownership()
except OSError:
pass def take_ownership(self, owner='mysql'):
"""
Set ownership of all directories and files under config.datadir
to `owner`'s UID and GID. Defaults to setting ownership for
mysql user.
"""
directory = self.datadir
user = pwd.getpwnam(owner)
os.chown(directory, user.pw_uid, user.pw_gid)
for root, dirs, files in os.walk(self.datadir):
for di in dirs:
os.chown(os.path.join(root, di), user.pw_uid, user.pw_gid)
for fi in files:
os.chown(os.path.join(root, fi), user.pw_uid, user.pw_gid) def setup_root_user(self, conn):
"""
Create the root user and optionally give it a random root password
"""
if self.mysql_random_root_password:
# we could use --random-passwords in our call to `mysql_install_db`
# instead here but we want to have the root password available
# until we're done with this setup.
chars = string.ascii_letters + string.digits + '!@#$%&^*()'
passwd = ''.join([chars[int(os.urandom(1).encode('hex'), 16) % len(chars)]
for _ in range(20)])
self.mysql_root_password = passwd
log.info('Generated root password: %s', self.mysql_root_password) self.add('SET @@SESSION.SQL_LOG_BIN=0;')
self.add('DELETE FROM `mysql`.`user` where user != \'mysql.sys\';')
self.add('CREATE USER `root`@`%` IDENTIFIED BY %s ;',
(self.mysql_root_password,))
self.add('GRANT ALL ON *.* TO `root`@`%` WITH GRANT OPTION ;')
self.add('DROP DATABASE IF EXISTS test ;')
self.add('FLUSH PRIVILEGES ;')
self.execute_many(conn=conn) def expire_root_password(self, conn):
""" optionally expire the root password """
if self.mysql_onetime_password:
self.execute('ALTER USER `root`@`%` PASSWORD EXPIRE', conn=conn) def create_db(self, conn):
""" this optional schema will be used by the application """
if not self.mysql_db:
log.warn('No default database configured.')
return
sql = 'CREATE DATABASE IF NOT EXISTS `{}`;'.format(self.mysql_db)
self.execute(sql, conn=conn) def create_default_user(self, conn):
""" this optional user will be used by the application """
if not self.mysql_user or not self.mysql_password:
log.warn('No default user/password configured.')
return # there's some kind of annoying encoding bug in the lib here
# so we have to format the string rather than passing it as
# a param. totally safe, I bet.
self.add('CREATE USER `{}`@`%` IDENTIFIED BY %s;'
.format(self.mysql_user), (self.mysql_password,))
if self.mysql_db:
self.add('GRANT ALL ON `{}`.* TO `{}`@`%`;'
.format(self.mysql_db, self.mysql_user))
self.add('FLUSH PRIVILEGES;')
self.execute_many(conn=conn) def create_repl_user(self, conn):
""" this user will be used for both replication and backups """
if not self.repl_user or not self.repl_password:
log.error('No replication user/password configured.')
return self.add('CREATE USER `{}`@`%` IDENTIFIED BY %s; '
.format(self.repl_user), (self.repl_password,))
self.add('GRANT SUPER, SELECT, INSERT, REPLICATION SLAVE, RELOAD'
', LOCK TABLES, GRANT OPTION, REPLICATION CLIENT'
', RELOAD, DROP, CREATE '
'ON *.* TO `{}`@`%`; '
.format(self.repl_user))
self.add('FLUSH PRIVILEGES;')
self.execute_many(conn=conn) def set_timezone_info(self):
"""
Write TZ data to mysqld by piping mysql_tzinfo_to_sql to the mysql
client. This is kinda gross but piping it avoids having to parse the
output for a bulk insert with the Connector/MySQL client.
"""
try:
subprocess.check_output(
'/usr/bin/mysql_tzinfo_to_sql /usr/share/zoneinfo | '
'/usr/bin/mysql -uroot --protocol=socket '
'--socket=/var/run/mysqld/mysqld.sock')
except (subprocess.CalledProcessError, OSError) as ex:
log.error('mysql_tzinfo_to_sql returned error: %s', ex) def restore_from_snapshot(self, filename):
"""
Use innobackupex to restore from a snapshot.
"""
self.make_datadir()
infile = '/tmp/backup/{}'.format(filename)
subprocess.check_call(['tar', '-xif', infile, '-C', '/tmp/backup'])
subprocess.check_call(['/usr/bin/innobackupex',
'--force-non-empty-directories',
'--copy-back',
'/tmp/backup'])
self.take_ownership() def get_primary(self):
"""
Returns the server-id and hostname of the primary as known to MySQL
"""
result = self.query('show slave status')
if result:
return result[0]['Master_Server_Id'], result[0]['Master_Host'] result = self.query('show slave hosts')
if not result:
raise Error('no prior replication setup found')
return result[0]['Master_id'], self.ip def setup_replication(self, primary_ip):
"""
Set up GTID-based replication to the primary; once this is set the
replica will automatically try to catch up with the primary's last
transactions.
"""
self.add('CHANGE MASTER TO '
'MASTER_HOST = %s, '
'MASTER_USER = %s, '
'MASTER_PASSWORD = %s, '
'MASTER_PORT = 3306, '
'MASTER_CONNECT_RETRY = 60, '
'MASTER_AUTO_POSITION = 1, '
'MASTER_SSL = 0; ',
(primary_ip, self.repl_user, self.repl_password))
self.add('START SLAVE;')
self.execute_many() def failover(self, ips):
"""
Call external `mysqlrpladmin failover`. This will determine
best primary candidate, set up replication for all candidates
to the new primary, and catch up stale replicas.
"""
user = self.repl_user
passwd = self.repl_password
candidates = ','.join(
["{}:'{}'@{}".format(user, passwd, ip) for ip in ips]
)
return subprocess.check_call(
['mysqlrpladmin',
'--slaves={}'.format(candidates),
'--candidates={}'.format(candidates),
'--rpl-user={}:{}'.format(user, passwd),
'failover']
) def get_binlog(self):
""" Gets the current binlog file name """
results = self.query('show master status')
binlog_file = results[0]['File']
return binlog_file =======================================================================
from mysqltest.client import MySQL
cli = MySQL()
conn = cli.wait_for_connection(user="root",password="root.123")
print cli.query("show databases;",conn=conn)
=========================================================================
[{u'Database': u'information_schema'}, {u'Database': u'mhc'}, {u'Database': u'mysql'}, {u'Database': u'performance_schema'}, {u'Database': u'springbootdb'}]
============================================================================
python mysql connector的更多相关文章
- Snippet: Fetching results after calling stored procedures using MySQL Connector/Python
https://geert.vanderkelen.org/2014/results-after-procedure-call/ Problem Using MySQL Connector/Pytho ...
- Installing MySQL Connector/Python using pip v1.5
The latest pip versions will fail on you when the packages it needs to install are not hosted on PyP ...
- MySQL Connector/Python 安装、测试
安装Connector/Python: # wget http://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-pyth ...
- python使用mysql的三个模块:mysql.connector、sqlalchemy、MySQLdb
在python中使用mysql其实很简单,只要先安装对应的模块即可,那么对应的模块都有什么?官方也没指定也没提供,pcat就推荐自己遇到的3个模块:mysql.connector.sqlalchemy ...
- python操作mysql——mysql.connector
连接mysql, 需要mysql connector, conntector是一种驱动程序,python连接mysql的驱动程序,mysql官方给出的名称为connector/python, 可参考m ...
- Python:安装MYSQL Connector
在Python中安装MySQL Connector有如下三种方法: 1.直接安装客户端[建议使用] pip install mysqlclient 2.安装mysql连接器 pip install - ...
- python导入模块报错:ImportError: No module named mysql.connector(安装 mysql)
python的版本是 $ python --version Python 2.7.12 报错代码如下 import mysql.connector 报错信息是 ImportError: No modu ...
- MySQL Connector/Python 接口 (二)
连接数据库 本文参见这里,示例如何连接MySQL 数据库. import mysql.connector from mysql.connector import errorcode # 连接数据库需要 ...
- MySQL Connector/Python 接口 (一)
这里仅介绍 MySQL 官方开发的 Python 接口,参见这里: https://dev.mysql.com/doc/connector-python/en/ Chapter 1 Introduct ...
随机推荐
- innotop监控mysql
InnoTop 是一个系统活动报告,类似于Linux性能工具,它与Linux的top命令相仿,并参考mytop工具而设计. 它专门用后监控InnoDB性能和MySQL服务器.主要用于监控事务,死锁,外 ...
- 浏览器同源策略,跨域请求jsonp
浏览器的同源策略 浏览器安全的基石是"同源政策"(same-origin policy) 含义: 1995年,同源政策由 Netscape 公司引入浏览器.目前,所有浏览器都实行这 ...
- 在HTML标签的事件后面直接写JS代码
<input type="button" onclick="document.getElementdByTagName('div')[0].innrtHTML='S ...
- python之路day03
1 复习计算机基础 计算机基础我们讲到完整的计算机系统包括了:应用程序,操作系统,硬件三部分.那么硬件又分为:cpu,内,和硬盘. 对于用户来说我们操作计算机是通过应用程序来间接控制计算机.当我们打 ...
- Python - Django - ORM 操作表
ORM 的对应关系: 类 ---> 数据库表对象 ---> 数据库行属性 ---> 字段 操作数据库表 ---> ...
- HTML|CSS之布局相关总结
知识内容: 1.浮动相关 2.display属性 3.居中显示 4.盒模型和box-sizing 5.position属性 6.响应式设计 7.flex布局 8.其他 参考:http://zh.lea ...
- zabbix监控vCenter报错,无法自动发现主机
公司机房停电检修,检修完成后重新上电,发现VCSA起不来了,尝试多次无法解决,无奈只好重装.重装VCSA 6.5U2之后又发现无法自动发现主机,报错如下: 'config.vpxd.stats.max ...
- Table-Driven Design 表驱动设计
注:本文所有代码来自 http://www.codeproject.com/Articles/42732/Table-driven-Approach 在许多程序中,经常需要处理那些拥有种种色色不同特性 ...
- Requests库入门
安装: $ pip install requests Response对象的一些基本属性: Response.status_code 请求的返回状态,正常为200 Response.text 页面的字 ...
- jmeter随机函数
有些接口的字段,入参须唯一. 高并发压测的时候,这个比较棘手,可以用多个随机函数组合 如:两个__RandomString中间,夹个__Random ${__RandomString(2,qwerty ...