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.
Related
I am trying to query MS SQL Server from a client with below connection string.
connStr='Driver={SQL Server};Server=Svr1;Database=testDB;User Id=DOMAIN1\User1;Password=test123;'
When I logged in to the client as a domain user, say DOMAIN1\User2 it works fine.
But in production environment, my script will be running as a service under admin account. Hence, when I try to execute the same script when logged in as local admin in same client machine, I get the below error
pyodbc.InterfaceError: ('28000', '[28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed. The login is from an untrusted domain and cannot be used with Integrated authentication. (18452) (SQLDriverConnect); [28000] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0); [28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed. The login is from an untrusted domain and cannot be used with Integrated authentication. (18452); [28000] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0)')
When I search for this error, many of the solutions are related to password expiry cases. Is there some attribute I can add to connection string to make this query work? I tried adding IntegratedSecurity=False, but it didn't work.
I am trying to access SQL Server 14.0.2027.2 using Python PyODBC module.
I am trying to connect my qt application to ms sql database. I dont know server machine's IP addres and PORT number. I allready have Data Source Name in System DSN. Here is the sample of my code :
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("DRIVER={SQL Server};SERVER={serverName};DATABASE=dbname;UID=username;PWD=password;WSID=.;Trusted_connection=yes");
bool ok = db.open();
if(ok == true){
qDebug()<<"connected";
}
else{
qDebug()<<"not connected";
qDebug()<<db.lastError().text();
}
And i get this error :
[Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user
'xxx'. QODBC3: Unable to connect.
Any help will be appreciated.
A few things here.
First, you either use windows authentication, or you use SQL authentication. Not both.
If your connection string specifies Trusted_connection=yes, or integrated security=SSPI, then you're using windows authentication.
If your connection string specifies uid=someuser; pwd=somepassword then you're using SQL auth.
Pick one.
Now, if you're using an ODBC DSN, then the DSN already specifies the driver and server, so you don't need that in your application connection string. Instead, you just tell the application which DSN to use.
Let's say you created your DSN and called it "MySqlServer". Then your code will look like this:
db.setDatabaseName("MySqlServer");
Now, if the DSN is set up to use windows authentication, that's all you need to do. On the other hand, if it's set up to use SQL authentication, then you can take the credentials already specified in the DSN, or set a username and password in your code:
db.setUserName("myUserName");
db.setPassword("myPassword");
I have a local installed SQL Server 2016 (Developer edition) installation on Windows 10 Pro. There is one named instance. I am a developer using MS SQL Management Studio and JetBrains DataGrip.
I just want my connections within my local machine to work with SQL Server over TCPIP using a SQL Server user account..! At the moment this only works using my Windows Authentication mapped to ServerAdmin
Named instance
RAXLENOVOT560\MSSQL2016MAIN
(authentication is mixed mode)
There are 3 issues that maybe linked:
TCP Port number does not appear to take effect within the Sql Server Configuration Manager.
The Sql Server Configuration Manager only shows limited properties on the Client Protocols/TCPIP settings. (please see screen shots)
Sql Server login user account does not connect via TCP due to an error. Login within local machine, local development from DataGrip to SQL Server.
1. TCP Port number
I have set this to 65321. However when I try and connect using this port no connection is made.
When I checked using the following SQL query I see the port number is actually 65514
SELECT DISTINCT
local_tcp_port
FROM sys.dm_exec_connections
WHERE local_tcp_port IS NOT NULL
results
local_tcp_port
--------------
65514
I change the port number in the config tool, restart ALL SQL Server services, reboot and still nothing works. Still listening on port 65514
Note I did not choose 65514, this seems to be a default from SQL Server.
2. The Sql Server Configuration Manager Properties Missing
Note the properties screen is limited only to a few options, there are usually lots more why is is? Perhaps due to running on Windows 10 Pro vs Windows Server?
3. Connection Login failure
Login from the local machine only.
I have opened the firewall for all the relevant SQL Server ports
TCP 1433
TCP 65514 (the forced sql server port)
TCP 65123 (what I would like to use)
UDP 1434 Sql Server Browser
I attempted login via a number of means
Sql Server Management Studio
Udl File
Java connection via DataGrip
Since opening the firewall, all the connections work but only if the hostname is 127.0.0.1, why can't I use the computer name? The connections also work if I use my Windows Login but NOT a SQL Server login
The login fails due to the error shown below from the SQL Server Log (which proves the TCPIP connection is reaching SQL Server).
Login-based server access validation failed with an infrastructure error. Login lacks connect endpoint permission
Date 24/04/2018 11:25:05
Log SQL Server (Current - 24/04/2018 11:20:00)
Source Logon
Message
Login failed for user 'SvrDeveloper'. Reason: Login-based server access validation failed with an infrastructure error. Login lacks connect endpoint permission. [CLIENT: 127.0.0.1]
The Sql Server User has the following permissions and settings (please see screenshot)
Name = SvrDeveloper
Server Roles = public
Securables = RAXLENOVOT560\MSSQL2016MAIN Type=Server
Securables = TSQL Default TCP Type=EndPoint
Connections
Udl Connection
[oledb]
; Everything after this line is an OLE DB initstring
Provider=SQLNCLI11.1;Integrated Security=SSPI;Persist Security Info=True;User ID="SvrAdmin";Password="Password123";Initial Catalog="";Data Source=TCP:127.0.01\MSSQL2016MAIN,65514;Network Library=DBMSSOCN;Initial File Name="";Server SPN=""
Java connection:
jdbc:sqlserver://127.0.0.1:65514;databaseName=CcTools;user=SvrDeveloper; password=Password123
Some additional information:
Version
I'm trying to run an application that I've developped recently. This one uses an ODBC connection to browse a defined database.
I'm trying to execute this app from a remote computer. Using VMWare, I've created a new Windows 7 environment and connected on. After that I've tried to create a new System DSN, but, when I set the logins that I'm using to connect on my local SQL Server 2008 I get
Connection failed: SQLState: '28000' SQL Server Error: 18452
[Microsoft][ODBC SQL Server Driver][SQL Server]Login failed. The login
is from an untrusted domain and cannot be used with Windows
authentication.
How may I to deal with this, please?
Thanks a lot!
You will have to use SQL Server authentication to connect or add the local machine to the same domain where the server is.
http://msdn.microsoft.com/en-us/library/ms144284.aspx
I created an ODBC database on my local machine with driver SQL server Native client 10.0, which connects to a remote server, see
.
I am working on a project about customized ODBC to an inhouse database and want to Test how I can connect to a data source using ODBC.
The question is how can I connect to the local ODBC using sql server studio manager? I tried
but it returns an error:
A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or
was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections. (provider: Named
Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Any idea?
Connect to your local server through SSMS then create a linked server to the ODBC connection.
Echoing #Brian Boyd...
It is possible to create an ODBC connection to a SQL server running locally.
If you start SSMS and point it to a local database engine the Server Name should be Computer_Name\SQLEXPRESS with Authentication set to Windows Auth (ie the logged-in user: you). Computer_Name is found in the System window (Windows Key + Break) and is not localhost, 127.0.0.1, etc.
So, to set up an ODBC connection by running %windir%\syswow64\odbcad32.exe
In the System DSN tab, click [Add...]
Select SQL Server in the drivers list, click [Finish]
Now add a Name and Description but most importantly set the Server to be Computer_Name\SQLEXPRESS (whatever was shown in SSMS)
Click [Next] and leave the authentication set to Windows NT
Click [Next] and tick the default database tickbox to reveal a list of databases locally (if yours is listed here the ODBC settings have already worked)
Click [Next], [Finish] then [Test Data Source...] then all should be well
To connect to a remote server you don't need a DSN. You can enter the server name in the Server name field of SQL Server Management Studio and select Windows authentication or Database authentication.
It is not possible to connect SSMS to an ODBC data source. The only way is to create a linked server in your local SQL server as #Brian Boyd described.
Instructions are:
https://community.sagecrm.com/partner_community/b/hints_tips_and_tricks/archive/2010/05/10/connecting-to-a-sage-mas-erp-90-database-as-a-linked-server-within-ms-sql-server-2008.aspx
... and sp_AddLinkedServer documentation from MS HERE
… and with search = “sp_addlinkedserver for SOTAMAS90“, even an example from 2005
https://blog.coryfoy.com/2005/06/lets-go-crazy-accessing-timberline-pervasive-data-from-a-sql-linked-server/
Let’s see what turn says …. And think I will / would get the same error adding a linked server through UI that I get TSQL
Based on above, I tried …
EXEC sp_addlinkedserver
#server = 'TimberlineTest',
#provider = 'SOTAMAS90', -- Original command #provider = MSDASQL',
#srvproduct = '', --- MAS 90 4.0 ODBC Driver Original is #srvproduct='Timberline Data',
#datasrc = 'DataTest'