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 ...
随机推荐
- ML(4.3): R Random Forest
随机森林模型是一种数据挖掘模型,常用于进行分类预测.随机森林模型包含多个树形分类器,预测结果由多个分类器投票得出. 决策树相当于一个大师,通过自己在数据集中学到的知识对于新的数据进行分类.俗话说得好, ...
- CMake Error: Curses library not found. Please install appropriate package
编译安装MySQL的时候,出现错误: -- Could NOT find Curses (missing: CURSES_LIBRARY CURSES_INCLUDE_PATH) CMake Err ...
- 1113 Integer Set Partition (25 分)
1113 Integer Set Partition (25 分) Given a set of N (>1) positive integers, you are supposed to pa ...
- 可以ping通ip地址,但是访问80,或者8080报错
这个是服务器的防火墙的原因,一般linux防火墙默认是打开的,并且只开放22端口,用于远程连接. 这个时候需要关闭防火墙,或者开放端口.
- Eclipse里面的Maven项目打包(Maven build)
eclipse里面执行maven build打包的时候,如何设置参数? 主要就是设置一个goals
- Lua中的metatable详解
转自:http://www.jb51.net/article/56690.htm Lua 中 metatable 是一个普通的 table,但其主要有以下几个功能: 1.定义算术操作符和关系操作符的行 ...
- Spring Batch批处理以及编程模型
1.批处理: 类似于SQL里面的批处理提交 2.场景: 业务定时进行批处理操作,但是批处理的编程模型是怎么的呢? 3.开源框架 Spring Batch 4.编程模型: reader-processo ...
- PHP mysqli_autocommit() 函数
定义和用法 mysqli_autocommit() 函数开启或关闭自动提交数据库修改. 提示:请查看 mysqli_commit() 函数,用于提交指定数据库连接的当前事务.请查看 mysqli_ro ...
- jshint在bat批处理中闪退,代码中无法调用的问题
先说解决办法:加个call eg: call jshint --version Pause 具体原因有空再更
- dos批处理文件中的变量小结
很多情况下我们需要用到一些批处理文件中的变量,方便我们执行一些操作,这里简单整理下,方便需要的朋友 批处理中的变量,我把他分为两类,分别为"系统变量"和"自定义变量& ...