问题描述
在使用Docker部署Grafana和PostgreSQL时,遇到以下认证错误:
1
Error: ✗ pq: no pg_hba.conf entry for host "172.19.0.3", user "postgres", database "grafana", no encryption
问题分析
🔍 错误原因
pg_hba.conf文件缺失 :PostgreSQL容器中没有正确配置客户端认证文件网络访问限制 :Docker网络中的IP地址(172.19.0.3)没有被允许连接用户配置不一致 :Grafana使用的数据库用户与PostgreSQL配置不匹配📋 影响范围
Grafana无法连接到PostgreSQL数据库 数据库迁移工具无法正常工作 应用程序启动失败 解决方案
1. 创建pg_hba.conf文件
创建PostgreSQL客户端认证配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 在grafana目录下创建pg_hba.conf
cat > pg_hba.conf << 'EOF'
# PostgreSQL Client Authentication Configuration File
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 md5
# IPv6 local connections:
host all all ::1/128 trust
# Docker network connections
host all all 172.16.0.0/12 md5
host all all 192.168.0.0/16 md5
host all all 10.0.0.0/8 md5
# Grafana specific connections
host grafana grafana 172.19.0.0/16 md5
host grafana postgres 172.19.0.0/16 md5
EOF
2. 修改docker-compose.yaml
更新PostgreSQL服务配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
version : '3.8'
services :
postgres-primary :
image : postgres:15
environment :
POSTGRES_DB : grafana
POSTGRES_USER : grafana
POSTGRES_PASSWORD : grafana
volumes :
- postgres_data:/var/lib/postgresql/data
- ./postgresql.conf:/etc/postgresql/postgresql.conf
- ./pg_hba.conf:/etc/postgresql/pg_hba.conf
command : postgres -c config_file=/etc/postgresql/postgresql.conf -c hba_file=/etc/postgresql/pg_hba.conf
ports :
- "5432:5432"
grafana :
image : grafana/grafana:12.0.2
container_name : grafana
ports :
- 3000 : 3000
environment :
- GF_DATABASE_URL=postgres://grafana:grafana@postgres-primary:5432/grafana
depends_on :
- postgres-primary
volumes :
postgres_data :
driver : local
3. 重启服务
1
2
3
4
5
6
7
8
# 停止现有服务
docker-compose down
# 清理数据卷(如果需要重新初始化)
docker-compose down -v
# 重新启动服务
docker-compose up -d
验证解决方案
1. 检查PostgreSQL日志
1
2
# 查看PostgreSQL容器日志
docker-compose logs postgres-primary
2. 测试数据库连接
1
2
# 从Grafana容器测试连接
docker-compose exec grafana sh -c "apk add postgresql-client && psql -h postgres-primary -U grafana -d grafana -c 'SELECT version();'"
3. 检查Grafana状态
1
2
3
4
5
# 查看Grafana日志
docker-compose logs grafana
# 访问Grafana Web界面
curl -I http://localhost:3000
预防措施
🛡️ 安全配置
限制网络访问 :
1
2
# 只允许特定网络段访问
host grafana grafana 172.19.0.0/16 md5
使用强密码 :
1
2
environment :
POSTGRES_PASSWORD : "your-strong-password-here"
启用SSL连接 :
1
2
# 在pg_hba.conf中要求SSL
hostssl grafana grafana 172.19.0.0/16 md5
📊 监控配置
连接监控 :
1
2
-- 监控活跃连接
SELECT count ( * ) FROM pg_stat_activity WHERE datname = 'grafana' ;
认证失败监控 :
1
2
# 监控PostgreSQL日志中的认证失败
docker-compose logs postgres-primary | grep "authentication failed"
常见问题
Q1: 修改pg_hba.conf后仍然无法连接
A : 需要重新加载PostgreSQL配置:
1
docker-compose exec postgres-primary pg_ctl reload
Q2: Docker网络IP地址经常变化
A : 使用更宽泛的网络段或固定IP:
1
2
3
4
5
6
networks :
grafana-net :
driver : bridge
ipam :
config :
- subnet : 172.20.0.0 /16
Q3: 生产环境安全考虑
A :
使用专用网络 启用SSL/TLS 定期轮换密码 限制连接数 启用审计日志 相关文档
注意 :在生产环境中,请确保遵循安全最佳实践,包括使用强密码、启用SSL连接和限制网络访问。