简体   繁体   中英

Can't connect to SQL Server on AWS RDS via Python

I am trying to connect a python terminal to a SQL Server database on AWS RDS.

I've looked through Microsoft's documentation on how to do it here , and I've seen aplethora of different questions asked here on Stack Overflow, however I think my unique problem is mapping the connection process to specific aspects of the RDS environment, and I'm unsure if my environment is setup correctly, and I can't find a question that maps directly to the issues I'm trying to research.

WHAT I HAVE RIGHT NOW:

import pyodbc as pydb

connection = pydb.connect('DRIVER={SQL Server};PORT=1433;SERVER=aws-database-endpoint;UID=instance-master-username;PWD=instance-password;DATABASE=db-instance-id')

print("Connecting....")

connection.close()

And getting the following traceback:

InterfaceError: ('28000', "[28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'admin'. (18456) (SQLDriverConnect); [28000] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0); [28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'admin'. (18456); [28000] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0)")

To provide more detail, here's a picture of what my database setup currently looks like:

For port, and server, I'm getting the info from here:

在此处输入图像描述

For UID I'm going to RDS --> Databases , clicking on my instance and then going to Configuration , and I'm entering the Master Username , which is admin , as can be seen here:

在此处输入图像描述

And for my DATABASE value, I'm at the same spot, and using the database instance-id , which can be seen here:

在此处输入图像描述

I've created a rule that allows my IP address for incoming TCP traffic via VPC resource groups here:

在此处输入图像描述

What I've also tried:

  • using my account ID for the UID argument
  • using my AWS username for the UID argument
  • database is publicly accessible
  • I have tried logging in both as a root user, and as an IAM user with administrator privileges

I can't tell if I'm making a clerical mistake or if there is a larger admin issue that I'm not identifying.

Thank you.

use the sample connection below, note there is not port if its the default 1433

cnxn = pyodbc.connect('DRIVER={/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.3.so.1.1};SERVER='+server+';DATABASE='+database+';uid='+username+';pwd='+ password) 

If you are running the python script from your local machine (wsl, linux box or ec2) make sure the msodbc drivers are correctly installed

You can also try this, it was easier than pyodbc for me:

import pymssql

connection={
   'host': '',
   'username': '',
   'password': '',
   'db': '' 
}
con=pymssql.connect(connection['host'],connection['username'],connection['password'],connection['db'])

cursor=con.cursor()

Running into this problem myself I found that AWS RDS doesn't actually give you an option to name your database within the instance in SQL server like it does with PostreSQL when instantiating or afterwords and rather than giving it the name of the instance it leaves this field blank. You can confirm this by looking at the configuration tab within the instance summary in the AWS dashboard, under DB name there is simply a dash to indicate a blank field. I was able to solve the problem by just removing the database name field from my connection string. So the string the original example would look like this:

import pyodbc as pydb

connection = pydb.connect('DRIVER={SQL Server};PORT=1433;SERVER=aws-database-endpoint;UID=instance-master-username;PWD=instance-password;')

Try to bring each component to a variable

import pyodbc 

server = 'tcp:myserver.database.windows.net' 
database = 'mydb' 
username = 'myusername' 
password = 'mypassword' 
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)

cursor = cnxn.cursor()

#Sample select query
query = 'SELECT....'
df = pd.sql_read(query, cnxn)
print(df)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM