MySQL is the world’s most popular open source database. With its reliability, ease of use and performance, MySQL has become the database of choice for web applications.
1. Check the available MySQL versions
Alternatively, you can search for available versions by docker search mysql.
2. Pull the mysql image
3. Running mysql
Starting a MySQL instance is simple
|
|
Parameter description.
- -p 3306:3306 : Maps the 3306 port of the container service to the 3306 port of the host, so that external hosts can access the MySQL service directly via host ip:3306.
- MYSQL_ROOT_PASSWORD=123456 : Set the password for the MySQL service root user.
Start mysql via yml file
The following is stack.yml
|
|
Run docker stack deploy -c stack.yml mysql
(or docker-compose -f stack.yml up
), wait for it to fully initialise, then access http://swarm-ip:8080
, http://localhost:8080
, or http://host-ip:8080
(as appropriate).
4. Container shell access and viewing MySQL logs
The exec command allows access to the mysql container to execute commands
|
|
Get container logs via logs
|
|
5. Using a custom MySQL configuration file
The default configuration for MySQL can be found in /etc/mysql/my.cnf
.
If /my/custom/config-file.cnf
is the path and name of your custom configuration file, you can start your mysql
container like this
|
|
To change the default encoding and sorting rules for all tables to use UTF-8 ( utf8mb4
), simply run the following command.
|
|
View the full list of support
|
|
6. Environment variables
parameters | must | description |
---|---|---|
MYSQL_ROOT_PASSWORD | Must | The password set as the MySQLroot superuser account. |
MYSQL_DATABASE | Optional | Specifies the name of the database created at startup, and if a user/password is provided (see below), that user will be granted superuser access to that database |
MYSQL_USER MYSQL_PASSWORD | Optional | Creates a new user and sets the password for that user, who will be granted superuser access to the database specified by the MYSQL_DATABASE variable. These two variables are required to create a user. Not using this to create the root superuser will by default use MYSQL_ROOT_PASSWORD. |
MYSQL_ALLOW_EMPTY_PASSWORD | Optional | default no, set to allow root to log in with an empty password if yes. |
MYSQL_RANDOM_ROOT_PASSWORD | optional | default no, set if yes to generate a random password for root, the generated password is printed to stdout( GENERATED ROOT PASSWORD: …..) . |
MYSQL_ONETIME_PASSWORD | Optional | On completion of initialization, sets the root (not the user specified in MYSQL_USER!) user to expire, forcing the password to be changed on first login. Any non-null value will activate this setting. Note: This feature is only supported on MySQL 5.6+. Using this option on MySQL 5.5 will raise an appropriate error during initialization. |
MYSQL_INITDB_SKIP_TZINFO | Optional | By default, the entry point script will automatically load the time zone data required by the CONVERT_TZ() function. If not required, any non-null value will disable time zone loading |
7. Password file transfer
For example, a loaded password from the /run/secrets/<secret_name>
file. For example.
|
|
Currently, this only supports MYSQL_ROOT_PASSWORD
, MYSQL_ROOT_HOST
, MYSQL_DATABASE
, MYSQL_USER
, and MYSQL_PASSWORD
.
8. Data storage
The preferred option here is to create a directory on the host (outside the container) and mount it to the container data directory
For example
- create a data directory on a suitable volume on the host system, e.g.
/my/own/datadir
. mysql
Start your container like this.
|
|
For used data directories, you can omit $MYSQL_ROOT_PASSWORD
9. Create database dumps
The following data is dumped to a file via exec
|
|
10. Recovering data from data files
For recovering data. You can use the command docker exec
with the -i
flag, similar to the following.
|
|