1.从镜像库搜索mysql镜像
docker search mysql
2.获取5.6版本镜像
docker pull mysql:5.6
3.创建mysql目录用来放置相关文件
mkdir -p ~/mysql/data ~/mysql/logs ~/mysql/conf
4.创建容器
docker run -p 3306:3306 --name mymysql -v ~/mysql/conf:/etc/mysql/conf.d -v ~/mysql/logs:/logs -v
~/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6
#-p参数:讲宿主机3306端口映射到容器的3306端口
#-v参数:将之前创建的mysql目录挂载到容器对应的目录
#-e MYSQL_ROOT_PASSWORD=123456:初始化 root 用户的密码
#--name参数:给容器命名
5.查看已创建好的容器
[root@idc ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
2864479fcf21 mysql:5.6 "docker-entrypoint..." 59 minutes ago Up
59 minutes 0.0.0.0:3306->3306/tcp mymysql
6.进入容器
[root@idc ~]# docker exec -it 2864479fcf21 /bin/bash
7.登陆mysql数据库
root@2864479fcf21:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.46 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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>