Docker compose Mysql - mysql

i tried to do 2 changes on my docker-compose, but i'm didn't find how to do it.
1- I want to create 2 users.
2- I want to import a sql file from docker-compose.
version: '3.8'
services:
mysql:
image: mysql:8.0.21
command: --default-authentication-plugin=mysql_native_password
restart: always
container_name: mysql
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_USER="user, user2"
- MYSQL_PASSWORD="pass, pass2"
- MYSQL_DATABASE=templateProject
- MYSQL_MAX_ALLOWED_PACKET=1024M
- MYSQL_INNODB_BUFFER_POOL_SIZE=1G
- MYSQL_INNODB_LOG_FILE_SIZE=256M
- MYSQL_INNODB_LOG_BUFFER_SIZE=256M
ports:
- '3361:3360'
volumes:
- ./dump.sql:/my_dump.sql
phpmyadmin:
image: phpmyadmin
restart: always
container_name: phpmyadmin
ports:
- 8080:80
environment:
- PMA_ARBITRARY=1

For #2 you should bind mount the directory containing your sql file like this:
"./folderwithsql:/docker-entrypoint-initdb.d"
For #1 You may be able to include another sql file in your "folderwithsql" mount that includes SQL statements to create the extra user. The file would contain something similar to these:
CREATE USER 'extrauser'#'%' IDENTIFIED BY 'passwordforextrauser';
GRANT INSERT, UPDATE, SELECT, DELETE ON mynewdb.* TO 'extrauser'#'%';

Related

Docker importing a sql script on mysql container

How can this docker script be modified to allow a sql file to be imported into the mysql container? I need to modify the database on the mysql container.
version: '3'
services:
devbox:
build:
context: ./
dockerfile: DevBox.DockerFile
ports:
- "80:80"
- "443:443"
volumes:
- .:/var/gen4
- ./offers:/var/www/vhosts/offers
devmysql:
image: mysql:5.7
platform: linux/x86_64
environment:
MYSQL_ROOT_PASSWORD: mypwd
MYSQL_DATABASE: offers
ports:
- "3306:3306"
restart: always
The official MySQL images support creating a volume called /docker-entrypoint-initdb.d/
So in your devmysql section of your compose file do something like this
volumes:
- ./data:/docker-entrypoint-initdb.d/:ro
In this case, you'd want to have a data/ folder in the root of your project (wherever compose is being run from) and in that data/ folder you can put a SQL file with whatever commands you want. They'll be run.
If you're not running the official images, you might be able to create your own image that manually does something similar.

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!

Is there any way to create schema and tables in mysql docker without build a new image for mysql?

I have 2 images. One of them is custom and the other one is mysql. I am using docker-compose. Its database part is given below.
db:
image: mysql
container_name: mysql-docker-test
volumes:
- test-sql:/var/lib/mysql
restart: always
networks:
test-net:
ipv4_address: 172.17.0.5
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: my_secret_pw
There is no problem with communication but I need to run sql script when I run docker-compose up.
Is there any way to run sql script in docker-compose.yml?
you can override the entrypoint or the command in docker-compose.yml like this :
db:
image: mysql
entrypoint: ./path_to_your_script.sh
In your script you must start mysql server and then run your sql scripts. Of course you msut put the shell script in a volume.

Why did mysql data ownership change to systemd-journal-remote after running a docker container

I have the mysql database stored in /home/mysql instead of /var/lib/mysql. The directory used to be owned by mysql. However, when I run the command docker-compose up with this yml file:
version: '3'
services:
mariadb:
image: mariadb
restart: always
volumes:
- /home/mysql:/var/lib/mysql
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:5.6.4
environment:
- "ES_JAVA_OPTS=-Xms750m -Xmx750m"
- bootstrap.memory_lock=false
site:
build: .
volumes:
- "./app:/app"
links:
- mariadb:mysql
environment:
- DOCKER_IP=172.19.0.2
depends_on: ['elasticsearch','mariadb']
ports:
- "3000:3000"
The docker container is able to run, but the entire folder and files in /home/mysql are owned by systemd-journal-remote, which causes the node server fails to connect to mariadb. I have to stop the docker instance, restore the mysql folder ownership and delete ib_logfile0 and ib_logfile1.
Why does mounting /home/mysql cause such a fatal problem?
Update:
My solution is to add user: "mysql":
version: '3'
services:
mariadb:
image: mariadb
restart: always
volumes:
- /home/mysql:/var/lib/mysql
user: "mysql"
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:5.6.4
environment:
- "ES_JAVA_OPTS=-Xms750m -Xmx750m"
- bootstrap.memory_lock=false
site:
build: .
volumes:
- "./app:/app"
links:
- mariadb:mysql
environment:
- DOCKER_IP=172.19.0.2
depends_on: ['elasticsearch','mariadb']
ports:
- "3000:3000"
You should start Docker's container with --user parameter. If you do this and set the same uid:gid as owner of the MySQL storage you will no have problems with permissions. You have to check how exactly to do this in Docker Compose because I show you example for normal command line execution
Most likely, uid of your user systemd-journal-remote is the same as uid of user mysqld in container. Check with ls -n. To avoid confusion, either use common uids, perhaps test as root:root with chmod o+rwx.

Docker Compose - How reference many schemas in one mysql container

I'm trying to use two schemas into one mysql container. I have two flyway services that connect to two different schemas. The .yml file of Docker Compose looks like:
version: '2'
services:
mysqldb:
image: mysql:5.6.26
environment:
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE:
- my
- my_post
ports:
- "3306:3306"
flyway-service1-i:
image: mik/flyway-service
volumes:
- "../resources/db/migration:/migrations/ro"
depends_on:
- mysqldb
links:
- mysqldb
command: migrate -url=jdbc:mysql://mysqldb:3306/mi -user=user -password=password -baselineOnMigrate=true -locations='filesystem:/migrations'
flyway-service2-i:
image: mialk/flyway-post-service
volumes:
- "../../../service2/src/main/resources/db/migration:/migrations/ro"
depends_on:
- mysqldb
links:
- mysqldb
command: migrate -url=jdbc:mysql://mysqldb:3306/mi_post -user=user -password=password -baselineOnMigrate=true -locations='filesystem:/migrations'
But when I run the command sudo docker-compose up the terminal show this message:
ERROR: The Compose file './docker-compose.yml' is invalid because:
services.mysqldb.environment.MYSQL_DATABASE contains ["mialquiler", "mialquiler_post"], which is an invalid type, it should be a string, number, or a null
I traid without specifying MYSQL_DATABASE property, but it didn't work.
Is there any way to do that?
The MYSQL_DATABASE variable allows a single database to be created, and permissions granted on the database to the MYSQL_USER if specified.
You can use a single database to house multiple schema's.
If you need to create multiple databases you may need to run some custom SQL as flyway can't do database creation for you. The flyway test resources include a mysql example.