open static method

Future<MysqlDatabase> open({
  1. required String host,
  2. int port = 3306,
  3. required String database,
  4. required String username,
  5. required String password,
  6. bool useTls = true,
  7. SecurityContext? tlsContext,
  8. bool allowPublicKeyRetrieval = false,
  9. Duration connectTimeout = const Duration(seconds: 10),
  10. String collation = 'utf8mb4_general_ci',
  11. int preparedStatementCacheSize = 32,
})

Opens and owns one MySQL or MariaDB connection.

TLS is enabled by default and always rejects an invalid certificate. Disable it only for an explicitly trusted transport, such as a local development socket or tunnel.

Implementation

static Future<MysqlDatabase> open({
  required String host,
  int port = 3306,
  required String database,
  required String username,
  required String password,
  bool useTls = true,
  SecurityContext? tlsContext,
  bool allowPublicKeyRetrieval = false,
  Duration connectTimeout = const Duration(seconds: 10),
  String collation = 'utf8mb4_general_ci',
  int preparedStatementCacheSize = 32,
}) async {
  _validateConnectionOptions(
    port: port,
    connectTimeout: connectTimeout,
    collation: collation,
    preparedStatementCacheSize: preparedStatementCacheSize,
  );

  return MysqlDatabase._connection(
    await _openConnection(
      host: host,
      port: port,
      database: database,
      username: username,
      password: password,
      useTls: useTls,
      tlsContext: tlsContext,
      allowPublicKeyRetrieval: allowPublicKeyRetrieval,
      connectTimeout: connectTimeout,
      collation: collation,
      preparedStatementCacheSize: preparedStatementCacheSize,
    ),
  );
}