pyodbc doesn't seem to be able to connect when trying to connect through specifying Driver. I am able to connect to setting up a DSN but I also want to make connection when user has got the Driver, Server, UID, PWD and Database details.
I am on Mac and and using FreeTDS driver.
freetds.conf
[MYMSSQL]
host = 0.0.0.0
port = 1433
tds version = 7.3
odbcinst.ini
[FreeTDS]
Description=FreeTDS Driver for Linux & MSSQL
Driver=/usr/local/lib/libtdsodbc.so
Setup=/usr/local/lib/libtdsodbc.so
UsageCount=10
Here is how I am trying to connect:
conn_str = "DRIVER=FreeTDS;SERVER={0};UID={1};PWD={2};DATABASE={3}".format('MYMSSQL', 'sa', 'password','tempdb')
conn = pyodbc.connect(conn_str)
The error I get is this:
pyodbc.OperationalError: ('08001', '[08001] [FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')
Exact same database details work when I try to connect through DSN.
If you have a freetds.conf file containing the host name/IP and port, e.g.,
gord#xubuntu64-nbk1:~$ cat /etc/freetds/freetds.conf
[myFreeTdsHost]
host = 192.168.0.179
port = 49242
then you can use both of those values in your DSN-less connection string by simply specifying the SERVERNAME=
# get host name/IP and port from freetds.conf
cnxn_str = (
'DRIVER=FreeTDS_ODBC;'
'SERVERNAME=myFreeTdsHost;'
'DATABASE=myDb;'
'UID=sa;PWD=_whatever_;'
)
You can also supply the host name/IP and port directly via SERVER= and PORT= like so
# supply host name/IP and port directly (bypassing freetds.conf)
cnxn_str = (
'DRIVER=FreeTDS_ODBC;'
'SERVER=192.168.0.179;'
'PORT=49242;'
'DATABASE=myDb;'
'UID=sa;PWD=_whatever_;'
)
For details, see the FreeTDS documentation here.
Related
I am trying to access tables in SQL database in Azure Managed Instance (with IP: xxxx.database.windows.net) from a python script in Azure VM machine but I am getting the Operational Error below. I have tried with 2 different ways below.
Error:
OperationalError: ('08001', '[08001] [Microsoft][SQL Server Native Client 11.0]TCP Provider: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.\r\n (10060) (SQLDriverConnect); [08001] [Microsoft][SQL Server Native Client 11.0]Login timeout expired (0); [08001] [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (10060)')
1st way with connectionString:
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine("mssql+pyodbc://<username>:<password>#<server>/<database>?driver=SQL+Server+Native+Client+11.0")
query = "select * from table"
df=pd.read_sql(query,engine)
2nd way with connectionString:
import pyodbc
server = 'xxx.database.windows.net'
database = 'database'
username = 'username'
password = 'password'
driver= '{SQL Server Native Client 11.0}'
with pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password) as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT TOP 3 name, collation_name FROM sys.databases")
row = cursor.fetchone()
while row:
print (str(row[0]) + " " + str(row[1]))
row = cursor.fetchone()
Besides, I have also tried to change the driver to drivers below, still no luck.
{ODBC Driver 11 for SQL Server}
{ODBC Driver 13 for SQL Server}
{ODBC Driver 17 for SQL Server}
{SQL Server Native Client 11.0}
Interesting part is, if I try the connect with the same connection string from on-premise machine which is not Azure VM (ex: my local machine or other servers I can RDP to), I can access the database. But when I try on a Azure VM machine, it is timing out. Do you have any ideas how to fix this problem?
Thank you for inputs.
So in the end we foud out that firewall caused this problem. We need to check firewall rules first.
I have a database server which can be accessed by Remote desktop login to the server machine. This is what we do manually:
Login with Remote desktop to the machine from local.
Open Database client in the connected machine.
Then connect to database.
Now, I need to connect to this DB using python.
What I already tried?.. below works for all DB that I connect without remote.
conn = pyodbc.connect("DRIVER={ODBC Driver 17 for SQL Server};SERVER=<server name>;database=<DB name>;UID=<user>;PWD=<pwd>")
Error:
pyodbc.OperationalError: ('08001', u'[08001] [Microsoft][ODBC Driver
17 for SQL Server]Named Pipes Provider: Could not open a connection to
SQL Server [53]. (53) (SQLDriverConnect); [08001] [Microsoft][ODBC
Driver 17 for SQL Server]Login timeout expired (0); [08001]
[Microsoft][ODBC Driver 17 for SQL Server]A network-related or
instance-specific error has occurred while establishing a connection
to SQL Server. Server is not found or not accessible. Check if
instance name is correct and if SQL Server is configured to allow
remote connections. For more information see SQL Server Books Online.
(53)')
before you use below code in python, you have to follow this guide to configure your SQL server https://knowledgebase.apexsql.com/configure-remote-access-connect-remote-sql-server-instance-apexsql-tools/
note: 1434 is my UDP port in Inbound Rules
conn = pyodbc.connect('DRIVER={SQL Server};SERVER = your_server_ipv4,1434;DATABASE=B_SQL;UID=sa;PWD=123456;')
cursor = conn.cursor()
#cursor.execute("DELETE FROM my_table")
for index, row in df.iterrows():
#print(row)
cursor.execute("INSERT INTO my_table([Name],[Volume]) values(?,?)", row['Name'], row['Volume'])
conn.commit()
cursor.close()
conn.close()
it works very well for me!
Can you connect to your SQL Server from another application, including Excel?
If you cannot, I would check the following:
Remote into the server and open the SQL Server Configuration Manager.
There should be a section labeled SQL Server Network Configuration that will have an entry for "Protocols for ". If you click on that entry, you will see which protocols are enabled for your database.
Click on TCP/IP and select properties. Under the IP addresses, each IP listed may need to have a port listed.
Once that is done, make sure that port is enabled in your firewall on the server for both Inbound and Outbound.
When I managed a SQL-driven application, we normally got the Named Pipes error when there was other connection issues such as firewall issues or the SQL Server Browser or instance not running.
If this is not sufficient to resolve the issue, there are a ton of other options on MSSQL Tips.
I have tried solutions found on other SO questions but none of them have worked for me. I am attempting to pull data from a mysql db running on a remote server by setting up an ssh tunnel. My code is as follows:
server = sshtunnel.SSHTunnelForwarder(
('10.6.41.10', 22),
ssh_username= 'serveruser',
ssh_password= 'serverpw',
remote_bind_address=('127.0.0.1', 3306))
server.start()
print(server.local_bind_port)
cnx = mysql.connector.connect(user='root', password='mysqlpw',
host='127.0.0.1',
database='mydb',
charset='utf8',
use_unicode='FALSE',
port = 3306)
However, when I run this code I receive:
1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
I have also tried adding
local_bind_address = ('0.0.0.0', 3306)
to the sshtunnel setup and instead recieved
Problem setting SSH Forwarder up: Couldn't open tunnel 0.0.0.0:3306 <> 127.0.0.1:3306 might be in use or destination not reachable
I don't fully understand the remote_bind_address and local_bind_address, so my guess is that must be doing something wrong there. I know my username/pw/server info is correct, I am able to ssh into my server via terminal and then use
mysql -h 127.0.0.1 -u root -p
to successfully log into my mysql server. So what do I need to fix to get it running in python? Thanks.
If you don't specify local_bind_address in sshtunnel.SSHTunnelForwarder, the local port is allocated randomly. In that case set port=server.local_bind_port in mysql.connector.connect().
Instead, you can also set local_bind_address=('0.0.0.0', [some port which is not in use]) in sshtunnel.SSHTunnelForwarder. The sshtunnel.HandlerSSHTunnelForwarderError ("Problem setting...") tells you that you can't use local_bind_address=('0.0.0.0', 3306).
I am trying to use python to get a connection to MySQL,
It is python code is :
import MySQLdb
conn= MySQLdb.connect(
host='public ip',
port = 3306,
user='root',
passwd='123456',
db ='test_schema',
)
so it always give the errors:
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'pubilic ip' (10060)")
Building off AK47's answer
You need to first find the public facing ip address of your machine.
Go to google and search What's my ip address you should get a set of numbers xx.xx.xxxx.xx for example 12.42.111.2
Then in your mysql you need to modify the conf file if linux, ini file if windows
/etc/mysql/my.cnf Unix/OSX systems.
C:\Program Files\MySQL\MySQL Server 5.5\ Windows system
change bind-address to your ip address from google.
Then in AK47's answer replace host='127.0.0.1' to host=<ip from google>
Update your connection parameters to have an actual IP address in the 'host' field
import MySQLdb
conn= MySQLdb.connect(
host='127.0.0.1',
port = 3306,
user='root',
passwd='123456',
db ='test_schema')
I have a MS SQL database in the same network but in other computer.
Using the SQL Server Management Studio (SSMS) Express, I can find the database and connect without problems.
But when I use pyodbc to connect to the same server using:
import pyodbc
server = r"xxxER\xxxSQLSERV"
db = "xxxDB"
user = "xxx"
password = "xxxx"
conn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server + ';DATABASE=' + db +';UID=' + user + ';PWD=' + password)
I get following error:
pyodbc.OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC SQL Server Driver]Login timeout expired (0) (SQLDriverConnect)')
OBS: I guess that the server string should be right, since if I change it I get always the following error:
pyodbc.Error: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (17) (SQLDriverConnect); [01000] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (53)')
Here the image showing success while using SQL Server Studio Express to connect remotely.
Try specifying the port:
import pyodbc
server = r"xxxER\xxxSQLSERV"
db = "xxxDB"
user = "xxx"
password = "xxxx"
port = "1433"
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=' + server + ';PORT=' + port + ';DATABASE=' + db +';UID=' + user + ';PWD=' + password)
If you're still having issues, try using the IP or FQDN of the server.
"But why ...?"
For those interested in why SQL Server Management Studio (SSMS) can connect to servername\instance while other applications (like our pyodbc apps) cannot, it's because SSMS keeps an MRU (Most Recently Used) list of port numbers in the Windows registry at
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Client\SuperSocketNetLib\LastConnect
HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\MSSQLServer\Client\SuperSocketNetLib\LastConnect
Each MRU entry (registry value) looks something like this:
Name: PANORAMA\SQLEXPRESS
Type: REG_SZ
Data: -1006030326:tcp:PANORAMA,52865
Once SSMS has successfully connected by instance name via the SQL Browser service on the remote machine, it can continue to connect by instance name even if the SQL Browser is no longer running on the remote machine, provided that the port number has not changed. Apps that don't use this MRU list (like our pyodbc app) need to have the SQL Browser service running on the remote machine every time they want to connect by instance name.
The most common scenario:
I want to connect to YOUR-PC\SQLEXPRESS. I try doing that from SSMS on MY-PC, but it doesn't work because the SQL Browser was installed with "Start Mode" set to "Manual" on YOUR-PC.
I ask you to start the SQL Browser service on YOUR-PC, and you kindly comply, but you just start the service and forget to change the "Start Mode" setting to "Automatic".
I am able to connect via SSMS (which caches the YOUR-PC\SQLEXPRESS port in the MRU). My python app can connect, too.
After the next time YOUR-PC restarts, I can connect via SSMS (via the MRU) but my python app cannot (because the SQL Browser service is no longer running on YOUR-PC).
Try changing the Driver from 'SQL Server' to 'SQL Server Native Client 11.0'.
I had the same error message and this fixed it for me.
I have this problem.I can connect with Management Studio (SSMS) but not work with pyodbc.
I add version odbc of sql and worked.
change your code to:
conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server + ';DATABASE=' + db +';UID=' + user + ';PWD=' + password)
If not work change version 17 to 13 if not to 11 .
List versions of ODBC.