MySQL Docker Container

################### Dockerfile Build Method ######################
mkdir /var/lib/docker/mysql && cd /var/lib/docker/mysql
vim Dockerfile

################## Dockerfile contents #####################
# Derived from official mysql image (our base image)
FROM mysql/mysql-server:latest

# Add a database
#ENV MYSQL_DATABASE kimconnect

# Set default password format - this must be done prior to creating any new users!
RUN echo "default_authentication_plugin=mysql_native_password" >> /etc/my.cnf

# Add the content of the sql-scripts/ directory to your image
# All scripts in docker-entrypoint-initdb.d/ are automatically
# executed during container startup
COPY ./scripts/* /docker-entrypoint-initdb.d/
########################################################

mkdir scripts
vim /var/lib/docker/mysql/scripts/initialize.sql
############### Insert Contents #############################
# This is the method to allow kimconnect user to connect remotely from anywhere
CREATE USER 'kimconnect'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
GRANT ALL PRIVILEGES ON kimconnect.* TO 'kimconnect'@'%';
#############################################################

# Cool trick: create directory from within vim (if parent directory doesn't exist), then save
:!mkdir -p %:h
:wq

# Create Docker Image from Dockerfile and then run it
cd /var/lib/docker/mysql
docker build -t mysql .
docker run --privileged --restart always -d -p 3306:3306 --name mysql-server -e MYSQL_ROOT_PASSWORD='password' mysql

# Check logs
docker logs --tail 50 --follow --timestamps mysql-server

# Connect to Bash inside container
docker exec -ti mysql-server /bin/bash
mysql -kimconnect -ppassword

MySQL as Container in Docker

# Run MySQL Server container
mkdir /var/lib/docker/volumes/mysql_data
docker run -d --privileged -p 3306:3306 --name=mysql -v /var/lib/docker/volumes/mysql_data:/var/lib/mysql mysql/mysql-server:latest

# Run mysql-client
docker exec -it mysql_container mysql -uroot -p

# Check logs of container using Portainer GUI to find the auto-generated password

# Reset root password
ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';

# Reset accesses

FLUSH PRIVILEGES;

# This is the method to allow kimconnect user to connect remotely from anywhere
CREATE USER 'kimconnect'@'%' IDENTIFIED WITH mysql_native_password BY 'password' ;
GRANT ALL PRIVILEGES ON * . * TO 'kimconnect'@'%';

# Check MySQL logs
docker logs mysql_container


# The Oracle version of MySQL-Server
docker run --privileged --restart always -d -p 3306:3306 --name=mysql-server -v /root/mysql/scripts:/docker-entrypoint-initdb.d/ -e MYSQL_ROOT_PASSWORD='password' -e MYSQL_DATABASE=kimconnect mysql/mysql-server:latest

docker run --privileged --restart always -d -p 3306:3306 --name=mysql-server -e MYSQL_ROOT_PASSWORD='password' -e MYSQL_DATABASE=kimconnect mysql/mysql-server:latest

# Different version of mysql server
docker run --privileged --restart always
-d -p 3306:3306 --name mysql \
-v /root/mysql/scripts:/docker-entrypoint-initdb.d/ \
-e MYSQL_ROOT_PASSWORD='password' \
-e MYSQL_DATABASE=kimconnect \
mysql:latest

Leave a Reply

Your email address will not be published. Required fields are marked *