How to make mysql connection that requires CA-CERT with sqlalchemy or SQLObject

旧时模样 提交于 2019-11-26 14:23:09

问题


I would like to connect to a MySQL database that requires ca-cert. I can do it with MySQLdb like below:

MySQLdb.connect(host = self.host,
                port = self.port,
                unix_socket = self.unix_socket,
                user = self.user,                                
                passwd = self.passwd,
                db = self.db,
                ssl = { 'cert': self.sslcert,
                        'key': self.sslkey,
                         'ca': self.sslca } 

How do I do the same think in SQLAlchemy or SQLObject?

Thanks, peter


回答1:


create_engine() in SQLAlchemy has a connect_args parameter:

connect_args – a dictionary of options which will be passed directly to the DBAPI’s connect() method as additional keyword arguments.




回答2:


To use SSL certs with SQLAlchemy and MySQLdb, use the following python code:

db_connect_string='mysql://<user>:<pswd>@<db server>:3306/<database>'
ssl_args = {'ssl': {'cert':'/path/to/client-cert', 
                     'key':'/path/to/client-key', 
                      'ca':'/path/to/ca-cert'}}

create_engine(db_connect_string, connect_args=ssl_args)



回答3:


According to their docs, SQLAlchemy's create_engine function takes a db url with the following format: dialect[+driver]://user:password@host/dbname[?key=value..] meaning you could pass the ssl key, cert, and ca as key value pairs.




回答4:


SQLObject (untested):

from sqlobject.mysql import MySQLConnection
connection = MySQLConnection(
    db=self.db,
    user=self.user,
    password=self.password,
    host=self.host,
    ssl_key=self.sslkey,
    ssl_cert=self.sslcert,
    ssl_ca=self.sslca,
)


来源:https://stackoverflow.com/questions/8014781/how-to-make-mysql-connection-that-requires-ca-cert-with-sqlalchemy-or-sqlobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!