elowy01
4/21/2020 - 12:48 PM

mysql_server_with_docker

# 1) Get the docker image for MYSQL server
$ docker pull mysql/mysql-server:latest
# 2) After download finished, you can see it through the docker image command:
$ docker images
# 3) We can create a container from this image:
$ docker run --name=mysql1 -d mysql/mysql-server
# 4) We can check the container logs by doing:
$ docker logs mysql1
# 5) If you only want to access it locally, it’s enough, but if you want to 
# access your mysql server remotely, you need mapping the port 3306 of the
# container to port 3306 of the host machine. Also, you need to set the 
# environment variable MYSQL_ROOT_HOST with wildcards to allow root 
# connections from other hosts. So, stop and remove the previous container
$ docker stop mysql1
$ docker rm mysql1
# And do:
$ docker run --name=mysql1 -e MYSQL_ROOT_HOST=% -p 3306:3306 -d mysql/mysql-server
# 6) Connecting to the MySQL Server instance
# Get the root password:
$ docker logs mysql1 2>&1 | grep GENERATED # copy the obtained password
# Login as root and paste the copied password
$ docker exec -it mysql1 mysql -u root -p
# Now, you can modify the root password by doing:
$ ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '<password>';
# If you want to exit from container:
ctrl+d
# Now, we can check the status of the container by doing:
$ docker ps
# We can run a command on the mysql-server container:
$ docker exec -it mysql1 mysql -h localhost -u root -pserinus -P3306 -e "show databases"
##########
docker run --volume $HOME:$HOME --workdir $PWD --name=mysql1 -e MYSQL_ROOT_HOST=% -p 3306:3306 -d mysql/mysql-server