简体   繁体   English

连接到本地 SQL Server 实例

[英]Connecting to local SQL Server instance

I'm attempting to connect to a local instance of SQL Server running on my machine.我正在尝试连接到在我的机器上运行的 SQL Server 的本地实例。 I am able to connect to a local instance with this code from our server, but it fails on my local machine.我能够从我们的服务器使用此代码连接到本地实例,但它在我的本地机器上失败。

I've enabled named pipes and all the ips in the SQL Server configuration.我在 SQL Server 配置中启用了命名管道和所有 ip。

The code I'm using is as follows:我使用的代码如下:

from pymssql import connect
server = r'.\SQLEXPRESS2014' # I've also tried MORGANT-PC\SQLEXPRESS and SQLEXPRESS2014
username = 'MyUserName'
password = 'MyPassword'
master_database_name = 'SuperSecretDatabase'
port = 5000
server_args = {'host': server, 'user': username, 'password': password,
               'database': master_database_name, 'port': port} # I've tried having the first key be both host and server, because pymssql's docs are unclear on the difference.
master_database = connect(**server_args)

If I use the instance name, I get this error:如果我使用实例名称,则会收到此错误:

pymssql.InterfaceError: Connection to the database failed for an unknown reason.

I set the port to 5000 so that I could try connecting to it with我将端口设置为 5000,以便我可以尝试连接到它

server = 127.0.0.1
port = 5000

which fails with the slightly different error message:失败,错误消息略有不同:

pymssql.OperationalError: (20009, b'DB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist\nNet-Lib error during Unknown error (10035)\n')

I've read a bunch of answers here on SO, and most of them seem to indicate it's an issue with FreeTDS, but I'm on Windows 8.1, so I don't have FreeTDS.我在这里阅读了很多关于 SO 的答案,其中大多数似乎表明这是 FreeTDS 的问题,但我使用的是 Windows 8.1,所以我没有 FreeTDS。

I've tried connecting with sqlcmd with the host\\instance name and that works fine.我已经尝试使用主机\\实例名称连接sqlcmd并且工作正常。 It also works in SSMS.它也适用于 SSMS。

I've tried passing .\\SQLEXPRESS2014 to both the host and server parameter in pymssql.connect() and they both fail with the same aforementioned error.我尝试将.\\SQLEXPRESS2014传递给pymssql.connect()hostserver参数, pymssql.connect()它们都因上述相同的错误而失败。

I briefly tried using adodbapi , but I'm getting exactly the same error messages.我曾短暂尝试使用adodbapi ,但收到完全相同的错误消息。

The solution ended up being a combination of things.解决方案最终成为了多种事物的组合。

  1. I needed to disable all IPs other than 127.0.0.1.我需要禁用 127.0.0.1 以外的所有 IP。
  2. I needed to create C:\\freetds.conf with the following text:我需要使用以下文本创建 C:\\freetds.conf:

    [global] port = 1433 tds version = 7.0

  3. I needed to change the account my SQL instance logs in with to LocalSystem.我需要将我的 SQL 实例登录的帐户更改为 LocalSystem。

Yes, 1433 s the default.是的,默认值为 1433。

This works fine for me:这对我来说很好用:

library(RODBC)
dbconnection <- odbcDriverConnect("Driver=ODBC Driver 11 for SQL Server;Server=server_name; Database=db_name;Uid=; Pwd=; trusted_connection=yes")
initdata <- sqlQuery(dbconnection,paste("select * from MyTable;"))
odbcClose(channel)

尝试连接默认 SQL 服务器端口,即 1433,而不是 5000。并检查您是否可以使用 SQL mgmt studio 连接到正确的实例。

I had a similar issue, with the following error:我有一个类似的问题,有以下错误:

_mssql.MSSQLDatabaseException: (18456, b"Login failed for user
'script_svc'.DB-Lib error message 20018, severity 14:\nGeneral SQL Server
error: Check messages from the SQL Server\nDB-Lib error message 20002,
severity 9:\nAdaptive Server connection failed\n")

The user I had established was a local user on the machine.我建立的用户是机器上的本地用户。

The solution for me was putting ".\\" in front of the username and it then recognized it as a local user and allowed the query to work.我的解决方案是将“.\\”放在用户名前面,然后它将其识别为本地用户并允许查询工作。

Your results may vary.. but I thought I would mention it.你的结果可能会有所不同......但我想我会提到它。

Have you tried using pyodbc instead?您是否尝试过使用pyodbc代替?

import pyodbc
cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER=SERVERNAME;DATABASE=testdb;UID=me;PWD=pass')
cursor = cnxn.cursor()
cursor.execute("select user_id, user_name from users")
rows = cursor.fetchall()
for row in rows:
    print row.user_id, row.user_name

Don't forget to add the ODBC driver to your Windows.不要忘记将 ODBC 驱动程序添加到您的 Windows。 Go to: Control Panel > Systems and Security > Administrative Tools > ODBC Data Sources转到:控制面板 > 系统和安全 > 管理工具 > ODBC 数据源

Either the 32-bit or 64-bit version depending on your computer. 32 位或 64 位版本取决于您的计算机。

Then you click on the System DNS file.然后单击系统 DNS 文件。 If you do not see any MySQL driver you have to click ADD.如果您没有看到任何 MySQL 驱动程序,您必须单击添加。 It brings up a list, from that list select the MySQL driver.它会弹出一个列表,从该列表中选择 MySQL 驱动程序。

For me, it was ODBC Driver 13 for SQL Server .对我来说,它是ODBC Driver 13 for SQL Server Click finish.单击完成。 Once you do that then you have to change your connection line in your code to the corresponding Driver that you just filled out.完成此操作后,您必须将代码中的连接线更改为刚刚填写的相应驱动程序。

Source: pyodbc + MySQL + Windows: Data source name not found and no default driver specified来源: pyodbc + MySQL + Windows:未找到数据源名称且未指定默认驱动程序

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

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