mysql中文乱码记录

database

0.中文乱码

mysql中表结构">1.查看mysql中表结构

show create table log_data

charset为utf8,代码端:通过gorm修改 在创建表时候修改(这里在连接数据库时已经修改过了,但是没有修改成功)

//db为已经连接好的gorm数据

err := db.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8").CreateTable(&LogData{}).Error

2.再次查看

show create table table_name

已经修改成功

但是还是乱码。在本地获取数据库内容,可以正常显示,所以不是字符问题。

查看表中数据段

show variables like "%char%";

3.修改mysql配置文件

修改配置

#1.(容器创建的mysql)进入容器

cd /etc/mysql

#先下载vim编辑器,然后编辑

apt-get update

apt-get install vim

#编辑/etc/mysql/my.cnf

#mysql的配置文件如下

# 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

当然也可以在外面编辑好

#宿主机复制到容器内 mysql为容器名称

docker cp /root/my.cnf mysql:/etc/mysql/my.cnf

#容器内复制到宿主机中

docker cp mysql:/etc/mysql/my.cnf /root/my.cnf

为什么要修改/etc/mysql/my.cnf的文件

我用docker创建mysql5.7时候/etc/mysql/路径下没有my.cnf,然后自己创建了my.cnf,修改后需要重启容器

4.查看修改后的mysql

查看表数据

已正常显示

以上是 mysql中文乱码记录 的全部内容, 来源链接: utcz.com/z/535930.html

回到顶部