恭喜你完成了第一章的準(zhǔn)備工作!
前面我們提到過,Mix 是 Elixir 提供的構(gòu)建工具。這一章里,我們就用它來創(chuàng)建一個(gè) Phoenix 項(xiàng)目:
$ mix phx.new menu --database mysql
這里,?mix phx.new
? 表示創(chuàng)建一個(gè) Phoenix 項(xiàng)目,?menu
? 指示新項(xiàng)目的路徑,即當(dāng)前目錄下的 menu 目錄。如果目錄已存在,命令會(huì)提示我們是否覆蓋目錄下的內(nèi)容,否則會(huì)新建 ?menu
? 目錄。?--database mysql
? 則表示該項(xiàng)目的數(shù)據(jù)庫類型是 MySQL,而不是默認(rèn)的 PostgreSQL。
?mix phx.new
? 命令在執(zhí)行過程中,會(huì)提示我們:
Fetch and install dependencies? [Yn]
是否安裝依賴?默認(rèn)是 Y,回車即可。如果你輸入 n,后期啟動(dòng) Phoenix 服務(wù)時(shí)還會(huì)提示一遍。
安裝完依賴,我們會(huì)看到如下說明:
We are all set! Go into your application by running: $ cd menu Then configure your database in config/dev.exs and run: $ mix ecto.create Start your Phoenix app with: $ mix phx.server You can also run your app inside IEx (Interactive Elixir) as: $ iex -S mix phx.server
首先,我們需要配置數(shù)據(jù)庫。
打開 ?config/dev.exs
? 文件,可以看到數(shù)據(jù)庫相關(guān)的配置內(nèi)容:
# Configure your database config :menu, Menu.Repo, adapter: Ecto.Adapters.MySQL, username: "root", password: "", database: "menu_dev", hostname: "localhost", pool_size: 10
如果你的 MySQL 數(shù)據(jù)庫?用戶名/密碼
?不是? root/
? 組合,請(qǐng)修改后再執(zhí)行 ?mix ecto.create
?:
? menu mix ecto.create Compiling 13 files (.ex) Generated menu app The database for Menu.Repo has been created
數(shù)據(jù)庫已成功創(chuàng)建。
更多建議: