PostgreSQL database is not created using keycloak - database

I need to create a new external PostgreSQL database to replace the default H2 database that Keycloak uses by default. When I create a database using the keycloak file, the database is not created when running Keycloak:
Caused by: org.postgresql.util.PSQLException: FATAL: database "sso" does not exist
at org.postgresql.jdbc#42.2.5//org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2440)
at org.postgresql.jdbc#42.2.5//org.postgresql.core.v3.QueryExecutorImpl.readStartupMessages(QueryExecutorImpl.java:2559)
at org.postgresql.jdbc#42.2.5//org.postgresql.core.v3.QueryExecutorImpl.<init>(QueryExecutorImpl.java:133)
at org.postgresql.jdbc#42.2.5//org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:250)
at org.postgresql.jdbc#42.2.5//org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc#42.2.5//org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
at org.postgresql.jdbc#42.2.5//org.postgresql.Driver.makeConnection(Driver.java:454)
at org.postgresql.jdbc#42.2.5//org.postgresql.Driver.connect(Driver.java:256)
at org.jboss.ironjacamar.jdbcadapters#1.4.22.Final//org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.createLocalManagedConnection(LocalManagedConnectionFactory.java:321)
... 57 more
When I am giving the application database as the value I am able to connect to the database. But my intention is to connect to a different database:
cat Keycloak
CREATE DATABASE IF NOT EXISTS sso;
docker-compose.yaml
data-keycloak:
image: jboss/keycloak:$KEYCLOAK_TAG
container_name: data-keycloak-container
ports:
- "$KEYCLOAK_EXTERNAL_PORT:$KEYCLOAK_INTERNAL_PORT"
depends_on:
- data-keycloak-db
environment:
KEYCLOAK_USER: $KEYCLOAK_USER
KEYCLOAK_PASSWORD: $KEYCLOAK_PASSWORD
DB_VENDOR: $KEYCLOAK_DB_VENDOR
DB_USER: $KEYCLOAK_DB_USER
DB_PASSWORD: $KEYCLOAK_DB_PASSWORD
DB_DATABASE: $KEYCLOAK_DB_DATABASE
DB_ADDR: $KEYCLOAK_DB_ADDR
DB_SCHEMA: public
WAIT_HOSTS: data-keycloak-db:$data_KEYCLOAK_DB_PORT
KEYCLOAK_IMPORT: /tmp/keycloak-realm.json
volumes:
- ./keycloak-realm.json:/tmp/keycloak-realm.json
- ./keycloak:/tmp/keycloak
networks:
- $data_NETWORK

Related

Spring-boot and MS SQL Server in docker compose: Failed to determine suitable jdbc url

I created a docker compose file to run a spring-boot application (2.5.7) and a MS SQL Server:
version: '3.8'
services:
db:
ports:
- 1434:1433
build: ./db
api:
depends_on:
- db
build: ./mdm-web-api
restart: on-failure
env_file: ./.env
ports:
- $SPRING_LOCAL_PORT:$SPRING_DOCKER_PORT
environment:
- SPRING_DATASOURCE_URL=jdbc:sqlserver://db:1434;databaseName=dwh-demo;encrypt=true;logingTimeout=30
stdin_open: true
tty: true
I defined a Dockerfile for the mssql in the folder 'db' and successfully tested the SQL server alone.
I added my spring-boot application, which I also tested separately.
I have an application.properties file as follow (I replace sensible values):
logging.level.root=INFO
spring.datasource.username=<my-db-user>
spring.datasource.password=<my-ultra-strong-pw>
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.default_schema:entity_reference
spring.jpa.hibernate.use-new-id-generator-mappings=false
spring.jpa.generate-ddl=false
spring.jpa.show-sql=true
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.data.rest.basePath=/api
springdoc.api-docs.path=/api-docs
I just added the spring.datasource.url properties in the docker-compose.yml to be able to point to the database server using the service name:
environment:
- SPRING_DATASOURCE_URL=jdbc:sqlserver://db:1434;databaseName=dwh-demo;encrypt=true;logingTimeout=30
It seems like the SPRING_DATASOURCE_URL value is completely ignored and I'm getting the following error:
Failed to determine suitable jdbc url
It shouldn't be a problem to use both places to add configuration, but maybe I'm wrong.
What could be the problem and which alternative do I have?
Additional details:
java openjdk 19
maven 3.8.5
Docker on Windows
SQL Server 2019-latest for Linux

Connecting to host mysql database using via sock

I am trying to connect to a mysql database on my host using a docker container (so I can use it in other containers).
I would like to do it this way, because I cannot connect to the database from a docker container -> throws Connection refused because the IP is not allowed.
I tried mounting the sock file using following compose:
version: '3'
services:
mysql:
image: mariadb:10.3
volumes:
- /var/run/mysqld/mysqld.sock:/var/run/mysqld/mysqld.sock
networks:
- database
networks:
database:
external: true
but it's failing on (even though the host database already contains many databases):
2022-06-07 20:14:46+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
You need to specify one of MARIADB_ROOT_PASSWORD, MARIADB_ALLOW_EMPTY_ROOT_PASSWORD and MARIADB_RANDOM_ROOT_PASSWORD,
and when I added the volume: - /var/lib/mysql:/var/lib/mysql, I am getting a Connection refused error (as if I was connecting to it normally and not via unix)
Here you go, consider checking the documentation before asking thoses types of questions
https://hub.docker.com/_/mariadb
version: 3
services:
sql:
image: mariadb:10.3
container_name: <container_name>
restart: always
networks:
- database
volumes:
- <volume>:/var/lib/mysql
- /var/run/mysqld/mysqld.sock:/var/run/mysqld/mysqld.sock
environment:
MARIADB_RANDOM_ROOT_PASSWORD: 1
MARIADB_USER: <USER_TO_BE_CREATED>
MARIADB_DATABASE: <DATABASE_TO_BE_CREATED>
MARIADB_PASSWORD: <PASSWORD_TO_BE_CREATED>
networks:
database:
external: true

Can not connect to the azure-sql-edge. Why?

I have the following docker-compose.yml:
version: "3.9"
services:
# Database instance
mssql:
image: mcr.microsoft.com/azure-sql-edge:latest
volumes:
- events_mssql:/var/opt/mssql
ports:
- 1433:1433
environment:
- ACCEPT_EULA=1
- MSSQL_SA_PASSWORD=Passw#rd
volumes:
events_mssql:
setup.sql:
CREATE DATABASE $(MSSQL_DB);
CREATE DATABASE $(MSSQL_DB_AUDIT_LOG);
GO
USE $(MSSQL_DB);
GO
CREATE LOGIN $(MSSQL_USER) WITH PASSWORD = '$(MSSQL_PASSWORD)';
GO
CREATE USER $(MSSQL_USER) FOR LOGIN $(MSSQL_USER);
GO
ALTER SERVER ROLE sysadmin ADD MEMBER [$(MSSQL_USER)];
GO
Here is the running container:
I am trying to connect to the database through the Visual Studio Code extension:
Using this connection string: Server=http://localhost:1433/;Database=master;User Id=sa;Password=Passw#rd;
I am getting the error:
mssql: Error: Unable to connect using the connection information provided. Retry profile creation?
What am I doing wrong here?

Why can't connect my .net core container to the SQL Server docker container?

It fails to connect to the SQL Server container, even if the backend is running in local and the database in docker.
I tested with Azure SQL and everything worked perfectly.
Error(s):
After docker-compose up, and try to add element to the list:
2021-07-13 21:43:32.79 Logon Login failed for user 'sa'. Reason: Failed to open the explicitly specified database 'AsdDB'. [CLIENT: XXX.168.XX.3]
backend | fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
backend | An error occurred using the connection to database 'AsdDB' on server 'sqlserver'.
backend | fail: Microsoft.EntityFrameworkCore.Query[10100]
backend | An exception occurred while iterating over the results of a query for context type 'Asd.Db.AppDbContext'.
backend | Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot open database "AsdDB" requested by the login. The login failed.
backend | Login failed for user 'sa'.
dotnet ef update database
dotnet ef database update --project Asd.Api.csproj
Build started...
Build succeeded.
Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Backend Dockerfile:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-bionic AS build
WORKDIR /app
EXPOSE 80
COPY *.sln .
COPY Asd.Api/*.*.csproj ./Asd.Api/
COPY Asd.Db/*.*.csproj ./Asd.Db/
RUN dotnet restore
COPY Asd.Api/. ./Asd.Api/
COPY Asd.Db/. ./Asd.Db/
WORKDIR /app/Asd.Api
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-bionic AS runtime
WORKDIR /app
COPY --from=build /app/Asd.Api/out ./
ENTRYPOINT ["dotnet", "Asd.Api.dll"]
Docker compose:
version: "3.8"
services:
backend:
container_name: backend
build:
context: ./backend/
dockerfile: Dockerfile
depends_on:
- db
ports:
- "5000:80"
db:
image: "mcr.microsoft.com/mssql/server:2017-latest"
container_name: sqlserver
hostname: sqlserver
environment:
SA_PASSWORD: "ASDFGHJ12345"
ACCEPT_EULA: "Y"
restart: unless-stopped
ports:
- "1433:1433"
Connection string in the appsettings.json
"ConnectionStrings": {
"DefaultConnection": "Server=sqlserver;Database=AsdDB;user id=sa;password=ASDFGHJ12345;"
},
In the connection string, the Server had to be passed the following value:
Server=host.docker.internal;Database=AsdDB;user id=sa;password=ASDFGHJ12345%;
So now it works, it took 2 days of messing with it, but after I posted the question, I found the answer.

Error: Error establishing a database connection on wordpress when I search plugins

my wordpress app have an error when I search install plugins using wordpress CLI.
Error: Error establishing a database connection.
I'm using Docker container.
define('DB_NAME', 'wordpress');
/** MySQL database username */
define('DB_USER', 'wordpress');
/** MySQL database password */
define('DB_PASSWORD', 'wordpress');
/** MySQL hostname */
define('DB_HOST', 'db:3306');
/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');
/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');
docker-compose.yaml
version: '3'
services:
# Database
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- wpsite
# phpmyadmin
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- '8080:80'
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: password
networks:
- wpsite
# Wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- '8000:80'
restart: always
volumes: ['./:/var/www/html']
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
networks:
- wpsite
networks:
wpsite:
volumes:
db_data:
Most likely its due to low php memory as well as file permission system. I recommend to contact hosting to fix this issue and if not possible then you can increase php memory and reset file permission to have it solved.

Resources