Elixir: Ecto/Tds error when connecting to SQL Server database - sql-server

I've written an elixir application that connects to two different databases, and I have it working nicely in my remote database dev environment -- connecting with the same exact setup to a different database that is across my vpn. When I run it against a different remote SQL Server database, I get a strange error:
** (DBConnection.ConnectionError) bad return value: {:error, %Tds.Protocol{env: %{trans: <<0>>}, itcp: nil, opts: [password:
:REDACTED, idle_timeout: 5000, name: EventBridge.C3poRepo.Pool,
otp_app: :event_bridge, repo: EventBridge.C3poRepo, timeout: 15000,
pool_timeout: 5000, adapter: Tds.Ecto, username: :ALSO_REDACTED,
database: :REDACTED, hostname: :REDACTED, pool:
DBConnection.Poolboy], pak_data: "", pak_header: "", query:
%Tds.Query{handle: 1, statement: nil}, result: %Tds.Result{columns:
[], command: nil, num_rows: 0, rows: []}, sock: {:gen_tcp,
#Port<0.6472>}, state: :ready, tail: "", transaction: nil, usock: nil}}
The credentials I've got in my configuration for this database are correct--I verified them manually with DBeaver. I believe I'm capable of debugging this but I've run into a bit of that error message that I don't understand. Specifically:
{:error, %Tds.Protocol{env: %{trans: <<0>>}
For the sake of completeness, here the deps section in mix.exs:
defp deps do
[
{:tds_ecto, "~> 2.0.3"},
{:postgrex, ">= 0.0.0"},
{:gen_stage, "~> 0.12"},
{:timex, "~> 3.1"},
{:timex_ecto, "~> 3.0"},
{:poison, "~> 3.1"},
{:ecto, "~> 2.1"}
]
end
What I'm asking is for some insight into what that part of the error message means, or perhaps even some more general feedback on the entire error output.
Here are the two instances of configuration for the Repo in question. First the one that works:
config :event_bridge, EventBridge.FirstRepo,
adapter: Tds.Ecto,
database: "first_repo_development",
username: "testuser",
password: "password_redacted",
hostname: "server_one"
And here is the configuration for the one that doesn't work:
config :event_bridge, EventBridge.FirstRepo,
adapter: Tds.Ecto,
database: "first_repo_staging",
username: "staging_username",
password: "password_redacted",
hostname: "server_twelve"
Both work in DBeaver.

Add
pool_size:
in the db config and try again

Related

Receiving error when using mix ecto.create

This is my configuration for the database:
config :hello, Hello.Repo,
username: "postgres",
password: "admin",
hostname: "localhost",
database: "hello_dev_postgres",
show_sensitive_data_on_connection_error: true,
pool_size: 10
config :hello, Hello.MRepo,
adapter: Tds.Ecto,
username: "sa",
password: "server",
hostname: "localhost",
instance: "SERVER",
port: 1433,
database: "hello_dev_mssql",
show_sensitive_data_on_connection_error: true,
pool_size: 10
And this is my two repo:
defmodule Hello.MRepo do
use Ecto.Repo,
otp_app: :hello,
adapter: Ecto.Adapters.Tds
end
defmodule Hello.Repo do
use Ecto.Repo,
otp_app: :hello,
adapter: Ecto.Adapters.Postgres
end
When I run mix ecto.create, I get this error:
16:37:43.867 [error] GenServer #PID<0.309.0> terminating
** (Tds.Error) tcp connect: econnrefused
(db_connection 2.4.2) lib/db_connection/connection.ex:100: DBConnection.Connection.connect/2
(connection 1.1.0) lib/connection.ex:622: Connection.enter_connect/5
(stdlib 3.17) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: nil
State: Tds.Protocol
(Mix) The database for Hello.MRepo couldn't be created: killed
Have you enabled TCP in your database configuration file?
There is a similar post on the elixirforum where someone had the same problem as you and enabling TCP in one of the configuration file helped them.

How do I connect Ecto to CockroachDB Serverless?

I'd like to use CockroachDB Serverless for my Ecto application. How do I specify the connection string?
I get an error like this when trying to connect.
[error] GenServer #PID<0.295.0> terminating
** (Postgrex.Error) FATAL 08004 (sqlserver_rejected_establishment_of_sqlconnection) codeParamsRoutingFailed: missing cluster name in connection string
(db_connection 2.4.1) lib/db_connection/connection.ex:100: DBConnection.Connection.connect/2
CockroachDB Serverless says to connect by including the cluster name in the connection string, like this:
postgresql://username:<ENTER-PASSWORD>#free-tier.gcp-us-central1.cockroachlabs.cloud:26257/defaultdb?sslmode=verify-full&sslrootcert=$HOME/.postgresql/root.crt&options=--cluster%3Dcluster-name-1234
but I'm not sure how to get Ecto to create this connection string via its configuration.
The problem is that Postgrex is not able to parse all of the information from the connection URL - notable the SSL configuration. The solution is to specify the connection parameters explicitly, including the cacertfile SSL option. Assuming that you have downloaded your cluster's CA certificate to priv/certs/ca-cert.crt, you can use the following config as a template:
config :my_app, MyApp.Repo,
username: "my_user",
password: "my_password",
database: "defaultdb",
hostname: "free-tier.gcp-us-central1.cockroachlabs.cloud",
port: "26257",
ssl: true,
ssl_opts: [
cacertfile: Path.expand("priv/certs/ca-cert.crt"),
],
parameters: [options: "--cluster=my-cluster-123"]
Possible Other Issues
Table Locking
Since that CockroachDB also does not support the locking that Ecto/Postgrex attempts on the migration table, the :migration_lock config needs to be disabled as well:
config :my_app, MyApp.Repo,
# ...
migration_lock: false
Auth generator
Finally, the new phx.gen.auth generator defaults to using the citext extension for storing a user's email address in a case-insensitive manner. The line in the generated migration that executes CREATE EXTENSION IF NOT EXISTS citext should be removed, and the column type for the :email field should be changed from :citext to :string.
This configuration allows Ecto to connect to CockroachDB Serverless correctly:
config :myapp, MyApp.repo,
username: "username",
password: "xxxx",
database: "defaultdb",
hostname: "free-tier.gcp-us-central1.cockroachlabs.cloud",
port: 26257,
ssl: true,
ssl_opts: [
cert_pem: "foo.pem",
key_pem: "bar.pem"
],
show_sensitive_data_on_connection_error: true,
pool_size: 10,
parameters: [
options: "--cluster=cluster-name-1234"
]

Getting started with grails 3.3.9 and PostgreSQL 12.7: Error "org.postgresql.Driver"

i tried to connect a postgresdatabase in my grails 3.3.9 project;
My posgrest server is working, since I can connect and operate on the posgres database from Intelli J 2021 databse, but I can't connect to grails 3.3.9.
The password and user are correct but it always throws me this error:
Running application...
2021-07-05 19:06:25.298 ERROR --- [ main] org.postgresql.Driver : Connection error:
org.postgresql.util.PSQLException: El servidor requiere autenticación basada en contraseña, pero no se ha provisto ninguna contraseña.
And this mi Application.yml
hibernate:
cache:
queries: false
use_second_level_cache: false
use_query_cache: false
dataSource:
IkebanaUsuarios:
pooled: true
jmxExport: true
driverClassName: "org.postgresql.Driver"
username: "postgres"
password: "postgres"
environments:
development:
dataSource:
dbCreate: update
url: jdbc:postgresql://localhost:5432/IkebanaERP
test:
dataSource:
dbCreate: update
url: jdbc:postgresql://localhost:5432/IkebanaERP
production:
dataSource:
dbCreate: update
url: jdbc:postgresql://localhost:5432/IkebanaERP
properties:
jmxEnabled: true
initialSize: 5
maxActive: 50
minIdle: 5
maxIdle: 25
maxWait: 10000
maxAge: 600000
timeBetweenEvictionRunsMillis: 5000
minEvictableIdleTimeMillis: 60000
validationQuery: SELECT 1
validationQueryTimeout: 3
validationInterval: 15000
testOnBorrow: true
testWhileIdle: true
testOnReturn: false
jdbcInterceptors: ConnectionState
defaultTransactionIsolation: 2 # TRANSACTION_READ_COMMITTED
and my build.graddle is this
.......
compile "org.grails.plugins:cache"
compile "org.grails.plugins:async"
compile "org.grails.plugins:scaffolding"
compile "org.grails.plugins:events"
compile "org.grails.plugins:hibernate5"
compile "org.hibernate:hibernate-core:5.1.16.Final"
compile "org.grails.plugins:gsp"
console "org.grails:grails-console"
profile "org.grails.profiles:web"
runtime "org.glassfish.web:el-impl:2.1.2-b03"
runtime "com.h2database:h2"
runtime "org.apache.tomcat:tomcat-jdbc"
runtime "com.bertramlabs.plugins:asset-pipeline-grails:2.15.1"
runtime 'org.xerial:sqlite-jdbc:3.6.17'
runtime 'org.postgresql:postgresql:9.4.1208.jre1.8'
runtime 'mysql:mysql-connector-java:5.1.29'
runtime 'org.postgresql:postgresql:42.2.1.jre7'
testCompile "org.grails:grails-gorm-testing-support"
testCompile "org.grails.plugins:geb"
testCompile "org.grails:grails-web-testing-support"
testRuntime "org.seleniumhq.selenium:selenium-htmlunit-driver:2.47.1"
testRuntime "net.sourceforge.htmlunit:htmlunit:2.18"
}
Help pleases; thanks a lot
You are defining properties for your IkebanaUsuarios dataSource, but not for the default one used by Grails.
You may remove the IkebanaUsuarios block, leaving this:
dataSource:
pooled: true
jmxExport: true
driverClassName: "org.postgresql.Driver"
username: "postgres"
password: "postgres"
Or, if you needed that secondary datasource as well, you may define properties for both. I'm guessing you did not need it, since you did not mention intentionally having two.

After specifying an alternate datasource, grails is throwing a sql exception for embedded h2

I have a small set of grails 3.0.11 applications. We have a domain module which is shared between the applications.
When one of the apps(app1, app2, etc) starts up, it connects to the datasource and creates a table for every class in the domain module. All of the apps in the suite will attempt to create these tables.
I modified the application.yml so that it would use an MSSQL instance rather than the internal h2 db, and I set dbCreate to update so that the schema will persist through shutdowns of the application.
I am trying to split up the domain so that each application will only manage the schema for its relevant classes. ie, app1 will handle ddl for classA, classB, and classC on startup, and app2 will handle classX, classY, classZ.
Following this guide, I have defined a second datasource unique to each app (i.e., app1db, app2db, etc), and I have added mapping = { datasource 'appXdb'} to each class specifying the relevant app.
Now, when I start the app, I am getting a sql exception:
Caused by: java.sql.SQLException: Driver:jTDS 1.3.1 returned null for
URL:jdbc:h2:mem:grailsDB;MVCC=TRUE;LOCK_TIMEOUT=10000
Why is my app still trying to access an h2 db after redefining datasource to point to an mssql instance and adding a second datasource which also points to mssql?
application.yml:
---
server:
port: 3434
contextPath: '/app1'
---
grails:
profile: web
codegen:
defaultPackage: cars.app
info:
app:
name: '#info.app.name#'
version: '#info.app.version#'
grailsVersion: '#info.app.grailsVersion#'
spring:
groovy:
template:
check-template-location: false
---
grails:
mime:
disable:
accept:
header:
userAgents:
- Gecko
- WebKit
- Presto
- Trident
types:
all: '*/*'
atom: application/atom+xml
css: text/css
csv: text/csv
form: application/x-www-form-urlencoded
html:
- text/html
- application/xhtml+xml
js: text/javascript
json:
- application/json
- text/json
multipartForm: multipart/form-data
pdf: application/pdf
rss: application/rss+xml
text: text/plain
hal:
- application/hal+json
- application/hal+xml
xml:
- text/xml
- application/xml
urlmapping:
cache:
maxsize: 1000
controllers:
defaultScope: singleton
converters:
encoding: UTF-8
views:
default:
codec: html
gsp:
encoding: UTF-8
htmlcodec: xml
codecs:
expression: html
scriptlets: html
taglib: none
staticparts: none
---
hibernate:
cache:
queries: false
use_second_level_cache: true
use_query_cache: false
region.factory_class: 'org.hibernate.cache.ehcache.EhCacheRegionFactory'
endpoints:
jmx:
unique-names: true
shutdown:
enabled: true
dataSources:
dataSource:
pooled: true
jmxExport: true
driverClassName: net.sourceforge.jtds.jdbc.Driver
username: grails
password: password
app1DataSource:
pooled: true
jmxExport: true
driverClassName: net.sourceforge.jtds.jdbc.Driver
username: grails
password: password
environments:
development:
dataSource:
dbCreate: update
url: jdbc:jtds:sqlserver://127.0.0.1;databaseName=cars_demo
appDataSource:
dbCreate: update
url: jdbc:jtds:sqlserver://1127.0.0.1;databaseName=cars_demo
dataSource:
dbCreate: update
url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1
production:
dataSource:
dbCreate: update
url: jdbc:h2:mem:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1
properties:
jmxEnabled: true
initialSize: 5
maxActive: 50
minIdle: 5
maxIdle: 25
maxWait: 10000
maxAge: 600000
timeBetweenEvictionRunsMillis: 5000
minEvictableIdleTimeMillis: 60000
validationQuery: SELECT 1
validationQueryTimeout: 3
validationInterval: 15000
testOnBorrow: true
testWhileIdle: true
testOnReturn: false
jdbcInterceptors: ConnectionState
defaultTransactionIsolation: 2 # TRANSACTION_READ_COMMITTED
mapping element: static mapping = {datasource 'app1DataSource'}
edit1: added application.yml and mapping element.
turns out, I had not properly nested my environmental overwrites in application.yml. I added a parent element dataSources: about the actual ds defs in the main section but did not do the same in the environment sections which mean that my ds's were loading w/o a url which then defaulted to grails h2 db.
Thanks #quindimildev! I recognized my oversight while trying to figure out formatting to follow your suggestion.
In my case, I had to clear all old compiled files which had an old configuration in them.
To make sure I had a fresh compile I did the following:
grails clean-all
grails refresh-dependencies
grails compile
grails prod war
After that the error that returned "null for URL:jdbc:h2:mem:grailsDB;MVCC=TRUE;LOCK_TIMEOUT=10000" went away.

RabbitMQ logins via LDAP failing

We are currently running RabbitMQ 3.5.6, and though it is able to successfully bind to the LDAP server, logins to the management UI via LDAP credentials are failing. I've been unable to track down the cause of this.
Our end goal is to have users be able to log into the RabbitMQ management UI with their LDAP credentials, and have RabbitMQ assign them permissions based on the groups that they are a member of in LDAP.
Upon login, both with a local account that I created for testing purposes and with my LDAP credentials I am presented with an internal server error:
Got response code 500 with body {"error":"Internal Server Error","reason":"{error,\n {try_clause,\n [{\"CN=rabbit,OU=System,OU=People,DC=domain,DC=tld\",\n \"LDAP_PASSWORD\"}]},\n [{rabbit_auth_backend_ldap,with_ldap,3,\n [{file,\"rabbitmq-auth-backend-ldap/src/rabbit_auth_backend_ldap.erl\"},\n {line,271}]},\n {rabbit_auth_backend_ldap,user_login_authentication,2,\n [{file,\"rabbitmq-auth-backend-ldap/src/rabbit_auth_backend_ldap.erl\"},\n {line,59}]},\n {rabbit_access_control,try_authenticate,3,\n [{file,\"src/rabbit_access_control.erl\"},{line,91}]},\n {rabbit_access_control,'-check_user_login/2-fun-0-',4,\n [{file,\"src/rabbit_access_control.erl\"},{line,77}]},\n {lists,foldl,3,[{file,\"lists.erl\"},{line,1262}]},\n {rabbit_mgmt_util,is_authorized,6,\n [{file,\"rabbitmq-management/src/rabbit_mgmt_util.erl\"},{line,121}]},\n {webmachine_resource,resource_call,3,\n [{file,\n \"webmachine-wrapper/webmachine-git/src/webmachine_resource.erl\"},\n {line,186}]},\n {webmachine_resource,do,3,\n [{file,\n \"webmachine-wrapper/webmachine-git/src/webmachine_resource.erl\"},\n {line,142}]}]}\n"}
The rabbitmq.config that I am currently using is below, followed by the log entries generated by RabbitMQ.
%% -*- mode: erlang -*-
[
{rabbit,
[{tcp_listeners, []},
{ssl_listeners, [{"10.7.232.1", 5672}]},
{log_levels, [{connection, info}, {channel, info}]},
{reverse_dns_lookups, true},
{ssl_options, [{certfile, "/usr/local/etc/rabbitmq/rmqs01.cer"},
{keyfile, "/usr/local/etc/rabbitmq/rmqs01.key"}]},
{auth_backends, [rabbit_auth_backend_ldap, rabbit_auth_backend_internal]},
{auth_mechanisms, ['PLAIN']}
]},
{rabbitmq_auth_backend_ldap,
[{servers, ["dc01.domain.tld", "dc02.domain.tld", "dc03.domain.tld"]},
%%{user_dn_pattern, "cn=${username},ou=People,dc=domain,dc=tld"},
{user_dn_pattern, []},
{use_starttls, true},
%% necessary for our ldap setup
{dn_lookup_attribute, "sAMAccountName"},
{dn_lookup_base, "OU=People,DC=domain,DC=tld"},
{dn_lookup_bind, [{"CN=rabbit,OU=System,OU=People,DC=domain,DC=tld", "rmqpassword"}]},
{port, 389},
{timeout, 30000},
{other_bind, [{"CN=rabbit,OU=System,OU=People,DC=domain,DC=tld", "rmqpassword"}]},
{log, network},
%% ACL testing
{resource_access_query,
{for, [{resource, exchange,
{for, [{permission, configure,
{ in_group, "OU=Systems,OU=People,DC=domain,DC=tld" } },
{permission, write, {constant, true}},
{permission, read, {constant, true}}
]}},
{resource, queue, {constant, true}} ]}}
]},
{rabbitmq_management,
%%{http_log_dir, "/var/log/rabbitmq/access.log"},
[{listener,
[{port, 15672},
{ip, "10.7.232.1"},
{ssl, true},
{ssl_opts,
[{certfile, "/usr/local/etc/rabbitmq/rmqs01.cer"},
{keyfile, "/usr/local/etc/rabbitmq/rmqs01.key"}
]}
]}
]}
].
=INFO REPORT==== 10-May-2016::10:17:47 ===
LDAP CHECK: login for username
=INFO REPORT==== 10-May-2016::10:17:47 ===
LDAP connecting to servers: ["dc01.domain.tld",
"dc02.domain.tld",
"dc03.domain.tld"]
=ERROR REPORT==== 10-May-2016::10:17:47 ===
webmachine error: path="/api/whoami"
{error,
{try_clause,
[{"CN=rabbit,OU=System,OU=People,DC=domain,DC=tld",
"rmqpassword"}]},
[{rabbit_auth_backend_ldap,with_ldap,3,
[{file,"rabbitmq-auth-backend-ldap/src/rabbit_auth_backend_ldap.erl"},
{line,271}]},
{rabbit_auth_backend_ldap,user_login_authentication,2,
[{file,"rabbitmq-auth-backend-ldap/src/rabbit_auth_backend_ldap.erl"},
{line,59}]},
{rabbit_access_control,try_authenticate,3,
[{file,"src/rabbit_access_control.erl"},{line,91}]},
{rabbit_access_control,'-check_user_login/2-fun-0-',4,
[{file,"src/rabbit_access_control.erl"},{line,77}]},
{lists,foldl,3,[{file,"lists.erl"},{line,1262}]},
{rabbit_mgmt_util,is_authorized,6,
[{file,"rabbitmq-management/src/rabbit_mgmt_util.erl"},{line,121}]},
{webmachine_resource,resource_call,3,
[{file,
"webmachine-wrapper/webmachine-git/src/webmachine_resource.erl"},
{line,186}]},
{webmachine_resource,do,3,
[{file,
"webmachine-wrapper/webmachine-git/src/webmachine_resource.erl"},
{line,142}]}]}

Resources