Docker 创建 MySQL 容器

1. 拉取镜像
docker pull mysql:5.7
2. 查看当前所有的镜像
docker image ls
3. 创建并启动一个容器
docker run --name test-mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql:5.7
  • -name: 给新创建的容器命名,此处命名为test-mysql
  • -e: 配置信息,此处配置 mysql 的 root 用户的登录密码
  • -p: 端口映射,此处映射主机的3306端口到容器test-mysql的3306端口
  • -d: 成功启动同期后输出容器的完整ID
  • 最后一个mysql:5.7指的是mysql镜像
4. 查看容器运行状态
docker ps
5. 进入容器
docker exec -it test-mysql /bin/bash
6. 连接 mysql
  • 可以通过工具直接连接服务器的3306端口
  • 也可以进入容器,然后通过mysql -u root -p连接
7. 可以启动多个 mysql 容器
docker run --name test2-mysql -e MYSQL_ROOT_PASSWORD=123456 -p 6666:3306 -d mysql
  • 只要容器名字、映射端口不一样就可以。
8. 查看所有容器
docker ps -a
9. 启动和关闭容器
docker start test-mysql # 指定容器名称
docker start 73f8811f669e # 指定容器ID
docker start test-mysql # 指定容器名称
docker start 73f8811f669e # 指定容器ID
10. 修改MySQL配置文件
  1. 方法一: 进入容器,然后修改容器里的配置文件 /etc/mysql/my.cnf,重新启动容器。

  2. 方法二: 挂载主机的配置文件(官方文档)

    The MySQL startup configuration is specified in the file /etc/mysql/my.cnf, and that file in turn includes any files found in the /etc/mysql/conf.d directory that end with .cnf. Settings in files in this directory will augment and/or override settings in /etc/mysql/my.cnf. If you want to use a customized MySQL configuration, you can create your alternative configuration file in a directory on the host machine and then mount that directory location as /etc/mysql/conf.d inside the mysql container.
    
    If /my/custom/config-file.cnf is the path and name of your custom configuration file, you can start your mysql container like this (note that only the directory path of the custom config file is used in this command):
    
    docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
    
    This will start a new container some-mysql where the MySQL instance uses the combined startup settings from /etc/mysql/my.cnf and /etc/mysql/conf.d/config-file.cnf, with settings from the latter taking precedence.
    

转载于:https://my.oschina.net/shadowolf/blog/3048918

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值