
In this post to install MySQL with Docker. Before you begin, you must install the Docker engine and client Docker the machine where you are going to perform the installation. If you do not have Docker , you can follow any of the many tutorials available on the web.
To install correctly, follow the instructions at the following URL:
mysql
If you want to perform an installation by downloading the official image supported by Oracle, you can follow the instructions at the following URL:
mysql
In our case, since we are dealing with a test database, we will use the official MySQL version supported by the community, i.e., the one in the first link.
First, we need to download the MySQL image MySQL our Docker, as indicated at the top of the screen in the URL mentioned above. If you add the command "latest" or leave it blank, the latest available version will be downloaded. If, on the other hand, you want a specific version, you can specify it in the command. In our case, we will download the latest available version.
1. Download the MySQL image MySQL your Docker.
docker mysql:latest
[root@ic-1 ~]# sudo docker mysql:latest
Trying to pull repository docker.mysql …
latest: Pulling from docker.mysql
6a4a3ef82cdc: Pull complete
5518b09b1089: Pull complete
b6b576315b62: Pull complete
349b52643cc3: Pull complete
abe8d2406c31: Pull complete
c7668948e14a: Pull complete
c7e93886e496: Pull complete
Digest: sha256:d6c8301b7834c5b9c2b733b10b7e630f441af7bc917c74dba379f24eeeb6a313
Status: Downloaded newer image formysql:latest
We can list the Docker images Docker have downloaded to verify that we have our mySQL image mySQL this command:
docker
REPOSITORY TAG IMAGE ID CREATED SIZE
mysqlmysql latest 1d9c2219ff69 11 months ago 496MB
2. We execute and start our MySQL instance.
To start a new MySQL container, we need to run the following command:
docker –name=container_name –restart on-failure -d image_name:tag
Options:
image_name : Name of the image that we are going to use to execute the container.
- -name : Optional, serves to give a customized name to our container, if omitted, a random name will be generated.
- -restart : Configures the restart policy of the container, a value must be established in case of error. Check documentation to apply this option.
For example, let's launch a new Docker container Docker MySQL using the following command:
docker mysql-dmysql:latest
With this command, we are starting the MySQL container MySQL "mysqltest" using the latest version of MySQL .
We check that the container is running with the following command:
docker
root@ic-1 ~]# docker
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5b7ba4107799mysql:latest "/entrypoint.sh mysq..." About a minute ago Up About a minute (health: starting) 3306/tcp, 33060-33061/tcp mysql
3.- We recover the temporary password for the mysql container.
docker mysql 2> 1 | grep GENERATED
[root@ic-1 ~]# docker mysql 2> 1 | grep GENERATED
[Entrypoint] GENERATED ROOT PASSWORD: @WK5/k24XE;%/lrGn51v6;q@2Q_KEe5A
4.- We execute the necessary command to connect to the MySQL database running in the container MySQL providing the temporary password obtained in the previous step, as follows.
docker -it mysql mysql -p
[root@ic-1 ~]# docker -it mysql mysql -p
Enter password:
Welcome to the MySQL . Commands end with ; or \g.
Your MySQL id is 471
Server version: 8.0.32
Copyright (c) 2000, 2023, Oracle its affiliates.
Oracle a registered trademark of Oracle and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql>
5.- Once inside MySQL change the temporary root password. To do this, we run the following command:
ALTER USER USER () identified by 'password';
mysql> ALTER USER USER () identified by 'oracleconraul';
Query OK, 0 rows affected (0.19 sec)
6.- With the password changed, in our case to "oracleconraul," you can now start enjoying MySQL
[root@ic-1 ~]# docker -it mysql mysql -p
Enter password:
Welcome to the MySQL . Commands end with ; or \g.
Your MySQL id is 519
Server version: 8.0.32 MySQL Server – GPL
Copyright (c) 2000, 2023, Oracle its affiliatesOracle
Oracle a registered trademark of Oracle and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql> select version();
+———–+
| version() |
+———–+
| 8.0.32 |
+———–+
1 row in set (0.00 sec)
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql
| performance_schema |
| sys |
+——————–+
4 rows in set (0.00 sec)
6.- To stop and/or start the docker MySQL, simply do the following:
Stopping the conenedor:
[root@ic-1 ~]# docker mysql
mysql
–Verify that it has stopped correctly by running the command:
docker -a
[root@ic-1 ~]# docker -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5b7ba4107799mysql:latest "/entrypoint.sh mysq..." 4 hours ago Exited ( 0) 3 minutes ago mysql
–Restart the MySQL container MySQL verify that it is running correctly:
[root@ic-1 ~]# docker mysql
mysql
[root@ic-1 ~]# docker -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5b7ba4107799mysql:latest "/entrypoint.sh mysq..." 5 hours ago Up 4 minutes (healthy) 3306/tcp, 33060-33061/tcp mysql
7.- We log back into mysql and start enjoying this database manager.
[root@ic-1 ~]# docker -it mysql mysql -p
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL . Commands end with ; or \g.
Your MySQL id is 21
Server version: 8.0.32 MySQL Server – GPL
Copyright (c) 2000, 2023, Oracle its affiliates.
Oracle a registered trademark of Oracle and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement
These are the general steps you need to follow to run MySQL in a docker environment. Currently, this type of deployment is becoming increasingly common not only in development environments, but also in production environments, especially due to its versatility and agility in deployment.
For installation on a MySQL machine or server, you can check out this post my blog, which provides step-by-step instructions on how to install MySQL .0 on a Linux machine or server. linux






