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 ...
随机推荐
- DBA的规范
DBA操作规范 1.涉及业务上的修改/删除数据,在得到业务方.CTO的邮件批准后方可执行,执行前提前做好备份,必要时可逆. 2.所有上线需求必须走工单系统,口头通知视为无效. 3.在对大表做表 ...
- BASIC-20_蓝桥杯_数的读法
示例代码: #include <stdio.h>#include <string.h>#define N 10 char num[N] = {0} ; void yuyin(i ...
- print 出来的信息添加到text文件里
- sublime快捷键功能记录
shift+tab 向前缩进 ctrl+shift+k 删除当前行 菜单栏 view--side bar 选项 控制左侧文件展示视图 菜单栏“view”——“hideMiniMap” or &qu ...
- python 可视化 二维坐标标注等等
基本画图操作: import matplotlib.pyplot as plt import numpy as np x = np.linspace(-3,3,50) y1 = 2*x+1 y2 = ...
- [转]Windows 注册自定义的协议
[转自] http://blog.sina.com.cn/s/blog_86e4a51c01010nik.html 1.注册应用程序来处理自定义协议 你必须添加一个新的key以及相关的value到HK ...
- PHP使用FPDF pdf添加水印中文乱码问题 pdf合并版本问题
---恢复内容开始--- require_once('../fpdf/fpdf.php');require_once('../fpdi/fpdi.php'); 使用此插件 pdf 合并 并添加水印 期 ...
- python中__name__的使用
这几天开始学习Python,遇到一些问题,也解决了一些问题. 其中if __name__ == '__main__':这句估计很多和我一样的初学者都是不求甚解.这里作一下解释: 1:__name__是 ...
- HTML5中常用的标签(及标签的属性和作用)
1.标签:<!DOCTYPE>作用:声明是文档中的第一成分,位于<html>标签之前. 2.标签:<html>作用:此元素可告知浏览器其自身是一个HTML文档.属性 ...
- jshint在bat批处理中闪退,代码中无法调用的问题
先说解决办法:加个call eg: call jshint --version Pause 具体原因有空再更