Skip to content
TypeScript
2.7 KB
Raw Blame
---
title: MySQL
description: Official MySQL in a Project. Bind Railway.ConnectMySQL and query with Drizzle or SQL.
---

A [`Railway.MySQL`](/providers/railway/mysql) is MySQL as a Service:
the official `mysql:9` image, a Volume at `/var/lib/mysql`, `MYSQL_*`
/ `MYSQL_URL` variables, and an optional TCP proxy for the public URL.
The IaC helper is `Railway.mysql`.

Private hostname is `{name}.railway.internal`. From a
[Service](/railway/compute/services), yield
[`ConnectMySQL`](/providers/railway/connectmysql). From a laptop, use
`publicConnectionUri`.

## Create MySQL

Pass a Project. Alchemy generates a unique name, password, volume,
and a public TCP proxy.

```typescript
const site = yield* Railway.Project("Site");
const db = yield* Railway.MySQL("Db", { project: site });
```

:::caution[Changing `project` replaces MySQL]
A new service + volume are created in the new Project. The old
service and volume are deleted. Data is not copied.
:::

## Connect from a Service

Yield `ConnectMySQL` in the Service's constructor. Provide
`ConnectMySQLHttp`. Pass `conn.connectionString` to Drizzle or SQL.

```typescript
import * as Drizzle from "@alchemy.run/drizzle/MySQL";
import * as HttpServerResponse from "effect/http/HttpServerResponse";

export default class Api extends Railway.Service<Api>()(
  "Api",
  {
    project: Site,
    main: import.meta.url,
    build: { install: ["mysql2"] },
  },
  Effect.gen(function* () {
    const conn = yield* Railway.ConnectMySQL(Db);
    const db = yield* Drizzle.MySQL(conn.connectionString);
    return {
      fetch: Effect.gen(function* () {
        const rows = yield* db.execute("select 1 as ok", "objects");
        return HttpServerResponse.json({ rows });
      }),
    };
  }).pipe(Effect.provide(Railway.ConnectMySQLHttp)),
) {}
```

`connectionString` is the private URI
(`{name}.railway.internal:3306`). Packed into the Service env — not
`Config.Redacted`.

To store Railway's `${{Db.MYSQL_URL}}` template instead of a
resolved URI, pass [`Railway.ref`](/providers/railway/ref)
(`Railway.ref(Db, "MYSQL_URL")`) as a
[Variable](/railway/data/variables) `value`.

## Public TCP

`public` (default `true`) creates a TCP proxy on 3306.
`publicConnectionUri` is `{domain}:{proxyPort}` for laptop access.

```typescript
const db = yield* Railway.MySQL("Db", {
  project: site,
  public: false,
});
```

## Where next

[Postgres](/railway/data/postgres) is the same shape for Postgres.
[Mongo](/railway/data/mongo) is MongoDB.
[TcpProxy](/railway/networking#tcp-proxies) is the public TCP
resource.
The [`MySQL` reference](/providers/railway/mysql) lists every prop.
The [`ConnectMySQL` reference](/providers/railway/connectmysql) is
the runtime binding.