如何使用 sqlalchemy 或 SQLObject 建立需要 CA-CERT 的 mysql 连接

发布于 2024-12-14 06:59:22 字数 551 浏览 0 评论 0原文

我想连接到需要 ca-cert 的 MySQL 数据库。我可以使用 MySQLdb 来完成此操作,如下所示:

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?

谢谢, 彼得

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忱杏 2024-12-21 06:59:23

要将 SSL 证书与 SQLAlchemy 和 MySQLdb 一起使用,请使用以下 python 代码:

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)

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)
心在旅行 2024-12-21 06:59:23

SQLAlchemy 中的 create_engine() 有一个 connect_args参数:

connect_args – 将直接传递的选项字典
到 DBAPI 的 connect() 方法作为附加关键字参数。

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.

一梦浮鱼 2024-12-21 06:59:23

SQLObject(未经测试):

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,
)

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,
)
仲春光 2024-12-21 06:59:23

根据他们的文档,SQLAlchemy的create_engine 函数采用以下格式的 db url:dialect[+driver]://user:password@host/dbname[?key=value..] 表示您可以通过ssl key、c​​ert 和 ca 作为键值对。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文