Home > OS >  Not able to connect to AWS Aurora DB server-less -- mysql engine
Not able to connect to AWS Aurora DB server-less -- mysql engine

Time:12-24

I have created an AWS Aurora DB serverless publically available, and I am trying to connect to the DB using python. But I am unable to connect and I suspect the VPC.

Please suggest if I have to check anything else, also I have these below queries;

  • Is AWS Aurora serverless with min configuration a free tire DB?
  • My VPC while creating the DB is Public already, yet I am not able to connect so do I need to perform any additional configuration changes?

Code Snippets:

import mysql.connector as mysq
import sys, os, boto3 as aws, pandas as pd, pymysql
from sqlalchemy import create_engine, inspect

ENDPOINT = "random.cluster-random.ap-south-1.rds.amazonaws.com"
PORT = "3306"
USER = "random"
REGION = "ap-south-1"
DBNAME = "random"
PASSWORD = "random"

Method 1:

client = aws.client('rds')
token = client.generate_db_auth_token(DBHostname=ENDPOINT, Port=PORT, DBUsername=USER, Region=REGION)
print(token)

try:
    conn =  mysq.connect(host=ENDPOINT, user=USER, passwd=token, port=PORT, database=DBNAME)

    cur = conn.cursor()
    cur.execute("""SELECT now()""")
    query_results = cur.fetchall()
    print(query_results)
except Exception as e:
    print("Database connection failed due to {}".format(e))

Method 2:

CONNECTION_STRING = 'mssql pymssql'   '://'   USER   ':'   PASSWORD   '@'   ENDPOINT   ':'   str(PORT)   '/'   DBNAME
engine = create_engine(CONNECTION_STRING)
print(inspect(engine).get_table_names())

Method 3:

conn = pymysql.connect(host=ENDPOINT, user=USER,port=int(PORT), passwd=PASSWORD, db=DBNAME)

Thanks, Nikhil

CodePudding user response:

Aurora serverless does not have public ip. Form docs.

You can't give an Aurora Serverless v1 DB cluster a public IP address. You can access an Aurora Serverless v1 DB cluster only from within a VPC.

Same for Aurora v2.

So you have to setup a VPN between your home/work network and your VPC, or use SSH tunneling through some ec2 instance bastion host.

  • Related