Docker 相关应用(10)--部署数据库

安装数据库

$ docker pull mysql:5.7.22

数据卷持久化

$ docker run -p 3306:3306 --name mysql \
  -v /var/lib/docker/mysql/conf:/etc/mysql \
  -v /var/lib/docker/mysql/logs:/var/log/mysql \
  -v /var/lib/docker/mysql/data:/var/lib/mysql \
  -e MYSQL_ROOT_PASSWORD=123456 \
  -d mysql:5.7.22
$ docker ps # 查看是否启动
$ docker run -it --rm mysql:5.7.22 bash # 交互的方式进入到容器
$ whereis mysql # 查看mysql安装在哪
$ cd /etc/mysql/
$ ls -al
$ cd mysql.conf.d/
$ ls -al
$ cat mysqld.cnf

    # Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
    #
    # This program is free software; you can redistribute it and/or modify
    # it under the terms of the GNU General Public License as published by
    # the Free Software Foundation; version 2 of the License.
    #
    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    # GNU General Public License for more details.
    #
    # You should have received a copy of the GNU General Public License
    # along with this program; if not, write to the Free Software
    # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

    #
    # The MySQL  Server configuration file.
    #
    # For explanations see
    # http://dev.mysql.com/doc/mysql/en/server-system-variables.html

    [mysqld]
    pid-file    = /var/run/mysqld/mysqld.pid
    socket      = /var/run/mysqld/mysqld.sock
    datadir     = /var/lib/mysql  # 数据库存放路径
    #log-error  = /var/log/mysql/error.log
    # By default we only accept connections from localhost
    #bind-address   = 127.0.0.1
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    
# 其他配置查看dockerhub官方mysql文档

将数据库放入到MySQL中

[图片上传失败...(image-38fd8-1574149377581)]

$ cd ..
$ ls -al
$ cd conf.d/
$ ls -al
$ cat mysqldump.cnf
    [mysqldump]
    quick
    quote-names
    max_allowed_packet  = 16M
$ cd ..
$ ls -al
$ cd mysql.conf.d/
$ ls -al
$ cat mysqld.cnf 
# 需要将配置文件放到数据卷中进行持久化
$ exit
$ docker ps -a
$ docker rm -f 1c06c01a415b
$ docker run -p 3306:3306 --name mysql \
  -v /var/lib/docker/mysql/logs:/var/log/mysql \
  -v /var/lib/docker/mysql/data:/var/lib/mysql \
  -e MYSQL_ROOT_PASSWORD=123456 \
  -d mysql:5.7.22
$ docker ps
$ docker exec -it mysql bash # 交互的方式进入容器
$ cd /etc/mysql/mysql.conf.d/
$ ls -al
$ cat mysqld.cnf
$ cd ..
$ ls -al
$ cd conf.d/
$ ls -al
$ cat mysqldump.cnf
$ cd ..
$ cd mysqldump.cnf
$ ls -al
$ echo "max_allowed_packet= 128M" >> mysqld.cnf
$ cat mysqld.cnf

将容器里的文件复制到宿主机

$ docker exec -it mysql bash
$ cd /etc/mysql/
$ ls -al
$ cd /var/lib/docker/mysql/conf/
$ docker cp mysql:/etc/mysql .  # 将容器中的配置文件移动到当前目录
$ ls
$ cd mysql/
$ mv *.* ..
$ rm -rf mysql
$ ll
$ docker ps -a
$ docker rm -f 035e7a946455
$ docker run -p 3306:3306 --name mysql \
  -v /var/lib/docker/mysql/conf:/etc/mysql \
  -v /var/lib/docker/mysql/logs:/var/log/mysql \
  -v /var/lib/docker/mysql/data:/var/lib/mysql \
  -e MYSQL_ROOT_PASSWORD=123456 \
  -d mysql:5.7.22
$ docker stop mysql  # 关闭数据库容器
$ ll
$ rm -fr data/
$ ll
$ docker restart mysql # 重启数据库容器
# 查看数据库 发现连接不上
$ docker ps
$ docker rm -f adaacf7270d3
$ docker run -p 3306:3306 --name mysql \
  -v /var/lib/docker/mysql/conf:/etc/mysql \
  -v /var/lib/docker/mysql/logs:/var/log/mysql \
  -v /var/lib/docker/mysql/data:/var/lib/mysql \
  -e MYSQL_ROOT_PASSWORD=123456 \
  -d mysql:5.7.22
$ docker ps

$ cd conf/mysql.conf.d/
$ cat mysqld.cnf

你可能感兴趣的:(Docker 相关应用(10)--部署数据库)