Skip to content

Getting Started

Mirro Mutth edited this page Feb 15, 2024 · 6 revisions

Check out the Configuration Options page to see about all configuration options.

URL Discovery

// Notice: the query string must be URL encoded
ConnectionFactory connectionFactory = ConnectionFactories.get(
    "r2dbcs:mysql://root:database-password-in-here@127.0.0.1:3306/r2dbc?" +
    "zeroDate=use_round&" +
    "sslMode=verify_identity&" +
    "useServerPrepareStatement=true&" +
    "tlsVersion=TLSv1.3%2CTLSv1.2%2CTLSv1.1&" +
    "sslCa=%2Fpath%2Fto%2Fmysql%2Fca.pem&" +
    "sslKey=%2Fpath%2Fto%2Fmysql%2Fclient-key.pem&" +
    "sslCert=%2Fpath%2Fto%2Fmysql%2Fclient-cert.pem&" +
    "sslKeyPassword=key-pem-password-in-here"
);

// Creating a Mono using Project Reactor
Mono<Connection> connectionMono = Mono.from(connectionFactory.create());

Or use unix domain socket like following:

// Minimum configuration for unix domain socket
ConnectionFactory connectionFactory = ConnectionFactories.get("r2dbc:mysql://root@unix?unixSocket=%2Fpath%2Fto%2Fmysql.sock");

Mono<Connection> connectionMono = Mono.from(connectionFactory.create());

Programmatic Discovery

ConnectionFactoryOptions options = ConnectionFactoryOptions.builder()
    .option(DRIVER, "mysql")
    .option(HOST, "127.0.0.1")
    .option(USER, "root")
    .option(PORT, 3306)
    .option(PASSWORD, "database-password-in-here")
    .option(DATABASE, "r2dbc")
    .option(Option.valueOf("createDatabaseIfNotExist"), true)
    .option(CONNECT_TIMEOUT, Duration.ofSeconds(3))
    .option(SSL, true)
    .option(Option.valueOf("sslMode"), "verify_identity")
    .option(Option.valueOf("sslCa"), "/path/to/mysql/ca.pem")
    .option(Option.valueOf("sslCert"), "/path/to/mysql/client-cert.pem")
    .option(Option.valueOf("sslKey"), "/path/to/mysql/client-key.pem")
    .option(Option.valueOf("sslKeyPassword"), "key-pem-password-in-here")
    .option(Option.valueOf("tlsVersion"), "TLSv1.3,TLSv1.2,TLSv1.1")
    .option(Option.valueOf("sslHostnameVerifier"), "com.example.demo.MyVerifier")
    .option(Option.valueOf("sslContextBuilderCustomizer"), "com.example.demo.MyCustomizer")
    .option(Option.valueOf("zeroDate"), "use_null")
    .option(Option.valueOf("useServerPrepareStatement"), true)
    .option(Option.valueOf("sessionVariables"), "sql_mode=ANSI_QUOTES,@@innodb_lock_wait_timeout=3000")
    .option(Option.valueOf("allowLoadLocalInfileInPath"), "/opt")
    .option(Option.valueOf("tcpKeepAlive"), true)
    .option(Option.valueOf("tcpNoDelay"), true)
    .option(Option.valueOf("compressionAlgorithms"), "zstd")
    .option(Option.valueOf("loopResources"), LoopResources.create("r2dbc"))
    .option(Option.valueOf("autodetectExtensions"), false)
    .option(Option.valueOf("passwordPublisher"), Mono.just("password"))
    .build();
ConnectionFactory connectionFactory = ConnectionFactories.get(options);

// Creating a Mono using Project Reactor
Mono<Connection> connectionMono = Mono.from(connectionFactory.create());

Or use unix domain socket like following:

ConnectionFactoryOptions options = ConnectionFactoryOptions.builder()
    .option(DRIVER, "mysql")
    .option(Option.valueOf("unixSocket"), "/path/to/mysql.sock")
    .option(USER, "root")
    .build();
ConnectionFactory connectionFactory = ConnectionFactories.get(options);

Mono<Connection> connectionMono = Mono.from(connectionFactory.create());

Programmatic Configuration

MySqlConnectionConfiguration configuration = MySqlConnectionConfiguration.builder()
    .host("127.0.0.1")
    .user("root")
    .port(3306)
    .password("database-password-in-here")
    .database("r2dbc")
    .createDatabaseIfNotExist(true)
    .connectTimeout(Duration.ofSeconds(3))
    .build();
ConnectionFactory connectionFactory = MySqlConnectionFactory.from(configuration);

// Creating a Mono using Project Reactor
Mono<Connection> connectionMono = Mono.from(connectionFactory.create());

Or use unix domain socket like following:

MySqlConnectionConfiguration configuration = MySqlConnectionConfiguration.builder()
    .unixSocket("/path/to/mysql.sock")
    .user("root")
    .build();
ConnectionFactory connectionFactory = MySqlConnectionFactory.from(configuration);

Mono<Connection> connectionMono = Mono.from(connectionFactory.create());