简体   繁体   English

使用 Pyodbc 连接到 SQL 服务器,无需密码字符串和证书

[英]Connect to SQL server using Pyodbc with out password string and a certificate

I am able to connect to the SQL server using the 'TrustedConnection = Yes' in the Pyodbc.connect() But I dont want to use neither the windows credentials nor provide password literal string in the connection()我可以使用 Pyodbc.connect() 中的“TrustedConnection = Yes”连接到 SQL 服务器,但我既不想使用 Windows 凭据,也不想在 connection() 中提供密码文字字符串

I am thinking whether i can connect to the sql server using only certificate with out the concept of password in the connection string我在想我是否可以仅使用证书连接到 sql 服务器,而连接字符串中没有密码的概念

At present i am using目前我正在使用

        pyodbc.connect(
            'DRIVER={ODBC Driver 17 for SQL Server};'
            'SERVER= ' + serverName + ';'
            'DATABASE=' + dbName + ';'
            'Trusted_Connection=yes;')

I want to use certificate and not use password in connection string我想使用证书而不是在连接字符串中使用密码

cnx = pyodbc.connect(user="{username@servername}", 
                             ❌password="{password}"❌,  # thinking if we can remove this
                             host="{server_name}",
                             database="{database_name}", 
                            ssl_ca="/home/sachin//root.crt",
                             ssl_verify_cert=True)

To quote from the documentation Authentication in SQL Server :引用SQL Server 中的 Authentication文档:

Authentication in SQL Server SQL Server 中的身份验证

SQL Server supports two authentication modes, Windows authentication mode and mixed mode. SQL Server 支持两种身份验证模式,Windows 身份验证模式和混合模式。

  • Windows authentication is the default, and is often referred to as integrated security because this SQL Server security model is tightly integrated with Windows. Windows 身份验证是默认设置,通常称为集成安全性,因为此 SQL Server 安全模型与 Windows 紧密集成。 Specific Windows user and group accounts are trusted to log in to SQL Server.信任特定的 Windows 用户和组帐户登录 SQL Server。 Windows users who have already been authenticated do not have to present additional credentials.已经通过身份验证的 Windows 用户不必提供额外的凭据。

  • Mixed mode supports authentication both by Windows and by SQL Server.混合模式支持通过 Windows 和 SQL Server 进行身份验证。 User name and password pairs are maintained within SQL Server.用户名和密码对在 SQL Server 中维护。

Those are you're only 2 options.那些是你只有2个选择。 If you then read further on:如果您然后进一步阅读:

Mixed Mode Authentication混合模式认证

If you must use mixed mode authentication, you must create SQL Server logins, which are stored in SQL Server.如果必须使用混合模式身份验证,则必须创建存储在 SQL Server 中的 SQL Server 登录名。 You then have to supply the SQL Server user name and password at run time.然后您必须在运行时提供 SQL Server 用户名和密码。

Note that, again, it explicitly states password , nothing else.请注意,再次明确指出password ,没有别的。 You can't use something else instead of a password for the connection string.您不能使用其他东西来代替连接字符串的密码。 You either need to use a trusted connection, and then the authentication is handled via the domain, or you use SQL Authentication and use a Username and Password.您要么需要使用受信任的连接,然后通过域处理身份验证,要么使用 SQL 身份验证并使用用户名和密码。

If you don't want to use these Authentication Methods, then SQL Server is not the right RDBMS choice for you, and you would need to try and find an alternative RDBMS that supports a authentication method you want to use.如果您不想使用这些身份验证方法,那么 SQL Server 不是您的正确 RDBMS 选择,您需要尝试寻找支持您要使用的身份验证方法的替代 RDBMS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM