running mysql in a kubernetes init container

1/14/2022

I am running mysql as an init container for a kubernetes deployment.

For various reasons i need to spin up mysql in a init container/restore a mysql dump -> save it to a pvc and then the main pod will be a mysql pod with the data attached.

This is because i need to take a snapshot of the disk and im going to have CI watch for the pod to be "ready/running" before i take the snapshot.

So i cant just dump the dump.sql in docker-entrypoint-initdb.d and be done with it.

This is because the volumesnapshot kubernetes resources would be taken before the data is restored/ready. Hence why i need the data to be prepared in an init container.

The problem i am having is an init container needs a command which overwrites the entrypoint.sh (which actually starts mysql ect)

So far i have this bash script which is run on the container startup.

./entrypoint.sh --ignore-db-dir=lost+found 
echo "done" (this is just purely for testing purposes to see if it ever gets processed and it doesnt)
mysql -u root -ppassword < /data/backups/backup.sql
mysql -u root -ppassword < /sql-files/sql-files.sql

the problem is, the entrypoint is run but then just hangs with

2022-01-14T15:07:54.809983Z 0 [Note] Event Scheduler: Loaded 0 events
2022-01-14T15:07:54.810442Z 0 [Note] mysqld: ready for connections.
Version: '5.7.36'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server (GPL)

and never moves onto the next step of the bash script. i've tried adding a & to the end so it is run in the background and this doesn't solve the problem.

has anyone ever come across this issue? How can i manually run the entrypoint and then execute some commands after.

Ive also tried this, which should run it in the background and exit, but it still doesnt.

#!/bin/sh

start_mysql () {
    sleep 30
    #mysql -u root -ppassword < /data/backups/backup.sql
    #mysql -u root -ppassword < /sql-files/sql-files.sql
    echo "hi"
    sleep 30
    echo "done 123354543543" >> /data/backups/test.txt
    echo "actually done"
    exit 0 
}

start_mysql &
./entrypoint.sh --ignore-db-dir=lost+found 
-- TheOne745665
kubernetes
mysql

1 Answer

1/15/2022

you can start mysql sleeping in the background before mysqld. Would this work for you?

start_mysql {
    sleep 30
    mysql -u root -ppassword < /data/backups/backup.sql
    mysql -u root -ppassword < /sql-files/sql-files.sql
}

start_mysql &
./entrypoint.sh
-- serge-v
Source: StackOverflow