Troubleshooting
Telegraf 401 on database creation failed: check the listener
Short answer
A 401 Unauthorized on `database "telegraf" creation failed` proves a live HTTP service answered at 127.0.0.1:8086, so a missing InfluxDB is not the cause. Confirm with netstat that InfluxDB owns port 8086, match the output plugin to the server's write API, then provision the database with an account authorized to create it.

On this page
Telegraf reporting database "telegraf" creation failed: 401 Unauthorized against http://127.0.0.1:8086 has already reached an HTTP responder: nothing that is not listening can return a 401. So before touching credentials, prove which process owns port 8086, then match the output plugin's API to that server and provision the database with an account authorized to create it.
Confirm which service answers on port 8086 #
Run netstat -lptn on the Telegraf host with enough privilege to show process names, and read the local address as well as the port. The tool documents itself as listing active TCP connections and the ports on which the computer is listening, so it answers exactly this question. The Microsoft netstat reference covers the Windows form; on Linux the same flags list listening sockets with owning process IDs.
- A listener on 8086 owned by the InfluxDB process on IPv4 loopback matches the configured URL: move on to credentials.
- A listener bound only on IPv6 may still accept IPv4 traffic, but test the configured IPv4 URL directly before assuming it does. In the reported case the binding appeared on IPv6, which put listener identity, not authentication, first in the diagnosis.
- A different port held by another service (in the reported case, a service on 4222) is not a substitute for 8086. Identify its owning process, but do not point Telegraf at it.
If another service owns the socket, correct the URL or the service binding before investigating anything else. Compare current sockets against current logs: an old 401 in the log and a changed listener can describe different conditions.
Separate the two data paths #
The SQL input and the InfluxDB output authorize different things. The [[inputs.sqlserver]] connection string with query_version = 2 authorizes collection from Azure SQL Managed Instance; those credentials do nothing for database creation or writes in InfluxDB. Grafana then reads InfluxDB with its own data-source configuration. Diagnose each hop independently with systemctl status -l telegraf and the service logs: the database-creation failure locates a problem on the output path only, and it does not prove SQL collection works.
Read the 401 as authentication or API mismatch #
The reported output block carried only urls and database = "telegraf", with no InfluxDB authentication settings. If the listener requires authentication, the creation request needs credentials that server accepts; SQL usernames and passwords belong only in the SQL input. Grafana's own troubleshooting guidance lists 401 Unauthorized as invalid or missing authentication credentials for an InfluxDB data source, and a related InfluxDB issue report documents 401 responses for all tokens after a reboot, so a once-working token can stop working across a restart.
Also compare the server's write API with the installed [[outputs.influxdb]] plugin. That plugin speaks a database-oriented API; an InfluxDB 2.x server or InfluxDB Cloud uses tokens and organizations instead, and needs the matching output configuration for that API model. A 401 can simply be the wrong API being spoken to the right port.
One fix per failure mode #
- Wrong process on the port: fix the URL or the binding; no credential change helps.
- Credentials missing or rejected: configure the authentication the selected API requires, in the output block the running service actually loads.
- Authenticated but lacking creation rights: create
telegrafyourself through an authorized administrative account, then configure Telegraf for writes to the existing database. Database creation and metric writing are separate authorization decisions. - API mismatch: switch to the output plugin and settings matching the server's API version.
Reinstalling the operating system fixes none of these: the reported symptom reproduced identically on Ubuntu 18.04 LTS and a fresh 20.04 host, which is what an unchanged authentication or API mismatch predicts.
Worked check: a Telegraf VM writing to local InfluxDB #
Inputs: configured URL http://127.0.0.1:8086 from the output block; netstat -lptn shows an IPv6 listener on 8086 whose owning process is InfluxDB (the thread's case); the output block names database = "telegraf" with no token or username settings.
Rule: a 401 requires a live HTTP responder, so listener identity comes first; then the request must carry credentials the server accepts for the operation attempted, and creation of a new database is a separate operation from writing to an existing one.
Arithmetic of the decision, in order: the responder answered, so the port is reachable (1 of 3 conditions met); the process is InfluxDB, so the URL is correct (2 of 3); the output block carries no credentials, so authentication is the missing condition (3 of 3 identified). The fix is to add the credentials that server's API accepts, or create the database with an admin account and configure the output for writes only.
Result and decision: because the listener is InfluxDB and the port matches, do not reinstall, do not change the URL, and do not touch the SQL input. Provision telegraf with an authorized account or add output credentials, restart Telegraf, then confirm a fresh write lands with a current timestamp rather than trusting existing rows.
Verify the write, not the absence of the error #
After applying the change, read fresh logs: database creation or access should succeed and writes should stop returning 401. Then query InfluxDB for newly collected SQL metrics and compare their timestamps with the current collection interval. Existing historical records prove nothing about current writes. Finally, test the Grafana data source from the host where the Grafana server process runs; if Grafana is in a container or on another machine, its loopback address is a different host's loopback, and its Bad Request is a separate response needing its own request details, not interchangeable with Telegraf's 401.
Once the pipeline is stable, sizing the box that runs the stack is a separate exercise; the guide on sizing one PC for InfluxDB, Telegraf, Grafana and Node-RED covers the hardware side. If you are still provisioning the collector itself, the article on storing MQTT data in InfluxDB with Telegraf walks the output setup end to end.
A reboot that previously broke authentication once will break it again: after any InfluxDB restart, re-check that the configured credentials still authenticate before assuming a silent failure means a quiet pipeline.
Questions asked
How do I fix Telegraf database creation failed: 401 Unauthorized?
First confirm which process owns port 8086 on the host, because a 401 proves some HTTP responder answered there. If it is InfluxDB, give the outputs.influxdb plugin credentials that server accepts, or create the database yourself with an admin account and let Telegraf write only. If it is another service, fix the URL first.
Does a 401 mean InfluxDB is not running?
No. A connection failure means nothing is listening; a 401 means something answered with an HTTP status, so a responder is live on that address. The question is whether that responder is InfluxDB and whether it accepted the credentials, not whether the port is reachable.
Why did a fresh OS install not fix the error?
Reinstalling the operating system reproduces the same configuration and the same authentication or API mismatch. The error followed an Ubuntu 18.04 LTS host to a fresh 20.04 host in the reported case, which is expected when the output credentials and server API have not changed.
Can the SQL Server input credentials authorize the InfluxDB write?
No. The SQL connection string in inputs.sqlserver authorizes metric collection from the database engine only. InfluxDB authentication lives in the outputs.influxdb settings, and database creation may require more privilege than writing to an existing database.
