---
title: "Deploy MySQL Server"
description: "Production-ready MySQL with persistent storage and TCP access."
category: "Storage"
url: https://railway.com/deploy/mysql-server
---

# Deploy MySQL Server

Production-ready MySQL with persistent storage and TCP access.

**[Deploy MySQL Server on Railway](https://railway.com/template/mysql-server)**

- **Creator:** INF Labs
- **Category:** Storage

## Template content

### mysql https://devicons.railway.com/i/mysql.svg

- **Image:** mysql:latest

## Documentation

# Deploy and Host MySQL Server on Railway

MySQL is a widely used open-source relational database built for web applications, APIs, backend services, transactional systems, and general-purpose data storage. It provides SQL, ACID transactions, indexing, replication, stored procedures, JSON support, and broad compatibility across modern application frameworks and development tools.

## About Hosting MySQL Server

Hosting MySQL on Railway gives you a persistent relational database without manually managing a virtual machine, operating system, package installation, or database server lifecycle.

This template runs MySQL as a dedicated database service with persistent Railway storage. Applications inside the same Railway project can connect through Railway private networking, while external database clients can connect through Railway TCP Proxy.

The deployment also creates a dedicated application database and user alongside the root administrator account, making it easier to separate normal application access from database administration.

## Common Use Cases

* Backend databases for web and mobile applications
* API and microservice data storage
* SaaS application databases
* E-commerce and CMS platforms
* Transactional business systems
* Development and testing environments
* Applications built with Laravel, Node.js, Django, Java, PHP, and other frameworks

## Dependencies for MySQL Hosting

* Official MySQL Docker image
* Persistent Railway volume mounted at `/var/lib/mysql`
* Railway private networking for internal connections
* Optional Railway TCP Proxy for external access

### Implementation Details

MySQL communicates using its native wire protocol on:

```text
TCP 3306
```

Database files are stored persistently at:

```text
/var/lib/mysql
```

A Railway volume is mounted at this location so databases, tables, indexes, users, and other database state remain available across redeployments and container restarts.

## MySQL Architecture on Railway

```text
Application / Backend
        │
        │ Railway Private Network
        ▼
     MySQL :3306
        │
        ▼
 Railway Volume
 /var/lib/mysql
```

External tools connect through Railway TCP Proxy:

```text
DBeaver / Workbench / CLI
          │
          │ TCP Proxy
          ▼
       MySQL :3306
```

MySQL is not an HTTP service, so a standard Railway public domain is not used for database connections.

## Database Users

This template initializes two access levels:

* **Root user** — intended for database administration
* **Application user** — intended for normal application connections

Applications should normally use the dedicated application user instead of the root account.

This reduces unnecessary privilege exposure and makes access management cleaner.

## Database Connection

### Applications Inside Railway

Applications running inside the same Railway project should use the private database connection URL:

```text
${{MySQL-Server.DATABASE_URL}}
```

This connection stays inside Railway private networking and uses the internal MySQL port `3306`.

### External Applications and Database Clients

For applications outside Railway, use:

```text
${{MySQL-Server.DATABASE_PUBLIC_URL}}
```

This connection uses Railway TCP Proxy.

Compatible tools include:

* MySQL Workbench
* DBeaver
* DataGrip
* TablePlus
* HeidiSQL
* MySQL CLI
* application drivers for Node.js, Python, Java, PHP, Go, and other languages

## Connection Details

A typical MySQL connection contains:

```text
Host
Port
Database
Username
Password
```

Internal Railway services connect directly on:

```text
3306
```

External clients use the hostname and dynamically assigned port provided by Railway TCP Proxy.

Example URI structure:

```text
mysql://USERNAME:PASSWORD@HOST:PORT/DATABASE
```

## Testing MySQL

After connecting, verify the MySQL version:

```sql
SELECT VERSION();
```

Check the active database:

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

Create a simple table:

```sql
CREATE TABLE users (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    email VARCHAR(255) UNIQUE NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```

Insert a record:

```sql
INSERT INTO users (name, email)
VALUES ('Example User', 'user@example.com');
```

Query the table:

```sql
SELECT *
FROM users;
```

## MySQL Features

| Capability                      | MySQL |
| ------------------------------- | :---: |
| Open-source relational database |   ✅   |
| SQL support                     |   ✅   |
| ACID transactions               |   ✅   |
| JSON support                    |   ✅   |
| Secondary indexes               |   ✅   |
| Full-text search                |   ✅   |
| Views                           |   ✅   |
| Stored procedures               |   ✅   |
| Triggers                        |   ✅   |
| Replication                     |   ✅   |
| Role and user management        |   ✅   |
| Persistent Railway storage      |   ✅   |
| Railway private networking      |   ✅   |
| External TCP access             |   ✅   |

MySQL is particularly well suited to application workloads that require a mature relational database with broad framework and tooling compatibility.

## MySQL vs Similar Relational Databases

| Feature                       | MySQL | PostgreSQL | MariaDB | SQLite |
| ----------------------------- | :---: | :--------: | :-----: | :----: |
| Relational SQL                |   ✅   |      ✅     |    ✅    |    ✅   |
| ACID transactions             |   ✅   |      ✅     |    ✅    |    ✅   |
| JSON support                  |   ✅   |      ✅     |    ✅    |    ✅   |
| Multi-user database server    |   ✅   |      ✅     |    ✅    |    ❌   |
| Stored procedures             |   ✅   |      ✅     |    ✅    |    ❌   |
| Replication support           |   ✅   |      ✅     |    ✅    |    ❌   |
| MySQL ecosystem compatibility |   ✅   |      ❌     |    ✅    |    ❌   |
| Strong extension ecosystem    |   ❌   |      ✅     |    ❌    |    ❌   |
| Embedded single-file database |   ❌   |      ❌     |    ❌    |    ✅   |

MySQL is a strong choice when compatibility with the MySQL ecosystem, web frameworks, CMS platforms, and conventional application stacks is important.

PostgreSQL generally offers more advanced extensibility and query capabilities, while MariaDB provides a MySQL-compatible open-source alternative with its own feature set. SQLite is better suited to embedded and lightweight local workloads.

## Persistent Storage

A Railway volume keeps MySQL data persistent across deployments.

Persistent data includes:

* databases
* tables
* indexes
* users
* privileges
* stored procedures
* triggers
* schemas
* transaction state

Without persistent storage, database data would be lost when the container is replaced.

## Security and Access

For safer application deployments:

* use Railway private networking whenever possible
* use the dedicated application account instead of root
* keep the root password for administrative tasks
* enable Railway TCP Proxy only when external access is required
* create additional users with restricted privileges for separate applications

## Getting Started After Deployment

1. Deploy the MySQL Server template.
2. Wait for the database service to become available.
3. Use `DATABASE_URL` for applications running inside Railway.
4. Use `DATABASE_PUBLIC_URL` for external database connections.
5. Connect with MySQL Workbench, DBeaver, DataGrip, TablePlus, or your application.
6. Verify the server with:

```sql
SELECT VERSION();
```

7. Create your application tables, indexes, users, and data as required.

## Why Deploy MySQL Server on Railway?

Railway is a singular platform to deploy your infrastructure stack. Railway will host your infrastructure so you don't have to deal with configuration, while allowing you to vertically and horizontally scale it.

By deploying MySQL Server on Railway, you get persistent storage, private networking, optional external TCP access, and ready-to-use connection details for modern application and backend workloads.


## Similar templates

- [Garage S3 Storage](https://railway.com/deploy/garage-s3-storage) — Ultra-light S3 server: fast, open-source, plug-and-play.
- [Redis](https://railway.com/deploy/redis-1) — Self Host Latest Redis with Railway
- [EasyImg](https://railway.com/deploy/easyimg) — Simple self-hostable Nuxt.js personal image hosting system.

Open this page in a browser: https://railway.com/deploy/mysql-server
