大家好,又见面了,我是你们的朋友全栈君。
Docker 修改MySQL配置
作者:YoungJ
前言:大部分程序员在刚使用docker时,按照教程迅速的就装完了mysql,在使用的过程中出现各种问题:比如中文乱码、
com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1053 > 1024).
等等,最简单的方式通过sql去设置,但每次重启又还原了,那么本文就是通过docker下MySQL的配置去解决该问题。
-
从安装说起,获取镜像
docker pull mysql:5.7.18
-
查看镜像
docker images
-
运行docker容器
docker run -d -p 3306:3306 --name mymysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.18
(* 这里没有设置挂载外部的配置)
-
查看容器是否启动成功
docker ps
-
进入容器
docker exec -it 1383f2e49cb7 /bin/bash
-
查看配置
more /etc/mysql/mysql.conf.d/mysqld.cnf
(* 如果你执行到这步提示没有该文件,别着急,往下看)
-
编辑配置
-
第一种方式:将配置从容器中copy到宿主机(之前文章讲到过,可以去看下,如果没有该文件,那这一步省略,稍后直接将我写好的配置放到容器中就好)
docker cp mysql:/etc/mysql/mysql.conf.d/mysqld.cnf /root/mysqld.cnf
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
-
-
接下来重启容器就可以了(可以参考之前的文章)
docker restart 1383f2e49cb7
(* 1383f2e49cb7是容器的CONTAINER ID,可以通过docker ps看到 )
可以参考:
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/144833.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...