Skip to content

Grafana Datasource Connection Error Fix

DodaTech Updated 2026-06-24 3 min read

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 curl from the Grafana server
  • Use access: proxy for server-side connections
  • Enable basicAuth for production datasources
  • Add datasources via provisioning YAML for version control

Common Mistakes with datasource error

  1. Using return to exit a function early instead of wrapping a pure value in the monad
  2. Mixing let bindings with <- bindings in do notation, producing type errors
  3. 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

### Why does Grafana show "Datasource connected but no data"?

The connection to the datasource works, but either the query returns no data, the time range is wrong, or the metric doesn't exist in the time range you selected. Check the raw query response with a curl request to verify data exists.

What's the difference between access: proxy and access: browser?

access: proxy (server-side) — Grafana server connects to the datasource. The browser connects only to Grafana. This is more secure and avoids CORS issues. access: browser (client-side) — the browser connects directly to the datasource. Use proxy for production.

How do I provision datasources automatically?

Create YAML files in /etc/<a href="/devops/prometheus-grafana/">grafana</a>/provisioning/datasources/. Grafana loads them on startup. Use environment variables for secrets: secureJsonData.basicAuthPassword: ${PROM_PASSWORD}. Provisioning eliminates manual datasource setup in the UI.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro