Cannot establish connection to sql-server using pyodbc on Windows 10 - python

Getting this error when trying to run development server on django.
django.db.utils.Error: ('08001', '[08001] [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [53]. (53) (SQLDriverConnect)')
Here is my settings.py databases
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'mydata',
'USER': 'user#mydata',
'PASSWORD': '**password**',
'HOST': 'mydata.database.windows.net',
'PORT':'**port**',
'OPTIONS': {
'driver': 'SQL Server',
'host_is_server': True,
'MultipleActiveResultSets': False,
'Encrypt': True,
'TrustServerCertificate': False,
'Connection Timeout': 30,
'Persist Security Info': False,
},
},
}
I have django-pyodbc and django-pyodbc-azure installed.
Using django version 1.11.
Any help would be awesome! Thank you.
EDIT 1
I changed driver to SQL Sever and updated the driver. Now I am getting this error.
django.db.utils.Error: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (17) (SQLDriverConnect)')
I have the database set up on Azure.

According to your error information, there are two possible reason which caused the issue, as below.
The driver option value should be ODBC Driver 13 for SQL Server as the figure below from Azure portal. Please change it and try again.
The access denied error may be caused by not adding your client IP to the firewall of Azure SQL Database to allow access, as the figure below.
Hope it helps.

Related

Django deploy error on Heroku App "connection to server at "127.0.0.1", port 5432 failed: Connection refused"

I am trying to re-deploy my Python Django App on Heroku after the free dyno canceled.
However, I got OperationalError at /catalog/ in Heroku:
connection to server at "127.0.0.1", port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
Exception Location: /app/.heroku/python/lib/python3.10/site-packages/psycopg2/__init__.py, line 122, in connect
I ran the App locally without any issue, and I used this as a reference but not solving my problem. I tried several changes in my settings.py:
ALLOWED_HOSTS = ['.herokuapp.com','127.0.0.1']
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'somename',
'USER': 'someuser',
'PASSWORD': 'somepassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
I personally believe the error was caused by those code above, and I had changed PORT to "8000", or used DATABASES['default'] = dj_database_url.config(), or set ENGINE to 'django.db.backends.postgresql', but those attempts were not working neither.
Since it rans without error locally, so I am wondering what caused the Operational Error appeared?
I would sincerely appreciate all the help since this issue bothered me for few days.
Edit: After multiple tryouts I realized the error was probably caused by dj_database_url, but the error was not solved yet. Please let me know how to set my DATABASES_URL correctly.

Unable to establish connection to sql-server using pyodbc on Windows 10

import pyodbc as sql
import pandas as pd
source_db_config = {
'Trusted_Connection': 'yes',
'driver': '{SQL Server}',
'server': '.',
'database': 'AdventureWorksDW2017',
'autocommit': True,
}
source_connect = sql.connect(**source_db_config)
cannot connect with sql server. It's showing OperationalError:
OperationalError: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][Shared Memory]SQL Server does not exist or access denied. (17) (SQLDriverConnect); [08001] [Microsoft][ODBC SQL Server Driver][Shared Memory]ConnectionOpen (Connect()). (2)')
How can I solve this?
Python required additional driver to be install to get connect with Databases.
In your case download python driver for sql server
https://learn.microsoft.com/en-us/sql/connect/python/python-driver-for-sql-server

Django database connection with Host and Instances with SQL Server

I'm using SQL Server as the backend for the database. In the setting.py file, I need to use the host name with instance. Due to this, I got the error given below:
The above exception (('08001', '[08001] [Microsoft][SQL Server Native Client 11.0]TCP Provider: No connection could be made because the target machine actively refused it.\r\n (10061) (SQLDriverConnect); [08001] [Microsoft][SQL Server Native Client 11.0]Login timeout expired (0); [08001] [Microsoft][SQL Server Native Client 11.0]Invalid connection string attribute (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. (10061)')) was the direct cause of the following exception:
# "settings.py"
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'xxxxx',
'USER': 'xxx',
'PASSWORD': 'xxxx',
'PORT': '1433',
'HOST': 'aaa\bbb',(hostname\instance)
'OPTIONS': {
'driver': 'SQL Server Native Client 11.0',
},
}
}
How could I resolve this error and connect with my database?
Your post is near two years ago, but I faced the same issue now.
By looking on https://github.com/michiya/django-pyodbc-azure/issues/201
It seems that the problem comes from the way the port is included in the connection string. The issue will be solved when the port is specified in this way:
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'xxxxx',
'USER': 'xxx',
'PASSWORD': 'xxxx',
'HOST': 'aaa\bbb',(hostname\instance)
'OPTIONS': {
'driver': 'SQL Server Native Client 11.0',
},
'extra_params': 'PORT=1433'
I tried it, it works fine.
Since your settings.py looks fine,I think its a general issue that can be caused from multiple wrong configurations on the database side, such as:
TCP/IP Connections are disabled in the SQL Server Configuration Management
In TCP/IP properties in the "IP ALL" section port 1433 could not be configured, so it refuses to your requests
One of many SQL windows services suddenly could be stopped
I found this stack overflow post very useful with multiple answers that covers each of the points above.
You can use the latest package mssql-django to connect Django to MSSQL(SQL Server) with SQL Server Authentication. *I use SQL Server 2019 Express.
So, try this code below. *"ENGINE" must be "mssql" and keep it blank for "PORT" because there will be error if setting any port number e.g. "2244", "9877" or even "1433" which is the default port number of MSSQL:
# "settings.py"
DATABASES = {
'default': {
'ENGINE': 'mssql', # Must be "mssql"
'NAME': 'xxxxx',
'USER': 'xxx',
'PASSWORD': 'xxxx',
'HOST': 'aaa\bbb',(hostname\instance)
'PORT': '', # Keep it empty
# 'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
},
},
}

How to fix 'Named Pipes Provider: Could not open a connection to SQL Server [53]'?

I can't connect MSsql database to django.
When I trying to do with python, It works well, but when I try to run server on Django I get error.
Pyodbc version : 4.0.27;
Django version : 2.1.13;
Microsoft SQL Server 2017 (RTM) - 14.0.1000.169 (X64).
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': '<djangoBanks>',
'HOST': '<DESKTOP-0FKB14V>',
'USER': '<test>',
'PASSWORD': '<12345>',
'OPTIONS': {
'driver': "ODBC Driver 17 for SQL Server",
'Trusted_Connection' : 'Yes',
}
}
}
When I try to run python manage.py runserver I get this error:
'08001', '[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]Invalid connection string attribute (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)'
I edited 'Options' and then it started work.
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'djangoBanks',
'HOST': 'DESKTOP-0FKB14V',
'USER': 'test',
'PASSWORD': '12345',
'OPTIONS': {
'driver': "ODBC Driver 17 for SQL Server",
'SERVER' : 'DESKTOP-0FKB14V',
'DATABASE' : 'djangoBanks',
'UID' : 'test',
'PWD' : '12345',
'Trusted_Connection' : 'Yes',
}
}
}

python connection with SQL server

I used the pyodbc and pypyodbc python package to connect to SQL server.
Drivers used anyone of these ['SQL Server', 'SQL Server Native Client 10.0', 'ODBC Driver 11 for SQL Server', 'ODBC Driver 13 for SQL Server'].
connection string :
connection = pyodbc.connect('DRIVER={SQL Server};'
'Server=aaa.database.windows.net;'
'DATABASE=DB_NAME;'
'UID=User_name;'
'PWD=password')
now I am getting error message like
DatabaseError: (u'28000', u"[28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user
But I can connect to the server through the SQL server management studio.
its on SQL Server Authentication, Not Windows Authentication.
Is this about python package and driver issue or DB issue??? how to solve?
You can add Trusted_Connection=NO; in your connection string after the password
I see you have no port defined in your script.
The general way to connect to a server using pyodbc ${DBName} ${DBUser} ${DBPass} ${DBHost} ${DBPort}, where DBName is your database name, DBUser is the username used to connect to it, DBPass is the password, DBHost is the URL of your database, and DBPort is the port you use to connect to your DB.
I use MS SQL so my port is 1433, yours might be different.
I've had this issue just today, and that fixed it.
The problem is not driver issue, you can see the error message is DatabaseError: Login failed for user, it means this problem occurs if the user tries to log in with credentials that cannot be validated. I suspect you are login with your windows Authentication, if so, use Trusted_Connection=yes instead:
connection = pyodbc.connect('DRIVER={SQL Server};Server=aaa.database.windows.net;DATABASE=DB_NAME;Trusted_Connection=yes')
For more details, please refer to my old answer about the difference of SQL Server Authentication modes.
I think problem because of driver definition in your connection string. You may try with below.
connection = pyodbc.connect('DRIVER={SQL Server Native Client 10.0}; Server=aaa.database.windows.net; DATABASE=DB_NAME; UID=User_name; PWD=password')
I applied your connection string and updated it with my server connections details and it worked fine.
Are you sure your are passing correct user name and password ?
Login failed implies connection was established successfully, but authentication didn't pass.

Categories