使用加密的选项文件将 Python 连接到 MySQL

sha*_*ker 6 python mysql python-3.x

我曾经用密码mysql_config_editor创建一个.mylogin.cnf文件。我知道它工作正常,因为我可以使用它通过命令行实用程序mysql和 R 包进行连接RMySQL而不会出现问题。

但是,当尝试使用 Mysql-Connector/Python 进行连接时:

# using mysql-connector-python-rf
import os
import mysql.connector
con = mysql.connector.connect(option_files=os.path.expanduser('~/.mylogin.cnf'))
Run Code Online (Sandbox Code Playgroud)

或使用 PyMySQL:

# using pymysql
import os
import pymysql
con = pymysql.connect(option_files=os.path.expanduser('~/.mylogin.cnf'))
Run Code Online (Sandbox Code Playgroud)

我犯了同样的错误:

---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-64-d17e56ef7010> in <module>()
----> 1 con = mysql.connector.connect(option_files=os.path.expanduser('~/.mylogin.cnf'))

/usr/local/lib/python3.5/site-packages/mysql/connector/__init__.py in connect(*args, **kwargs)
    140     # Option files
    141     if 'option_files' in kwargs:
--> 142         new_config = read_option_files(**kwargs)
    143         return connect(**new_config)
    144 

/usr/local/lib/python3.5/site-packages/mysql/connector/optionfiles.py in read_option_files(**config)
     66             config['option_files'] = [config['option_files']]
     67         option_parser = MySQLOptionsParser(list(config['option_files']),
---> 68                                            keep_dashes=False)
     69         del config['option_files']
     70 

/usr/local/lib/python3.5/site-packages/mysql/connector/optionfiles.py in __init__(self, files, keep_dashes)
    162             self.files = files
    163 
--> 164         self._parse_options(list(self.files))
    165         self._sections = self.get_groups_as_dict()
    166 

/usr/local/lib/python3.5/site-packages/mysql/connector/optionfiles.py in _parse_options(self, files)
    193                                      "than once in the list".format(file_))
    194                 with open(file_, 'r') as op_file:
--> 195                     for line in op_file.readlines():
    196                         if line.startswith('!includedir'):
    197                             _, dir_path = line.split(None, 1)

/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/encodings/ascii.py in decode(self, input, final)
     24 class IncrementalDecoder(codecs.IncrementalDecoder):
     25     def decode(self, input, final=False):
---> 26         return codecs.ascii_decode(input, self.errors)[0]
     27 
     28 class StreamWriter(Codec,codecs.StreamWriter):

UnicodeDecodeError: 'ascii' codec can't decode byte 0x96 in position 28: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

浏览源代码,看起来这些人正在尝试以明文形式读取文件。但是,mysql_config_editor加密它生成的登录文件。在代码中手动输入密码时,两个模块都可以正常工作。

如何使用这些生成的配置文件之一连接到 Python?我使用的是 Python 3,所以 MySQLdb 不是一个选项。

更新:现在,我使用RPy2在 R 中运行查询并将结果通过管道返回到 Python。代码有点难看,但工作流程还不错。

exi*_*xic 3

pip install myloginpath为我工作,然后:

import myloginpath
import pymysql
conf = myloginpath.parse('client')
db = pymysql.connect(**conf, host='mydbhost', db='whatever')
Run Code Online (Sandbox Code Playgroud)