MySQL Docker Installation
- May 01, 2020
The MySQL Docker images maintained by the MySQL team are built specifically for Linux platforms. Other platforms are not supported, and users using these MySQL Docker images on them are doing so at their own risk. If you have MySQL installed and running locally on your laptop, I suggest that you stop it before creating and running your new Docker container(s). With these instructions your new container will be sharing the same port (3306) because we’re going to setup your Docker version of MySQL to appear as your localhost, and they’re going to have a conflict.
Basic steps for MySQL Database in your docker ;
- Installation of Docker
- Pulling MySQL Server Docker Image
- Building/Running MySQL image
- Connecting MySQL DB within container
then, you can pull mysql image
docker pull mysql:latest
you can run mysql docker image by using specific attributes such as
This variable is mandatory and specifies the password that will be set for the MySQL root superuser account. In the above example, it was set to my-secret-pw.
This variable is optional and allows you to specify the name of a database to be created on image startup. If a user/password was supplied (see below) then that user will be granted superuser access (corresponding to GRANT ALL) to this database.
- MYSQL_USER, MYSQL_PASSWORD
These variables are optional, used in conjunction to create a new user and to set that user’s password. This user will be granted superuser permissions (see above) for the database specified by the MYSQL_DATABASE variable. Both variables are required for a user to be created.
Do note that there is no need to use this mechanism to create the root superuser, that user gets created by default with the password specified by the MYSQL_ROOT_PASSWORD variable.
This is an optional variable. Set to yes to allow the container to be started with a blank password for the root user. NOTE: Setting this variable to yes is not recommended unless you really know what you are doing, since this will leave your MySQL instance completely unprotected, allowing anyone to gain complete superuser access.
This is an optional variable. Set to yes to generate a random initial password for the root user (using pwgen). The generated root password will be printed to stdout (GENERATED ROOT PASSWORD: …..).
Sets root (not the user specified in MYSQL_USER!) user as expired once init is complete, forcing a password change on first login. NOTE: This feature is supported on MySQL 5.6+ only. Using this option on MySQL 5.5 will throw an appropriate error during initialization.
then run the container
docker run -e MYSQL_ROOT_PASSWORD=root-pwd -e MYSQL_DATABASE=test-mysql \
--name test-mysql -p 3306:3306 -v /Users/test/Volumes/blume/mysql/test:/var/lib/mysql \
--restart always -d mysql:latest