18 Temmuz 2019 Perşembe

How to deploy and use a MySQL Docker container

# Pull latest docker mysql image from docker repo
docker pull mysql/mysql-server:latest
# Create a docker container and run it
docker run -d --name CONTAINER --restart=always -p 3306:3306 mysql/mysql-server:latest
# Check the automatically generated password of root user
docker logs CONTAINER 2>&1 | grep GENERATED
# Connect mysql server with root account and enter the password (that will appear after previous command above!)
docker exec -it CONTAINER mysql -u root -p
# Give a new password to root
ALTER USER 'root'@'localhost' IDENTIFIED BY '<db_root_password>';
# Create user a database and a user. Give all privileges to this user for all tables in test DB.
CREATE DATABASE test_db;
CREATE USER 'test-user'@'localhost' IDENTIFIED BY '<db_test_password>';
GRANT ALL PRIVILEGES ON test_db.* TO 'test-user'@'localhost';
FLUSH PRIVILEGES;
exit;
# Restore DB with sql file that is created in backup by MySQL Workbench
cat backup.sql | docker exec -I CONTAINER /usr/bin/mysql -u test-user -p<db_test_password> test_db
# If necessary, to backup DB:
cat backup.sql | docker exec -I CONTAINER /usr/bin/mysql -u test-user —password=<db_test_password> test_db