This tutorial will walk you through installing and configuring InfluxDB with Python on a VPS running Ubuntu 22.04.
InfluxDB is an open-source time-series database (TSDB) developed by InfluxData. It’s written in Go and optimized for fast, high-availability storage and retrieval of time series data in fields such as operations monitoring, application metrics, Internet of Things sensor data, and real-time analytics.
Python, a high-level, interpreted programming language with dynamic semantics, is known for its easy readability with great design principles. When you combine Python’s power with InfluxDB’s efficiency, you get a potent tool for your data analytics needs.
Step 1: Update and Upgrade Your System
Before we get started, it’s a good idea to update and upgrade your Ubuntu 22.04 VPS system packages and repositories.
sudo apt update && sudo apt upgrade -y
Step 2: Install InfluxDB with Python
You can use the official InfluxData repository to install InfluxDB on Ubuntu 22.04. First, add the InfluxData repository using the commands below:
# wget -qO- https://repos.influxdata.com/influxdb.key | sudo apt-key add -
# sudo echo "deb https://repos.influxdata.com/ubuntu bionic stable" | sudo tee /etc/apt/sources.list.d/influxdb.list
Then, install InfluxDB:
apt install influxdb influxdb-client -y
Step 3: Configure and Start InfluxDB
Once InfluxDB is installed, you need to enable and start the InfluxDB service:
sudo systemctl enable --now influxdb
You can check the status of the InfluxDB service with:
sudo systemctl status influxdb
Step 4: Install Python and InfluxDB Python Client
Next, execute the commands listed below to install Python and pip (Python’s package installer):
sudo apt install python3 python3-pip -y
Now, install the InfluxDB client for Python:
pip3 install influxdb
Step 5: Create a Database and User in InfluxDB
Access the InfluxDB shell:
influx
Create a new database and user with the following commands:
CREATE DATABASE mydb
CREATE USER "myuser" WITH PASSWORD 'mypassword' WITH ALL PRIVILEGES
Exit the InfluxDB shell by typing exit.
Step 6: Write a Python Script to Interact with InfluxDB
The next step is to create a Python script that connects to the InfluxDB server, writes data, queries data, and reads the outcome. Put your database’s name, username, and password in the spaces provided:
from influxdb import InfluxDBClient
# Connect to InfluxDB server
client = InfluxDBClient(host='localhost', port=8086, username='myuser', password='mypassword')
# Switch to the created database
client.switch_database('mydb')
# Data to be written
json_body = [
{
"measurement": "cpu_load_short",
"tags": {
"host": "server01",
"region": "us-test"
},
"fields": {
"value": 0.64
}
}
]
# Write data
client.write_points(json_body)
# Query data
result = client.query('SELECT "value" FROM "cpu_load_short" WHERE "region"=\'us-west\'')
# Print result
print("Result: {0}".format(result))
Then enter the following command to execute the script:
python3 influxdb_python.py
Summary
This guide has provided a step-by-step overview of how to set up and use InfluxDB with Python on an Ubuntu 22.04 VPS, and how to access your application using an external IPv4. With this setup, you can efficiently store, retrieve, and analyze your time-series data.
1 Comment
How To Install Flask On Ubuntu And CentOS - Virtono Community · September 20, 2023 at 1:14 PM
[…] a micro web framework written in Python, is an excellent tool for developers looking to create robust web applications. Its simplicity and […]