gcgarner / IOTstack

docker stack for getting started on IOT on the Raspberry PI
GNU General Public License v3.0
1.5k stars 578 forks source link

influxdb ignores influxdb.env #192

Open ryanwalexander opened 4 years ago

ryanwalexander commented 4 years ago

It may be something I'm doing wrong.

influxdb.env:

INFLUXDB_DB=mydb
INFLUXDB_DATA_ENGINE=tsm1
INFLUXDB_REPORTING_DISABLED=true
#INFLUXDB_HTTP_AUTH_ENABLED=true
INFLUXDB_ADMIN_ENABLED=true
INFLUXDB_ADMIN_USER=myadminuser
INFLUXDB_ADMIN_PASSWORD=myadminpassword
INFLUXDB_USER=nodered
INFLUXDB_USER_PASSWORD=nodered
INFLUXDB_READ_USER=myreaduser
INFLUXDB_READ_USER_PASSWORD=myreadpassword
INFLUXDB_WRITE_USER=mywriteuser
INFLUXDB_WRITE_USER_PASSWORD=mywritepassword

When I docker-compose down and up again, there is no database called mydb - there are the only 2 shown below.

Connected to http://localhost:8086 version 1.8.0
InfluxDB shell version: 1.8.0
> show databases
name: databases
name
----
telegraf
_internal
> 

I'm sure it's something I'm doing wrong...

Paraphraser commented 4 years ago

OK. I think the short answer is that you are not doing anything wrong. I don't think the INFLUXDB_DB key works. I think its presence in the IOTstack environment template for InfluxDB is an error.

I say this because:

  1. I can't find INFLUXDB_DB anywhere in the current documentation;
  2. A restricted Google search for that exact key results in:

    Your search - "INFLUXDB_DB" site:docs.influxdata.com - did not match any documents.

    so it doesn't look to me like any part of the InfluxDB docs ever knew about that exact key; and

  3. The experimentation below couldn't discern any effect in either of the two obvious use-cases:
    • Automatically creating a default database
    • Automatically selecting a default database
  4. I hunted around the current documentation but could not find any environment key that looks even close to being something that would satisfy either of those use-cases.

From what you wrote, it sounds like you are talking about the first use-case (expecting that INFLUXDB_DB will create the database "mydb" automatically). I don't have all that much experience with InfluxDB but I haven't found a way of doing that.

I have played with quite a few SQL databases over the years and, while I might be mis-remembering (rose-coloured glasses and all that), I can't actually think of one where you did not at least have to create each database by hand, and usually the table schema too. Sure, there might be an API between you and the database engine that takes care of details like that but not if you are talking direct to the CLI for the database.

I have these aliases defined and I will use them in the examples:

alias INFLUX_RESTART='pushd ~/IOTstack;docker-compose up -d; popd'
alias INFLUX_SERVICES='cd ~/IOTstack/services/influxdb; ls'
alias INFLUX_SHELL='docker exec -it influxdb bash'
alias influx='docker exec -it influxdb influx -precision=rfc3339'

To create "mydb" I would go into the InfluxDB CLI:

$ influx
Connected to http://localhost:8086 version 1.8.0
InfluxDB shell version: 1.8.0
>

First, demonstrate that "mydb" does not exist:

> use mydb
ERR: Database mydb doesn't exist. Run SHOW DATABASES for a list of existing databases.
DB does not exist!

> show databases
name: databases
name
----
power
weather
airquality
_internal

Then, create the database:

> create database mydb

> show databases
name: databases
name
----
power
weather
airquality
_internal
mydb

> exit
$

Now, what about selecting a database by default. Does INFLUXDB_DB do that? Let's give that a whirl:

$ INFLUX_SERVICES

$ vi influxdb.env
# added "INFLUXDB_DB=power" as the first line

$ head -1 influxdb.env
INFLUXDB_DB=power

$ INFLUX_RESTART
Recreating influxdb ... done

Prove that the container environment knows about the change:

$ INFLUX_SHELL

# set | grep INFLUXDB_DB
INFLUXDB_DB=power

# exit
$

Does that result in "power" being selected as the default database?

$ influx

> show series
ERR: database name required

> exit 
$

Answer = no.

Is it possible to have a database selected by default when I go into the Influx CLI? Yes, by passing an argument to the command:

$ influx -database power

> show series
key
---
hiking
solax

> select * from hiking limit 1
name: hiking
time                     current frequency importEnergy meterErrors voltage
----                     ------- --------- ------------ ----------- -------
2018-04-10T14:00:06.663Z 0.16    50.11     27.19        0           247.4

> exit
$

Finally, if I pass influx a database name on the command line, does that result in the database being created automatically if it doesn't exist?

$ influx -database fred

> show series
ERR: database not found: fred

> show databases
name: databases
name
----
power
weather
airquality
_internal
mydb

> exit
$

And, just to be tidy...

$ influx

> show databases
name: databases
name
----
power
weather
airquality
_internal
mydb

> drop database mydb

> show databases
name: databases
name
----
power
weather
airquality
_internal

> exit
$ 

Did I actually answer your question somewhere in all of that, or did I miss the point entirely?

I think I might step through all the environment keys in the template and check if each actually exists, then prepare a pull request to clean this up.

ryanwalexander commented 4 years ago

@Paraphraser yes, you completely answered my question, thank you for your time and attention. The username fields also don't appear to generate any meaningful usernames, but I think you captured this in your other issue.