拉取镜像:
docker pull mysql:5.7
[root@iZm5eeq7orgtvuxpgma2f4Z docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 b84d68d0a7db 21 hours ago 448MB
[root@iZm5eeq7orgtvuxpgma2f4Z docker]#
如果不知道镜像上是否有自己想要的版本,请访问网址:https://hub.docker.com/
启动镜像
docker run -p 3306:3306 --restart=always --name mysql -v /dockerDateSet/mysql/conf:/etc/mysql/conf.d
-v /dockerDateSet/mysql/logs:/logs
-v /dockerDateSet/mysql/data:/var/lib/mysql
-e MYSQL_ROOT_PWD=123456 -d mysql:5.7
--restart=always docker重启容器自动重启
-p 指定端口映射,格式为:主机(宿主)端口:容器端口
--name 为容器指定一个名称
-v 绑定一个卷
-e 设置环境变量
-d 后台运行容器,并返回容器ID
查看容器
[root@iZm5eeq7orgtvuxpgma2f4Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2451d18a726c mysql:5.7 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
进入容器
[root@iZm5eeq7orgtvuxpgma2f4Z ~]# docker exec -it 2451d18a726c /bin/bash
root@2451d18a726c:/# ls
bin boot dev docker-entrypoint-initdb.d entrypoint.sh etc home lib lib64 logs media mnt opt proc root run sbin srv sys tmp usr var
进入mysql
root@2451d18a726c:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 173
Server version: 5.7.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
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 |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
网友评论