Connection failed to dockarized MySQL service - mysql

I have dockarized MySQL container running. When I try to connect to this mysql-docker-service from other dockarized applications, it fails.
My docker-compose :
version: '3'
services:
mysqlserver:
image: mysql-server
container_name: mysqlcontainer
build:
context: mysql-module/
dockerfile: Dockerfile
env_file: mysql-module/mysql.env
networks:
- backend
volumes:
- ./mysql-data:/var/lib/mysql
ports:
- 8001:3306
users:
image: users
build:
context: .
dockerfile: Dockerfile
ports:
- 7171:7171
env_file: ./api.env
networks:
- backend
networks:
backend:
driver: "bridge"
MySQL dockerfile :
FROM mysql/mysql-server:latest
ENV MYSQL_DATABASE userservice
ADD sql-scripts/createUsers.sql /docker-entrypoint-initdb.d
I get database credentials from external file.
MYSQLDBGATEWAY=root:7890#tcp(mysqlcontainer://mysqlserver:8001)/userservice?parseTime=true
Error getting user details dial tcp: lookup mysqlcontainer://mysqlserver:8001: no such host
What am I missing here? This is my first dockerized MySQL work.
Also in sql-scripts/createUsers.sql file I have :
CREATE TABLE IF NOT EXISTS managerMinQuota (
id int NOT NULL AUTO_INCREMENT,
plan varchar(50) DEFAULT NULL,
minLimit int DEFAULT NULL,
PRIMARY KEY (id)
);
But this is not created at run time.

Related

mysql docker container : create database, user and execute commands automatically

I have followed this post create database and table automatically with docker-compose to execute a script at startup of my container.
The database is created and a user i defined in the docker-compose is created, my database is not populated like i have set in the sql script.
Can anyone help me?
here is the dockerfile:
FROM phpmyadmin
COPY DockerCreateAllTablesDBwithData.sql /docker-entrypoint-initdb.d/initdb.sql
Here is the docker-compose.yml:
---
version: '3.7'
services:
db:
image: mysql:8-debian
container_name: db
restart: always
networks:
- network_app
cap_add:
- SYS_NICE
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_USER=admin
- MYSQL_PASSWORD=password
- MYSQL_DATABASE=CantineTest
- TZ='Europe/Paris'
volumes:
- mydatavolume:/var/lib/mysql
phpmyadmin:
build:
context: .
dockerfile: ./Dockerfile
container_name: phpmyadmin
restart: always
ports:
- 8080:80
depends_on:
- db
# volumes:
# - ./data/certbot/conf:/etc/letsencrypt
networks:
- network_app
environment:
- PMA_HOST=db
- TZ="Europe/Paris"
#volumes:
# frontendbuild:
# name: frontendbuild
networks:
network_app:
name: network_app
volumes:
mydatavolume:
And here is the DockerCreateAllTablesDBwithData.sql:
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
--
-- Bdd : `CantineTest`
--
create database CantineTest;
use CantineTest;
DROP TABLE IF EXISTS `Alias`;
CREATE TABLE `Alias` (
`AliasID` smallint(5) UNSIGNED NOT NULL,
`AliasName` varchar(50) NOT NULL,
`AliasDescription` varchar(255) DEFAULT NULL,
`AliasMailingList` mediumtext NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
You have added the DB script to a wrong place. Must be added to the docker-composer where you have MySQL. Add as a volume
volumes:
- ./mysql-dump:/docker-entrypoint-initdb.d
Create a mysql-dump folder where you have docker-compose and add your SQL script to it.

Connect to MariaDB in Docker via R

I have a few Docker containers up and running. The main ones are RStudio (for the app that connects to the database) and a database that's been synced with github container registry (ghcr.io). The end goal will be transitioning the R Shiny app to a Dash app in Python. Right now I'm having trouble connecting the database to either the Shiny app in another container or Python (the Python app has yet to be developed). See below for the docker-compose.yml
version: '3'
services:
frontapp:
container_name: frontend
build:
context: .
dockerfile: shinyserver-dockerfile
ports:
- "3838:3838"
volumes:
- type: bind
source: ../
target: /XXXX/XX_front_end_R
networks:
- frontend
rstudio:
container_name: rstudio
build:
context: .
dockerfile: rstudio-dockerfile
ports:
- "8787:8787"
volumes:
- type: bind
source: ../
target: /home/rstudio
networks:
- frontend
db:
image: ghcr.io/XXXX/XX_db:main
container_name: db
ports:
- "3306:3306"
volumes:
- dbdata:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: pma
links:
- db
environment:
PMA_HOST: db
PMA_PORT: 3306
PMA_ARBITRARY: 1
restart: always
ports:
- "8081:80"
networks:
- frontend
volumes:
dbdata:
networks:
frontend:
driver: bridge
Below is the result from docker ps
Then when I log into R Studio and try to run the app (in http://localhost:8787/), there's an error trying to connect to the database.
mydb <- dbConnect(MariaDB(), user = "XXXX", password = "YYYY",
dbname = "prototype", host = "db", port="3306")
I then get the following error:
Error: Failed to connect: Unknown MySQL server host 'db' (-2)
It looks like the app should be running on http://localhost:3838 although that page just returns an error.
Any help with connecting to the database is helpful. I'm not sure if there are other dependencies or installations that need to be done or if I'm not correcting in the right way. Thanks.
Note: I'm coming in fresh to this project. A previous developer has currently created what I have access to now.

How can I initialize a database with some tables in a container using docker compose?

I have my db service:
...
mysql-server:
container_name: silos-database
init: true
restart: always
build:
context: .
dockerfile: Dockerfile-db
environment:
- MYSQL_ROOT_PASSWORD=${ROOT_PASSWORD}
- MYSQL_DATABASE=${DATABASE}
- MYSQL_USER=${USER}
- MYSQL_PASSWORD=${USER_PASSWORD}
ports:
- target: 3036
published: 3036
networks:
- backend-net
volumes:
- type: volume
source: db_vol
target: /var/lib/mysql
...
This is my Dockerfile
FROM mysql:latest
The init.sql file contain same tables that my database must have
...
-- users data
CREATE TABLE IF NOT EXISTS users (
email VARCHAR(128) PRIMARY KEY,
username VARCHAR(64) UNIQUE,
-- password is SHA256 hashed, so 64 characters
password CHAR(64) NOT NULL
);
CREATE TABLE IF NOT EXISTS sessions (
email VARCHAR(128) PRIMARY KEY,
token TEXT NOT NULL,
expireDate BIGINT(13) NOT NULL,
FOREIGN KEY (email) REFERENCES users(email) ON UPDATE CASCADE ON DELETE CASCADE
);
...
How can I build a service with the content of init.sql file already inside the service?
The simplest way would be to add init.sql file to Your docker image:
FROM mysql:latest
ADD init.sql /docker-entrypoint-initdb.d
If You want to provide init.sql during container start instead of build time, You can mount directory containing init.sql into container:
mysql-server:
container_name: silos-database
init: true
restart: always
build:
context: .
dockerfile: Dockerfile-db
environment:
- MYSQL_ROOT_PASSWORD=${ROOT_PASSWORD}
- MYSQL_DATABASE=${DATABASE}
- MYSQL_USER=${USER}
- MYSQL_PASSWORD=${USER_PASSWORD}
ports:
- target: 3036
published: 3036
networks:
- backend-net
volumes:
- type: volume
source: db_vol
target: /var/lib/mysql
- /local/path/with/initsql:/docker-entrypoint-initdb.d

How to create docker-compose for JIRA and MySQL

I'm trying to create a docker-compose.yml file that will bring up JIRA and MySQL. Here's my file:
version: '3'
services:
jira:
depends_on:
- mysql
container_name: jira
restart: always
networks:
- jiranet
build:
context: .
dockerfile: Dockerfile.jira
environment:
- ATL_DB_TYPE=mysql
- ATL_DB_DRIVER=com.mysql.cj.jdbc.Driver
- ATL_JDBC_URL=jdbc:mysql://mysql:3306/jiradb
- ATL_JDBC_USER=jira
- ATL_JDBC_PASSWORD=jellyfish
ports:
- 8080:8080
volumes:
- jira-data:/var/atlassian-data/jira
mysql:
container_name: mysql
restart: always
image: mysql:5.7
networks:
- jiranet
environment:
- MYSQL_ROOT_PASSWORD=ChangeMe!
- MYSQL_DATABASE=jiradb
- MYSQL_USER=jira
- MYSQL_PASSWORD=jellyfish
command: [mysqld, --character-set-server=utf8, --collation-server=utf8_bin, --default-storage-engine=INNODB, --max_allowed_packet=256M, --innodb_log_file_size=2GB, --transaction-isolation=READ-COMMITTED, --binlog_format=row]
volumes:
- mysql-data:/var/lib/mysql
networks:
jiranet: {}
volumes:
jira-data:
mysql-data:
Unfortunately, I'm getting JIRA startup errors when it tried to initialize the database, of the form:
CREATE command denied to user 'jira'#'172.22.0.3' for table 'jiraaction'
I'm guessing it's because the mysql container is creating user jira, but only allowing it to connect from localhost. But, the JIRA container is being seen as coming from an an external IP.
Any ideas on how I can get the jiradb database in mysql to be accessible by the JIRA container by user jira?
I figured out the problem -- I was missing an environment variable in the jira container:
ATL_DB_SCHEMA_NAME=jiradb
After that, things worked fine!

Prisma 2 docker container can't connect MySQL Database container

I'm developing Backend server with prisma 2, MySQL, Docker-compose, GraphQL-Yoga server
My OS is Windows 10, docker compose use debian-openssl-1.1.x
problem is prisma 2 container can't connect MySQL container
docker-compose.yml
version: "3.7"
services:
mysql:
image: mysql:8.0.19
container_name: mysql
ports:
- 3306:3306
restart: always
environment:
MYSQL_DATABASE: $${MYSQL_DATABASE}
MYSQL_ROOT_PASSWORD: $${MYSQL_ROOT_PASSWORD}
volumes:
- /var/lib/mysql
prisma:
links:
- mysql
depends_on:
- mysql
container_name: prisma
ports:
- "5555:5555"
build:
context: back/prisma
dockerfile: Dockerfile
environment:
MYSQL_URL: $${MYSQL_URL}
MYSQL_ROOT_PASSWORD: $${MYSQL_ROOT_PASSWORD}
volumes:
- /app/prisma
backend:
links:
- mysql
depends_on:
- mysql
container_name: backend
ports:
- "4000:4000"
build:
context: back
dockerfile: Dockerfile
environment:
MYSQL_URL: $${MYSQL_URL}
FRONTEND_URL: $${FRONTEND_URL}
volumes:
- ./back:/app
- ./back/node_modules:/app/node_modules
- ./back/prisma:/app/prisma
frontend:
container_name: frontend
ports:
- "3000:3000"
build:
context: front
dockerfile: Dockerfile
environment:
BACKEND_URL: $${BACKEND_URL}
volumes:
- ./front:/app
- ./front/node_modules:/app/node_modules
.env
MYSQL_URL="mysql://root:prisma#mysql:3306/prisma"
BACKEND_URL=http://localhost:4000
FRONTEND_URL=http://localhost:3000
MYSQL_DATABASE:"prisma"
MYSQL_ROOT_PASSWORD:"prisma"
-schema.prisma
generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "debian-openssl-1.1.x"]
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
.env in prisma folder
DATABASE_URL="mysql://root:prisma#mysql:3306/prisma"
when i run docker-compose (docker-compose up), prisma container (prisma 2 studio) show this logs
Error in Prisma Client request:
Error:
Invalid `prisma.user.count()` invocation:
Authentication failed against database server at `mysql`, the provided database credentials for `root` are not valid.
Please make sure to provide valid database credentials for the database server at `mysql`.
at PrismaClientFetcher.request (/root/.cache/prisma/studio/app/runtime/index.js:1:52273)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
enter image description here
I think this error occurred because MySQL container has a password different from MYSQL_ROOT_PASSWORD which I wrote in the doctor-compose.yml.
But, i think there seems to be no problem with the setup, why is this error happening?
And if i did not set up MYSQL_ROOT_PASSWORD, what is the default password value generated by MySQL container?
I searched for days to find the answer to this problem, but I could not get the answer. please give me some advices. thank you