简体   繁体   English

Sqlalchemy + pymssql 连接失败

[英]Sqlalchemy + pymssql connection fails

I have freetds.conf with dbserver connection to database server (which I am routinely using for PHP) , the server is on separate server on our own network.我有freetds.conf与数据库服务器的 dbserver 连接(我经常用于 PHP) ,该服务器位于我们自己网络上的单独服务器上。

Just to test the connection, I have this small program:只是为了测试连接,我有这个小程序:

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

engine = create_engine(r"mssql+pymssql://{0}:{1}@dbserver/db_database?charset=utf8".format('user','p@ssw0rd'))

def main():
    print("In main")
    connection=engine.connect()
    print("Connected")

if __name__ == "__main__":
    main()

The program prints in main, but never gets to connect.该程序在 main 中打印,但永远无法连接。 How can I debug this kind of problem and what is causing this?我该如何调试此类问题以及导致此问题的原因?

I have no problems connecting with PHP, so there are no network restrictions and I am running this with Python 3.6.我用 PHP 连接没有问题,所以没有网络限制,我用 Python 3.6 运行它。

I found the error after all myself. 我毕竟找到了错误。 The problem was that there is an instance on SQL server I am using. 问题是我正在使用的SQL服务器上有一个实例。 Though in freetds.conf there is the instance port mentioned, I also had to add it to the create_engine call. 虽然在freetds.conf中提到了实例端口,但我还必须将它添加到create_engine调用中。

The working version of create_engine is so: create_engine的工作版本是这样的:

engine = create_engine(r"mssql+pymssql://{0}:{1}@dbserver:12345/db_database?charset=utf8".format('user','p@ssw0rd'))

where 12345 is the port number the instance is listening to. 其中12345是实例正在侦听的端口号。

I am sorry that missed that one crucial information bit, but then again, if I had realized it, I probably wouldn't have even asked - and then somebody else might later fight with the same problem. 我很遗憾错过了一个重要的信息位,但话又说回来,如果我意识到这一点,我可能甚至不会问 - 然后其他人可能会在以后遇到同样的问题。

hank 汉克

I encounted the same error when connect to sqlserver2008 
used sqlalchemy and pymssql but if i connect to the same sever with pymssql only ,it work fine!

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
import pymssql

# Password connection
engine = create_engine('mssql+pymssql://rsmonitor:xxx#123@172.16.100.209/rsmonitor')

def main():
    print("In main")
    # this fail 
    connection=engine.connect()
    
    # this fine
    #conn = pymssql.connect('172.16.100.209', 'rsmonitor', 'xxx#123', 'rsmonitor')
    print("Connected")

if __name__ == "__main__":
    main()

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

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