20211123のMySQLに関する記事は2件です。

【NestJS】Docker ComposeでDBとAdminerを起動してアプリケーションと接続する

はじめに Docker ComposeでDB(MySQLとPostgreSQL)とAdminer(DB管理ツール)を起動して、NestJSアプリケーションと接続してみました。 本記事はその手順のメモです。 実装 実装は大きく分けてコンテナの起動とNestJSアプリケーションの接続の2つです。 コンテナの起動 MySQL, PostgreSQL, AdminerのDockerイメージは公式のものを使用します。 これらをDocker Composeで一発で起動できるように、docker-compose.ymlを以下のように記述します。 docker-compose.yml version: "3.8" services: mysql: image: mysql:8.0.23 command: --default-authentication-plugin=mysql_native_password restart: always environment: MYSQL_ROOT_PASSWORD: example ports: - 3306:3306 postgres: image: postgres:13.1 restart: always environment: POSTGRES_PASSWORD: example ports: - 5432:5432 adminer: image: adminer restart: always ports: - 8080:8080 docker-compose upを実行し、localhost::8080にアクセスするとAdminerが開きます。 以下の情報を入力するとMySQLとPostgreSQLにログインすることができます。 データベース種類:MySQL (PostgreSQL) サーバ:mysql (postgres) ユーザ名:root (postgres) パスワード:example MySQLとPostgresQLでデータベースnest-eventsを作成します。 NestJSアプリケーションの接続 MySQLとの接続についてのみ記述します。 アプリケーションからDBを操作するにあたり、ORMとしてTypeORMを使用します。 そのため、まずは以下のパッケージをインストールします。 npm install @nestjs/typeorm typeorm mysql 次に、app.module.ts内でTypeOrmの設定を行います。 databaseに前項で作成したnest-events、entitiesにEventを記述することで、DBとEntityの紐付け(マッピング)を行います。 app.module.ts @Module({ imports: [ TypeOrmModule.forRoot({ type: 'mysql', host: '127.0.0.1', port: 3306, username: 'root', password: 'example', database: 'nest-events', entities: [Event], synchronize: true }), ], controllers: [AppController, EventsController], providers: [AppService], }) EntityであるEventは以下のように記述します。 各カラムにデコレータを付加することで、属性を指定しています。 @PrimaryGeneratedColumn()を付加すると、MySQLでいうAUTO_INCREMENTなカラムとなります。 event.entity.ts import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; @Entity() export class Event { @PrimaryGeneratedColumn() id: number; @Column() name: string; @Column() description: string; @Column() when: Date; @Column() address: string; } app.module.tsでsynchronize: trueを指定していたため、npm run start:devでアプリケーションを起動すると、以下のように自動的にテーブルが作られます。 参考資料
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

JDBC経由でMySQLに突然接続できなくなった

事象 アプリケーション起動後、DB接続するとcom.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure 発生。 InteliJのバージョンを上げた後、JDBC経由でMySQLに接続できなくなった。その前までは接続できていた。 MySQLには、terminalからクライアントから接続できることは確認できている。 環境 MySQL 5.7 MySQL Connector 8.0.18 IntelliJのRuntime JDK11 ログ com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.18.jar:8.0.18] at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.18.jar:8.0.18] ~ Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. ~ ... 75 common frames omitted Caused by: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate) at java.base/sun.security.ssl.HandshakeContext.<init>(HandshakeContext.java:172) ~[na:na] at java.base/sun.security.ssl.ClientHandshakeContext.<init>(ClientHandshakeContext.java:103) ~[na:na] at java.base/sun.security.ssl.TransportContext.kickstart(TransportContext.java:221) ~[na:na] at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:434) ~[na:na] at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:412) ~[na:na] at com.mysql.cj.protocol.ExportControlled.performTlsHandshake(ExportControlled.java:316) ~[mysql-connector-java-8.0.18.jar:8.0.18] at com.mysql.cj.protocol.StandardSocketFactory.performTlsHandshake(StandardSocketFactory.java:188) ~[mysql-connector-java-8.0.18.jar:8.0.18] at com.mysql.cj.protocol.a.NativeSocketConnection.performTlsHandshake(NativeSocketConnection.java:99) ~[mysql-connector-java-8.0.18.jar:8.0.18] at com.mysql.cj.protocol.a.NativeProtocol.negotiateSSLConnection(NativeProtocol.java:329) ~[mysql-connector-java-8.0.18.jar:8.0.18] 原因 Caused by: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate) JDK11で TLSの1.0と1.1がdisableになっている。defaultはTLS1.3 https://www.java.com/en/configure_crypto.html#DisableTLS MySQLのConnectorでは、TLS1.2とそれより高いバージョンはdefaultでは使用できない https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-reference-using-ssl.html つまり JDBCでTLS1.3で接続しようとしたが、MySQLのConnectorでTLS1.3はデフォルトで利用できないため、エラーになった。 以下の通り、今回の組み合わせ(connctor8.0.18/MySQL5.7)では、TLSv1.2を指定可能とあるので1.2を指定すれば良い。 For Connector/J 8.0.18 and earlier when connecting to MySQL Community Server 5.6 and 5.7 using the JDBC API: Due to compatibility issues with MySQL Server compiled with yaSSL, Connector/J does not enable connections with TLSv1.2 and higher by default. When connecting to servers that restrict connections to use those higher TLS versions, enable them explicitly by setting the Connector/J connection property enabledTLSProtocols (e.g., set enabledTLSProtocols=TLSv1,TLSv1.1,TLSv1.2). 関連 https://stackoverflow.com/questions/67332909/why-can-java-not-connect-to-mysql-5-7-after-the-latest-jdk-update-and-how-should 対応 jdbc接続している箇所に以下のように ?enabledTLSProtocols=TLSv1.2 をつけて明示的にTLSの利用バージョンを指定すればOK jdbc:mysql://localhost:3306/testdb?enabledTLSProtocols=TLSv1.2
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む