查看这篇文章的前半部分即可:
虚拟机上安装docker,并安装flink镜像
可以使用docker search mysql命令查看远程镜像仓库中的镜像信息,或者访问dockerhub去找需要的镜像
这里直接拉取镜像:
docker pull mysql:5.7
docker run -d -p 3306:3306 --privileged=true \
-v /amos/mysql/log:/var/log/mysql \
-v /amos/mysql/data:/var/lib/mysql \
-v /amos/mysql/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=goodme \
--name mysql \
mysql:5.7
说明:上面三个-v分别挂载了三个本地卷,分别对应容器内的日志目录,data目录,和配置目录
初始密码是MYSQL_ROOT_PASSWORD对应的值,容器名字为mysql
用docker ps命令查看mysql容器是否启动
[root@localhost conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3c16d0f3a9c0 mysql:5.7 "docker-entrypoint.s…" 3 seconds ago Up 2 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
进入容器
docker exec -it 3c16d0f3a9c0 bash
在容器中执行命令,myql -uroot -p
bash-4.2# mysql -uroot -p
需要的密码是步骤2.2中的MYSQL_ROOT_PASSWORD对应的密码
执行如下命令show variables like ‘character%’;,查看字符集相关参数:
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)
需要将latin1相关的选项改成utf8,这些latin1的选项会导致中文乱码
需要特别注意的是:不要通过sqlyog或者其他数据库连接工具来执行show variables like ‘character%’,应为这些数据库连接客户端的查询结果可能和在数据库中直接查看的结果不一样。
在外部挂载的配置文件目录中添加配置文件,我用的外部挂载目录是
/amos/mysql/conf
在该文件下vi新建个配置文件,my.cnf, 写入如下内容
[client]
default_character_set=utf8
[mysqld]
collation_server = utf8_general_ci
character_set_server = utf8
重启mysql容器
docker restart 3c16d0f3a9c0
一定要注意:配置只会对新建的数据库和表生效
[root@localhost conf]# docker exec -it 3c16d0f3a9c0 bash
bash-4.2# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.44 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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> 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.00 sec)
都变成了utf8,搞定,gagaga!