docker中mysql数据库默认字符集修改

为什么80%的码农都做不了架构师?>>>   hot3.png

#1 安装docker mysql镜像。

这个简单,原来用过直截在docker-compose.yml文件中添加mysql一节,

mysql:
 16   image: mysql 
 17   ports:
 18     - "3306:3306"
 19   volumes:
 20     - ./data:/var/lib/mysql
 21   environment:
 22     - MYSQL_DATABASE=*****
 23     - MYSQL_ROOT_PASSWORD=******************

然后直截,进行构建,下载mysql镜像

docker-compose build

#2 起动数据库

启动docker项目

docker-compose up

数据库已经运行起来了,下一步就是创建数据库了,可是mysql的默认字符集还没有修改,查看默认字符集的设置:

mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     |
| character_set_connection | latin1                     |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | latin1                     |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

这样可不行,得改成utf8才能支持中文。

#3 查找my.cnf

mysql的官方镜像中,my.cnf文件放在/etc/mysql/中。

root@8fd3e5751f88:/etc/mysql# ls -al
total 24
drwxr-xr-x  8 root root 4096 Mar 28 08:29 .
drwxr-xr-x 61 root root 4096 Mar 28 08:08 ..
drwxr-xr-x  2 root root 4096 Mar 28 08:28 conf.d
lrwxrwxrwx  1 root root   24 Mar 21 21:53 my.cnf -> /etc/alternatives/my.cnf
-rw-r--r--  1 root root 1050 Nov 28 19:25 my.cnf.fallback
-rw-r--r--  1 root root  797 Mar 28 08:23 mysql.cnf
drwxr-xr-x  2 root root 4096 Mar 28 08:32 mysql.conf.d
root@8fd3e5751f88:/etc/mysql# 

经查看验证,my.cnf 中,除了指定 包含conf.d 和mysql.conf.d目录之外,什么内容都没有。 要找mysqld和client节,都找不到,那只发在这两个目录中找了。 终于在 /etc/mysql/mysql.conf.d/mysql.cnf中找到一些配置,最起码是有[mysqld]节内容的。

#4 修改配置

在原有的基础上,参照网上的说法添加

[client]
default-character-set=utf8

在[mysqld]节后面添加如下内容:

#changed by janl at 20170328
default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci

#5 验证配置结果

然后,重启docker,看看默认字符集变过来了没有

mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)

mysql> 

#6 设备root密码

先设置root密码。

mysql> set password for root@localhost = password('***********');
Query OK, 0 rows affected, 1 warning (0.00 sec)

重新登录验证

root@8fd3e5751f88:/# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

加密码登录

root@8fd3e5751f88:/# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.17 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

OK了,这回可以开始工作了。

转载于:https://my.oschina.net/janl/blog/869321

你可能感兴趣的:(docker中mysql数据库默认字符集修改)