Migrate from MySQLDB to pymysql

Replacing MySQLDB with PyMySQL.

PyMySQL is a drop-in replacement for MySQLdb
and released under MIT License instead of GPL
(OpenStack incompatible
http://governance.openstack.org/reference/licensing.html).

Change-Id: I76ecac112c9a5a373f2f0362de77f9e32e97511c
This commit is contained in:
Artur Basiak 2016-04-07 12:35:12 +02:00 committed by Witold Bedyk
parent 5e71ddaf54
commit 0af745ff63
4 changed files with 22 additions and 23 deletions

View File

@ -63,12 +63,12 @@ class Cacti(AgentCheck):
# Try importing MySQL
try:
import MySQLdb
import pymysql
except ImportError:
raise Exception(
"Cannot import MySQLdb module. This module is required for the cacti plugin to work correctly")
"Cannot import PyMySQL module. This module is required for the cacti plugin to work correctly")
connection = MySQLdb.connect(config.host, config.user, config.password, config.db)
connection = pymysql.connect(config.host, config.user, config.password, config.db)
self.log.debug("Connected to MySQL to fetch Cacti metadata")

View File

@ -56,14 +56,14 @@ class MySql(checks.AgentCheck):
@staticmethod
def get_library_versions():
try:
import MySQLdb
version = MySQLdb.__version__
import pymysql
version = pymysql.__version__
except ImportError:
version = "Not Found"
except AttributeError:
version = "Unknown"
return {"MySQLdb": version}
return {"PyMySQL": version}
def check(self, instance):
host, port, user, password, mysql_sock, defaults_file, options = self._get_config(
@ -94,25 +94,25 @@ class MySql(checks.AgentCheck):
def _connect(self, host, port, mysql_sock, user, password, defaults_file):
try:
import MySQLdb
import pymysql
except ImportError:
raise Exception(
"Cannot import MySQLdb module. Check the instructions "
"to install this module at https://app.datadoghq.com/account/settings#integrations/mysql")
"Cannot import PyMySQl module. Check the instructions "
"to install this module at https://pypi.python.org/pypi/PyMySQL")
if defaults_file != '':
db = MySQLdb.connect(read_default_file=defaults_file)
db = pymysql.connect(read_default_file=defaults_file)
elif mysql_sock != '':
db = MySQLdb.connect(unix_socket=mysql_sock,
db = pymysql.connect(unix_socket=mysql_sock,
user=user,
passwd=password)
elif port:
db = MySQLdb.connect(host=host,
db = pymysql.connect(host=host,
port=port,
user=user,
passwd=password)
else:
db = MySQLdb.connect(host=host,
db = pymysql.connect(host=host,
user=user,
passwd=password)
self.log.debug("Connected to MySQL")

View File

@ -41,11 +41,10 @@ class MySQL(monasca_setup.detection.Plugin):
configured_mysql = False
# Attempt login, requires either an empty root password from localhost
# or relying on a configured /root/.my.cnf
if self.dependencies_installed(): # ensures MySQLdb is available
import _mysql_exceptions
import MySQLdb
if self.dependencies_installed(): # ensures PyMySQL is available
import pymysql
try:
MySQLdb.connect(read_default_file=mysql_conf)
pymysql.connect(read_default_file=mysql_conf)
log.info(
"\tUsing client credentials from {:s}".format(mysql_conf))
# Read the mysql config file to extract the needed variables.
@ -76,24 +75,24 @@ class MySQL(monasca_setup.detection.Plugin):
except IOError:
log.error("\tI/O error reading {:s}".format(mysql_conf))
pass
except _mysql_exceptions.MySQLError:
except pymysql.MySQLError:
log.warn("\tCould not connect to mysql using credentials from {:s}".format(mysql_conf))
pass
# Try logging in as 'root' with an empty password
if not configured_mysql:
try:
MySQLdb.connect(host='localhost', port=3306, user='root')
pymysql.connect(host='localhost', port=3306, user='root')
log.info("\tConfiguring plugin to connect with user root.")
config['mysql'] = {'init_config': None, 'instances':
[{'name': 'localhost', 'server': 'localhost', 'user': 'root',
'port': 3306}]}
configured_mysql = True
except _mysql_exceptions.MySQLError:
except pymysql.MySQLError:
log.warn("\tCould not connect to mysql using root user")
pass
else:
exception_msg = 'The mysql dependency MySQLdb is not installed;' \
exception_msg = 'The mysql dependency PyMySQL is not installed;' \
' the mysql plugin is not configured'
log.error(exception_msg)
raise Exception(exception_msg)
@ -108,7 +107,7 @@ class MySQL(monasca_setup.detection.Plugin):
def dependencies_installed(self):
try:
import MySQLdb
import pymysql
except ImportError:
return False

View File

@ -9,7 +9,7 @@ class TestMySql(unittest.TestCase):
# This should run on pre-2.7 python so no skiptest
self.skip = False
try:
import MySQLdb
import pymysql
except ImportError:
self.skip = True