createConnection static method

Future<MySQLConnection> createConnection({
  1. required dynamic host,
  2. required int port,
  3. required String userName,
  4. required String password,
  5. bool secure = true,
  6. String? databaseName,
  7. String collation = 'utf8mb4_general_ci',
})

Creates connection with provided options.

Keep in mind, this is async function. So you need to await result. Don't forget to call MySQLConnection.connect to actually connect to database, or you will get errors. See examples directory for code samples.

host host to connect to. Can be String or InternetAddress. userName database user name. password user password. secure If true - TLS will be used, if false - ordinary TCL connection. databaseName Optional database name to connect to. collation Optional collaction to use.

By default after connection is established, this library executes query to switch connection charset and collation:

SET @@collation_connection=$_collation, @@character_set_client=utf8mb4, @@character_set_connection=utf8mb4, @@character_set_results=utf8mb4

Implementation

static Future<MySQLConnection> createConnection({
  required dynamic host,
  required int port,
  required String userName,
  required String password,
  bool secure = true,
  String? databaseName,
  String collation = 'utf8mb4_general_ci',
}) async {
  final Socket socket = await Socket.connect(host, port);

  if (socket.address.type != InternetAddressType.unix) {
    // no support for extensions on sockets
    socket.setOption(SocketOption.tcpNoDelay, true);
  }

  final client = MySQLConnection._(
    socket: socket,
    username: userName,
    password: password,
    databaseName: databaseName,
    secure: secure,
    collation: collation,
  );

  return client;
}