---
title: "How to Connect Replit to AWS RDS MySQL: Security Groups, Secrets, and Connection Setup"
description: "Learn how to connect Replit to AWS RDS MySQL with this practical guide covering security groups, public accessibility settings, credential management with Replit Secrets, and connection code examples in Python and TypeScript. Includes troubleshooting for common issues like timeouts, access denied errors, SSL handshake problems, and connection pooling best practices."
image: "https://cdn.sethserver.com/blog-images/connecting-your-replit-app-to-aws-rds-mysql-social-1767152664.webp"
---

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

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:

- **Public accessibility: Yes**

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:

- **Type:** MySQL/Aurora
- **Port:** 3306
- **Source:** `0.0.0.0/0` (temporary)

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:

- keep `0.0.0.0/0` (not great), or
- move the app into a network you control (like an EC2 instance in the same VPC), or
- put the database behind something else (like an API layer) so the DB isn't exposed

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

This happens a lot:

- You add the inbound rule to a security group you *think* is attached.
- The RDS instance is attached to a different one.
- You still can't connect.
- You start questioning reality.

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:

- **Endpoint** like:
  `your-db-name.abc123xyz.us-east-1.rds.amazonaws.com`
- **Port:** `3306`
- **Database name**
- **Master username**
- **Master password**

A few things that'll save you time:

- The endpoint is a hostname, not an IP. Don't copy the "Resource ID" or the ARN. AWS gives you lots of strings. Only one of them is useful here.
- If you didn't create a database name during setup, you might only have the default system databases. That's fine. You can create one later.
- "Master username" is just a user. You can (and should) create an app-specific user later with limited permissions. For initial setup, master is fine.

## Step 3: Put credentials in Replit Secrets

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

- `DATABASE_HOST`
- `DATABASE_PORT`
- `DATABASE_USER`
- `DATABASE_PASSWORD`
- `DATABASE_NAME`

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:

```bash
pip install mysql-connector-python
```

Then use it like this:

```python
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:

```sql
SELECT DATABASE();
```

In Python:

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

## Python: PyMySQL alternative

Install it:

```bash
pip install pymysql
```

Then:

```python
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`](https://www.npmjs.com/package/mysql2). It behaves.

Install it:

```bash
npm install mysql2
```

Then:

```ts
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:

```ts
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

```ts
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:

- **Timeout / can't connect:**
  This is almost always networking.
- RDS not publicly accessible
- wrong security group edited
- inbound rule missing port 3306
- inbound rule source doesn't include Replit's outbound IP
  If it times out, it's usually not your password. Timeouts are packets getting blocked.
- **Access denied for user:**
  Usually creds or permissions.
- wrong username/password
- MySQL usernames are case-sensitive in some setups (treat them as case-sensitive anyway)
- user exists but doesn't have rights on that database
  If you created a non-master user, make sure it has privileges:

`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.

- **Unknown database:**
  The database name in your env vars doesn't exist yet. You can connect without specifying a database, then create it:

`sql
CREATE DATABASE your_database_name;`

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

- **SSL handshake issues:**
  RDS may require SSL. In Node, try `ssl: { rejectUnauthorized: false }`.
  In Python drivers, look for SSL options. Some connectors use `ssl_ca`, `ssl_verify_cert`, or similar flags. The exact parameter names vary, which is annoying but expected. If you hit this, check the driver docs for "SSL" and "RDS".
- **Too many connections:**
  You aren't closing connections, or you're creating a new connection per query/request. Fix it with pooling (Node) or a sane connection lifecycle (Python). Also check for background tasks that open connections and never close them.
- **ER\_NOT\_SUPPORTED\_AUTH\_MODE:**
  You're using the old `mysql` Node package against MySQL 8+. Switch to `mysql2`, or change the user to use `mysql_native_password`. Switching packages is usually less messy than changing auth settings.
- **"It worked yesterday" and now it doesn't:**
  Check whether your Replit outbound IP changed (free plan), or whether you tightened the security group and forgot to include the new IP.

## 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:

- stop the instance when you're not using it (if your setup supports stopping), or
- delete it when you're done, and
- delete snapshots you don't need

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


```json
{"@context": "https://schema.org", "@type": "BlogPosting", "headline": "How to Connect Replit to AWS RDS MySQL: Security Groups, Secrets, and Connection Setup", "datePublished": "2025-12-11T07:29:19", "dateModified": "2026-09-17T19:47:51", "image": {"@type": "ImageObject", "url": "https://cdn.sethserver.com/blog-images/connecting-your-replit-app-to-aws-rds-mysql-social-1767152664.webp", "width": 400, "height": 400}, "url": "https://www.sethserver.com/programming/connecting-your-replit-app-to-aws-rds-mysql.html", "author": {"@type": "Person", "name": "Seth Black", "url": "https://www.linkedin.com/in/seth-black-tx"}, "publisher": {"@type": "Organization", "name": "Sethserver", "logo": {"@type": "ImageObject", "url": "https://www.sethserver.com/static/images/PaperAirplane512.png"}}, "description": "Learn how to connect Replit to AWS RDS MySQL with this practical guide covering security groups, public accessibility settings, credential management with Replit Secrets, and connection code examples in Python and TypeScript. Includes troubleshooting for common issues like timeouts, access denied errors, SSL handshake problems, and connection pooling best practices.", "articleSection": "Technology"}
{"@context": "https://schema.org", "@type": "BreadcrumbList", "itemListElement": [{"@type": "ListItem", "position": 1, "name": "sethserver", "item": "https://www.sethserver.com/"}, {"@type": "ListItem", "position": 2, "name": "Programming", "item": "https://www.sethserver.com/programming/"}, {"@type": "ListItem", "position": 3, "name": "How to Connect Replit to AWS RDS MySQL: Security Groups, Secrets, and Connection Setup", "item": "https://www.sethserver.com/programming/connecting-your-replit-app-to-aws-rds-mysql.html"}]}
```
