InfluxDB is an open source database for time series data. This means, it is the perfect tool for storing sensor data from IoT sensors or systems. This article will show you how to install InfluxDB on Linux.
Step 1: Download and install the Docker container
At the time of writing (July 2020), the current stable version of InfluxDB is 1.8.1. However, the following docker command is set to always pull the latest version.
docker pull influxdb
Step 2: Prepare the environment
InfluxDB inside the container can either be configured using a configuration file, which we have to pass along with the start command, or using environment variables. We will use environment variables written to an env-file which can be passed along with the Docker command.
Use nano to open a new file influxdb-env.list
nano influxdb-env.list
And paste the following lines:
INFLUXDB_DATA_DIR=/var/lib/influxdb/data
INFLUXDB_HTTP_BIND_ADDRESS=:8086
INFLUXDB_HTTP_AUTH_ENABLED=true
INFLUXDB_DB=NOAA_water_database
INFLUXDB_ADMIN_USER=admin
INFLUXDB_ADMIN_PASSWORD=admin
INFLUXDB_USER=user
INFLUXDB_USER_PASSWORD=user
INFLUXDB_REPORTING_DISABLED=true
Press Ctrl+O and Ctrl+X to write the changes and to exit nano editor.
What did we set here?
- We set the data directory (it’s the default value, but we set it here anyways).
- We set the port to 8086 (also the default).
- Just to be safe, we enable authentication.
- A new database with the name “NOAA_water_database” will be created (used for the test data in the next steps).
- A new admin user “admin” with the password “admin” is created.
- A new user “user” with the password “user” is created.
- We disable usage statistics reporting.
Next, we create the directory where the InfluxDB container will store the shared data:
sudo mkdir -p /var/lib/influxdb
Step 3: Run the InfluxDB container
Since we have specified port 8086 above, we also have to map this port to the local machine using Docker. The HTTP API port will be automatically exposed when running the container.
docker run -d --network=host --name=influxdb \
--volume /var/lib/influxdb:/var/lib/influxdb \
--env-file ./influxdb-env.list influxdb
Step 4: Fill database with sample data
In the beginning, we don’t have any data yet, so for our first steps with InfluxDB we fill the testdb database with sample data. We use the same example dataset used in the official InfluxDB documentation: https://docs.influxdata.com/influxdb/v1.8/query_language/sample-data/
It contains water levels (in ft) collected at two stations (Santa Monica, CA and Coyote Creek, CA), as well as temperatures.
First, we fetch the NOAA water dataset.
curl https://s3.amazonaws.com/noaa.water-database/NOAA_data.txt -o /tmp/NOAA_data.txt
Next, we write this data to the database:
docker run --rm --network=host \
--env INFLUX_USERNAME=user --env INFLUX_PASSWORD=user \
--volume /tmp/NOAA_data.txt:/tmp/NOAA_data.txt \
-it influxdb \
influx -host localhost -import -path=/tmp/NOAA_data.txt -precision=s \
-database=NOAA_water_database
This connects to the same network (host network) as the first container. Then it authenticates using our user, and maps the file of the dataset to the same location in the container. Then it calls influx in the container with the import switch and the path to this file.
Step 5 (optional): Connect user to InfluxDB shell
Since InfluxDB is running in the container, we would either need to access the shell of this container to get access to the InfluxDB shell, or we start another temporary container.
Starting a second container with an influx client works as follows:
docker run --rm --network=host \
--env INFLUX_USERNAME=user --env INFLUX_PASSWORD=user \
-it influxdb influx -host localhost
Step 6 (optional): Select some data from the dataset
Now that we have the influx shell, we execute a command to select the database:
USE NOAA_water_database;
Then, we show the “tables” in this database (called measurements in InfluxDB):
SHOW measurements;
We should see the entries average_temperature, h2o_feet, h2o_pH and so on.
To select data from average_temperature, we do the following query:
SELECT * FROM average_temperature LIMIT 10;