1. 安装docker
查看这篇文章的前半部分即可:
虚拟机上安装docker,并安装flink镜像
2. 安装mysql 5.7
2.1 下载mysql镜像
可以使用docker search mysql命令查看远程镜像仓库中的镜像信息,或者访问dockerhub去找需要的镜像
这里直接拉取镜像:
docker pull mysql:5.7
2.2 安装mysql镜像
docker run -d -p 3306:3306 --privileged=true \
-v /opt/module/mysql/log:/var/log/mysql \
-v /opt/module/mysql/data:/var/lib/mysql \
-v /opt/module/mysql/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=root \
--name mysql \
mysql:5.7
说明:上面三个-v分别挂载了三个本地卷,分别对应容器内的日志目录,data目录,和配置目录
初始密码是MYSQL_ROOT_PASSWORD对应的值,容器名字为mysql
2.3 进入容器并查看数据库参数信息
用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,需要的密码是步骤2.2中的MYSQL_ROOT_PASSWORD对应的密码
bash-4.2# mysql -uroot -p
需要注意的是:可能出现==ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)==问题
直接去新建文件my.cnf,文件内容如下:
vi /opt/module/mysql/conf/my.cnf
[client]
default_character_set=utf8
[mysqld]
collation_server = utf8_general_ci
character_set_server = utf8
skip-grant-tables
其中skip-grant-tables是为了解决不能登录问题,其他语句是解决中文乱码问题
- 停止docker容器:docker stop mysql
- 重启docker服务:systemctl restart docker
- 重启mysql容器: docker start mysql
重新进入容器:docker exec -it mysql bash;
执行如下命令show variables like ‘character%’;,查看字符集相关参数:
[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)
需要将latin1相关的选项改成utf8,如果有value为latin1会导致中文乱码
需要特别注意的是:不要通过sqlyog或者其他数据库连接工具来执行show variables like ‘character%’,应为这些数据库连接客户端的查询结果可能和在数据库中直接查看的结果不一样。
都变成了utf8,搞定,gagaga!