Deploy MySQL
Railway offers two MySQL deployment options to accommodate different needs: a Standalone Instance and a High Availability (HA) Cluster.
-
Standalone Instance - a single MySQL database server that is easy to manage; ideal for development environments, smaller projects, or services which are less sensitive to disruption.
-
High Availability (HA) Cluster - intended for production workloads where uptime is critical. It consists of three MySQL nodes configured as an InnoDB Cluster as well as a MySQL Router service for connecting to the cluster.
Standalone MySQL
Let's talk about how to deploy, connect, and manage the standalone instance.
Deploy
You can deploy a standalone MySQL database via the CMD + K
menu or by clicking the + New
button on the Project Canvas.
You can also deploy it via the template from the template marketplace.
Deployed Service
Upon deployment, you will have a standalone MySQL service running in your project, deployed directly from the mysql Docker image.
Connect
Connect to MySQL from another service in your project by referencing the environment variables made available in the MySQL service:
MYSQLHOST
MYSQLPORT
MYSQLUSER
MYSQLPASSWORD
MYSQLDATABASE
MYSQL_URL
Connecting Externally
It is possible to connect to MySQL externally (from outside of the project in which it is deployed), by using the TCP Proxy which is enabled by default.
Keep in mind that you will be billed for Network Egress when using the TCP Proxy.
Modify the Deployment
Since the deployed container is pulled from the official MySQL image in Docker hub, you can modify the deployment based on the instructions in Docker hub.
High Availability MySQL InnoDB Cluster
We'll cover how to deploy, connect, and manage the High Availability (HA) MySQL InnoDB Cluster in this section.
Deploy
You can deploy a HA MySQL InnoDB cluster via the template in the marketplace.
You will need a Railway API token to deploy the service. You will be prompted for your token upon deploying the template.
Deployed Services
Upon deployment, a cluster of 3 MySQL nodes will be added to your project. The nodes are deployed from a custom Dockerfile. The Dockerfile pulls the mysql Docker image and copies a my.cnf
file into each container, configuring the necessary settings to prep the nodes to join the InnoDB cluster.
An initialization service is also deployed, which waits for the nodes to come online before initializing the cluster and joining the nodes.
Lastly, a MySQL Router service is also deployed, which serves as a proxy to the cluster. This router is built from the MySQL Router image.
Multi-region Deployment
By default, each node is deployed to a different region (US West, US East, and EU West) for fault tolerance.
Since region selection is a Pro-only feature, this only applies to Pro users. If you deploy this template as a Hobby user, all nodes will deploy to US West.
Connect
You should connect to the cluster via a proxy service which is aware of all of the cluster nodes. We have included a MySQL Router service in the template deployment for this purpose.
Connect to the cluster via the environment variables provided in the MySQL Router:
MYSQL_ROUTER_HOST
MYSQL_ROUTER_PORT
MYSQL_USER
MYSQL_PASSWORD
MYSQL_DB
For an example, check out the example app in the template's source repo.
Connecting Externally
It is possible to connect to the MySQL cluster externally (from outside of the project in which it is deployed), by using the TCP Proxy.
Keep in mind that you will be billed for Network Egress when using the TCP Proxy.
Modify the Deployment
Since the containers deployed are based on MySQL images in Docker hub, you can reference the documentation for each, to understand how to customize them using environment variables.
We also encourage you to fork the MySQL Cluster repository to make changes not supported by environment variables.
Backups and Observability
Especially for production environments, performing regular backups and monitoring the health of your database is essential. Consider adding:
-
Backup solutions: Automate regular backups to ensure data recovery in case of failure. We suggest checking out this Database S3 backups template as an example.
-
Observability: Implement monitoring for insights into performance and health of your databases. If you're not already running an observability stack, check out these templates to help you get started building one:
Additional Resources
While these templates are available for your convenience, they are considered unmanaged, meaning you have total control over their configuration and maintenance.
We strongly encourage you to refer to the source documentation to gain deeper understanding of their functionality and how to use them effectively. Here are some links to help you get started:
Edit this file on GitHub