Debezium Connector for SQL Server - connection refused - sql-server

I want to use Kafka to publish MSSQL CDC events.
I am using Docker containers for:
debezium/zookeeper
debezium/kafka
debezium/connect
Microsoft SQL Server
Containers started as follows:
docker run -it --name zookeeper -p 2181:2181 -p 2888:2888 -p 3888:3888 debezium/zookeeper
docker run -it --name kafka -p 9092:9092 --link zookeeper:zookeeper debezium/kafka
docker run -it --name connect -p 8083:8083 -e GROUP_ID=1 -e CONFIG_STORAGE_TOPIC=my-connect-configs -e OFFSET_STORAGE_TOPIC=my-connect-offsets -e ADVERTISED_HOST_NAME="localhost" --link zookeeper:zookeeper --link kafka:kafka debezium/connect
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=xxxxxxxxxxxxx" -p 1433:1433 --name sql1 -d mcr.microsoft.com/mssql/server:2017-CU8-ubuntu
All containers start running successfully.
Then I created new MSSQL db in SQL Server container. Created 1 table in new db and turned on CDC for that table. CDC is working fine.
Then I send connector configuration below to Kafka Connect REST API as follows:
curl -X POST -H "Content-Type: application/json" -d #test-mssql-connector.json http://localhost:8083/connectors
using test-mssql-connector.json
{
"name": "test-mssql-connector5",
"config": {
"connector.class": "io.debezium.connector.sqlserver.SqlServerConnector",
"database.hostname": "localhost",
"database.port": "1433",
"database.user": "SA",
"database.password": "xxxxxxxxxxxxx",
"database.dbname": "test",
"database.server.name": "sql1",
"table.whitelist": "dbo.Persons",
"database.history.kafka.bootstrap.servers": "kafka:9092",
"database.history.kafka.topic": "dbhistory.sql1"
}
}
However, Kafka connector cannot connect to the MSSQL db giving error message below:
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection
to the host localhost, port 1433 has failed. Error: \"Connection
refused. Verify the connection properties. Make sure that an instance
of SQL Server is running on the host and accepting TCP/IP connections
at the port. Make sure that TCP connections to the port are not
blocked by a firewall.
Most troubleshooting are if database actually running, or port is blocked, but there is no problem with new MSSQL db. It's container is active, and the db is successfully running. The port is not blocked. I can successfully connect to it from host machine using DbVisualizer or other query tools with following configuration:
database server = localhost
database port = 1433
user = SA
pw = xxxxxxxxxxxxx
database name = test
I can successfully use telnet localhost 1433 to connect to server.
Is there something missing in the connector configuration above?

IMHO the localhost is not correct as localhost is something else in Connect container and in SQL Server container. You should link the database container into Connect container and use the appropriate hostname.

You need to first set up your sql container and THEN only start the connect service specifying the sql server as an additional link:
docker run -it --name connect -p 8083:8083 -e GROUP_ID=1 -e CONFIG_STORAGE_TOPIC=my-connect-configs -e OFFSET_STORAGE_TOPIC=my-connect-offsets -e ADVERTISED_HOST_NAME="localhost" --link zookeeper:zookeeper --link kafka:kafka --link kafka:kafka debezium/connect

Related

SQL Server not initating a DB on docker deployment

I know this has been covered multiple times, but I still cannot execute an SQL script on docker deployment.
docker-compose
version: '3.9'
services:
mssql:
build: ./docker/mssql
container_name: db
ports:
- 1433:1433
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=Welcome#toSQL2022
- MSSQL_PID=Developer
volumes:
- ./docker/mssql/db-data:/var/opt/mssql/data
- ./docker/mssql/scripts:/var/opt/mssql/scripts
networks:
- supernet
networks:
supernet: {}
Dockerfile
FROM mcr.microsoft.com/mssql/server:2019-CU15-ubuntu-20.04
COPY ./create-from-mdf.sql .
EXPOSE 1433
#tried this: CMD ["/opt/mssql-tools/bin/sqlcmd", "-i", "create-from-mdf.sql", "-S", "localhost", "-U", "SA", "-P" "Welcome#toSQL2022"]
#tried this: CMD sleep 30 && /opt/mssql-tools/bin/sqlcmd -S localhost,1433 -U sa -P Welcome#toSQL2022 -i create-from-mdf.sql#
CMD /opt/mssql-tools/bin/sqlcmd -S localhost,1433 -U sa -P Welcome#toSQL2022 -i create-from-mdf.sql
I get this error:
db | Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
db | Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2749.
db | Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..
db exited with code 1
However, if I connect to container via CLI the same command gets executed succefully:
/opt/mssql-tools/bin/sqlcmd -S localhost,1433 -U sa -P Welcome#toSQL2022 -i create-from-mdf.sql

I can't connect to SQL Server inside container

This is my docker-compose.yml file:
version: "3.9"
services:
api:
# configuration of API here
db:
image: mcr.microsoft.com/mssql/server
restart: always
container_name: Database
volumes:
- ./Data:/var/opt/mssql
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=lksU2o412f7tBj58t07B
- MSSQL_PID=Express
ports:
- 1433:1433
command: >
sh -c
"
tail -f /dev/null
"
As you can see, I'm using the official docker image for MS SQL Server, mcr.microsoft.com/mssql/server.
My laptop is Ubuntu 20.04 LTS.
I run docker-compose up -d and then docker exec -it db sh to get interactive shell.
Then I write this command:
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'lksU2o412f7tBj58t07B'
And I get this response:
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2749.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..
This is the official image and I have not changed anything.
Also in my api container, I can ping db or Database. But I can't telnet port 1433.
Also from my host machine I can telnet localhost 1433, but I get this response:
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
This is a terrible experience in working with containers from Microsoft. Aren't containers supposed to work out of the box? Isn't container technology there only to solve these stupid problems?
What should I do?

Debezium How do I correctly register the SqlServer connector with Kafka Connect - connection refused

Question:
How do I correctly register the SqlServer connector with Kafka Connect to connect to a standalone SQL Server Instance? Note: I am NOT running SQL Server in Docker.
Error:
Error: Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The
TCP/IP connection to the host 127.0.0.1, port 1433 has failed. Error:
"Connection refused (Connection refused). Verify the connection
properties. Make sure that an instance of SQL Server is running on the
host and accepting TCP/IP connections at the port. Make sure that TCP
connections to the port are not blocked by a firewall.".
Troubleshooting:
Enabled CDC on the dbo.Posts table (see below)
Verified that TCP/IP is enabled and set all addresses to enabled and active on port 1433 in SQL Server Configuration Manager's SQL Server Network Configuration
Disabled Windows Firewall
Enabled SQL Server Browser in SQL Server Configuration Manager
Verified that I can telnet in on the following: (127.0.0.1 1433; localhost 1433; lt-ls231 1433)
Context:
I am attempting to setup Debezium on Windows 10 using Docker. I can successfully run the following commands in Powershell without problem:
docker run -it --rm --name zookeeper -p 2181:2181 -p 2888:2888 -p 3888:3888 debezium/zookeeper:1.1
docker run -it --rm --name kafka -p 9092:9092 --link zookeeper:zookeeper debezium/kafka:1.1
docker run -it --rm --name connect -p 8083:8083 -e GROUP_ID=1 -e CONFIG_STORAGE_TOPIC=my_connect_configs -e OFFSET_STORAGE_TOPIC=my_connect_offsets -e STATUS_STORAGE_TOPIC=my_connect_statuses --link zookeeper:zookeeper --link kafka:kafka debezium/connect:1.1
I get an error (see above) when I attempt to register the SQL Server connection with Kafka-Connect connect, I run the following command:
Invoke-RestMethod -Method Post -Uri 'http://localhost:8083/connectors/' -Headers #{'Accept' = 'application/json'; 'Content-Type' = 'application/json'} -Body '{"name": "mssqlserver-localhost-testDb-connector", "config": {"connector.class": "io.debezium.connector.sqlserver.SqlServerConnector", "database.hostname": "127.0.0.1", "database.port": "1433", "database.user": "svc_kafka", "database.password": "password", "database.dbname": "Posts", "database.server.name": "LT-LS231", "table.whitelist": "dbo.Posts", "database.history.kafka.bootstrap.servers": "kafka:9092", "database.history.kafka.topic": "dbhistory.LT-LS231" }}'
The formatted JSON is as follows:
Invoke-RestMethod -Method Post -Uri 'http://localhost:8083/connectors/' -Headers #{'Accept' = 'application/json'; 'Content-Type' = 'application/json'} -Body '
{
"name": "mssqlserver-localhost-testDb-connector",
"config": {
"connector.class": "io.debezium.connector.sqlserver.SqlServerConnector",
"database.hostname": "127.0.0.1",
"database.port": "1433",
"database.user": "svc_kafka",
"database.password": "password",
"database.dbname": "Posts",
"database.server.name": "LT-LS231",
"table.whitelist": "dbo.Posts",
"database.history.kafka.bootstrap.servers": "kafka:9092",
"database.history.kafka.topic": "dbhistory.LT-LS231"
}
}'
This is modeled after the JSON provided on Debezium's site: https://debezium.io/documentation/reference/1.1/connectors/sqlserver.html#sqlserver-deploying-a-connector
The problem is in the reference to the localhost. If you run the Connect in Docker container then the localhost is the localhost of the container not of the machine. Try exposing the SQL Server on docker0 interface or all intefaces and then use docker0 IP in the registration request.
The solution for a non-production environment is to use host.docker.internal. This is found on Docker's website here: https://docs.docker.com/docker-for-windows/networking/. See section titled I WANT TO CONNECT FROM A CONTAINER TO A SERVICE ON THE HOST. I will need to look into the Docker0 that is recommended in Jiri's answer.
The correct JSON is as follows:
{
"name": "LT-LS231-mssqlserver-connector",
"config": {
"connector.class": "io.debezium.connector.sqlserver.SqlServerConnector",
"database.server.name": "staging",
"database.hostname": "host.docker.internal",
"database.port": "1433",
"database.user": "svc_kafka",
"database.password": "*********",
"database.dbname": "Test",
"table.whitelist": "dbo.Posts",
"database.history.kafka.bootstrap.servers": "kafka:9092",
"database.history.kafka.topic": "dbhistory.Posts"
}
}

Connection to PgBouncer Admin Database

I am trying to connect to the Pgbouncer Admin Database but I am little confused where the Admi user is stored.
Below my pgbouncer.ini configuration.
[databases]
alias_prod = host=abc.com.br port=5432 dbname=edf user=akl password=kji
[pgbouncer]
listen_addr = *
listen_port = 5432
auth_type = md5
auth_file = /etc/pgbouncer/userlist.txt
server_reset_query =
min_pool_size=20
pool_size=20
admin_users = FOO
Userlist.txt
"AAA" "XXXXX"
So to connect using python, I am using:
postgresql+psycopg2://AAA:XXXXX#ip-1.2.3.4.5:5432/alias_prod
The userlist.txt has a list of users that I use to connect to PGBouncer. The alias_prod will have the connection string to the Production Database.
The list_port and list_addr are configuration setting for the PGBouncer.
Now I am trying to connect do PgBouncer Admin database using the command:
psql -p 5432 --dbname=pgbouncer -U AAA
But I getting:
psql: ERROR: not allowed
I have already tried FOO admin_users configuration.
Any suggestion in how to solve it?
Try this command psql -h *Host address -U AAA -d pgbouncer -p 5432

Docker Container: Unable to connect to SQL Server by name

I created two container on the same network and one of them as a Sql Server instance running. In the other container (with SQL Tools) i'm able to connect to the SQL using the IP Address, but if i swith to machine name it fails.
I already tried do ping the machine and the dns is solving the right IP, i also tried dnslookup and it's also working. Does anyone as a clue on how to fix this?
Full test scenario:
Created new network
docker network create --driver=bridge specsnet
Run SQL Container
docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=Password!123456' -p 1401:1433 -d --name=TestDBServer --net=specsnet --rm microsoft/mssql-server-linux:latest
Run New Container with SQL Tools (to test connection)
docker run -it --net=specsnet --rm --entrypoint "bash" mcr.microsoft.com/mssql-tools:latest
Loaded some tools for troubleshooting (into SQL Tools container)
apt-get update | apt-get install telnet -y | apt-get install iputils-ping -y | apt-get install dnsutils -y | apt-get install nmap -y | apt-get install nano -y
Test Connection with IP (Success - IP was 172.18.0.2)
sqlcmd -S tcp:172.18.0.2,1433 -U sa -P 'Password!123456'
Test Connection with Name (Fails)
sqlcmd -S tcp:TestDBServer,1433 -U sa -P 'Password!123456'
So as Bjoern suggested i created a docker compose file and after doing some test i realized the issue was not fixed.
Then i started to manipulate the file, tweaking the properties, and discovered the problem was on the SQL container name (the container name had upper case letters). I set the SQL container name to "testdbserver" and everything worked fine.
Docker Compose File
version: '2'
services:
testdbserver:
image: microsoft/mssql-server-linux:latest
ports:
["1401:1433"]
environment:
SA_PASSWORD: Password!123456
ACCEPT_EULA: Y
networks:
- specsnet
sqltools:
image: mcr.microsoft.com/mssql-tools:latest
depends_on:
- testdbserver
networks:
- specsnet
networks:
specsnet:
driver: bridge
ipam:
config:
- subnet: 10.5.0.0/16
gateway: 10.5.0.1
Start SQL Tools Container on Bash Mode
docker-compose run sqltools bash
Execute SQL Test Connection (works now)
sqlcmd -S tcp:testdbserver,1433 -U sa -P 'Password!123456'
The TestDBServer part of your sqlcmd references a so-called Server Alias. These server aliases are not accessible due to the way you have set up the Docker networking currently. If you switch to Docker compose to set up the networking for you it should work.
An alternative approach would be to --link the containers in question together.
I had the same issue when using a gateway and a microservice:
Create network:
docker network create mynet
Add containers:
docker network connect mynet my-gateway
docker network connect mynet my_service_8080
Doing so, I wasn't able to communicate between both containers when using container name. E.g. when connecting to the gateway container and doing a curl to the service:
curl -i http://my_service_8080:8080/management/info
HTTP/1.1 400
Transfer-Encoding: chunked
Date: Tue, 23 Feb 2021 12:51:47 GMT
Connection: close
After doing a lot of research on the net which led to nothing, I finally had the idea to provide an alias name:
docker network connect --alias gateway mynet my-gateway
docker network connect --alias myservice mynet my_service_8080
Now, I can successfully communicate between the containers by using alias name as host name:
curl -i http://myservice:8080/management/info
HTTP/1.1 200
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 23 Feb 2021 12:54:36 GMT
{"name":"myservice-app","description":"my service","version":"20210202"}
EDIT:
After some more digging, I found that communication using container names doesn't work if the name (or alias) contains an underline character. So if you experience problems, check your container names.

Categories

Resources