Mysql
Reference doc for the `sst.aws.Mysql` component.
The Mysql
component lets you add a MySQL database to your app using
Amazon RDS MySQL.
Create the database
const vpc = new sst.aws.Vpc("MyVpc");const database = new sst.aws.Mysql("MyDatabase", { vpc });
Link to a resource
You can link your database to other resources, like a function or your Next.js app.
new sst.aws.Nextjs("MyWeb", { link: [database], vpc});
Once linked, you can connect to it from your function code.
import { Resource } from "sst";import mysql from "mysql2/promise";
const connection = await mysql.createConnection({ user: Resource.MyDatabase.username, password: Resource.MyDatabase.password, database: Resource.MyDatabase.database, host: Resource.MyDatabase.host, port: Resource.MyDatabase.port,});await connection.execute("SELECT NOW()");
Running locally
By default, your RDS MySQL database is deployed in sst dev
. But let’s say you are
running MySQL locally.
docker run \ --rm \ -p 3306:3306 \ -v $(pwd)/.sst/storage/mysql:/var/lib/mysql/data \ -e MYSQL_DATABASE=local \ -e MYSQL_ROOT_PASSWORD=password \ mysql:8.0
You can connect to it in sst dev
by configuring the dev
prop.
const mysql = new sst.aws.Mysql("MyMysql", { vpc, dev: { username: "root", password: "password", database: "local", port: 3306 }});
This will skip deploying an RDS database and link to the locally running MySQL database instead.
Cost
By default this component uses a Single-AZ Deployment, On-Demand DB Instances of a
db.t4g.micro
at $0.016 per hour. And 20GB of General Purpose gp3 Storage
at $0.115 per GB per month.
That works out to $0.016 x 24 x 30 + $0.115 x 20 or $14 per month. Adjust this for the
instance
type and the storage
you are using.
The above are rough estimates for us-east-1, check out the RDS for MySQL pricing for more details.
RDS Proxy
If you enable the proxy
, it uses Provisioned instances with 2 vCPUs at $0.015 per hour.
That works out to an additional $0.015 x 2 x 24 x 30 or $22 per month.
The above are rough estimates for us-east-1, check out the RDS Proxy pricing for more details.
Constructor
new Mysql(name, args, opts?)
Parameters
-
name
string
-
args
MysqlArgs
-
opts?
ComponentResourceOptions
MysqlArgs
database?
Type Input
<
string
>
Default Based on the name of the current app
Name of a database that is automatically created.
The name must begin with a letter and contain only lowercase letters, numbers, or underscores. By default, it takes the name of the app, and replaces the hyphens with underscores.
{ database: "acme"}
dev?
Type Object
Configure how this component works in sst dev
.
By default, your MySQL database is deployed in sst dev
. But if you want to instead
connect to a locally running MySQL database, you can configure the dev
prop.
This will skip deploying an RDS database and link to the locally running MySQL database instead.
Setting the dev
prop also means that any linked resources will connect to the right
database both in sst dev
and sst deploy
.
{ dev: { username: "root", password: "password", database: "mysql", host: "localhost", port: 3306 }}
dev.database?
Type Input
<
string
>
Default Inherit from the top-level database
.
The database of the local MySQL to connect to when running in dev.
dev.host?
Type Input
<
string
>
Default “localhost”
The host of the local MySQL to connect to when running in dev.
dev.password?
Type Input
<
string
>
Default Inherit from the top-level password
.
The password of the local MySQL to connect to when running in dev.
dev.port?
Type Input
<
number
>
Default 3306
The port of the local MySQL to connect to when running in dev.
dev.username?
Type Input
<
string
>
Default Inherit from the top-level username
.
The username of the local MySQL to connect to when running in dev.
instance?
Type Input
<
string
>
Default “t4g.micro”
The type of instance to use for the database. Check out the supported instance types.
{ instance: "m7g.xlarge"}
By default, these changes are not applied immediately by RDS. Instead, they are applied in the next maintenance window. Check out the full list of props that are not applied immediately.
multiAz?
Type Input
<
boolean
>
Default false
Enable Multi-AZ deployment for the database.
This creates a standby replica for the database in another availability zone (AZ). The standby database provides automatic failover in case the primary database fails. However, when the primary database is healthy, the standby database is not used for serving read traffic.
{ multiAz: true}
password?
Type Input
<
string
>
Default A random password is generated.
The password of the master user.
{ password: "Passw0rd!"}
Use Secrets to manage the password.
{ password: new sst.Secret("MyDBPassword").value}
proxy?
Type Input
<
boolean
|
Object
>
-
credentials?
Input
<
Input
<
Object
>
[]
>
Default false
Enable RDS Proxy for the database.
{ proxy: true}
proxy.credentials?
Type Input
<
Input
<
Object
>
[]
>
Additional credentials the proxy can use to connect to the database. You don’t need to specify the master user credentials as they are always added by default.
{ credentials: [ { username: "metabase", password: "Passw0rd!", } ]}
Use Secrets to manage the password.
{ credentials: [ { username: "metabase", password: new sst.Secret("MyDBPassword").value, } ]}
proxy.credentials[].password
Type Input
<
string
>
The password of the user.
proxy.credentials[].username
Type Input
<
string
>
The username of the user.
storage?
Type Input
<
“
${number} GB
”
|
“
${number} TB
”
>
Default “20 GB”
The maximum storage limit for the database.
RDS will autoscale your storage to match your usage up to the given limit. You are not billed for the maximum storage limit, You are only billed for the storage you use.
By default, gp3 storage volumes are used without additional provisioned IOPS. This provides a good baseline performance for most use cases.
The minimum storage size is 20 GB. And the maximum storage size is 64 TB.
{ storage: "100 GB"}
transform?
Type Object
Transform how this component creates its underlying resources.
transform.instance?
Type InstanceArgs
|
(
args
:
InstanceArgs
,
opts
:
ComponentResourceOptions
,
name
:
string
)
=>
void
Transform the database instance in the RDS Cluster.
transform.parameterGroup?
Type ParameterGroupArgs
|
(
args
:
ParameterGroupArgs
,
opts
:
ComponentResourceOptions
,
name
:
string
)
=>
void
Transform the RDS parameter group.
transform.proxy?
Type ProxyArgs
|
(
args
:
ProxyArgs
,
opts
:
ComponentResourceOptions
,
name
:
string
)
=>
void
Transform the RDS Proxy.
transform.subnetGroup?
Type SubnetGroupArgs
|
(
args
:
SubnetGroupArgs
,
opts
:
ComponentResourceOptions
,
name
:
string
)
=>
void
Transform the RDS subnet group.
username?
Type Input
<
string
>
Default “root”
The username of the master user.
{ username: "admin"}
version?
Type Input
<
string
>
Default “8.0.40”
The MySQL engine version. Check out the available versions in your region.
{ version: "8.4.4"}
vpc
Type Vpc
|
Input
<
Object
>
The VPC subnets to use for the database.
{ vpc: { subnets: ["subnet-0db7376a7ad4db5fd ", "subnet-06fc7ee8319b2c0ce"], }}
Or create a Vpc
component.
const myVpc = new sst.aws.Vpc("MyVpc");
And pass it in. The database will be placed in the private subnets.
{ vpc: myVpc}
vpc.subnets
Type Input
<
Input
<
string
>
[]
>
A list of subnet IDs in the VPC.
Properties
database
Type Output
<
string
>
The name of the database.
host
Type Output
<
string
>
The host of the database.
id
Type Output
<
string
>
The identifier of the MySQL instance.
nodes
Type Object
nodes.instance
Type undefined
|
Instance
password
Type Output
<
string
>
The password of the master user.
port
Type Output
<
number
>
The port of the database.
proxyId
Type Output
<
string
>
The name of the MySQL proxy.
username
Type Output
<
string
>
The username of the master user.
SDK
Use the SDK in your runtime to interact with your infrastructure.
Links
This is accessible through the Resource
object in the SDK.
-
database
string
The name of the database.
-
host
string
The host of the database.
-
password
string
The password of the master user.
-
port
number
The port of the database.
-
username
string
The username of the master user.
Methods
static get
Mysql.get(name, args, opts?)
Parameters
The name of the component.name
string
The arguments to get the MySQL database.args
MysqlGetArgs
-
opts?
ComponentResourceOptions
Returns Mysql
Reference an existing MySQL database with the given name. This is useful when you create a MySQL database in one stage and want to share it in another. It avoids having to create a new MySQL database in the other stage.
Imagine you create a database in the dev
stage. And in your personal stage frank
,
instead of creating a new database, you want to share the same database from dev
.
const database = $app.stage === "frank" ? sst.aws.Mysql.get("MyDatabase", { id: "app-dev-mydatabase", proxyId: "app-dev-mydatabase-proxy", }) : new sst.aws.Mysql("MyDatabase", { proxy: true, });
Here app-dev-mydatabase
is the ID of the database, and app-dev-mydatabase-proxy
is the ID of the proxy created in the dev
stage. You can find these by outputting
the database ID and proxy ID in the dev
stage.
return { id: database.id, proxyId: database.proxyId,};
MysqlGetArgs
id
Type Input
<
string
>
The ID of the database.
proxyId?
Type Input
<
string
>
The ID of the proxy.