How to Install RethinkDB on Debian
RethinkDB is a free and open-source database system for building real-time applications. It is a distributed and scalable NoSQL database for today's web applications. RethinkDB provides solutions to make processes faster and more reliable for real-time applications. Instead of using traditional polling data, the RethinkDB uses live push updates, which make you applications faster.
In this tutorial, you will learn how to install and configure RethinkDB, the open-source database for real-time web on the Debian 11 Server. You will also learn how to set up the RethinkDB client driver and how to use it to create a new database and insert some documents.
Prerequisite
- A Debian 11 Server
- A non-root user with sudo or administrator privileges.
Installing RethinkDB
RethinkDB can be installed in two ways: by compiling from the source or installing binary packages from the RethinkDB repository. For most Linux distributions, RethinkDB provides package repositories.
Now you will add the RethinkDB repository to your Debian system, then install RethinkDB packages from the official repository.
Before start installing any packages, run the below command to install the GnuPG tools to your Debian system.
sudo apt install gnupg2 -y
Next, run the below command to add the RethinkDB GPG key and the repository.
export CODENAME=`lsb_release -cs`
echo "deb https://download.rethinkdb.com/repository/debian-$CODENAME $CODENAME main" | sudo tee /etc/apt/sources.list.d/rethinkdb.list
wget -qO- https://download.rethinkdb.com/repository/raw/pubkey.gpg | sudo apt-key add -
Refresh and update your repository.
sudo apt update
In the below screenshot you will see the RethinkDB repository is added to the Debian system.
Now install the RethinkDB package using the apt command below.
sudo apt install rethinkdb
Input Y to confirm the installation and press ENTER to continue.
When the RethinkDB installation is completed, move to the next step to set up the RethinkDB instance.
Configuring RethinkDB
In this step, you will be configuring the RethinkDB installation. The default RethinkDB configuration is available on the /etc/rethinkdb/ directory.
Move your working directory to /etc/rethinkdb/.
cd /etc/rethinkdb
Copy the default configuration default.conf.sample to the instances.d/rethinkdb1.conf using the following command.
cp default.conf.sample instances.d/rethinkdb1.conf
Now edit the configuration instances.d/rethinkdb1.conf using nano editor.
sudo nano instances.d/rethinkdb1.conf
Uncomment the following lines and make sure to change the server-name value with your server hostname. This configuration allows RethinkDB to be run on the server IP address 192.168.5.20 and the default port 8080 for the web administration dashboard, and the default data directory /var/lib/rethinkdb/default.
http-port=8080
server-name=rethinkdb1
directory=/var/lib/rethinkdb/default
bind=192.168.5.20
Save and close the file when you are done.
Next, restart the RethinkDB service to apply new changes to your RethinkDB instance.
sudo systemctl restart rethinkdb
Verify the RethinkDB service using the below command.
sudo systemctl status rethinkdb
In the below screenshot, you will see the RethinkDB is running with custom configuration instances.d/rethinkdb1.conf.
Additionally, you can also check the list of open ports on your system using the following command.
ss -plntu
In the below screenshot, you will see that RethinkDB is running on the server IP address 192.168.5.20 with port 8080 for the HTTP web administration dashboard and port 28015 for client connections.
Or you can visit your server IP address with port 8080 (for example http://192.168.5.20.8080). And you will see the RethinkDB administration dashboard below.
Now you have completed the RethinkDB configuration on the Debian Server. Move to the next step to set up a client driver for RethinkDB.
Installing RethinkDB Client Driver
RethinkDB provides client driver packages for almost every Programming language such as Node.js, Python, C, Java, Python, Go, etc.
In this example, you will be using Python, so you will be installing the Python client driver for the RethinkDB.
Before installing the client driver, run the below command to install Python3-pip and Python-venv to your system. The Python3-pip is a python package manager, and the Python3-venv is for creating a virtual environment for Python.
sudo apt install python3-pip python3-venv
Input Y to confirm the installation.
After installation is completed, run the below command to create a new Python virtual environment with the name venv and activate the venv virtual environment.
python3 -m venv ./venv
source venv/bin/activate
Now you can install the Python RethinkDB client driver using the pip command below.
pip install rethinkdb
You have now installed the RethinkDB client driver for Python. Move to the next step to learn the basic usage of RethinkDB.
Creating New Database and Insert Data
Inside the venv virtual environment, run the python command to get the Python shell.
python
Now import the RethinkDB python module and connect to your RethinkDB instance using the following query.
from rethinkdb import r
r.connect('192.168.5.20', 28015).repl()
Create a new database test and the table tv_shows using the following query.
r.db('test').table_create('tv_shows').run()
Now insert the data to the tv_shows table as below.
r.table('tv_shows').insert({ 'name': 'Star Trek TNG' }).run()
The new data is successfully inserted into the RethinkDB in the screenshot below.
You can use the following query to retrieve the data you just inserted.
cursor = r.table("tv_shows").run()
for document in cursor:
print(document)
In the screenshot below, you will see the data you just inserted into the tv_shows table. And you just successfully retrieved the data from the RethinkDB.
Another way to verify the database is by visiting the RethinkDB web administration dashboard. Click on the TABLES menu on top and you will see the database test with the table tv_shows is available on the RethinkDB server.
Conclusion
Congratulation! You have now successfully installed and configured the RethinkDB open-source real-time database on the Debian 11 server. Also, you have learned how to use the Python RethinkDB client driver for creating a database and inserting new data into the RethinkDB.