Getting Ecto Friends example to work with MySql

Since I need to work with MySql instead of Postgresql I tried to replace the appropriate sections in the tutorial with what was needed. When I get to the mix ecto.create I get the following error:

** (KeyError) key :database not found in: [telemetry_prefix: [:friends, :repo], otp_app: :friends, timeout: 15000, pool_size: 10]
    (elixir) lib/keyword.ex:393: Keyword.fetch!/2
    lib/ecto/adapters/myxql.ex:139: Ecto.Adapters.MyXQL.storage_up/1
    lib/mix/tasks/ecto.create.ex:53: anonymous fn/3 in Mix.Tasks.Ecto.Create.run/1
    (elixir) lib/enum.ex:783: Enum."-each/2-lists^foreach/1-0-"/2
    (elixir) lib/enum.ex:783: Enum.each/2
    (mix) lib/mix/task.ex:331: Mix.Task.run_task/3
    (mix) lib/mix/cli.ex:79: Mix.CLI.run_task/2

This is my mix.exs deps:

      {:ecto_sql, "~> 3.0" },
      {:myxql, ">= 0.2.0"}

My config.exs:

config :friends, ecto_repos: [Friends.Repo],
  database: "friends_repo",
  username: "root",
  password: "",
  hostname: "LocalDB",       note: the example has localhost but it failed with that and my HeidiSql shows LocalDB
  port: 3306

This is my repo.ex

defmodule Friends.Repo do
  use Ecto.Repo,
    otp_app: :friends,
    adapter: Ecto.Adapters.MyXQL
end

and my application.ex

 def start(_type, _args) do
    children = [
      {MyXQL, username: "root", name: :myxql}     Note: this was recommended by the git documentation
    ]

Hi, while docs for MyXQL indeed mention that, when using it with Ecto we should add the Repo to our supervision tree:

def start(_type, _args) do
    children = [
      Friends.Repo
    ]
2 Likes

The configuration should be more like this:

config :friends, ecto_repos: [Friends.Repo]

config :friends, Friends.Repo,
  database: "friends_repo",
  username: "root",
  password: "",
  hostname: "LocalDB",       note: the example has localhost but it failed with that and my HeidiSql shows LocalDB
  port: 3306

Basically, all you would have had to do was changing dependencies, providing different database credentials and using a different adapter in your Repo. Let us know if it helps!

2 Likes

Thank-you will let you know if this works.

Success!!! I don’t understand everything that is going on here yet, but my goal was to get the Ecto to talk to MySql and it did create the database. The instructions are confusing, and I would like to see a simpler approach for the rank beginner.
This is from the Ecto Getting started tutorial.

Here are the successful files:
Here is the config.exs

use Mix.Config


config :friends, ecto_repos: [Friends.Repo]

config :friends, Friends.Repo,
  database: "friends_repo",
  username: "root",
  password: "",
  hostname: "localhost",
  port: 3306

Here is the repo.ex:

defmodule Friends.Repo do
  use Ecto.Repo,
    otp_app: :friends,
    adapter: Ecto.Adapters.MyXQL
end

And here is the application.ex

defmodule Friends.Application do
  # See https://hexdocs.pm/elixir/Application.html
  # for more information on OTP Applications
  @moduledoc false

  use Application

  def start(_type, _args) do
    children = [
      Friends.Repo
    ]
    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: Friends.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

1 Like