sethserver.com Subscribe
CrateDB distributed SQL database octopus mascot with tentacles connecting to AWS cloud, Python database icon, and data spiral symbol - illustrating cloud database architecture and Python integration

How to Connect Replit to AWS RDS MySQL: Security Groups, Secrets, and Connection Setup

By Seth Black • Updated: September 17, 2026

Programming ยท 9 min read

Like this kind of writing? Get one email a week with notes on startups, AI, and the occasional strong opinion about Python: subscribe to the newsletter.

You're going to get tripped up on exactly two things connecting Replit to AWS RDS MySQL:

1) networking (security groups), and
2) secrets (where you put the credentials so you don't paste them into Git like a raccoon hoarding shiny objects).

That's it. The rest is normal MySQL connection stuff. Port 3306. Endpoint. Username. Password. Database. Done.

This setup is almost identical to RDS PostgreSQL. The main difference is the port: PostgreSQL is 5432, MySQL is 3306. Everything else is the same AWS dance.

Step 1: Make sure the database is actually reachable

Go to your RDS instance - Connectivity & security. This page is where AWS hides the "why can't I connect" answers.

Public accessibility

For a Replit app on the public internet, your RDS instance needs:

If it's "No," your connection will hang until it times out, and you'll waste 30 minutes blaming Python.

Reality check: "Publicly accessible" does not mean "anyone can log in." It means the database has a public network path. The security group still decides who gets through.

Also: if you flip this from No to Yes, AWS may modify the instance and reboot it. Don't do this mid-demo unless you like surprises.

Security group inbound rule

Click the VPC security group attached to the RDS instance. (There can be multiple. You want the one that's actually attached.)

Add an inbound rule:

Yes, 0.0.0.0/0 is wide open. It's the "I just need it to work" setting. Use it to prove your code is fine, then tighten it.

If you want to be slightly less chaotic while testing from your laptop, set the source to your current IP (x.x.x.x/32). That helps for local testing. Replit won't come from your laptop IP, so you still need a plan for Replit.

Better move: pay for a Replit plan that gives you a static outbound IP (Reserved VM or Dedicated VM), then set the inbound rule source to that IP. For most small projects, static IP + a security group allowlist is the cleanest setup.

If you're staying on the free plan, your Replit outbound IP can change. That breaks the "allowlist one IP" approach. Your choices become:

One more networking gotcha: you might be editing the wrong security group

This happens a lot:

After adding the rule, go back to the RDS instance page and confirm the security group name matches the one you edited.

Step 2: Collect the five facts you need

From the RDS console:

A few things that'll save you time:

Step 3: Put credentials in Replit Secrets

In Replit, use Secrets (environment variables). I like the explicit ones:

You can use a single DATABASE_URL like:

mysql://username:password@your-endpoint:3306/database_name

I still prefer separate variables because debugging is less gross. If your app can't connect, you can print DATABASE_HOST and DATABASE_PORT without dumping a full connection string into logs.

Practical tip: in Replit, secrets are injected into the environment at runtime. If you change them, restart the repl. Otherwise you'll be debugging with yesterday's values.

Python: mysql-connector example

Install the dependency:

pip install mysql-connector-python

Then use it like this:

import os
import mysql.connector

cfg = {
    "host": os.environ["DATABASE_HOST"],
    "port": int(os.environ.get("DATABASE_PORT", "3306")),
    "user": os.environ["DATABASE_USER"],
    "password": os.environ["DATABASE_PASSWORD"],
    "database": os.environ["DATABASE_NAME"],
}

try:
    conn = mysql.connector.connect(**cfg)
    cur = conn.cursor()
    cur.execute("SELECT 1;")
    print("OK:", cur.fetchone())
finally:
    try:
        cur.close()
        conn.close()
    except Exception:
        pass

If that prints OK: (1,), networking is good and your creds are real.

Two things you'll want soon:

1) Autocommit vs manual commits
If you're doing inserts/updates, you'll need conn.commit() unless autocommit is enabled. For a lot of web apps, I prefer manual commit so you don't half-write data if something fails mid-request.

2) A query that proves you're in the right database
SELECT 1 proves the connection works. It doesn't prove you're connected to the schema you think you are. Follow up with:

SELECT DATABASE();

In Python:

cur.execute("SELECT DATABASE();")
print("DB:", cur.fetchone())

Python: PyMySQL alternative

Install it:

pip install pymysql

Then:

import os
import pymysql

conn = pymysql.connect(
    host=os.environ["DATABASE_HOST"],
    port=int(os.environ.get("DATABASE_PORT", "3306")),
    user=os.environ["DATABASE_USER"],
    password=os.environ["DATABASE_PASSWORD"],
    database=os.environ["DATABASE_NAME"],
    cursorclass=pymysql.cursors.DictCursor,
)

with conn:
    with conn.cursor() as cur:
        cur.execute("SELECT 1 AS ok;")
        print(cur.fetchone())

PyMySQL is nice because it's pure Python and tends to behave predictably in weird environments. If mysql-connector-python gives you dependency pain, PyMySQL is a good fallback.

Tradeoff: performance. For most small apps it won't matter. If you're pushing serious traffic, pooling and query patterns will matter more than which driver you picked.

TypeScript: use mysql2 (not mysql)

MySQL 8+ changed auth defaults. The old mysql package will happily ruin your afternoon with ER_NOT_SUPPORTED_AUTH_MODE. Use mysql2. It behaves.

Install it:

npm install mysql2

Then:

import mysql from "mysql2/promise";

const conn = await mysql.createConnection({
  host: process.env.DATABASE_HOST,
  port: parseInt(process.env.DATABASE_PORT || "3306"),
  user: process.env.DATABASE_USER,
  password: process.env.DATABASE_PASSWORD,
  database: process.env.DATABASE_NAME,
  ssl: { rejectUnauthorized: false },
});

const [rows] = await conn.execute("SELECT 1 AS ok;");
console.log(rows);
await conn.end();

About SSL: RDS often supports SSL, and sometimes enforces it depending on parameter groups and settings. The quick "make it work" move in Node is:

ssl: { rejectUnauthorized: false }

This skips certificate verification. Not ideal, but it gets you connected. The better version is to download AWS's RDS CA bundle and pass it in as ca. That's extra work, but you can do it after your first connection works.

Production: use a pool or suffer

import mysql from "mysql2/promise";

const pool = mysql.createPool({
  host: process.env.DATABASE_HOST,
  port: parseInt(process.env.DATABASE_PORT || "3306"),
  user: process.env.DATABASE_USER,
  password: process.env.DATABASE_PASSWORD,
  database: process.env.DATABASE_NAME,
  connectionLimit: 10,
  waitForConnections: true,
  queueLimit: 0,
  ssl: { rejectUnauthorized: false },
});

const [rows] = await pool.execute("SELECT NOW() AS now;");
console.log(rows);

If you create a new connection per request, you'll hit "Too many connections" fast. Pools aren't optional once real traffic shows up.

A practical rule: if your app is a web server, you want one pool per process, created at startup, reused for all requests. Don't create pools inside request handlers. That's just manufacturing new problems on every refresh.

Also, don't set connectionLimit to 100 because it "sounds safe." Your RDS instance has a max connection count based on instance size, and other things may use connections too (migrations, admin tools). Start with 5โ€“10, measure, adjust.

Troubleshooting: the usual fires

Here's what you'll actually hit:

sql GRANT ALL PRIVILEGES ON your_database_name.* TO 'appuser'@'%'; FLUSH PRIVILEGES;

Yes, '%' means "any host." If you're already locking down by security group IP, this is fine for many apps. If you want to be stricter, you can set a specific host, but that gets annoying if your app host changes.

sql CREATE DATABASE your_database_name;

In practice: temporarily remove database from your config, connect, run the create, then put it back.

Cost and cleanup (aka the part everyone ignores until the bill arrives)

RDS is not "free tier forever" unless you're very deliberate. Even small instances cost money if they run all month. Storage costs money. Backups can cost money. Snapshots can cost money. Data transfer can cost money.

I've had too many "why is my AWS bill $400" chats that start with "I forgot a database in us-east-1."

If this is a learning project:

If you're building something real, budget for it and monitor it. The surprise is what hurts.

If you can run SELECT 1, you're connected. Everything after that is just schema and queries. Getting packets through the wall was the hard part.

-Sethers

Share this post
Newsletter

One email, once a week.

Notes on databases, systems, and the occasional strong opinion about Python. No spam, unsubscribe anytime.

Seth Black
Written by

Seth Black

Engineer and founder based in Texas. Writes about databases, AI, and running things in production. Embeds in small teams as lead engineer.

More from Programming

View all โ†’
Programming

What's the Differences Between Relational, NoSQL, Vectors, and Caches

Apr 14, 2026
Programming

Git and Source Control in AI Land

Apr 14, 2026
Programming

Domains, DNS, and why it feels fragile

Apr 14, 2026