Ubuntu 16.04解决Docker安装MySQL后中文乱码问题

1、Docker 安装 MySQL
docker pull mysql:5.7
如果很慢,可以用下面的源:
docker pull daocloud.io/library/mysql:5.7
然后修改镜像名:
docker tag 镜像ID mysql:5.7
2、创建宿主机对应的目录
我将mysql相关的目录放到/opt下
cd /opt
sudo mkdir mysql
cd mysql
sudo mkdir data conf logs
3、解决中文乱码问题。在/opt/mysql/conf中创建并编辑my.cnf文件
cd /opt/mysql/conf
sudo touch my.cnf
sudo vi my.cnf
然后复制下面的内容到文件中:

Copyright (c) 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, version 2.0,

as published by the Free Software Foundation.

This program is also distributed with certain software (including

but not limited to OpenSSL) that is licensed under separate terms,

as designated in a particular file or component or in included license

documentation. The authors of MySQL hereby grant you an additional

permission to link the program and your derivative works with the

separately licensed software that they have included with MySQL.

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, version 2.0, 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

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

Solve Chinese Encode Problem

[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
4、在宿主机中,将项目的sql文件(例如jids.sql)放到/opt/mysql/data下
5、创建mysql容器
docker run -p 3306:3306 --name mysql -v PWD/logs:/logs -v PWD是指当前目录。比如,我在/opt/mysql下创建了三个目录:data、conf和logs,那么就在切换到/opt/mysql下执行上面的命令。
6、进入mysql容器,并将/etc/mysql/my.cnf替换为/etc/mysql/conf.d下的my.cnf
7、进入/var/lib/mysql,进入数据库,执行下面命令导入数据库:
create database jids;
use jids;
source jids.sql;

结束。

你可能感兴趣的:(Ubuntu 16.04解决Docker安装MySQL后中文乱码问题)