简体   繁体   English

将Phoenix应用程序连接到MS SQL Azure数据库

[英]Connecting Phoenix app to MS SQL Azure database

I am trying to connect a phoenix application to MS SQL. 我正在尝试将凤凰应用程序连接到MS SQL。 After looking around online I came across a couple of adapters called mssql_ecto & mssqlex . 在网上浏览后,我遇到了几个名为mssql_ectomssqlex的适配器。

I have added them to the project following the instructions in the readme, installed odbc and checked that the db is online but I am now getting the following error.. 我按照自述文件中的说明将它们添加到项目中,安装了odbc并检查了数据库是否在线,但我现在收到以下错误..

[error] Mssqlex.Protocol (#PID<0.13069.0>) failed to connect: ** (Mssqlex.Error) odbc_not_started

My app is configured as below.. 我的应用程序配置如下..

config :my_app, MyApp.Repo,
adapter: MssqlEcto,
username: "<my_username>",
password: "<my_password>",
database: "test",
hostname: "<my_server>.database.windows.net", 
pool_size: 10

My environment is as follows.. 我的环境如下..

  • MacOS Sierra 10.12.5 MacOS Sierra 10.12.5
  • Elixir version: 1.4.4 Elixir版本:1.4.4

There is an issue in the mssql_ecto repo already, and I have tried the suggestions from there, but it still has not worked. mssql_ecto repo中已经存在一个问题,我已经尝试了那里的建议,但它仍然无效。

If anyone has managed to connect their phoenix/elixir application to MSSQL on macOS and could provide some instructions it would be greatly appreciated (even if it was done in a completely different way to my approach 😉 ). 如果有人设法将他们的phoenix / elixir应用程序连接到macOS上的MSSQL并且可以提供一些指令,那将非常感激(即使它是以与我的方法完全不同的方式完成的)。

Side note: Tried it in node with the same db and was able to connect to and query the db. 附注:在具有相同数据库的节点中尝试并且能够连接并查询数据库。

I've been working through the same issue this week, connecting to an Azure SQL Database, and this is what I've got working. 本周我一直在处理相同的问题,连接到Azure SQL数据库,这就是我的工作。

You are seeing the first of 2 main problems I ran into: 你看到我遇到的两个主要问题中的第一个:

  1. Erlang installed without ODBC Erlang安装没有ODBC
  2. Mssqlex adapter is missing required configuration options Mssqlex适配器缺少必需的配置选项

#1: Erlang installed without ODBC #1:没有ODBC安装Erlang

If you've installed Elixir and Erlang using brew, or something like it, you are getting an installation of Erlang that does not include erlang-odbc. 如果您使用brew或类似的东西安装了Elixir和Erlang,那么您将获得不包含erlang-odbc的Erlang安装。 In order to remedy this, you will need to install Erlang from source. 为了解决这个问题,您需要从源代码安装Erlang。

I recommend that you install both Erlang and Elixir from source using the method documented here: http://www.lambdacat.com/how-to-install-elixir-on-mac/ 我建议您使用此处记录的方法从源代码安装Erlang和Elixir: http//www.lambdacat.com/how-to-install-elixir-on-mac/

Just make sure that you remove the Kerl option, --without-odbc , before you build Erlang. 在构建Erlang之前,请确保删除Kerl选项--without-odbc

Now that you have Erlang installed correctly, the odbc_not_started error should go away... to be replaced by connection errors to the database. 现在你已经正确安装了Erlang, odbc_not_started错误应该消失......被数据库的连接错误所取代。

#2: Mssqlex adapter is missing required configuration options #2:Mssqlex适配器缺少必需的配置选项

As of this moment, the mssqlex adapter does not support the following configuration options that Azure is looking for: 截至目前, mssqlex适配器不支持Azure正在寻找的以下配置选项:

  1. Encrypt 加密
  2. TrustServerCertificate TrustServerCertificate
  3. Connection Timeout 连接超时

To get around this issue, I forked the mssqlex library and added those options in a branch . 为了解决这个问题,我分叉了mssqlex库在分支中添加了这些选项。 I am not committing to maintaining this so feel free to fork it yourself. 我不是要保持这个,所以请随意自行分叉。

In order to use the modified mssqlex, replace the mix dependency with the github location and branch name: 要使用修改后的mssqlex,请使用github位置和分支名称替换mix依赖项:

{:mssqlex, git: "https://github.com/tvanhouten/mssqlex.git", branch: "azure-settings", override: true}

The override: true is important because mssql_ecto has a dependency on the mssqlex version in hex. override: true非常重要,因为mssql_ecto依赖于十六进制的mssqlex版本。

Now you can update your configuration: 现在您可以更新配置:

config :my_app, MyApp.Repo,
adapter: MssqlEcto,
username: "<my_username>",
password: "<my_password>",
database: "test",
hostname: "<my_server>.database.windows.net", 
pool_size: 10,
encrypt: "yes",
trust_server_certificate: "no",
connection_timeout: "30

Other "Gotchas" to Look Out For 要注意的其他“问题”

Azure requires TCP connection: Azure需要TCP连接:

In order to connect to an Azure SQL Database, you will need to make sure that the hostname: option starts with tcp: . 要连接到Azure SQL数据库,您需要确保hostname:选项以tcp:开头。 So, all together, it will look like hostname: "tcp:.database.windows.net" 所以,总之,它看起来像主机名:“tcp:.database.windows.net”

Mssql_ecto does not support Ecto 2.2+: Mssql_ecto不支持Ecto 2.2+:

If you're getting errors related to Ecto or Ecto.SubQuery, you probably need to make sure that you are using a version of Ecto that is supported by mssql_ecto. 如果您收到与Ecto或Ecto.SubQuery相关的错误,您可能需要确保使用的是mssql_ecto支持的Ecto版本。 At this point in time, that means Ecto 2.1.x. 此时,这意味着Ecto 2.1.x. Again, this requires a slight change to your mix dependencies: 同样,这需要对您的混合依赖项进行轻微更改:

{:ecto, ">= 2.1.0 and < 2.2.0"}

Dependency cache and lock issues: 依赖缓存和锁定问题:

If everything seems to be in order according to the above but it just doesn't seem like the new dependencies are being updated and compiled as expected, do the following: 如果根据上面所有内容似乎都按顺序排列,但似乎新的依赖项似乎没有按预期更新和编译,请执行以下操作:

  1. Run mix deps.clean --all 运行mix deps.clean --all
  2. Delete the mix.lock file from your root project directory 从根项目目录中删除mix.lock文件
  3. Run mix deps.get 运行mix deps.get
  4. Run mix deps.compile 运行mix deps.compile

That should do it! 应该这样做!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM