By using AWS re:Post, you agree to the AWS re:Post Terms of Use

Sagemaker Notebook to RDS( MS SQL Server Web Edition) connection issue using SQLDatabase from langchain_community.utilities

0

Not able to connect from Sagemaker Notebook to RDS( MS SQL Server Web Edition) connection using SQLDatabase from langchain_community.utilities.

from langchain_community.utilities import SQLDatabase
#db_uri = f"mysql+pyodbc://{USERNAME}:{PASSWORD}@{SERVER}:{PORT}/{DATABASE}"
db_uri = f"mysql+pyodbc://{USERNAME}:{PASSWORD}@{SERVER}:{PORT}/{DATABASE}?driver=FreeTDS&TDS_Version=7.4"
db = SQLDatabase.from_uri(db_uri)

But it gives: ProgrammingError: (pyodbc.ProgrammingError) ('42000', "[42000] [FreeTDS][SQL Server]'VERSION' is not a recognized built-in function name. (195) (SQLExecDirectW)") (Background on this error at: https://sqlalche.me/e/20/f405)

Although I was able to connect using connectionString = f'DRIVER={{FreeTDS}};SERVER={SERVER};DATABASE={DATABASE};UID={USERNAME};PWD={PASSWORD}' conn = pyodbc.connect(connectionString) so there is no network issue

Also not able to install driver=ODBC+Driver+17+for+SQL+Server in sagemaker notebook, please share some insights into issue

1 Answer
1
Accepted Answer

It seems you're encountering a few issues while trying to connect from a SageMaker Notebook to an RDS MS SQL Server Web Edition database using SQLDatabase from langchain_community.utilities. Let's address these issues one by one:

  1. Connection string format: The connection string you're using is incorrect for MS SQL Server. You're using a MySQL-style connection string, but you need to use a SQL Server-specific one. For SQL Server, you should use 'mssql+pyodbc' instead of 'mysql+pyodbc'.

  2. Driver issue: You're trying to use FreeTDS, which is causing compatibility problems. For SQL Server, it's recommended to use the Microsoft ODBC Driver for SQL Server. However, you mentioned you're unable to install it in the SageMaker notebook.

  3. ODBC Driver installation: SageMaker notebooks run on a Linux environment, so you need to install the Linux version of the Microsoft ODBC driver. This can be done by running shell commands in the notebook.

Here's a step-by-step approach to resolve these issues:

  1. First, install the Microsoft ODBC driver for SQL Server. Run these commands in a notebook cell (use the '!' prefix to run shell commands):
!curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/mssql-release.repo
!sudo ACCEPT_EULA=Y yum install -y msodbcsql18
!sudo yum install -y unixODBC-devel
  1. After installing the driver, modify your connection string to use the correct format for SQL Server:
from langchain_community.utilities import SQLDatabase

db_uri = f"mssql+pyodbc://{USERNAME}:{PASSWORD}@{SERVER}:{PORT}/{DATABASE}?driver=ODBC+Driver+18+for+SQL+Server"
db = SQLDatabase.from_uri(db_uri)

This approach should resolve the connection issues you're experiencing. If you still encounter problems, make sure that:

  • Your SageMaker notebook instance has network access to the RDS instance.
  • The security group for your RDS instance allows incoming connections from your SageMaker notebook's security group.
  • The username and password you're using have the necessary permissions to access the database.

Remember to handle your database credentials securely, preferably using AWS Secrets Manager or environment variables, rather than hardcoding them in your notebook.
Sources
Embed textual data in Amazon RDS for SQL Server using Amazon Bedrock | AWS Database Blog
Machine Learning Service – Amazon SageMaker Studio Notebooks – AWS
SQL extension data source connections (for administrators) - Amazon SageMaker

profile picture
answered a month ago
profile picture
EXPERT
reviewed a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions