我的电脑上安装了 PostgreSQL 9.3。当我尝试通过 pgAdminIII 启动数据库时,出现以下错误。
连接到服务器时出错:收到对 SSL 协商的无效响应
我通过编辑配置文件中的端口号将端口号设置为 5432。 但是,当我安装 postgresql 并输入 5432 作为端口号时,它给了我一条错误消息,“无法与此端口建立连接。该端口不可用”,我必须继续使用端口号 5433。稍后我在配置文件中将端口号更改为 5432 并重新启动了我的电脑。但我仍然遇到同样的错误。请帮忙。
我遇到了这个问题,我花了一段时间才意识到我实际上是在尝试使用
psql
客户端连接到 MySQL 服务器..
psql: error: could not connect to server: received invalid response to SSL negotiation: H
如果我们尝试使用 http 模式/协议访问通过某些代理容器服务(如 haproxy/nginx)公开的 postgres 容器服务,我们也会遇到上述错误。
我们需要使用tcp模式/协议。
请根据以下详细信息自行尝试。
nginx.conf
events {}
stream {
upstream postgres {
server pdb:5432;
}
server {
listen 5432;
proxy_pass postgres;
}
}
haproxy.cfg
global
log 127.0.0.1 local1
maxconn 4096
defaults
mode http
maxconn 2048
frontend postgresDB
bind *:5000
mode tcp
timeout connect 5s
timeout client 5s
timeout server 5s
default_backend postgresDB
backend postgresDB
mode tcp
server pdb pdb:5432 check inter 5s rise 2 fall 3
容器部署
sudo docker run -d --name testdb 'POSTGRES_PASSWORD=123456' postgres
sudo docker run -d --name pdb -e 'POSTGRES_USER=admin' -e 'POSTGRES_DB=testdb' -e 'POSTGRES_PASSWORD=admin123456' postgres
sudo docker run -d --name nginx-TCPreverseProxy -p 5432:5432 --link pdb:pdb -v /home/admin/nginx.conf:/etc/nginx/nginx.conf nginx
sudo docker run -d -p 5000:5000 --name haproxy --link pdb:pdb -v /home/admin/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg haproxy
#Testing of connection
sudo docker exec -it -u 0 testdb bash
#Inside test postgres container
#checking connection through nginx-proxy
root@34211600c3f7:/# psql -h 192.168.0.2 -p 5432 -d testdb -U admin -W
#checking connection through haproxy-proxy
root@34211600c3f7:/# psql -h 192.168.0.2 -p 5000 -d testdb -U admin -W
我的问题已经解决了。我杀死了端口号5432的pid,然后重新安装了postgresql,端口号条目为5432。我仍然收到端口号5432的错误。最后转到控制面板->管理工具->服务->右键单击postgresql服务->属性 -> 登录选项卡 -> 选中单选按钮本地系统帐户,然后单击确定。现在启动服务器。它将开始工作。
在我的例子中(WSL 中的 Ubuntu 18.04)我得到了同样的错误,结果是 postgresql 运行的端口号与我的 Ruby on Rails 应用程序
port
文件中指定的 config/database.yml
字段不匹配。
您可以通过运行以下命令找到 Linux 中的 postgresql 端口:
cat /etc/postgresql/12/main/postgresql.conf | grep port
备注:12是postgresql安装的版本,可以根据您的情况有所不同。