跨数据库Ecto配置失败求助:PostgreSQL与MySQL双库配置问题
Hey there, let's work through the common pitfalls when setting up multiple Ecto repositories across Postgres and MySQL—here’s how to troubleshoot your failed setup:
First up, let’s make sure your Mariaex dependency is playing nice:
- Run
mix deps.compile mariaexto check for compilation errors. The version~> 0.8.2you’re using is pretty old—Ecto 3.x+ typically requires Mariaex~> 0.9.1or newer. Try updating yourmix.exsdependency to a more recent compatible version, then runmix deps.getagain. - Double-check that
postgrexandmariaexare both listed in yourmix.lockfile after runningmix deps.get—if either is missing, there was an issue pulling the dependency.
Your truncated config might be missing critical details, and syntax/naming could be off. Here’s a full, proper example for your config/dev.exs:
config :drupex, Drupex.Repo, adapter: Ecto.Adapters.Postgres, username: "your_postgres_username", password: "your_postgres_password", database: "drupex_dev", hostname: "localhost", pool_size: 10 config :drupex, Drupex.DrupalRepo, adapter: Ecto.Adapters.MySQL, # Note the capitalized "MySQL"—this is the official adapter name username: "your_mysql_username", password: "your_mysql_password", database: "your_drupal_db_name", hostname: "localhost", pool_size: 10
Pro tip: Using the exact adapter name (Ecto.Adapters.MySQL instead of Mysql) avoids case-sensitivity issues that can pop up in some environments.
A common oversight is forgetting to add the second repo to your application’s supervision tree. Open lib/drupex/application.ex and update the start/2 function to include both repos:
def start(_type, _args) do children = [ Drupex.Repo, Drupex.DrupalRepo # Add this line to start the MySQL repo ] Supervisor.start_link(children, strategy: :one_for_one) end
If the repo isn’t in the supervision tree, it won’t initialize when your app starts, leading to "repo not started" errors.
Fire up an interactive Elixir session with iex -S mix and run these commands to test each repo:
# Test Postgres repo Drupex.Repo.start_link() # Test MySQL repo Drupex.DrupalRepo.start_link()
If either fails, you’ll get a detailed error message (e.g., invalid credentials, database doesn’t exist, hostname unreachable) that tells you exactly what’s wrong. You can also try creating the database explicitly for the MySQL repo with:
mix ecto.create -r Drupex.DrupalRepo
This command will fail fast with a clear reason if your config is off.
Make sure your Ecto version supports both adapters. Most modern Ecto versions (2.x and 3.x) work with both Postgres and MySQL, but if you’re on a very old Ecto version, you might need to adjust your dependency versions. Run mix deps to see your current Ecto version, and cross-reference it with the Mariaex and Postgrex docs to ensure compatibility.
内容的提问来源于stack exchange,提问作者chx

