拉取

 docker pull mysql:8.0
root@gxxc-CE3000F:~# docker pull mysql:8.0
8.0: Pulling from library/mysql
d095360c6402: Pull complete 
a0858301c0b2: Pull complete 
f442c1551840: Pull complete 
29d70ba84e12: Pull complete 
344316b4b482: Pull complete 
7c08d57290ee: Pull complete 
7f14c56729a2: Pull complete 
fdcd6640a567: Pull complete 
bd1a54628d1a: Pull complete 
9ec8cd7bbeec: Pull complete 
c65248fde325: Pull complete 
Digest: sha256:09cf783c365fcd00ecda11e10239775373c8040562aaf59f74d28ab17488af12
Status: Downloaded newer image for mysql:8.0
root@gxxc-CE3000F:~# 

启动

环境变量

参考: MariaDB Docker镜像环境变量说明

启动容器

docker run -tid -e MYSQL_ROOT_PASSWORD=demo123 \
-e MYSQL_DATABASE=demo \
-e MYSQL_USER=user \
-e MYSQL_PASSWORD=userPd \
-v ~/mysql:/var/lib/mysql:rw  mysql:8.0

效果

root@gxxc-CE3000F:~# docker run -tid -e MYSQL_ROOT_PASSWORD=demo123 \
> -e MYSQL_DATABASE=demo \
> -e MYSQL_USER=user \
> -e MYSQL_PASSWORD=userPd \
> -v ~/mysql:/var/lib/mysql:rw  mysql:8.0
7399c986673e5978c68ac18f53e8572a215cabd456a3dbe4be64c13a68a06831
root@gxxc-CE3000F:~# 

登录

普通用户

root@gxxc-CE3000F:~# docker exec -ti fervent_tesla bash
bash-4.4# mysql -uuser -puserPd
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.34 MySQL Community Server - GPL

Copyright (c) 2000, 2023, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| demo               |
| information_schema |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

mysql> 

Root用户

bash-4.4# mysql -uroot -pdemo123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.34 MySQL Community Server - GPL

Copyright (c) 2000, 2023, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| demo               |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> 

扩展

如果想使用Mariadb,只需要把mysql:8.0改成mariadb即可,其他保持不变