我正在使用Ubuntu18.04运行新的Linux 2 (WSL-2) Windows子系统。它非常快,运行得很好,但我似乎无法使用Python连接到外部PostgreSQL数据库。它只是挂着,从来没有回应过。这里有一个最小的复制:
$ python3.6
Python 3.6.8 (default, Jan 14 2019, 11:02:34)
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> psycopg2.connect(host="my-pg-server.mydomain.com", port=5432, user="my_user", dbname="my_db", password="")
[...crickets... doesn't time out, just hangs forever (at least an hour)...]这不是防火墙问题,因为我可以将telnet连接到同一台主机:
$ telnet my-pg-server.mydomain.com 5432
Trying 123.456.789.100...
Connected to my-pg-server.mydomain.com.
Escape character is '^]'.另一个奇怪的部分是,我可以连接到外部Server数据库。我确信这两台服务器的凭据都是正确的,它们直接来自Django设置文件,我在其他系统上使用该文件非常好。有什么想法吗?有什么是我必须做的psycopg2专门为WSL-2?
发布于 2019-08-17 22:33:12
原来我指错方向了。
我与一位同事登录到PostgreSQL服务器框,并发出以下命令:
ps -ef --sort=start_time | fgrep [db host name] | more
事实证明,现有的与服务器的连接是正常的,但是有些东西被阻塞了。我有一堆空闲的进程,然后有大量的进程说“启动等待”--超过100个。下面是命令的输出:
[...about 100 idle processes, truncated...]
postgres 26815 48821 0 Aug16 ? 00:00:00 postgres: my-pg-server: web_user web 192.168.9.187(55972) idle
postgres 27525 48821 0 Aug16 ? 00:00:00 postgres: my-pg-server: web_user web 192.168.9.187(55976) idle
postgres 14781 48821 0 00:00 ? 00:00:00 postgres: my-pg-server: postgres jsmith_d [local] VACUUM waiting
postgres 22738 48821 0 00:01 ? 00:00:00 postgres: my-pg-server: other_user other_db 192.168.9.187(57692) startup waiting
postgres 7683 48821 0 00:15 ? 00:00:00 postgres: my-pg-server: yetanother_user yetanother_db 192.168.9.187(57694) startup waiting
postgres 15951 48821 0 00:30 ? 00:00:00 postgres: my-pg-server: yetanother_user yetanother_db 192.168.9.187(57696) startup waiting
[...and about another 100 startup waiting processes, truncated...]啊哈!警方发现的罪犯:
postgres 14781 48821 0 00:00 ? 00:00:00 postgres: my-pg-server: postgres jsmith_d [local] VACUUM waiting
在VACUUM进程中,似乎有什么东西被阻塞了,这导致新连接挂起,而不会失败。是时候更深入地挖掘和清理它了,但是这种行为的答案是。
https://stackoverflow.com/questions/57539587
复制相似问题