【问题标题】:Unable to run `mix ecto.migrate` without password using peer authentication无法使用对等身份验证在没有密码的情况下运行“mix ecto.migrate”
【发布时间】:2019-02-28 07:17:35
【问题描述】:

更新 #2 - 感谢https://github.com/elixir-ecto/ecto/issues/2719

后代的初始问题:

来自 Rails 世界,我不需要像这样为开发配置数据库凭据:

default: &default
  adapter: postgresql

development:
  <<: *default
  database: app_development

这是通过在命令行上手动创建数据库来实现的:

$ sudo -u postgres createdb -O $(whoami) app_development
$ psql app_development
psql (9.5.14)
Type "help" for help.

app_development=>

这是因为默认情况下 Postgresql 在 Ubuntu 上使用对等身份验证。现在,尝试用 Phoenix + Ecto 做同样的事情似乎并不奏效,但我尝试了。我在运行mix ecto.migrate 时遇到这样的错误:

[error] GenServer #PID<0.504.0> terminating
** (RuntimeError) Connect raised a KeyError error. The exception details are hidden, as
they may contain sensitive data such as database credentials.

    (elixir) lib/keyword.ex:386: Keyword.fetch!/2
    (postgrex) lib/postgrex/protocol.ex:610: Postgrex.Protocol.auth_md5/4
    (postgrex) lib/postgrex/protocol.ex:504: Postgrex.Protocol.handshake/2
    (db_connection) lib/db_connection/connection.ex:135: 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
** (exit) exited in: :gen_server.call(#PID<0.504.0>, {:checkout, #Reference<0.3638587783.2274885638.15614>, true, :infinity}, 5000)
    ** (EXIT) an exception was raised:
        ** (RuntimeError) Connect raised a KeyError error. The exception details are hidden, as
they may contain sensitive data such as database credentials.

            (elixir) lib/keyword.ex:386: Keyword.fetch!/2
            (postgrex) lib/postgrex/protocol.ex:610: Postgrex.Protocol.auth_md5/4
            (postgrex) lib/postgrex/protocol.ex:504: Postgrex.Protocol.handshake/2
            (db_connection) lib/db_connection/connection.ex:135: 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
    (db_connection) lib/db_connection/poolboy.ex:112: DBConnection.Poolboy.checkout/3
    (db_connection) lib/db_connection.ex:928: DBConnection.checkout/2
    (db_connection) lib/db_connection.ex:750: DBConnection.run/3
    (db_connection) lib/db_connection.ex:1141: DBConnection.run_meter/3
    (db_connection) lib/db_connection.ex:592: DBConnection.prepare_execute/4
    (ecto) lib/ecto/adapters/postgres/connection.ex:86: Ecto.Adapters.Postgres.Connection.execute/4
    (ecto) lib/ecto/adapters/sql.ex:256: Ecto.Adapters.SQL.sql_call/6
    (ecto) lib/ecto/adapters/sql.ex:198: Ecto.Adapters.SQL.query!/5
    (ecto) lib/ecto/adapters/postgres.ex:96: anonymous fn/4 in Ecto.Adapters.Postgres.execute_ddl/3
    (elixir) lib/enum.ex:1925: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto) lib/ecto/adapters/postgres.ex:96: Ecto.Adapters.Postgres.execute_ddl/3
    (ecto) lib/ecto/migrator.ex:44: anonymous fn/2 in Ecto.Migrator.migrated_versions/2
    (ecto) lib/ecto/migrator.ex:300: Ecto.Migrator.verbose_schema_migration/3
    (ecto) lib/ecto/migrator.ex:157: Ecto.Migrator.run/4
    (ecto) lib/mix/tasks/ecto.migrate.ex:83: anonymous fn/4 in Mix.Tasks.Ecto.Migrate.run/2
    (elixir) lib/enum.ex:765: Enum."-each/2-lists^foreach/1-0-"/2
    (elixir) lib/enum.ex:765: Enum.each/2
    (mix) lib/mix/task.ex:316: Mix.Task.run_task/3
    (mix) lib/mix/cli.ex:79: Mix.CLI.run_task/2

我是否遗漏了什么或者是使用 PostgreSQL 和 Ecto 创建特定用户和密码的唯一方法?在开发/测试恕我直言,依赖对等身份验证要容易得多。

更新 #1

更多信息以及如何重现此问题:

$ mix phx.new --version
Phoenix v1.3.4
$ mix phx.new --no-brunch hello
* creating hello/config/config.exs
...
Fetch and install dependencies? [Yn] y
* running mix deps.get
* running mix deps.compile
...
$ cd hello
$ sudo -u postgres createdb -O $(whoami) hello_dev
$ psql hello_dev -c "select 1"
 ?column?
----------
        1
(1 row)

config/dev.exs 关于数据库有这个:

config :hello, Hello.Repo,
  adapter: Ecto.Adapters.Postgres,
  database: "hello_dev"

运行 mix ecto.migrate 会产生上述错误。

在 mix.lock 中有 ecto 的 2.2.10 版本和 phoenix_ecto 的 3.4.0 版本。

【问题讨论】:

  • ecto 是如何连接的?是使用IP还是管道?它从哪个 IP 地址连接?
  • 我尝试了ecto配置,指定我的linux用户名,没有指定它,将主机名指定为localhost,没有指定它等等。也许问题是我需要以某种方式配置它而不是默认情况下而且我只是不知道它接受什么作为对等身份验证的配置。
  • 请分享您的config/dev.exs 的内容(如果存在)(默认情况下)。
  • @mudasobwa 更新帖子以提供更多信息

标签: postgresql elixir phoenix-framework ecto


【解决方案1】:

我的config.exs 中有这个:

use Mix.Config

config :test_ecto, ecto_repos: [TestEcto]

config :test_ecto, TestEcto,
  database: "test_ecto"

通过createdb test_ecto 创建数据库后,它工作得很好(我正在使用 ecto 自述文件中的一些示例代码):

iex -S mix
Erlang/OTP 21 [erts-10.0.6] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe] [dtrace]

Compiling 2 files (.ex)
Generated test_ecto app
Interactive Elixir (1.7.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> TestEcto.start_link()
{:ok, #PID<0.250.0>}
iex(2)> TestEcto.pipe_query()

22:33:21.802 [debug] QUERY ERROR source="weather" db=3.9ms
SELECT w0."id", w0."city", w0."temp_lo", w0."temp_hi", w0."prcp" FROM "weather" AS w0 WHERE (w0."city" = 'Kraków') ORDER BY w0."temp_lo" LIMIT 10 []
** (Postgrex.Error) ERROR 42P01 (undefined_table): relation "weather" does not exist
    (ecto) lib/ecto/adapters/sql.ex:431: Ecto.Adapters.SQL.execute_and_cache/7
    (ecto) lib/ecto/repo/queryable.ex:133: Ecto.Repo.Queryable.execute/5
    (ecto) lib/ecto/repo/queryable.ex:37: Ecto.Repo.Queryable.all/4

您确定不是通过网络连接到数据库吗?

【讨论】:

  • 我将适配器设置为Ecto.Adapters.Postgres。当我尝试不使用 adapter 时,就像在您的示例中那样,我收到如下错误:“(ArgumentError)缺少:配置中的适配器配置:test_ecto,TestEcto”。会不会是我尝试使用 phoenix 进行连接而您只是使用 ecto 本身?
  • @JarmoPertman 在回答您的最后一个问题之前——您使用的是哪个版本的 elixir 和 ecto?
猜你喜欢
  • 1970-01-01
  • 2016-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
相关资源
最近更新 更多