Getting started
In this guide, you will deploy a single-node local YDB cluster and execute simple queries against your database.
Deploy a YDB cluster
To deploy your YDB cluster, use an archive with an executable or a Docker image.
Note
Currently, only a Linux build is supported. We'll add builds for Windows and macOS later.
-
Create a working directory and change to it:
mkdir ~/ydbd && cd ~/ydbd
-
Download and run the installation script:
curl https://binaries.ydb.tech/local_scripts/install.sh | bash
This will download and unpack the archive including the
idbd
executable, libraries, configuration files, and scripts needed to start and stop the cluster. -
Start the cluster in one of the following storage modes:
-
In-memory data:
./start.sh ram
When data is stored in-memory, it is lost when the cluster is stopped.
-
Data on disk:
./start.sh disk
The first time you run the script, an 80GB
ydb.data
file will be created in the working directory. Make sure there's enough disk space to create it.
Result:
Starting storage process... Initializing storage ... Registering database ... Starting database process... Database started. Connection options for YDB CLI: -e grpc://localhost:2136 -d /Root/test
-
-
Pull the current version of the Docker image:
docker pull cr.yandex/yc/yandex-docker-local-ydb:latest
Make sure that the pull operation was successful:
docker image list | grep cr.yandex/yc/yandex-docker-local-ydb
Result:
cr.yandex/yc/yandex-docker-local-ydb latest c37f967f80d8 6 weeks ago 978MB
-
Run the Docker container:
docker run -d --rm --name ydb-local -h localhost \ -p 2135:2135 -p 2136:2136 -p 8765:8765 \ -v $(pwd)/ydb_certs:/ydb_certs -v $(pwd)/ydb_data:/ydb_data \ -e YDB_DEFAULT_LOG_LEVEL=NOTICE \ -e GRPC_TLS_PORT=2135 -e GRPC_PORT=2136 -e MON_PORT=8765 \ cr.yandex/yc/yandex-docker-local-ydb:latest
If the container starts successfully, you'll see the container's ID. The container might take a few minutes to initialize. The database will not be available until the initialization completes.
Connect to the DB
To connect to the YDB database, use the cluster's Embedded UI or the YDB CLI command-line interface.
-
In your browser, open the page:
http://localhost:8765
-
Under Database list, select the database:
/Root/test
: If you used an executable to deploy your cluster./local
: If you deployed your cluster from a Docker image.
-
Install the YDB CLI:
-
For Linux or macOS:
curl -sSL https://storage.yandexcloud.net/yandexcloud-ydb/install.sh | bash
Note
The script will update the
PATH
variable only if you run it in the bash or zsh command shell. If you run the script in a different shell, add the CLI path to thePATH
variable yourself. -
For Windows:
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://storage.yandexcloud.net/yandexcloud-ydb/install.ps1'))"
Specify whether to add the executable file path to the
PATH
environment variable:Add ydb installation dir to your PATH? [Y/n]
Note
Some YDB CLI commands may use Unicode characters in their results. If these characters aren't displayed correctly in the Windows console, switch the encoding to UTF-8:
chcp 65001
To update the environment variables, restart the command shell session.
-
-
Save the DB connection parameters in the YDB CLI profile:
ydb config profile create quickstart --endpoint grpc://localhost:2136 --database <path_database>
-
path_database
: Database path. Specify one of these values:/Root/test
: If you used an executable to deploy your cluster./local
: If you deployed your cluster from a Docker image.
-
-
Check your database connection:
ydb --profile quickstart scheme ls
Result:
.sys_health .sys
Run queries against the database
Use the cluster's YDB Embedded UI or the YDB CLI to execute queries against the database.
-
Create tables in the database:
Enter the query text under Query:
CREATE TABLE series ( series_id Uint64 NOT NULL, title Utf8, series_info Utf8, release_date Date, PRIMARY KEY (series_id) ); CREATE TABLE seasons ( series_id Uint64, season_id Uint64, title Utf8, first_aired Date, last_aired Date, PRIMARY KEY (series_id, season_id) ); CREATE TABLE episodes ( series_id Uint64, season_id Uint64, episode_id Uint64, title Utf8, air_date Date, PRIMARY KEY (series_id, season_id, episode_id) );
Click Run Script.
-
Populate the resulting tables with data:
Enter the query text under Query:
UPSERT INTO series (series_id, title, release_date, series_info) VALUES ( 1, "IT Crowd", Date("2006-02-03"), "The IT Crowd is a British sitcom produced by Channel 4, written by Graham Linehan, produced by Ash Atalla and starring Chris O'Dowd, Richard Ayoade, Katherine Parkinson, and Matt Berry."), ( 2, "Silicon Valley", Date("2014-04-06"), "Silicon Valley is an American comedy television series created by Mike Judge, John Altschuler and Dave Krinsky. The series focuses on five young men who founded a startup company in Silicon Valley." ) ; UPSERT INTO seasons (series_id, season_id, title, first_aired, last_aired) VALUES (1, 1, "Season 1", Date("2006-02-03"), Date("2006-03-03")), (1, 2, "Season 2", Date("2007-08-24"), Date("2007-09-28")), (2, 1, "Season 1", Date("2014-04-06"), Date("2014-06-01")), (2, 2, "Season 2", Date("2015-04-12"), Date("2015-06-14")) ; UPSERT INTO episodes (series_id, season_id, episode_id, title, air_date) VALUES (1, 1, 1, "Yesterday's Jam", Date("2006-02-03")), (1, 1, 2, "Calamity Jen", Date("2006-02-03")), (2, 1, 1, "Minimum Viable Product", Date("2014-04-06")), (2, 1, 2, "The Cap Table", Date("2014-04-13")) ;
Click Run Script.
-
Select data from the
series
table:Enter the query text under Query:
SELECT series_id, title AS series_title, release_date FROM series;
Click Run Script.
You'll see the query result below:
series_id series_title release_date 1 IT Crowd 13182 2 Silicon Valley 16166
-
Delete data from the
episodes
table.Enter the query text under Query:
DELETE FROM episodes WHERE series_id = 2 AND season_id = 1 AND episode_id = 2 ;
Click Run Script.
-
Create tables in the database:
Write the query text to the
create-table.sql
file:CREATE TABLE series ( series_id Uint64 NOT NULL, title Utf8, series_info Utf8, release_date Date, PRIMARY KEY (series_id) ); CREATE TABLE seasons ( series_id Uint64, season_id Uint64, title Utf8, first_aired Date, last_aired Date, PRIMARY KEY (series_id, season_id) ); CREATE TABLE episodes ( series_id Uint64, season_id Uint64, episode_id Uint64, title Utf8, air_date Date, PRIMARY KEY (series_id, season_id, episode_id) );
Run the following query:
ydb --profile quickstart yql --file create-table.sql
-
Populate the resulting tables with data:
Write the query text to the
upsert.sql
file:UPSERT INTO series (series_id, title, release_date, series_info) VALUES ( 1, "IT Crowd", Date("2006-02-03"), "The IT Crowd is a British sitcom produced by Channel 4, written by Graham Linehan, produced by Ash Atalla and starring Chris O'Dowd, Richard Ayoade, Katherine Parkinson, and Matt Berry."), ( 2, "Silicon Valley", Date("2014-04-06"), "Silicon Valley is an American comedy television series created by Mike Judge, John Altschuler and Dave Krinsky. The series focuses on five young men who founded a startup company in Silicon Valley." ) ; UPSERT INTO seasons (series_id, season_id, title, first_aired, last_aired) VALUES (1, 1, "Season 1", Date("2006-02-03"), Date("2006-03-03")), (1, 2, "Season 2", Date("2007-08-24"), Date("2007-09-28")), (2, 1, "Season 1", Date("2014-04-06"), Date("2014-06-01")), (2, 2, "Season 2", Date("2015-04-12"), Date("2015-06-14")) ; UPSERT INTO episodes (series_id, season_id, episode_id, title, air_date) VALUES (1, 1, 1, "Yesterday's Jam", Date("2006-02-03")), (1, 1, 2, "Calamity Jen", Date("2006-02-03")), (2, 1, 1, "Minimum Viable Product", Date("2014-04-06")), (2, 1, 2, "The Cap Table", Date("2014-04-13")) ;
Run the following query:
ydb --profile quickstart yql --file upsert.sql
-
Select data from the
series
table:Write the query text to the
select.sql
file:SELECT series_id, title AS series_title, release_date FROM series;
Run the following query:
ydb --profile quickstart yql --file select.sql
Result:
┌───────────┬──────────────────┬──────────────┐ | series_id | series_title | release_date | ├───────────┼──────────────────┼──────────────┤ | 1 | "IT Crowd" | "2006-02-03" | ├───────────┼──────────────────┼──────────────┤ | 2 | "Silicon Valley" | "2014-04-06" | └───────────┴──────────────────┴──────────────┘
-
Delete data from the
episodes
table.Write the query text to the
delete.sql
file:DELETE FROM episodes WHERE series_id = 2 AND season_id = 1 AND episode_id = 2 ;
Run the following query:
ydb --profile quickstart yql --file delete.sql
View the
episodes
table:ydb --profile quickstart yql --script "SELECT * FROM episodes;"
Result:
┌──────────────┬────────────┬───────────┬───────────┬──────────────────────────┐ | air_date | episode_id | season_id | series_id | title | ├──────────────┼────────────┼───────────┼───────────┼──────────────────────────┤ | "2006-02-03" | 1 | 1 | 1 | "Yesterday's Jam" | ├──────────────┼────────────┼───────────┼───────────┼──────────────────────────┤ | "2006-02-03" | 2 | 1 | 1 | "Calamity Jen" | ├──────────────┼────────────┼───────────┼───────────┼──────────────────────────┤ | "2014-04-06" | 1 | 1 | 2 | "Minimum Viable Product" | └──────────────┴────────────┴───────────┴───────────┴──────────────────────────┘
You've deleted The Cap Table series row from the table.
Stop the cluster
Stop the YDB cluster when done:
To stop your cluster, change to the ~/ydbd
directory, then run this command:
./stop.sh
To stop the Docker container with the cluster, run this command:
docker kill ydb-local
What's next
- Read about YDB concepts.
- Learn more about these and other methods of YDB deployment.
- Find out how to access your YDB databases over the SDK.
- Learn more about the YQL query language.