YugabyteDB is a PostgreSQL-compatible Open-Source Distributed SQL database. It adds horizontal scalability to applications built for PostgreSQL. It offers all the benefits of a typical relational database (e.g. SQL, strong consistency, ACID transactions) with the advantages of a globally-distributed auto-sharded database system (e.g., NoSQL document databases).
Since YugabyteDB builds on top of PostgreSQL, every tool that works with PostgreSQL works with Yugabyte as well. Not only will you be able to use PgAdmin to connect to Yugabyte, but you can use any software framework or library that works with the PostgreSQL drivers.
YugabyteDB is versatile when it comes to data and traffic volumes. Because it provides auto-scaling, auto-sharding, and auto-balancing, you won’t have to rearchitect your system the moment it becomes too successful for the initial architecture to cope with.
It is the best fit for cloud-native OLTP (i.e. real-time, business-critical) applications that need absolute data correctness and requires at least one of the following: scalability, high tolerance to failures, and globally-distributed deployments.
YugabyteDB is Cassandra API compliant and Postgres API compliant which makes it very powerful. It combines and complements the strength of the NoSQL Cassandra database and the PostgreSQL database as well.
In this guide we will Create a local cluster on a single host.
Related content:
- How to Run Postgresql 14 with Docker and Docker-Compose
- How to run PGAdmin 4 Using Docker and Docker-Compose
Prerequisites
Before proceeding with this guide to install YugabyteDB, ensure that you have the Docker runtime installed on your local machine. To download and install Docker, checkout the installation page.
Running YugabyteDB with docker
To create a 1-node cluster with a replication factor (RF) of 1, first pull the docker image:
|
|
Once we have the docker image downloaded, we can create a new container using the following docker run
command:
|
|
If you are running macOS Monterey, replace -p7000:7000 with -p7001:7000. This is necessary because Monterey enables AirPlay receiving by default, which listens on port 7000. This conflicts with YugabyteDB and causes yugabyted start to fail unless you forward the port as shown. Alternatively, you can disable AirPlay receiving, then start YugabyteDB normally, and then, optionally, re-enable AirPlay receiving.
Below is a breakdown of the options:
-d
: The detach option runs the container as a background process and displays the container ID. This option is needed to regain control of the shell since the yugabyted process is intended to be long-lived.--name yugabyte
: This option gives the container a user-friendly name that can be used later.-p7000:7000 -p9000:9000 -p5433:5433 -p9042:9042
: These options expose internal ports to the host so they can be interacted with from outside the container. These are YugabyteDB significant ports and will be discussed later.yugabytedb/yugabyte:2.18.0.1-b4
: This is the container image and version (tag) to run.bin/yugabyted start --daemon=false
: This command starts yugabyted, the parent process for YugabyteDB and passes additional options to set the base directory for the YugabyteDB data folder and directs the process to not run in the background (the default behavior which would cause the container to stop).
It is important to note that YugabyteDB is a distributed SQL database and that the image used is only a single node deployment (i.e. a replication factor of 1). This is not typical for a production environment which would usually be RF=3 or even RF=5. Running a multi-node environment locally is possible but beyond the scope of this guide.
Run the following command to check the cluster status:
|
|
Check logs
|
|
Clients can now connect to the YSQL and YCQL APIs at http://localhost:5433
and http://localhost:9042
respectively.
Run YugabyteDB with Docker in a persistent volume
In the preceding docker run command, the data stored in YugabyteDB does not persist across container restarts. To make YugabyteDB persist data across restarts, you can add a volume mount option to the docker run command, as follows:
Create a ~/yb_data
directory by executing the following command:
|
|
Run Docker with the volume mount option by executing the following command:
|
|
Using docker-compose to run YugabyteDB
Development using the Docker Compose file is faster and error-free compared to provisioning Docker containers individually.
Save this as docker-compose.yaml
|
|
To run, use the following command:
|
|
Confirm that it is running as expected:
|
|
Connecting to the Admin UI
The cluster you have created consists of two processes: YB-Master
which keeps track of various metadata (list of tables, users, roles, permissions, and so on) and YB-TServer
which is responsible for the actual end user requests for data updates and queries.
Each of the processes exposes its own Admin UI that can be used to check the status of the corresponding process, as well as perform certain administrative operations. The yb-master Admin UI is available at http://localhost:7000
and the yb-tserver Admin UI is available at http://localhost:9000
. To avoid port conflicts, you should make sure other processes on your machine do not have these ports mapped to localhost
.
Connect to the Yugabyte database
Using the YugabyteDB SQL shell, ysqlsh, you can connect to your cluster and interact with it using distributed SQL. ysqlsh is installed with YugabyteDB and is located in the bin directory of the YugabyteDB home directory.
To open the YSQL shell, run ysqlsh
.
|
|
You can can also connect to YugaByteDB using any PostgreSQL-compatible tool like the the PgAdmin UI tool to connect to both my local PostgreSQL server and the YugabyteDB server running on Docker.
From your favorite programming language, you can connect to Yugabyte just like you’d do for PostgreSQL.
SQL Actions
Load Sample Dataset
|
|
Import data
|
|
Run queries:
|
|
Cleaning up
If you no longer want to run YugabyteDB, you can use these commands to clean up:
|
|
Conclusion
This should be enough information to get started using YugabyteDB locally in a Docker container.
Since YugabyteDB is postgres compatible, it allows you to reuse lots of tools that you’re already familiar with.
You can easily migrate existing applications and workloads from PostgreSQL to YugabyteDB and benefit from its many features like auto-scaling capabilities.