zoukankan      html  css  js  c++  java
  • Docker 修改MySQL配置

    Docker 修改MySQL配置

    转自:https://blog.csdn.net/zhaoyajie1011/article/details/98623666

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

    1. 从安装说起,获取镜像

      docker pull mysql:5.7.18
    2. 查看镜像

      docker images
    3. 在这里插入图片描述
    4. 运行docker容器

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

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

    5. 查看容器是否启动成功

      docker ps
      
      • 1

      在这里插入图片描述

    6. 进入容器

      docker exec -it 1383f2e49cb7 /bin/bash
      
      • 1

      在这里插入图片描述

    7. 查看配置

      more /etc/mysql/mysql.conf.d/mysqld.cnf
      
      • 1

      (* 如果你执行到这步提示没有该文件,别着急,往下看)

    8. 编辑配置

      • 第一种方式:将配置从容器中copy到宿主机(之前文章讲到过,可以去看下,如果没有该文件,那这一步省略,稍后直接将我写好的配置放到容器中就好)

        docker cp mysql:/etc/mysql/mysql.conf.d/mysqld.cnf /root/mysqld.cnf
        
        • 1

        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
        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
      • 将修改后的配置copy到容器
      • 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
        这时候需要敲:

        apt-get update
      • 这个命令的作用是:同步 /etc/apt/sources.list 和 /etc/apt/sources.list.d 中列出的源的索引,这样才能获取到最新的软件包。
        等更新完毕以后再敲命令:

        apt-get install vim
      • vim /etc/mysql/mysql.conf.d/mysqld.cnf
    9. 接下来重启容器就可以了(可以参考之前的文章)

      docker restart 1383f2e49cb7 
    10. (* 1383f2e49cb7是容器的CONTAINER ID,可以通过docker ps看到 )


    可以参考:

  • 相关阅读:
    最新macOS 11.4+Xcode 12.5+iOS 14.6 编译安装WebDriverAgent填坑记
    python +pytest +yaml + Allure 实现接口自动化框架
    DNS原理及其解析过程
    jmeter性能测试--ramp_up和同步定时器的区别
    jmeter常用定时器
    基本sql语句
    MySQL连表查询
    MySQL约束条件
    MySQL单表查询
    linux 查看文件的前几行 或者 某些行
  • 原文地址:https://www.cnblogs.com/liran123/p/13792579.html
Copyright © 2011-2022 走看看