Unable to connect to SQL server using PYMSSQL - python

I encounter this problem when trying to connect to my SQL database:
OperationalError: (20009, b'DB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist (LAPTOP-B3TE2LKC)\nDB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist (LAPTOP-B3TE2LKC)\n')
I am able to connect to my database with the very same credentials using PYODBC, so the access variables should be fine.
Does anyone know how to solve this error message? - I am using Python version 3.8.8 and PYMSSQL version 2.2.2.
This is my code, including the credentials I used to connect to this test database:
import pymssql
print(pymssql.__version__)
# Database credentials and variables
server = 'LAPTOP-B3TE2LKC\TESTEXPRESS'
user = 'FTP-user'
password = '1111'
database = 'TestA'
# Connect to SQL database
database_connection = pymssql.connect(server, user, password, database)
Any help is highly appreciated.

Related

Python vs Airflow pymssql

When I try to connect to a database using Airflow I get the error
'pymssql.OperationalError: (20009, b'DB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist'
What is strange I can connect using the exact same code in jupyter/python.
For reference this is a sample code of how I connect.
conn =
pymssql.connect(
server='server',
user='python_script',
password='password',
database='test_data',)
Any help if appreciated.

I have a connexion problem to sql server using sqlalchemy wih python

I want to connect into a sql server database using sqlalchemy and python.
but when I run the code below I get the following error
OperationalError: (pyodbc.OperationalError) ('08001', '[08001]
[Microsoft][SQL Server Native Client 11.0]SQL Server Network
Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF].
(-1) (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. (-1)') (Background on this error at:
http://sqlalche.me/e/e3q8)
I think the problem has something to do with the driver, but I can't seem to understand it.
here is my code:
from sqlalchemy.sql import text
from sqlalchemy import create_engine
import secrets
engine = create_engine( 'mssql+pyodbc://servername/test_db? driver=SQL+Server+Native+Client+11.0')
conn = engine.connect()
s = text("SELECT * FROM user_tab ")
result = conn.execute(s).fetchall()
print(result)
Can you help me please!
You're connecting to a DB without a user, password, hostname or port.
When you look at the example in the documentation this is written:
# pymssql
engine = create_engine('mssql+pymssql://scott:tiger#hostname:port/dbname')
As you can see, this url contains way more information than you have in your url.
You just got an url with a name and a dbname. I don't know what the first name represents..
Most of the time the hostname will be localhost and the port 1434. But make sure where your mssqlserver is running and which user is allowed to make queries on the db!
Documentation: https://docs.sqlalchemy.org/en/13/core/engines.html

OperationalError when trying to connect to a remote PostgreSQL server in Python

I have a task that asks:
You have two abstract reports made available to you on a PostgreSQL server located at: postgres://candidate.company.org/company
username = candidate password = abc
So I used this code to try connect to the database:
import psycopg2 as db
conn = db.connect(host='postgres://candidate.suade.org/company', database='randomname', user='candidate', password='abc', port='5432')
I received the following error message:
OperationalError: could not translate host name "postgres://candidate.suade.org/suade" to address: Unknown host
My experience of connecting to databases is limited. Can anyone see what I am doing wrong here?

Connecting to local SQL Server instance

I'm attempting to connect to a local instance of SQL Server running on my machine. 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.
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
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.
I've tried connecting with sqlcmd with the host\instance name and that works fine. It also works in 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.
I briefly tried using adodbapi, but I'm getting exactly the same error messages.
The solution ended up being a combination of things.
I needed to disable all IPs other than 127.0.0.1.
I needed to create C:\freetds.conf with the following text:
[global]
port = 1433
tds version = 7.0
I needed to change the account my SQL instance logs in with to LocalSystem.
Yes, 1433 s the default.
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)
Try connecting on the default SQL server port, which is 1433, not 5000.
And check that you can connect to the correct instance using 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?
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. Go to: Control Panel > Systems and Security > Administrative Tools > ODBC Data Sources
Either the 32-bit or 64-bit version depending on your computer.
Then you click on the System DNS file. If you do not see any MySQL driver you have to click ADD. It brings up a list, from that list select the MySQL driver.
For me, it was 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

How to connect to remote mysql server in python

Hi i have a requirement where i need to connect to remote mysql server. My application shall be running on local machine and my mysql will be running on remote server.I have tried the following code:
DB = 'gts'
DB_HOST = 'ps95074.dreamhost.com'
DB_USER = 'root'
DB_PASSWORD = 'dbadminpassword'
conn = MySQLdb.Connection(db=DB, host=DB_HOST, user=DB_USER,passwd=DB_PASSWORD)
cursor = conn.cursor()
But i am getting the following error
OperationalError: (2005, "Unknown MySQL server host 'ps95074.dreamhost.com' (1)")
Instead if i use
DB_HOST='localhost'
Everything works fine. How can same be possible with remote host.Any help shall be appreciated.
Check your firewall. That server is online and available from any machines:
> mysql -h ps95074.dreamhost.com
ERROR 1045 (28000): Access denied for user 'myuser'#'myhost' (using password: NO)
However, even if you can connect chances are good that your database user only allows local connections.
Update: I just tried it again and now it also fails using the commandline client. So clearly something is wrong with your server.

Categories

Resources