MySQL: 容器化方式启动

在这里插入图片描述
这篇文章记录一下使用容器化方式启动MySQL的执行命令与docker-compose.yml文件。

MySQL启动示例

以使用的较为广泛的旧版5.7.16为例,容器化的MySQL实例启动相关的设定希望如下所示:

设定项 设定内容
MySQL版本 5.7.16
DockerHub地址 liumiaocn/mysql:5.7.16
环境变量:root用户密码 liumiao123
环境变量:时区 Asia/Shanghai
数据卷:MySQL数据 /var/lib/mysql
配置卷:MySQL配置 /etc/mysql/conf.d

Docker方式

  • 执行命令如下所示
docker run --name mysql -d                          \
           -e MYSQL_ROOT_PASSWORD=liumiao123        \
           -e TZ=Asia/Shanghai                      \
           -v `pwd`/mysql/data/:/var/lib/mysql      \
           -v `pwd`/mysql/conf.d/:/etc/mysql/conf.d \
           -p 32011:3306 liumiaocn/mysql:5.7.16
  • 启动日志示例
liumiaocn:data liumiao$ docker run --name mysql -d                          \
>            -e MYSQL_ROOT_PASSWORD=liumiao123        \
>            -e TZ=Asia/Shanghai                      \
>            -v `pwd`/mysql/data/:/var/lib/mysql      \
>            -v `pwd`/mysql/conf.d/:/etc/mysql/conf.d \
>            -p 32011:3306 liumiaocn/mysql:5.7.16
adb83bc9d47625904b4b495317bd175aaccf06c6ed24c33146ef0ea03fe82268
liumiaocn:data liumiao$
  • 结果确认
liumiaocn:data liumiao$ docker ps |grep mysql
adb83bc9d476        liumiaocn/mysql:5.7.16                         "docker-entrypoint.s…"   5 seconds ago       Up 3 seconds        0.0.0.0:32011->3306/tcp                             mysql
liumiaocn:data liumiao$ docker exec -it mysql sh
# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.16 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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>

Docker-Compose方式

liumiaocn:mysql liumiao$ cat docker-compose.yaml 
version: '2'

services:
  # database service: mysql
  mysql:
    image: liumiaocn/mysql:5.7.16
    ports:
      - "32011:3306"
    volumes:
      - ./mysql/data/:/var/lib/mysql
      - ./mysql/conf.d/:/etc/mysql/conf.d
    environment:
      - TZ=Asia/Shanghai
      - MYSQL_ROOT_PASSWORD=liumiao123
    restart: "no"
liumiaocn:mysql liumiao$
  • 启动日志示例
liumiaocn:mysql liumiao$ ls
docker-compose.yaml
liumiaocn:mysql liumiao$ docker-compose up -d
Creating network "mysql_default" with the default driver
Creating mysql_mysql_1 ... done
liumiaocn:mysql liumiao$
  • 结果确认
liumiaocn:mysql liumiao$ docker-compose ps
    Name                  Command             State            Ports         
-----------------------------------------------------------------------------
mysql_mysql_1   docker-entrypoint.sh mysqld   Up      0.0.0.0:32011->3306/tcp
liumiaocn:mysql liumiao$ 
liumiaocn:mysql liumiao$ docker exec -it mysql_mysql_1 sh
# mysql -uroot -pliumiao123
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 2
Server version: 5.7.16 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> 
发布了1091 篇原创文章 · 获赞 1305 · 访问量 405万+

猜你喜欢

转载自blog.csdn.net/liumiaocn/article/details/104426749