Docker 修改MySQL配置

Posted by Sunfy on 2021-05-18
Words 731 and Reading Time 3 Minutes
Viewed Times
Viewed Times
Visitors In Total

Docker 修改MySQL配置

Docker 修改MySQL配置

前言:大部分程序员在刚使用docker时,按照教程迅速的就装完了mysql,在使用的过程中出现各种问题:比如中文乱码、com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1053 > 1024).等等,最简单的方式通过sql去设置,但每次重启又还原了,那么本文就是通过docker下MySQL的配置去解决该问题。


  • 从安装说起,获取镜像

    1
    docker pull mysql:5.7.18
  • 查看镜像

    1
    docker images

    在这里插入图片描述

  • 运行docker容器

    1
    docker run -d -p 3306:3306 --name mymysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.18

    (* 这里没有设置挂载外部的配置)

  • 查看容器是否启动成功

    1
    docker ps

    在这里插入图片描述

  • 进入容器

    1
    docker exec -it 1383f2e49cb7 /bin/bash

    在这里插入图片描述

  • 查看配置
    1
    more /etc/mysql/mysql.conf.d/mysqld.cnf
    (* 如果你执行到这步提示没有该文件,别着急,往下看)
  • 编辑配置
    • 第一种方式:将配置从容器中copy到宿主机(之前文章讲到过,可以去看下,如果没有该文件,那这一步省略,稍后直接将我写好的配置放到容器中就好)
      1
      docker cp mysql:/etc/mysql/mysql.conf.d/mysqld.cnf /root/mysqld.cnf
      mysqld.cnf脚本如下:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      # 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
      character_set_server=utf8
      init_connect='SET NAMES utf8'
      max_allowed_packet = 20M
      [mysql]
      default-character-set = utf8
      [mysql.server]
      default-character-set = utf8
      [mysqld_safe]
      default-character-set = utf8
      [client]
      default-character-set = utf8
      1
      将修改后的配置copy到容器
      1
      docker cp /root/mysqld.cnf mymysql:/etc/mysql/mysql.conf.d/
      (* mymysql是你容器的name,不清楚的可以通过docker ps看到)
    • 另一种方式,直接编辑
      在使用docker容器时,有时候里边没有安装vim,敲vim命令时提示说:vim: command not found,这个时候就需要安装vim,可是当你敲apt-get install vim命令时,提示:
      Reading package lists… Done
      Building dependency tree
      Reading state information… Done
      E: Unable to locate package vim
      这时候需要敲:
      1
      apt-get update
      这个命令的作用是:同步 /etc/apt/sources.list 和 /etc/apt/sources.list.d 中列出的源的索引,这样才能获取到最新的软件包。
      等更新完毕以后再敲命令:
      1
      apt-get install vim
      1
      vim /etc/mysql/mysql.conf.d/mysqld.cnf
  • 接下来重启容器就可以了(可以参考之前的文章)
    1
    docker restart 1383f2e49cb7

Copyright 2021 sunfy.top ALL Rights Reserved

...

...

00:00
00:00