

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# AWS Secrets Manager シークレットの認証情報を使用して JDBC を使用して SQL データベースに接続する
<a name="retrieving-secrets_jdbc"></a>

Java アプリケーションでは、Secrets Manager SQL 接続ドライバーを使用して、Secrets Manager に保存された認証情報を用いて MySQL、PostgreSQL、Oracle、MSSQLServer、Db2、Redshift のデータベースに接続できます。各ドライバーはベース JDBC ドライバーをラップしているため、JDBC 呼び出しを使用してデータベースにアクセスすることができます。ただし、接続用のユーザーネームとパスワードを渡す代わりに、シークレットの ID を指定します。ドライバーは、Secrets Manager を呼び出してシークレット値を取得してから、シークレット内の認証情報と接続情報を使用してデータベースに接続します。また、ドライバーは [Java のクライアント側のキャッシュライブラリ](retrieving-secrets_cache-java.md)を使用して認証情報をキャッシュするため、その後の接続では Secrets Manager を呼び出す必要はありません。デフォルトでは、1 時間ごと、およびシークレットがローテーションされたときに、キャッシュが更新されます。キャッシュを設定するには、[SecretCacheConfiguration](retrieving-secrets_cache-java-ref_SecretCacheConfiguration.md) を参照してください。

[GitHub](https://github.com/aws/aws-secretsmanager-jdbc ) からソースコードをダウンロードすることができます。

Secrets Manager SQL 接続ドライバーを使用するには、以下が必要です。
+ アプリケーションが Java 8 以降である必要があります。
+ シークレットが次のいずれかである必要があります。
  + [期待される JSON 構造のデータベースシークレット](reference_secret_json_structure.md)。シークレットの形式を確認するには、Secrets Manager コンソールで、シークレットを表示して **[Retrieve secret value]** を選択します。または、 で AWS CLI get[get-secret-value](https://docs.aws.amazon.com/cli/latest/reference/secretsmanager/get-secret-value.html)。
  + Amazon RDS [マネージドシークレット](integrating_how-services-use-secrets_RDS.md)。このタイプのシークレットでは、接続を確立するときにエンドポイントとポートを指定する必要があります。
  + Amazon Redshift [マネージドシークレット](integrating_how-services-use-secrets_RS.md)。このタイプのシークレットでは、接続を確立するときにエンドポイントとポートを指定する必要があります。

データベースが他のリージョンにレプリケートされている場合、別のリージョンのレプリカデータベースに接続するには、接続の作成時にリージョンのエンドポイントとポートを指定します。リージョン接続情報は、追加のキー/値のペア、SSM パラメータストアパラメータ、またはコード構成でシークレットに格納できます。

ドライバーをプロジェクトに追加するには、Maven ビルドファイル `pom.xml` で、次のドライバーの依存関係を追加します。詳細については、Maven Central Repository の web サイトの「[Secrets Manager SQL Connection Library](https://search.maven.org/artifact/com.amazonaws.secretsmanager/aws-secretsmanager-jdbc)」(Secrets Manager SQL 接続ライブラリ) を参照してください。

```
<dependency>
    <groupId>com.amazonaws.secretsmanager</groupId>
    <artifactId>aws-secretsmanager-jdbc</artifactId>
    <version>1.0.12</version>
</dependency>
```

このドライバーでは[https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/credentials.html](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/credentials.html)を使用します。Amazon EKS でドライバーを実行すると、サービスアカウントロールの代わりに、実行中のノードの認証情報が取得される可能性があります。これに対処するには、`com.amazonaws:aws-java-sdk-sts` のバージョン 1 を Gradle または Maven プロジェクトファイルに依存関係として追加します。

ファイルで DNS AWS PrivateLink エンドポイント URL とリージョンを設定するには`secretsmanager.properties`:

```
drivers.vpcEndpointUrl = endpoint URL
drivers.vpcEndpointRegion = endpoint region
```

プライマリリージョンをオーバーライドするには、`AWS_SECRET_JDBC_REGION` 環境変数を設定するか、`secretsmanager.properties` ファイルに次の変更を加えます。

```
drivers.region = region
```

**必要な許可:**
+ `secretsmanager:DescribeSecret`
+ `secretsmanager:GetSecretValue`

詳細については、「[アクセス許可に関するリファレンス](auth-and-access.md#reference_iam-permissions)」を参照してください。

**Topics**
+ [データベースへの接続を確立する](#retrieving-secrets_jdbc_example)
+ [エンドポイントとポートを指定して接続を確立する](#retrieving-secrets_jdbc_example_replica)
+ [c3p0 接続プールを使用して接続を確立する](#retrieving-secrets_jdbc_example_c3po)
+ [c3p0 接続プールを使用して、エンドポイントとポートを指定して接続を確立する](#retrieving-secrets_jdbc_example_c3p0_replica)

## データベースへの接続を確立する
<a name="retrieving-secrets_jdbc_example"></a>

次の例では、シークレット内の認証情報と接続情報を使用してデータベースへの接続を確立する方法を示しています。接続が確立すると、JDBC 呼び出しを使用してデータベースにアクセスすることができます。詳細については、Java ドキュメントのウェブサイトの「[JDBC Basics](https://docs.oracle.com/javase/tutorial/jdbc/basics/index.html)」(JDBC の基本) を参照してください。

------
#### [ MySQL ]

```
// Load the JDBC driver
Class.forName( "com.amazonaws.secretsmanager.sql.AWSSecretsManagerMySQLDriver" ).newInstance();

// Retrieve the connection info from the secret using the secret ARN
String URL = "secretId";

// Populate the user property with the secret ARN to retrieve user and password from the secret
Properties info = new Properties( );
info.put( "user", "secretId" );

// Establish the connection
conn = DriverManager.getConnection(URL, info);
```

------
#### [ PostgreSQL ]

```
// Load the JDBC driver
Class.forName( "com.amazonaws.secretsmanager.sql.AWSSecretsManagerPostgreSQLDriver" ).newInstance();

// Retrieve the connection info from the secret using the secret ARN
String URL = "secretId";

// Populate the user property with the secret ARN to retrieve user and password from the secret
Properties info = new Properties( );
info.put( "user", "secretId" );

// Establish the connection
conn = DriverManager.getConnection(URL, info);
```

------
#### [ Oracle ]

```
// Load the JDBC driver
Class.forName( "com.amazonaws.secretsmanager.sql.AWSSecretsManagerOracleDriver" ).newInstance();

// Retrieve the connection info from the secret using the secret ARN
String URL = "secretId";

// Populate the user property with the secret ARN to retrieve user and password from the secret
Properties info = new Properties( );
info.put( "user", "secretId" );

// Establish the connection
conn = DriverManager.getConnection(URL, info);
```

------
#### [ MSSQLServer ]

```
// Load the JDBC driver
Class.forName( "com.amazonaws.secretsmanager.sql.AWSSecretsManagerMSSQLServerDriver" ).newInstance();

// Retrieve the connection info from the secret using the secret ARN
String URL = "secretId";

// Populate the user property with the secret ARN to retrieve user and password from the secret
Properties info = new Properties( );
info.put( "user", "secretId" );

// Establish the connection
conn = DriverManager.getConnection(URL, info);
```

------
#### [ Db2 ]

```
// Load the JDBC driver
Class.forName( "com.amazonaws.secretsmanager.sql.AWSSecretsManagerDb2Driver" ).newInstance();

// Retrieve the connection info from the secret using the secret ARN
String URL = "secretId";

// Populate the user property with the secret ARN to retrieve user and password from the secret
Properties info = new Properties( );
info.put( "user", "secretId" );

// Establish the connection
conn = DriverManager.getConnection(URL, info);
```

------
#### [ Redshift ]

```
// Load the JDBC driver
Class.forName( "com.amazonaws.secretsmanager.sql.AWSSecretsManagerRedshiftDriver" ).newInstance();

// Retrieve the connection info from the secret using the secret ARN
String URL = "secretId";

// Populate the user property with the secret ARN to retrieve user and password from the secret
Properties info = new Properties( );
info.put( "user", "secretId" );

// Establish the connection
conn = DriverManager.getConnection(URL, info);
```

------

## エンドポイントとポートを指定して接続を確立する
<a name="retrieving-secrets_jdbc_example_replica"></a>

次の例は、シークレット内の認証情報を使用して、指定したエンドポイントとポートでデータベースへの接続を確立する方法を示しています。

[Amazon RDS マネージドシークレット](integrating_how-services-use-secrets_RDS.md)には、データベースのエンドポイントおよびポートは含まれていません。Amazon RDS が管理するシークレットのマスター認証情報を使用してデータベースに接続するには、コードでマスター認証情報を指定します。

[他のリージョンにレプリケートされるシークレット](replicate-secrets.md)は、リージョンデータベースへの接続のレイテンシーを改善できますが、ソースシークレット以外の接続情報を保持しません。各レプリカは、ソースシークレットのコピーです。リージョン接続情報をシークレットに保存するには、リージョンのエンドポイントとポート情報のキー/値のペアを追加します。

接続が確立すると、JDBC 呼び出しを使用してデータベースにアクセスすることができます。詳細については、Java ドキュメントのウェブサイトの「[JDBC Basics](https://docs.oracle.com/javase/tutorial/jdbc/basics/index.html)」(JDBC の基本) を参照してください。

------
#### [ MySQL ]

```
// Load the JDBC driver
Class.forName( "com.amazonaws.secretsmanager.sql.AWSSecretsManagerMySQLDriver" ).newInstance();

// Set the endpoint and port. You can also retrieve it from a key/value pair in the secret.
String URL = "jdbc-secretsmanager:mysql://example.com:3306";

// Populate the user property with the secret ARN to retrieve user and password from the secret
Properties info = new Properties( );
info.put( "user", "secretId" );

// Establish the connection
conn = DriverManager.getConnection(URL, info);
```

------
#### [ PostgreSQL ]

```
// Load the JDBC driver
Class.forName( "com.amazonaws.secretsmanager.sql.AWSSecretsManagerPostgreSQLDriver" ).newInstance();

// Set the endpoint and port. You can also retrieve it from a key/value pair in the secret.
String URL = "jdbc-secretsmanager:postgresql://example.com:5432/database";

// Populate the user property with the secret ARN to retrieve user and password from the secret
Properties info = new Properties( );
info.put( "user", "secretId" );

// Establish the connection
conn = DriverManager.getConnection(URL, info);
```

------
#### [ Oracle ]

```
// Load the JDBC driver
Class.forName( "com.amazonaws.secretsmanager.sql.AWSSecretsManagerOracleDriver" ).newInstance();

// Set the endpoint and port. You can also retrieve it from a key/value pair in the secret.
String URL = "jdbc-secretsmanager:oracle:thin:@example.com:1521/ORCL";

// Populate the user property with the secret ARN to retrieve user and password from the secret
Properties info = new Properties( );
info.put( "user", "secretId" );

// Establish the connection
conn = DriverManager.getConnection(URL, info);
```

------
#### [ MSSQLServer ]

```
// Load the JDBC driver
Class.forName( "com.amazonaws.secretsmanager.sql.AWSSecretsManagerMSSQLServerDriver" ).newInstance();

// Set the endpoint and port. You can also retrieve it from a key/value pair in the secret.
String URL = "jdbc-secretsmanager:sqlserver://example.com:1433";

// Populate the user property with the secret ARN to retrieve user and password from the secret
Properties info = new Properties( );
info.put( "user", "secretId" );

// Establish the connection
conn = DriverManager.getConnection(URL, info);
```

------
#### [ Db2 ]

```
// Load the JDBC driver
Class.forName( "com.amazonaws.com.rproxy.goskope.com.amazonaws.secretsmanager.sql.AWSSecretsManagerDb2Driver" ).newInstance();

// Set the endpoint and port. You can also retrieve it from a key/value pair in the secret.
String URL = "jdbc-secretsmanager:db2://example.com:50000";

// Populate the user property with the secret ARN to retrieve user and password from the secret
Properties info = new Properties( );
info.put( "user", "secretId" );

// Establish the connection
conn = DriverManager.getConnection(URL, info);
```

------
#### [ Redshift ]

```
// Load the JDBC driver
Class.forName( "com.amazonaws.com.rproxy.goskope.com.amazonaws.secretsmanager.sql.AWSSecretsManagerRedshiftDriver" ).newInstance();

// Set the endpoint and port. You can also retrieve it from a key/value pair in the secret.
String URL = "jdbc-secretsmanager:redshift://example.com:5439";

// Populate the user property with the secret ARN to retrieve user and password from the secret
Properties info = new Properties( );
info.put( "user", "secretId" );

// Establish the connection
conn = DriverManager.getConnection(URL, info);
```

------

## c3p0 接続プールを使用して接続を確立する
<a name="retrieving-secrets_jdbc_example_c3po"></a>

次の例は、ドライバーを使用してシークレットから認証情報および接続情報を取得する `c3p0.properties` ファイルで接続プールを確立する方法を示しています。`user` と `jdbcUrl` には、シークレット ID を入力して接続プールを設定します。その後、プールから接続を取得し、他のデータベース接続として使用することができます。詳細については、Java ドキュメントのウェブサイトの「[JDBC Basics](https://docs.oracle.com/javase/tutorial/jdbc/basics/index.html)」(JDBC の基本) を参照してください。

c3p0 の詳細については、Machinery For Change のウェブサイトの「[c3p0](https://www.mchange.com/projects/c3p0/)」を参照してください。

------
#### [ MySQL ]

```
c3p0.user=secretId
c3p0.driverClass=com.amazonaws.secretsmanager.sql.AWSSecretsManagerMySQLDriver
c3p0.jdbcUrl=secretId
```

------
#### [ PostgreSQL ]

```
c3p0.user=secretId
c3p0.driverClass=com.amazonaws.secretsmanager.sql.AWSSecretsManagerPostgreSQLDriver
c3p0.jdbcUrl=secretId
```

------
#### [ Oracle ]

```
c3p0.user=secretId
c3p0.driverClass=com.amazonaws.secretsmanager.sql.AWSSecretsManagerOracleDriver
c3p0.jdbcUrl=secretId
```

------
#### [ MSSQLServer ]

```
c3p0.user=secretId
c3p0.driverClass=com.amazonaws.secretsmanager.sql.AWSSecretsManagerMSSQLServerDriver
c3p0.jdbcUrl=secretId
```

------
#### [ Db2 ]

```
c3p0.user=secretId
c3p0.driverClass=com.amazonaws.secretsmanager.sql.AWSSecretsManagerDb2Driver
c3p0.jdbcUrl=secretId
```

------
#### [ Redshift ]

```
c3p0.user=secretId
c3p0.driverClass=com.amazonaws.secretsmanager.sql.AWSSecretsManagerRedshiftDriver
c3p0.jdbcUrl=secretId
```

------

## c3p0 接続プールを使用して、エンドポイントとポートを指定して接続を確立する
<a name="retrieving-secrets_jdbc_example_c3p0_replica"></a>

次の例は、ドライバーを使用して指定されたエンドポイントとポートでシークレットの認証情報を取得する `c3p0.properties` ファイルで接続プールを確立する方法を示しています。その後、プールから接続を取得し、他のデータベース接続として使用することができます。詳細については、Java ドキュメントのウェブサイトの「[JDBC Basics](https://docs.oracle.com/javase/tutorial/jdbc/basics/index.html)」(JDBC の基本) を参照してください。

[Amazon RDS マネージドシークレット](integrating_how-services-use-secrets_RDS.md)には、データベースのエンドポイントおよびポートは含まれていません。Amazon RDS が管理するシークレットのマスター認証情報を使用してデータベースに接続するには、コードでマスター認証情報を指定します。

[他のリージョンにレプリケートされるシークレット](replicate-secrets.md)は、リージョンデータベースへの接続のレイテンシーを改善できますが、ソースシークレット以外の接続情報を保持しません。各レプリカは、ソースシークレットのコピーです。リージョン接続情報をシークレットに保存するには、リージョンのエンドポイントとポート情報のキー/値のペアを追加します。

------
#### [ MySQL ]

```
c3p0.user=secretId
c3p0.driverClass=com.amazonaws.secretsmanager.sql.AWSSecretsManagerMySQLDriver
c3p0.jdbcUrl=jdbc-secretsmanager:mysql://example.com:3306
```

------
#### [ PostgreSQL ]

```
c3p0.user=secretId
c3p0.driverClass=com.amazonaws.secretsmanager.sql.AWSSecretsManagerPostgreSQLDriver
c3p0.jdbcUrl=jdbc-secretsmanager:postgresql://example.com:5432/database
```

------
#### [ Oracle ]

```
c3p0.user=secretId
c3p0.driverClass=com.amazonaws.secretsmanager.sql.AWSSecretsManagerOracleDriver
c3p0.jdbcUrl=jdbc-secretsmanager:oracle:thin:@example.com:1521/ORCL
```

------
#### [ MSSQLServer ]

```
c3p0.user=secretId
c3p0.driverClass=com.amazonaws.secretsmanager.sql.AWSSecretsManagerMSSQLServerDriver
c3p0.jdbcUrl=jdbc-secretsmanager:sqlserver://example.com:1433
```

------
#### [ Db2 ]

```
c3p0.user=secretId
c3p0.driverClass=com.amazonaws.secretsmanager.sql.AWSSecretsManagerDb2Driver
c3p0.jdbcUrl=jdbc-secretsmanager:db2://example.com:50000
```

------
#### [ Redshift ]

```
c3p0.user=secretId
c3p0.driverClass=com.amazonaws.secretsmanager.sql.AWSSecretsManagerRedshiftDriver
c3p0.jdbcUrl=jdbc-secretsmanager:redshift://example.com:5439
```

------