Grafana Datasource Connection Error Fix
In this tutorial, you'll learn about Grafana Datasource Connection Error Fix. We cover key concepts, practical examples, and best practices.
Grafana shows Datasource connected but no data or Error reading from datasource — the datasource URL is wrong, authentication is invalid, or network rules block Grafana from reaching the backend.
The Problem
Datasource: Prometheus
Error: Error reading Prometheus: request to http://localhost:9090 failed
Grafana runs in a Docker container and localhost:9090 refers to the container's localhost, not the host. Prometheus runs on the host, not inside the Grafana container.
Step-by-Step Fix
1. Use the correct URL for containerized Grafana
# docker-compose.yml
services:
prometheus:
image: prom/prometheus
ports:
- "9090:9090"
grafana:
image: grafana/grafana
ports:
- "3000:3000"
environment:
- GF_AUTH_ANONYMOUS_ENABLED=true
Datasource URL in Grafana: http://<a href="/devops/prometheus-grafana/">prometheus</a>:9090 (Docker service name) NOT http://localhost:9090.
2. Test the datasource URL
# From the Grafana container
docker exec grafana curl http://prometheus:9090/api/v1/query?query=up
# From the Grafana server (non-containerized)
curl http://localhost:9090/api/v1/query?query=up
3. Check authentication and access settings
# Prometheus datasource config
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
access: proxy # Grafana proxies requests (server-side)
url: http://prometheus:9090
isDefault: true
editable: true
For access: browser (client-side), the browser must reach Prometheus directly. For access: proxy (default), Grafana server reaches Prometheus.
4. Configure TLS/SSL for secure connections
datasources:
- name: Prometheus
type: prometheus
url: https://prometheus.example.com
basicAuth: true
basicAuthUser: admin
secureJsonData:
basicAuthPassword: ${PROM_PASSWORD}
jsonData:
tlsAuth: true
tlsAuthWithCACert: true
secureJsonData:
tlsCACert: "---BEGIN CERTIFICATE---..."
Expected output:
Datasource: Prometheus
Status: OK
Version: 2.53.0
✓ Connection successful
Prevention Tips
- Use service names (Docker/K8s) instead of localhost for containerized Grafana
- Test datasource URLs with
curlfrom the Grafana server - Use
access: proxyfor server-side connections - Enable basicAuth for production datasources
- Add datasources via provisioning YAML for version control
Common Mistakes with datasource error
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
These mistakes appear frequently in real-world GRAFANA code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro