我正在尝试使用 SSL 在本地连接到 Azure 上托管的 PostgreSQL(v. 9.6)。不幸的是它没有按预期工作。后端在 Docker 容器中运行,以 Alpine Linux 作为基础镜像,Elixir 1.7.4,当前的 ecto 版本带有 ecto-sql (v. 3.0.0)。
在 Azure 上,启用了强制 SSL 连接
并设置了相应的防火墙规则。
我在本地使用了一个数据库工具(TablePlus),可以轻松连接到在线数据库。
这是我的配置:
config :my_app, MyApp.Repo,
username: "admin@postgres-dev",
password: "secret",
database: "dev",
port: 5432,
hostname: "postgres-dev.postgres.database.azure.com",
ssl: true,
ssl_opts: [
versions: [:"tlsv1.2"],
cacertfile: "priv/root.pem"
],
pool_size: 15
此外,我还将 :ssl
添加到 mix.exs
中的 extra_applications
中。
由于answer,我显式设置了版本
这里在 stackoverflow 上。
我从Azure获得了证书并按照此处所述使用 OpenSSL 对其进行解码。
我在没有ssl_opts
的情况下进行了尝试,仅在ssl_opts
处设置了一个属性,并且还使用了database_url
(有或没有查询参数: ?ssl=true
) 而不是单独描述所有属性。
我总是遇到同样的错误:
(Mix) The database for MyApp.Repo couldn't be created: connection not available and request was dropped from queue after 2844ms. You can configure how long requests wait in the queue using :queue_target and :queue_interval. See DBConnection.start_link/2 for more information
使用不同的版本
也会导致错误:
14:39:26.222 [error] GenServer #PID<0.500.0> terminating
** (DBConnection.ConnectionError) ssl connect: Invalid TLS option: {'tlsv1.3',{versions,['tlsv1.3']}} - {:options, {:"tlsv1.3", {:versions, [:"tlsv1.3"]}}}
(db_connection) lib/db_connection/connection.ex:84: DBConnection.Connection.connect/2
(connection) lib/connection.ex:622: Connection.enter_connect/5
(stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
** (Mix) The database for MyApp.Repo couldn't be created: killed
所以这绝对不是一个解决方案
当不设置ssl: true
时,我会收到预期的错误:
10:19:02.036 [error] GenServer #PID<0.251.0> terminating
** (Postgrex.Error) FATAL 28000 (invalid_authorization_specification) SSL connection is required. Please specify SSL options and retry.
(db_connection) lib/db_connection/connection.ex:84: DBConnection.Connection.connect/2
(connection) lib/connection.ex:622: Connection.enter_connect/5
(stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
** (Mix) The database for MyApp.Repo couldn't be created: killed
有没有人已经尝试过这个并且可以提供帮助?
最佳答案
在这里使用 TSL 1.2。
在 TLS 握手中,您连接的服务器将发送其证书。您不需要提供它。
ssl: true,
ssl_opts: [
versions: [:"tlsv1.2"]
]
并避免超时问题......
timeout: 120_000,
queue_target: 10_000,
queue_interval: 100_000
关于postgresql - 使用 SSL 和 Elixir 后端连接到 Azure 上托管的 PostgreSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54185188/