在前篇《Docker方式安装ZabbixServer/Agent及Grafana》(https://www.jianshu.com/p/055a3cf63233
)一文中,创建zabbix数据库时,由于默认的字符集为latin1,导致在zabbix中,item中无法保存中文,因此,要想办法将其字符集修改为utf8。以下为详细步骤。
- 清理干净mysql-server容器及持久化数据
[root@k8s-node1 ~]# docker stop mysql-server && docker rm mysql-server
mysql-server
mysql-server
[root@k8s-node1 ~]# rm -rf /data/zabbix/mysql
[root@k8s-node1 ~]# mkdir -p /data/zabbix/mysql
[root@k8s-node1 ~]# chmod 777 /data/zabbix/mysql
- 打标签
[root@k8s-node1 ~]# docker tag mysql:5.7 mysql:test
- 建一个空数据库起来
[root@k8s-node1 ~]# docker run --name mysql-test -t \
> -e MYSQL_ROOT_PASSWORD="root" \
> -d mysql:test
7560a6fce6bbacbc22a43bacdeaf8a42a26db8b2b7f02b049201a7e69e1876b6
- 修改mysql初始配置
[root@k8s-node1 ~]# docker cp mysql-test:/etc/mysql/mysql.conf.d/mysqld.cnf .
[root@k8s-node1 ~]# vi mysqld.cnf
# 在文件末尾添加以下几行:
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
# to enforce using of utf8 encoding in db, you can add:
skip-character-set-client-handshake
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[root@k8s-node1 ~]# docker cp mysqld.cnf mysql-test:/etc/mysql/mysql.conf.d/.
- 制作新镜像
[root@k8s-node1 ~]# docker commit mysql-test mysql:test2
sha256:f0e697ed338425e91fecef71a28ed625c7455001d6b7b77a5d1bc1edcef1f8c1
[root@k8s-node1 ~]# docker stop mysql-test && docker rm mysql-test
mysql-test
mysql-test
- 运行新镜像
[root@k8s-node1 ~]# docker run --name mysql-server -t \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="zabbix" \
-e MYSQL_ROOT_PASSWORD="root" \
-v /data/zabbix/mysql:/var/lib/mysql \
-d --restart=always mysql:test2
a7e664c63f40586e8fe2a76cc2e43e57c3ab68e487e5bda9f93420c55d4f7508
- 验证一下
[root@k8s-node1 ~]# docker exec -it mysql-server /bin/sh
# mysql -uroot -proot -Dzabbix
mysql: [Warning] Using a password on the command line
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数据库服务器和数据库字符集
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.02 sec)
# 查看zabbix字符集
mysql> show create database zabbix;
+----------+-----------------------------------------------------------------------------------------+
| Database | Create Database |
+----------+-----------------------------------------------------------------------------------------+
| zabbix | CREATE DATABASE `zabbix` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci */ |
+----------+-----------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
# 可见成功字符集已经默认为utf8!
附:mysql5.7镜像简单分析
[root@k8s-node1 ~]# docker exec mysql-server ls -l /
......
lrwxrwxrwx 1 root root 34 Apr 23 04:15 entrypoint.sh -> usr/local/bin/docker-entrypoint.sh
......
可见,在此容器的根目录下有一个链接entrypoint.sh指向/usr/local/bin/docker-entrypoint.sh文件,该文件在容器启动后会被运行,执行初始化动作。分析其代码发现,它接受5个环境变量参数:
# 跳到183行
# Initialize values that might be stored in a file
file_env 'MYSQL_ROOT_HOST' '%'
file_env 'MYSQL_DATABASE'
file_env 'MYSQL_USER'
file_env 'MYSQL_PASSWORD'
file_env 'MYSQL_ROOT_PASSWORD'
在运行容器时如果指定了MYSQL_DATABASE、'MYSQL_USER、MYSQL_PASSWORD这三个参数,那么它就会创建一个指定用户密码的自定义空数据库。
# 再走到275行
# Creates a custom database and user if specified
if [ -n "$MYSQL_DATABASE" ]; then
mysql_note "Creating database ${MYSQL_DATABASE}"
docker_process_sql --database=mysql <<<"CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;"
fi
这一段是创建自定义数据库代码,因为在语句中未指定“default character set utf8”,因此它使用了默认的字符集“latin1”。