{"_id":"5777c96e5b2b430e00b982c1","version":{"_id":"5777c9635b2b430e00b982a5","__v":1,"project":"54348ec95b10711400c6c445","createdAt":"2016-07-02T14:02:11.084Z","releaseDate":"2016-07-02T14:02:11.084Z","categories":["5777c9635b2b430e00b982a6","5777c9635b2b430e00b982a7","5777c9635b2b430e00b982a8","5777c9635b2b430e00b982a9","5777c9635b2b430e00b982aa"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.2.0","version":"1.2.0"},"category":{"_id":"5777c9635b2b430e00b982aa","version":"5777c9635b2b430e00b982a5","project":"54348ec95b10711400c6c445","__v":0,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-12-03T21:36:49.014Z","from_sync":false,"order":4,"slug":"bonus-guides","title":"Bonus Guides"},"__v":0,"user":"5435b410495d5d0800f3a603","parentDoc":null,"project":"54348ec95b10711400c6c445","updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-06-30T04:45:10.444Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":3,"body":"Phoenix applications are configured to use PostgreSQL by default, but what if we want to use MySQL instead? In this guide, we'll walk through changing that default whether we are about to create a new application, or whether we have an existing one configured for PostgreSQL.\n\nIf we are about to create a new application, configuring our application to use MySQL is easy. We can simply pass the `--database mysql` flag to `phoenix.new` and everything will be configured correctly.\n\n```console\n$ mix phoenix.new hello_phoenix --database mysql\n```\n\nThis will set up all the correct dependencies and configuration for us automatically. Once we install those dependencies with `mix deps.get`, we'll be ready to begin working with Ecto in our application.\n\nIf we have an existing application, all we need to do is switch adapters and make some small configuration changes.\n\nTo switch adapters, we need to remove the Postgrex dependency and add a new one for Mariaex instead.\n\nLet's open up our `mix.exs` file and do that now.\n\n```elixir\ndefmodule HelloPhoenix.Mixfile do\n  use Mix.Project\n\n  . . .\n  # Specifies your project dependencies\n  #\n  # Type `mix help deps` for examples and options\n  defp deps do\n    [{:phoenix, \"~> 1.2.0\"},\n     {:phoenix_ecto, \"~> 3.0\"},\n     {:mariaex, \"~> 0.6.1\", override: true},\n     {:phoenix_html, \"~> 2.3\"},\n     {:phoenix_live_reload, \"~> 1.0\", only: :dev},\n     {:cowboy, \"~> 1.0\"}]\n  end\nend\n```\n\nWe also need to remove the `:postgrex` app from our list of applications and substitute the `:mariaex` app instead. Let's do that in `mix.exs` as well.\n\n```elixir\ndefmodule HelloPhoenix.Mixfile do\n  use Mix.Project\n\n. . .\ndef application do\n  [mod: {MysqlTester, []},\n  applications: [:phoenix, :cowboy, :logger,\n  :phoenix_ecto, :mariaex]]\nend\n. . .\n```\n\nNext, we need to configure our new adapter. Let's open up our `config/dev.exs` file and do that.\n\n```elixir\nconfig :hello_phoenix, HelloPhoenix.Repo,\nadapter: Ecto.Adapters.MySQL,\nusername: \"root\",\npassword: \"\",\ndatabase: \"hello_phoenix_dev\"\n```\n\nIf we have an existing configuration block for our `HelloPhoenix.Repo`, we can simply change the values to match our new ones. The most important thing is to make sure we are using the MySQL adapter `adapter: Ecto.Adapters.MySQL,`.\n\nWe also need to configure the correct values in the `config/test.exs` and `config/prod.secret.exs` files as well.\n\nNow all we need to do is fetch our new dependency, and we'll be ready to go.\n\n```console\n$ mix do deps.get, compile\n```\n\nWith our new adapter installed and configured, we're ready to create our database.\n\n```console\n$ mix ecto.create\nThe database for HelloPhoenix.repo has been created.\n```\n\nWe're also ready to run any migrations, or do anything else with Ecto that we might choose.\n\n```console\n$ mix ecto.migrate\n[info] == Running HelloPhoenix.Repo.Migrations.CreateUser.change/0 forward\n[info] create table users\n[info] == Migrated in 0.2s\n```","excerpt":"","slug":"using-mysql","type":"basic","title":"Using MySQL"}
Phoenix applications are configured to use PostgreSQL by default, but what if we want to use MySQL instead? In this guide, we'll walk through changing that default whether we are about to create a new application, or whether we have an existing one configured for PostgreSQL. If we are about to create a new application, configuring our application to use MySQL is easy. We can simply pass the `--database mysql` flag to `phoenix.new` and everything will be configured correctly. ```console $ mix phoenix.new hello_phoenix --database mysql ``` This will set up all the correct dependencies and configuration for us automatically. Once we install those dependencies with `mix deps.get`, we'll be ready to begin working with Ecto in our application. If we have an existing application, all we need to do is switch adapters and make some small configuration changes. To switch adapters, we need to remove the Postgrex dependency and add a new one for Mariaex instead. Let's open up our `mix.exs` file and do that now. ```elixir defmodule HelloPhoenix.Mixfile do use Mix.Project . . . # Specifies your project dependencies # # Type `mix help deps` for examples and options defp deps do [{:phoenix, "~> 1.2.0"}, {:phoenix_ecto, "~> 3.0"}, {:mariaex, "~> 0.6.1", override: true}, {:phoenix_html, "~> 2.3"}, {:phoenix_live_reload, "~> 1.0", only: :dev}, {:cowboy, "~> 1.0"}] end end ``` We also need to remove the `:postgrex` app from our list of applications and substitute the `:mariaex` app instead. Let's do that in `mix.exs` as well. ```elixir defmodule HelloPhoenix.Mixfile do use Mix.Project . . . def application do [mod: {MysqlTester, []}, applications: [:phoenix, :cowboy, :logger, :phoenix_ecto, :mariaex]] end . . . ``` Next, we need to configure our new adapter. Let's open up our `config/dev.exs` file and do that. ```elixir config :hello_phoenix, HelloPhoenix.Repo, adapter: Ecto.Adapters.MySQL, username: "root", password: "", database: "hello_phoenix_dev" ``` If we have an existing configuration block for our `HelloPhoenix.Repo`, we can simply change the values to match our new ones. The most important thing is to make sure we are using the MySQL adapter `adapter: Ecto.Adapters.MySQL,`. We also need to configure the correct values in the `config/test.exs` and `config/prod.secret.exs` files as well. Now all we need to do is fetch our new dependency, and we'll be ready to go. ```console $ mix do deps.get, compile ``` With our new adapter installed and configured, we're ready to create our database. ```console $ mix ecto.create The database for HelloPhoenix.repo has been created. ``` We're also ready to run any migrations, or do anything else with Ecto that we might choose. ```console $ mix ecto.migrate [info] == Running HelloPhoenix.Repo.Migrations.CreateUser.change/0 forward [info] create table users [info] == Migrated in 0.2s ```