如何为 SSL 配置 PyMySQL 连接? [英] How do I configure PyMySQL connect for SSL?

查看:98
本文介绍了如何为 SSL 配置 PyMySQL 连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 SSL 和 PyMySQL 连接我的数据库,但我找不到关于语法是什么的好的文档.

I'm trying to connect my database using SSL with PyMySQL, but I can't find good documentation on what the syntax is.

这些凭据可在 Workbench 和 CLI 中使用,但在使用 PyMySQL 时出现此错误.

These credentials work in Workbench and with the CLI, but I get this error when using PyMySQL.

无法连接到server.domain.com"上的 MySQL 服务器([WinError 10061] 无法建立连接,因为目标机器主动拒绝了它)")

Can't connect to MySQL server on 'server.domain.com' ([WinError 10061] No connection could be made because the target machine actively refused it)")

db_conn = pymysql.connect(
    host=db_creds['host'],
    user=db_creds['user'],
    passwd=db_creds['passwd'],
    db=db_creds['db'],
    charset=db_creds['charset'],
    ssl={'ssl':{'ca': 'C:/SSL_CERTS/ca-cert.pem',
                'key' : 'C:/SSL_CERTS/client-key.pem',
                'cert' : 'C:/SSL_CERTS/client-cert.pem'
                }
        }
)

如果我关闭 SSL 并删除 SSL 参数,我可以连接不安全就好了.我的 SSL 参数有什么问题?

If I shut SSL off and drop the SSL parameter, I can connect unsecured just fine. What am I doing wrong with the SSL parameter?

PyMySQL 现在希望像这样列出 ssl 参数,而不是在字典中.

PyMySQL now wants ssl parameters listed like this instead of in a dict.

db_conn = pymysql.connect(
     host=db_creds['host'],
     user=db_creds['user'],
     passwd=db_creds['passwd'],
     db=db_creds['db'],
     charset=db_creds['charset'],
     ssl_ca='C:/SSL_CERTS/ca-cert.pem',
     ssl_key='C:/SSL_CERTS/client-key.pem',
     ssl_cert='C:/SSL_CERTS/client-cert.pem'
                          )

推荐答案

感谢大家的帮助.问题中列出的语法是正确的,但我尝试连接的服务器使用的是非标准端口.我需要添加

Thanks for the help everyone. The syntax listed in the question is right, but the server I was attempting a connection to was using a non-standard port. I needed to add

port = db_creds['port']

port = db_creds['port']

谢谢曼尼凯瑞提供线索.

Thanks, MannyKary, for the clue.

这篇关于如何为 SSL 配置 PyMySQL 连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆