Aiven for MySQL x Deno Deploy【無料MySQL】

まずは、AivenでMySQLを作ろう

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/435735/7dbea73b-6384-b86d-6853-5803f26350e3.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/435735/3bfd77a2-b64c-650d-ce33-e273e470410b.png)

:::note warn
CAないと **ERROR 1045 (28000): Access denied for user** エラーになるので気を付けて
:::



hostnameとpasswordとcaは適宜書き換えてね！上の画面のコピーボダンでコピーできるよ

# MySQLを使う場合

```typescript
import { Client, TLSConfig, TLSMode } from "https://deno.land/x/mysql/mod.ts";

const ca = `-----BEGIN CERTIFICATE-----
MIIEQTCCAqmgAwIBAgIUIE49Fj6ZV6CZddI+nHp24GfarXUwDQYJKoZIhvcNAQEM
BQAwOjE4MDYGA1UEAwwvMTIxNjVhYWQtZTNjNi00YWIzLTgwZmMtM2JkM2VlNjUz
OWI5IFByb2plY3QgQ0EwHhcNMjQwMjI4MTEyNzAyWhcNMzQwMjI1MTEyNzAyWjA6
MTgwNgYDVQQDDC8xMjE2NWFhZC1lM2M2LTRhYjMtODBmYy0zYmQzZWU2NTM5Yjkg
UHJvamVjdCBDQTCCAaIwDQYJKoZIhvcNAQEBBQADggGPADCCAYoCggGBAN+F0r/P
aW1qGoDXK8IgoFm7ok5eaJPHbcvbK6sjJ9bNFxxMvQQy5luEc9mbbUJm8lThwcYw
+J/ybM8WfT++hJwDEFMEHY3LHAfzr90lFq4kr6zNLouL1RpGdnJAsqsgiXPy7yoP
k6ulCrqSJGsPh7G3aCEGkoWfvMzwbbXinzVm54G9svu/Mcx2reqiM691xCXpaSLx
u7GT0ZaNgGFO7ZRPrRQ7AF8FQ0QMHnRsCp1V8o9Jwo05lA9XCfFQXXpvuqMWBC8H
m1FRSsXRPeEvrRzn9Jfe8Z/iYqwPCYFndhOs6uK8LhLDDxECWL5b6/57ndaq/fwS
ieG/WK0wukymPymWCP2GonO3Y5StSJx3WwkICuAzaZucbgjaeJi2mdvq4HRcpOgQ
hLsBWf29PiVThrYPhesKh0z5ZqfAf/RS6E9mpGX3wDinqMkYGIKynv9+ZtFsSVe8
8fVtNOT9GdigpBTZbnW0kqjMRgEmA+qK4gKsQ7eePlysdo23u0runSQQSwIDAQAB
oz8wPTAdBgNVHQ4EFgQUvIb4N49ZkYxP5VDSD0wl4Qh8olkwDwYDVR0TBAgwBgEB
/wIBADALBgNVHQ8EBAMCAQYwDQYJKoZIhvcNAQEMBQADggGBAKcr4pBoWlvGm5Sc
ezhNSYycKm803GEEtkVEyCE5irXO6MIXLnmN2Hn6W0dViHC8ARA2Lu7O8vsGQ68f
SQswP4bHW6DT27N+GecNn33uYapTMDUGibqVdZtMXc4gMMPJhpg8OOMm7MgazPQk
QJm8RXJ6VOtKrhIHUiK2QhTHv5oQYy30XZxbIJyHxYALAda4FTc5cQ1+s/GTtajr
E3xc5BySrf/FZQrEzv6qCNx1/20GqrVZ5o3yzlDLqdDJacrkzs4SFHrHydEER8hZ
ze7lRrn+3P/6rVZdzzm+03ffh1ePvAIFTxpSSG7M0t7CJ1dUeZ71JuhOqtdA5KQa
p+WBZNjz1XkgzP6mfEjbr3oBIBBSbwSzBRn4k2haftxLgJnpyC5bgXKHRbZM+IXQ
VgjIIC2/GV/cN7o7zz0+Oj35BSrZ5ILKBYQBWuyWkH/brtKPB5ci/KyTdhm9ibMz
cM4Rr0plgUZ0wO/RX9WfhnWNtFpLb/lTNtkT1Qr6HxZb8OqeCg==
-----END CERTIFICATE-----
`

const tlsConfig: TLSConfig = {
  mode: TLSMode.VERIFY_IDENTITY,
  caCerts: [ ca ],
};

const connection = await new Client().connect({
  hostname: "apple-apple.a.aivencloud.com",
  port: 17201,
  username: "avnadmin",
  db: "defaultdb",
  password: "YOUR_PASSWORD_HERE",
  tls: tlsConfig,
});

Deno.serve(async (req: Request) => {
    const result = await connection.query(`select NOW()`);
    console.log(result)
    return new Response("Hello World")
});
```

https://deno.land/x/mysql@v2.12.1

https://docs.deno.com/runtime/manual/basics/connecting_to_databases

# MySQL2使う場合


```typescript
import mysql from "npm:mysql2/promise";

const ca = `-----BEGIN CERTIFICATE-----
MIIEQTCCAqmgAwIBAgIUIE49Fj6ZV6CZddI+nHp24GfarXUwDQYJKoZIhvcNAQEM
BQAwOjE4MDYGA1UEAwwvMTIxNjVhYWQtZTNjNi00YWIzLTgwZmMtM2JkM2VlNjUz
OWI5IFByb2plY3QgQ0EwHhcNMjQwMjI4MTEyNzAyWhcNMzQwMjI1MTEyNzAyWjA6
MTgwNgYDVQQDDC8xMjE2NWFhZC1lM2M2LTRhYjMtODBmYy0zYmQzZWU2NTM5Yjkg
UHJvamVjdCBDQTCCAaIwDQYJKoZIhvcNAQEBBQADggGPADCCAYoCggGBAN+F0r/P
aW1qGoDXK8IgoFm7ok5eaJPHbcvbK6sjJ9bNFxxMvQQy5luEc9mbbUJm8lThwcYw
+J/ybM8WfT++hJwDEFMEHY3LHAfzr90lFq4kr6zNLouL1RpGdnJAsqsgiXPy7yoP
k6ulCrqSJGsPh7G3aCEGkoWfvMzwbbXinzVm54G9svu/Mcx2reqiM691xCXpaSLx
u7GT0ZaNgGFO7ZRPrRQ7AF8FQ0QMHnRsCp1V8o9Jwo05lA9XCfFQXXpvuqMWBC8H
m1FRSsXRPeEvrRzn9Jfe8Z/iYqwPCYFndhOs6uK8LhLDDxECWL5b6/57ndaq/fwS
ieG/WK0wukymPymWCP2GonO3Y5StSJx3WwkICuAzaZucbgjaeJi2mdvq4HRcpOgQ
hLsBWf29PiVThrYPhesKh0z5ZqfAf/RS6E9mpGX3wDinqMkYGIKynv9+ZtFsSVe8
8fVtNOT9GdigpBTZbnW0kqjMRgEmA+qK4gKsQ7eePlysdo23u0runSQQSwIDAQAB
oz8wPTAdBgNVHQ4EFgQUvIb4N49ZkYxP5VDSD0wl4Qh8olkwDwYDVR0TBAgwBgEB
/wIBADALBgNVHQ8EBAMCAQYwDQYJKoZIhvcNAQEMBQADggGBAKcr4pBoWlvGm5Sc
ezhNSYycKm803GEEtkVEyCE5irXO6MIXLnmN2Hn6W0dViHC8ARA2Lu7O8vsGQ68f
SQswP4bHW6DT27N+GecNn33uYapTMDUGibqVdZtMXc4gMMPJhpg8OOMm7MgazPQk
QJm8RXJ6VOtKrhIHUiK2QhTHv5oQYy30XZxbIJyHxYALAda4FTc5cQ1+s/GTtajr
E3xc5BySrf/FZQrEzv6qCNx1/20GqrVZ5o3yzlDLqdDJacrkzs4SFHrHydEER8hZ
ze7lRrn+3P/6rVZdzzm+03ffh1ePvAIFTxpSSG7M0t7CJ1dUeZ71JuhOqtdA5KQa
p+WBZNjz1XkgzP6mfEjbr3oBIBBSbwSzBRn4k2haftxLgJnpyC5bgXKHRbZM+IXQ
VgjIIC2/GV/cN7o7zz0+Oj35BSrZ5ILKBYQBWuyWkH/brtKPB5ci/KyTdhm9ibMz
cM4Rr0plgUZ0wO/RX9WfhnWNtFpLb/lTNtkT1Qr6HxZb8OqeCg==
-----END CERTIFICATE-----
`

const connection = await mysql.createConnection({
  host: "apple-apple.a.aivencloud.com",
  port: 17201,
  user: "avnadmin",
  password: "YOUR_PASSWORD_HERE",
  database: "defaultdb",
  ssl: { ca },
});

Deno.serve(async (req: Request) => {
    const result = await connection.query(`select NOW()`);
    console.log(result)
    return new Response("Hello World")
});
```

https://docs.deno.com/runtime/tutorials/how_to_with_npm/mysql2