# MySQL

> Connect to MySQL from JavaScript and TypeScript with `elide:mysql`: the `elide:postgres` API with `?` placeholders, pooled connections and transactions.

`elide:mysql` connects to a MySQL server through a connection pool, with the same Promise-based API as
[`elide:postgres`](https://elide.help/docs/api-postgres).

```javascript
// my.mjs
import { Database } from "elide:mysql";

// A mysql:// URL: user and password, then host, port and database.
const db = await Database.connect(process.env.DATABASE_URL);
await db.exec("CREATE TABLE IF NOT EXISTS users (id INT AUTO_INCREMENT PRIMARY KEY, name TEXT NOT NULL, age INT)");
console.log(await db.exec("INSERT INTO users (name, age) VALUES (?, ?)", "Ada", 36));
console.log(await (await db.query("SELECT name, age FROM users WHERE age > ?", 18)).all());
await db.close();
```

```bash
elide run my.mjs
```

```text
{affectedRows: 1, insertId: 1, insertIds: [1]}
[{name: "Ada", age: 36}]
```

## Connect

`Database.connect()` takes the same options as [PostgreSQL](https://elide.help/docs/api-postgres#connect), or a `mysql://` URL; the default
port is `3306`. `ssl.key` is ignored. Connection failures reject with `Failed to connect to MySQL: …`.

## Differences from PostgreSQL

| | MySQL | PostgreSQL |
| --- | --- | --- |
| Placeholders | `?` only; `$1` fails with `Parameter index out of range` | `$1`, `$2`, … or `?` |
| Default port | `3306` | `5432` |
| `ssl.key` | Ignored | Client key path |

Queries, rows, statements, batches, transactions and `poolStats` work as on the
[PostgreSQL page](https://elide.help/docs/api-postgres#queries-and-rows).

## Related

- [PostgreSQL](https://elide.help/docs/api-postgres): the shared API in full.
- [Databases](https://elide.help/docs/api-database): choosing a driver.
