Initialize the driver
To connect to YDB, you need to specify the required and additional parameters that define the driver's behavior (learn more in Connecting to the YDB server).
Below are examples of the code for connecting to YDB (driver creation) in different YDB SDKs.
Go (native)
Go (database/sql)
Java
JDBC Driver
C# (.NET)
PHP
Rust
package main
import (
"context"
"github.com/ydb-platform/ydb-go-sdk/v3"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
db, err := ydb.Open(ctx, "grpc://localhost:2136/local")
if err != nil {
panic(err)
}
defer db.Close()
// ...
}
Using a connector (recommended)
package main
import (
"context"
"database/sql"
"os"
"github.com/ydb-platform/ydb-go-sdk/v3"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
nativeDriver, err := ydb.Open(ctx,
os.Getenv("YDB_CONNECTION_STRING"),
)
if err != nil {
panic(err)
}
defer nativeDriver.Close(ctx)
connector, err := ydb.Connector(nativeDriver)
if err != nil {
panic(err)
}
defer connector.Close()
db := sql.OpenDB(connector)
defer db.Close()
// ...
}
Using a connection string
The database/sql
driver is registered when importing the package of a specific driver separated by an underscore:
package main
import (
"database/sql"
_ "github.com/ydb-platform/ydb-go-sdk/v3"
)
func main() {
db, err := sql.Open("ydb", "grpc://localhost:2136/local")
if err != nil {
panic(err)
}
defer db.Close()
// ...
}
public void work() {
GrpcTransport transport = GrpcTransport.forConnectionString("grpc://localhost:2136/local")
.build());
// Do work with the transport
doWork(transport);
transport.close();
}
public void work() {
// JDBC Driver must be in the classpath for automatic detection
Connection connection = DriverManager.getConnection("jdbc:ydb:grpc://localhost:2136/local");
// Do work with the connection
doWork(connection);
connection.close();
}
using Ydb.Sdk;
var config = new DriverConfig(
endpoint: "grpc://localhost:2136",
database: "/local"
);
await using var driver = await Driver.CreateInitialized(config);
<?php
use YdbPlatform\Ydb\Ydb;
$config = [
// Database path
'database' => '/ru-central1/b1glxxxxxxxxxxxxxxxx/etn0xxxxxxxxxxxxxxxx',
// Database endpoint
'endpoint' => 'ydb.serverless.yandexcloud.net:2135',
// Auto discovery (dedicated server only)
'discovery' => false,
// IAM config
'iam_config' => [
// 'root_cert_file' => './CA.pem', // Root CA file (uncomment for dedicated server)
],
'credentials' => new \YdbPlatform\Ydb\Auth\Implement\AccessTokenAuthentication('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA') // use from reference/ydb-sdk/auth
];
$ydb = new Ydb($config);
let client = ClientBuilder::new_from_connection_string("grpc://localhost:2136?database=local")?
.with_credentials(AccessTokenCredentials::from("..."))
.client()?