文章目录

  • 环境
  • 拉取镜像
  • 启动
  • 进入Mysql
  • 查看mysql信息


环境

1、Centos 8.0
2、docker版本17.12.1-ce
3、mysql 8.0

拉取镜像
[root@iZuf68t6hada0ayijajs45Z docker]# docker pull mysql:8.0
8.0: Pulling from library/mysql
6ec8c9369e08: Pull complete 
177e5de89054: Pull complete 
ab6ccb86eb40: Pull complete 
e1ee78841235: Pull complete 
09cd86ccee56: Pull complete 
78bea0594a44: Pull complete 
caf5f529ae89: Pull complete 
cf0fc09f046d: Pull complete 
4ccd5b05a8f6: Pull complete 
76d29d8de5d4: Pull complete 
8077a91f5d16: Pull complete 
922753e827ec: Pull complete 
Digest: sha256:fb6a6a26111ba75f9e8487db639bc5721d4431beba4cd668a4e922b8f8b14acc
Status: Downloaded newer image for mysql:8.0

使用docker images可以查看已获取的镜像

[root@iZuf68t6hada0ayijajs45Z docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               8.0                 e3fcc9e1cc04        5 hours ago         544MB
启动
[root@iZuf68t6hada0ayijajs45Z svdb]# docker run --name mysql --restart=always \
>     -v /data/svdb/mysql/conf/my.cnf:/etc/mysql/my.cnf \
>     -v /data/svdb/mysql/data:/var/lib/mysql \
>     -p 3317:3306 \
>     -e MYSQL_ROOT_PASSWORD="root" \
>     -e TZ=Asia/Shanghai \
>     -d mysql:8.0 --lower-case-table-names=1

1、-name mysql : 给Mysql容器起一个别名
2、-restart=always:开机启动
3、-v:将容器的文件挂载到宿主目录,避免以后在卸载docker的时候,数据丢失。
如:-v /data/svdb/mysql/conf/my.cnf:/etc/mysql/my.cnf 是将容器的/etc/mysql/my.cnf 挂载到主机的/data/svdb/mysql/conf/my.cnf
4、-e MYSQL_ROOT_PASSWORD:设置密码
5、-p 3317:3306:mysql端口映射到主机的3317端口
6、-d mysql:8.0:后台运行容器
7、–lower-case-table-names=1一定要加,mysql8.0关方规定只有在初始化的时候给与配置,后续不管在my.cnf还是重新docker run --lower都无效(重新docker run --lower必须保证/var/lib/mysql仓库是干净的才可以,不然出错)

进入Mysql

先进入mysql镜像,在登入mysql.

[root@iZuf68t6hada0ayijajs45Z svdb]# docker exec -it mysql /bin/bash
root@f2c3eb8f47c2:/# 
root@f2c3eb8f47c2:/# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.21 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>
查看mysql信息
[root@iZuf68t6hada0ayijajs45Z ~]# docker inspect mysql

完事。