- Newest
- Most votes
- Most comments
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:
-
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'.
-
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.
-
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:
- 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
- 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
Relevant content
- asked 2 years ago
- Accepted Answerasked 4 years ago
- AWS OFFICIALUpdated 4 months ago
- AWS OFFICIALUpdated 2 days ago
- AWS OFFICIALUpdated 5 months ago
- AWS OFFICIALUpdated 2 years ago