How to Run MySql on Docker Desktop

This article demonstrates how to install the MySQL image and run a container on the Docker Desktop. Additionally, learn how to obtain assistance with MySQL’s CLI and content commands.

The MySQL Docker images are built specifically for Linux platforms. Other platforms are not supported by the MySQL team, and users who use these MySQL Docker images do it at their own risk.

How to Install MySQL on Docker Desktop

To download the MySQL Community Edition image, run the command:

docker pull mysql/mysql-server:8.0

To download the MySQL Enterprise Edition image from the OCR, you must first accept the OCR’s license agreement and log in with your Docker client to the container repository. See the link for details.

Run the CLI command to ensure the image is installed on Docker Desktop:

docker image ls

To start a new Docker container for a MySQL server, use the following command:

docker run -e MYSQL_ROOT_PASSWORD=psw123 -e MYSQL_DATABASE=MyDB -e MYSQL_USER=root -e MYSQL_PASSWORD=psw123 --mount type=volume,src=crv_mysql,dst=/var/lib/mysql -p 3306:3306 -d mysql/mysql-server:8.0

Enter the following command to ensure that the container is created:

docker ps

The command returns a response with the MySql container’s id, which is 77bf33c5d.

After container initialization is finished, the status of the container in the output changes from starting to healthy.

Check the MySQL server status and find the generated temporary password:

docker logs 77bf33c5d015

Note: you can put the container name in the command instead of its ID.

The returned log contains information about the MySql user and password:
root@localhost is created with an empty password ! Please consider switching off the –initialize-insecure option.

To establish a secure connection, you must first create a password.

Run the MySQL client within the MySQL Server container:

docker exec -it 77bf33c5d015 mysql -uroot -p

You are invited to enter a password for the MySql connection. Type the password (e.g., psw123) and click the Enter key. You get response:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 151
Server version: 8.0.28 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation 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>

Change the temporary password with your own:

mysql> ALTER USER root IDENTIFIED WITH mysql_native_password BY 'mysql12345';

Now your MySql connection has new password: mysql12345.

Note: this command provides native (old style) authentication to MySql server.

How to Get Help On MySQL CLI Commands

Type the command HELP to get list of the mysql CLI commands.

CommandAlias/ShortcutDescription
clear\cClear the current input statement.
connect\rReconnect to the server. Optional arguments are db and host.
delimiter\dSet statement delimiter.
edit\eEdit command with $EDITOR.
ego\GSend command to mysql server, display result vertically.
exit\qExit mysql. Same as quit.
go\gSend command to mysql server.
help\hDisplay this help.
nopager\nDisable pager, print to stdout.
notee\tDon’t write into outfile.
pager\PSet PAGER [to_pager]. Print the query results via PAGER.
print\pPrint current command.
prompt\RChange your mysql prompt.
quit\qQuit mysql.
rehash\#Rebuild completion hash.
source\.Execute an SQL script file. Takes a file name as an argument.
status\sGet status information from the server.
system\!Execute a system shell command.
tee\TSet outfile [to_outfile]. Append everything into given outfile.
use\uUse another database. Takes database name as argument.
charset\CSwitch to another charset. Might be needed for processing binlog with multi-byte charsets.
charset\WShow warnings after every statement.
nowarning\wDon’t show warnings after every statement.
resetconnection\xClean session context.
MySql CLI Commands

Type the command to get the content command categories:

mysql> help contents
You asked for help about help category: "Contents"
For more information, type 'help <item>', where <item> is one of the following
categories:
   Account Management
   Administration
   Components
   Compound Statements
   Contents
   Data Definition
   Data Manipulation
   Data Types
   Functions
   Geographic Features
   Help Metadata
   Language Structure
   Loadable Functions
   Plugins
   Prepared Statements
   Replication Statements
   Storage Engines
   Table Maintenance
   Transactions
   Utility

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *