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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. MySQL Connector/Python 安装、测试

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

  4. python使用mysql的三个模块:mysql.connector、sqlalchemy、MySQLdb

    在python中使用mysql其实很简单,只要先安装对应的模块即可,那么对应的模块都有什么?官方也没指定也没提供,pcat就推荐自己遇到的3个模块:mysql.connector.sqlalchemy ...

  5. python操作mysql——mysql.connector

    连接mysql, 需要mysql connector, conntector是一种驱动程序,python连接mysql的驱动程序,mysql官方给出的名称为connector/python, 可参考m ...

  6. Python:安装MYSQL Connector

    在Python中安装MySQL Connector有如下三种方法: 1.直接安装客户端[建议使用] pip install mysqlclient 2.安装mysql连接器 pip install - ...

  7. python导入模块报错:ImportError: No module named mysql.connector(安装 mysql)

    python的版本是 $ python --version Python 2.7.12 报错代码如下 import mysql.connector 报错信息是 ImportError: No modu ...

  8. MySQL Connector/Python 接口 (二)

    连接数据库 本文参见这里,示例如何连接MySQL 数据库. import mysql.connector from mysql.connector import errorcode # 连接数据库需要 ...

  9. MySQL Connector/Python 接口 (一)

    这里仅介绍 MySQL 官方开发的 Python 接口,参见这里: https://dev.mysql.com/doc/connector-python/en/ Chapter 1 Introduct ...

随机推荐

  1. JQuery 实现 Tab 切换 index

    $(function(){ var h_new=$('.new h4 a'); var new_dl=$('.new dl'); new_dl.first().show(); h_new.mousee ...

  2. JMS概念

    来自为知笔记(Wiz)

  3. bzoj2763 飞行路线

    Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并 ...

  4. 关于String.valueOf()和.toString的问题

    以下是String.valueOf()的源代码 public static String valueOf(Object obj) {     return (obj == null) ? " ...

  5. ASP.NET Web Pages:帮助器

    ylbtech-.Net-ASP.NET Web Pages:帮助器 1.返回顶部 1. ASP.NET Web Pages - 帮助器 Web 帮助器大大简化了 Web 开发和常见的编程任务. AS ...

  6. 关于单独使用ckfinder时去除版本号和提示信息的破解

    前端时间用ckfinder2.3时  吧他破解了 发现在火狐下报了一个这个错.....getItem() is  undefinder  一气之下就把报错的那句话if()语句给删了  没想到歪打正着 ...

  7. ExtJS的数据模型

    给大家介绍一下ExtJS的组件模型. 常见的Ajax的开发流程: 1.定义URL,metod,params 2.开发后台  接收JSON/XML数据 返回JSON/XML数据 3.前台回调 4.显示到 ...

  8. 蓝瓶的钙,好喝的钙——windows,我要蓝屏的

    原文地址:http://80x86.io/post/windows-blue-screen-0x00000050-page_fault_in_nonpaged_area 这里只截取一部分. windo ...

  9. 代码生成器 CodeSmith 的使用(三)

    在第二篇中,介绍了用 codesmith 生成数据库中的一些字段,可生成的属性不够简洁,这次对上一次的版本进行重构,生成一些简洁的属性访问器.代码如下: Camel 规则: <%-- Name: ...

  10. LinkedHashMap唯一,存储取出有序

    package cn.itcast_03; import java.util.LinkedHashMap; import java.util.Set; /* * LinkedHashMap:是Map接 ...