Execute the command below.
For bash:
docker run -d \
--name mysql-dev \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=p@55w0rd \
-v mysql-data:/var/lib/mysql \
mysql:8.0.44-debian
For powershell:
docker run -d `
--name mysql-dev `
-p 3306:3306 `
-e MYSQL_ROOT_PASSWORD=p@55w0rd `
-v mysql-data:/var/lib/mysql `
mysql:8.0.44-debian
š¢ For mac silicon specific:
mysql:8.0.44-debian does not support ARM architectureUse – so use mysql:latest instead of mysql:8.0.44-debian or check official docker hub for suitable image.
Note that, this image is not at your local machine yet. So, it will be pulled from the docker hub first and pulled only once (unless you change the tag).
--nameis the name of container.-dmeans detached mode.-pis a port. Its format is like [HOST_PORT]:[CONTAINER_PORT]. Host port is your machine’s port. This container is running in port 3306 of your machine.-esets environment and we are setting the password here.-vis a volume. It helps to persist the database even after the deletion of the container.mysql:8.0.44-debianis the image with tag.mysqlis image name and8.0.44-debianis image tag. Always visit the docker hub to get a latest image.
Verification
To verify container, execute docker ps command. It displays the running containers. It must show Up x seconds under the status tab (as shown below).
~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ab7dc0e5e718 mysql:8.0.44-debian "docker-entrypoint.sā¦" 10 seconds ago Up 10 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysql-dev
Run it in the terminal
docker exec -it mysql-dev mysql -u root -p
Execute this command and provide the password. After this, your terminal should look like as shown below:
mysql>
Show databases:
Show databases;
After this your terminal would look like this:
mysql> Show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.02 sec)
Create database:
create database person_db;
Use created db:
use person_db;
Create table:
create table person
(
id int auto_increment primary key,
first_name varchar(30) not null,
last_name varchar(30) not null
);
Show tables:
show tables;
Insert record(s):
insert into person(first_name,last_name)
values
('Ravindra','Devrani'),
('John','Doe'),
('Nayan','Singh');
Select record(s)
select
id,
first_name,
last_name
from person;
Exit from interactive terminal
exit;
SQL Clients
You can use DBeaver, mysql workbench or any vscode extension.
Common errors
- While using
DBeaver, You might get the errorPublic Key Retrieval is not allowed.- Click on “Driver properties” tab in connection settings
- Find or add:
allowPublicKeyRetrieval = true
