Skip to main content

Connecting Data Sources

Now that you're comfortable navigating Grafana's interface, it's time to connect your first data source. This lesson will guide you through connecting to popular data sources like Prometheus and MySQL, configuring authentication, and testing your connections.

Learning Goals:

  • Understand Grafana's data source architecture
  • Connect to time series databases (Prometheus)
  • Connect to SQL databases (MySQL/PostgreSQL)
  • Configure authentication and security settings
  • Test and troubleshoot data source connections

Understanding Data Sources in Grafana

Data sources are the foundation of Grafana - they're where your metrics, logs, and traces live. Grafana supports over 80 official and community data sources, from time series databases to SQL databases and cloud services.

tip

Think of data sources as "connections" rather than "databases." A single Grafana instance can query multiple data sources simultaneously, allowing you to correlate data across different systems.

Adding Your First Data Source

Accessing Data Source Configuration

Navigate to ConfigurationData Sources in the left sidebar. Click Add data source to see the available options.

The most common data sources you'll encounter are:

  • Prometheus - Time series database for metrics
  • Loki - Log aggregation system
  • Tempo - Distributed tracing
  • MySQL/PostgreSQL - SQL databases
  • InfluxDB - Time series database
  • Elasticsearch - Search and analytics engine

Connecting to Prometheus

Prometheus is one of the most popular data sources for Grafana. Here's how to configure it:

  1. Click Prometheus from the data source list
  2. Configure the HTTP settings:

URL: http://localhost:9090 (adjust based on your Prometheus instance) Access: Server (Default) - Grafana server proxies the requests

prometheus.yml
global:
scrape_interval: 15s

scrape_configs:
- job_name: 'node_exporter'
static_configs:
- targets: ['localhost:9100']
note

The "Server" access mode is recommended for most setups as it provides better security by keeping database credentials on the Grafana server side.

  1. Click Save & Test - you should see a green success message if the connection works.

Connecting to MySQL

SQL databases are great for business metrics and non-time-series data. Here's a MySQL configuration:

  1. Select MySQL from the data source list
  2. Configure the connection:

Host: localhost:3306 (or your MySQL server address) Database: your_database_name User: grafana_user Password: your_secure_password

Create Grafana User
CREATE USER 'grafana_user'@'localhost' IDENTIFIED BY 'secure_password';
GRANT SELECT ON your_database.* TO 'grafana_user'@'localhost';
  1. Set the Max open connections to 100 and Max idle connections to 100 for better performance
  2. Click Save & Test

Authentication Methods

Grafana supports various authentication methods depending on your data source:

Basic Authentication
# In data source configuration
auth:
type: basic
username: myuser
password: mypassword

Testing Your Connection

Always test your data source connection before using it in dashboards. The Save & Test button validates:

  • Network connectivity to the data source
  • Authentication credentials
  • Basic query capabilities
  • Required permissions

If the test fails, check:

  • Network connectivity (firewalls, DNS)
  • Authentication credentials
  • Data source service status
  • Required database permissions

Common Pitfalls

  • Connection Timeouts: Ensure your data source is running and accessible from the Grafana server
  • Permission Issues: Database users need at least read permissions for the required tables
  • Wrong Port Numbers: Double-check the port numbers (9090 for Prometheus, 3306 for MySQL, etc.)
  • SSL/TLS Configuration: Cloud data sources often require SSL, while local ones might not
  • Firewall Rules: Corporate networks may block connections to external data sources

Summary

You've successfully learned how to connect Grafana to various data sources. Remember that data sources are the foundation of all your visualizations - without properly configured connections, you can't build effective dashboards. The key steps are selecting the right data source type, configuring connection details, setting up appropriate authentication, and thoroughly testing the connection.

Quiz

Show quiz
  1. What is the recommended access mode for most data source configurations?
  2. Which authentication method would you use for a Grafana Cloud data source?
  3. What should you always do after configuring a new data source?
  4. Why is the "Server" access mode more secure than "Browser"?
  5. What database permission does a Grafana user typically need?

Answers:

  1. Server access mode - it keeps credentials on the server side
  2. Bearer token authentication - Grafana Cloud APIs typically use token-based auth
  3. Click "Save & Test" - to validate the connection before using it
  4. Credentials stay on server - browser mode exposes credentials to client browsers
  5. SELECT permission - Grafana needs read access to query data