0.中文亂碼

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
查看表資料

已正常顯示

?
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/300137.html
標籤:其他
上一篇:MySQL45講之前綴索引
下一篇:MySQL45講之前綴索引
