I have an Airflow server and want to declare a connection to my local SQL Server database. These is the code I used with pymssql:
conn = pymssql.connect(
host='localhost',
server="localhost\SQLEXPRESS",
port=1433,
user="sa",
password="demo",
database="Test")
This works, but what should be the settings in the connection in Airflow?Connection screen
The current settings gives me a 20009 error.
Related
I have configured a new SQL Server 2017.XXX instance in my Windows 10. Then, created a new database called CUSTOMER_DEVICES. I tried to connect to database using pyodbc. I could not and got below error:
Error - pyodbc.OperationalError: ('08001', u'[08001] [Microsoft][ODBC SQL Server Driver]
When I use the same code in existing production server it works. Code below
import pyodbc
conn = pyodbc.connect(r'Driver={SQL Server};Server=localhost;Database=CUSTOMER_DEVICES;Trusted_Connection=yes;')
cursor = conn.cursor()
print(cursor)
Check if SQL server Configuration Management is configured. Enable TCP, IP. Check below link Check here
I am trying to connect to my SQL Server 2016 Database using pyodbc with django.
In the SQL configuration manager i have all the network configurations as Enabled
shared memory
Named Pipes
TCP/IP
FireWall is turned OFF
I tried using localhost and it worked fine but when I tried to connect to a server on the same network it did not work and displayed the below error:
OperationalError at /connect/
('08001', '[08001] [Microsoft][SQL
Server Native Client 11.0]Named Pipes Provider: Could not open a
connection to SQL Server [53]. (53) (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.
(53)')
I tried to check the ODBC Driver on the server and it displays these drivers:
ODBC Driver 13 for SQL Server
SQL Server
SQL Server Native Client 11.0
and I tried both of them but no success.
views.py
from django.shortcuts import render
import pyodbc
# from .models import Artist
# Create your views here.
def connect(request):
conn = pyodbc.connect('Driver={ODBC Driver for SQL Server};'
'Server=AB-INT-SQL;'
'Database=testDB;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
c = cursor.execute('SELECT * FROM Artist')
return render (request,'connect.html',{"c":c})
connect.html
{% for row in c %}
{{ row }}
{% endfor %}
If there is only 1 SQL instance installed on the server, just do quick check with telnet HOSTNAME 1433 to confirm SQL Server accepting the communication on default port number.
if telnet doesn't work, add new rule in fire-wall (via Firewall advanced settings) with port numbers 1433 and 1434 even though have turn-off the firewall. Still doesn't work, restart SQL Service and then have a look at SQL error log for a message (following message must appear after restart time)
Server is listening on [ 'any' ipv4 1433].
Aside from this (once telnet test works), i believe, you need use the driver in connection string as "Driver={SQL Server Native Client 11.0};" which you may have already tried.
Edit: SQL Error log screenshot
I'm receiving an error when I try to connect to an Azure SQL server using pyodbc.
I found the connection parameters under 'Connection strings' for ODBC in the Azure portal. I have added my IP address in the firewall settings (and waited >1 hour) but this did not resolve the problem.
import pyodbc
DRIVER = '{SQL Server}'
SERVER = 'tcp:[server name].database.windows.net'
PORT = '1433'
DATABASE = [database name]
USERNAME = [username]
PASSWORD = [password]
CONNECTION_STRING = f'DRIVER={DRIVER};PORT={PORT};SERVER={SERVER};DATABASE={DATABASE};UID={USERNAME};PWD={PASSWORD}'
cursor = pyodbc.connect(CONNECTION_STRING).cursor()
I get the following error:
ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]
Cannot open server [server name] requested by the login. Client with IP address [my IP]
is not allowed to access the server. To enable access, use the Windows Azure
Management Portal or run sp_set_firewall_rule on the master database to create a
firewall rule for this IP address or address range. It may take up to five minutes
for this change to take effect. (40615) (SQLDriverConnect); [42000] [Microsoft]
[ODBC SQL Server Driver]Invalid connection string attribute (0); [42000] [Microsoft]
[ODBC SQL Server Driver][SQL Server]Cannot open server [server name] requested by the
login. Client with IP address [my IP] is not allowed to access the server. To enable
access, use the Windows Azure Management Portal or run sp_set_firewall_rule on the
master database to create a firewall rule for this IP address or address range. It may
take up to five minutes for this change to take effect. (40615); [42000] [Microsoft]
[ODBC SQL Server Driver]Invalid connection string attribute (0)")
Update:
I tried connecting using Visual Studio and it prompts me to create a new firewall rule. I choose 'Add my client IP' and click 'OK'. The prompt then immediately reappears. I tried clicking it a few times and the new rules do appear in the Azure portal, but I am still not able to connect through either Visual Studio or python.
Solution:
I was using SQL authentication instead of Active Directory authentication. Solved the problem by adding AUTHENTICATION=ActiveDirectoryPassword to the connection string.
Please ensure you have added client IP to firewall.
On the Azure SQL database overview, Set server firewall.
Add client IP:
Please modify you code like this and try again:
import pyodbc
server = '<server>.database.windows.net'
database = '<database>'
username = '<username>'
password = '<password>'
driver= '{ODBC Driver 17 for SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
This code example is provided by this documen: Create code to query your SQL database.
Updates:
The error has be solved..
Finally figured it out - it turns out the problem was that Jon123 was using SQL authentication instead of Active Directory authentication.
Hope this helps.
I am using pymssql=2.1.1 to connect Azure database from python. Due to idle connection for few minutes, i am getting error (Write to the server failed) and not able to fetch the data.
I am using connect method of pymssql to create connection to Azure DB.
conn = pymssql.connect(server=v_host, user=v_user, password=v_passwd, database=v_db)
cursor = self.conn.cursor(as_dict=True)
cursor.execute(query)
The error looks like
(20006, b'DB-Lib error message 20006, severity 9:\nWrite to the server failed\nNet-Lib error during Connection reset by peer (104)\n')
If you want to us pymssql connect to Azure SQL database, make sure the following requirements:
Examples:
import pymssql
conn=pymssql.connect("xxx.database.windows.net", "username#xxx", "password", "db_name")
cursor = conn.cursor()
cursor.execute(query)
For more details, please see: Connecting to Azure SQL Database. Starting with version 2.1.1 pymssql can be used to connect to Microsoft Azure SQL Database. And you can troubleshoot the error by pymssql Frequently asked questions.
Another way, you also can try the pyodbc example:
import pyodbc
server = '<server>.database.windows.net'
database = '<database>'
username = '<username>'
password = '<password>'
driver= '{ODBC Driver 17 for SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute(query)
Here is the Azure document: Quickstart: Use Python to query an Azure SQL database.
Hope this helps.
Well, after doing some research i created a database in MSSql server (2014) and can get a connection via sqlconnection with client, by using:
SqlConnection cn = new SqlConnection("user id = myUSER; password=myPASSWORD;server=mySERVER; Trusted_Connection=yes; database=myDatabase; connection timeout=30");
The connecion exist when my client is local and run on the same machine as MSSQL server, but when i'm on a remote pc i can't connect to the MSSQL server using sqlconnection.
How can i connect to that server from remote using sqlconnection - c# ?
Most likely, your SQL Server is not configured to allow remote connections, as Sean Lange points out in one of the comments above.
Carry out these steps from the Management Studio:
Right-click on the server name, click on 'Properties'. You'll get the 'Server
Properties' window.
Click on the 'Connections' tab in the left of this
window
You'll see a section on the right, that says 'Remote server
connections'
Check the 'Allow remote connections to this server'
checkbox.