Linux
docker安装Mysql
1.docker镜像地址配置
vim /etc/docker/daemon.json
2.加入配置信息
{"registry-mirrors": ["https://wghlmi3i.mirror.aliyuncs.com", "https://docker.mirrors.ustc.edu.cn", "https://6kx4zyno.mirror.aliyuncs.com"]}
3.重启docker
service docker restart
4.拉取mysql镜像
docker pull mysql:tag
如果不写tag则表示下载最新的版本:docker pull mysql
目前用mysql5.7较多,我们以mysql5.7为例
docker pull mysql:5.7
5.启动mysql容器
--name指定容器名字 -v目录挂载 -p指定端口映射 -e设置mysql参数 -d后台运行
启动容器前应先创建物理机与容器内的映射信息
创建映射路径,log为mysql日志路径,data为mysql数据路径,conf为mysql配置文件路径
mkdir -p /docker/mysql/var/log /docker/mysql/var/data /docker/mysql/var/conf
docker run -p 5306:3306 --name mysql57 -v /docker/mysql/var/log:/var/log/mysql -v /docker/mysql/var/data:/var/lib/mysql -v /docker/mysql/var/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=D7txumqc2b! -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
$\textcolor{RedOrange}{需注意:} $冒号前的为物理机的映射信息,冒号后的为容器的映射信息,映射前应先创建物理机路径
# 设置mysql字符集
--character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
$\textcolor{RedOrange}{注意事项:在docker中安装mysql映射端口信息时需确定端口是否占用} $
docker run -d -p 3306:3306 --privileged=true -v /docker/mysql/conf/my.cnf:/etc/my.cnf -v /docker/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci
参数说明:
run 运行一个容器
-d 后台运行
-p 3306:3306 容器内部端口和服务器端口映射关联
--privileged=true 设置mysql用户,否则外部不能使用root用户登录
-v /docker/mysql/conf/my.cnf:/etc/my.cnf 服务器的/docker/mysql/conf/my.cnf配置映射到docker的my.cnf
-v /docker/mysql/data:/var/lib/mysql 映射数据库的数据目录,避免docker删除重新运行mysql容器,导致数据丢失
-e MYSQL_ROOT_PASSWORD=123456 设置root账号的密码
--name mysql mysql:5.7 从docker镜像mysql:5.7启动一个容器,并设置容器的名称为mysql
--character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci 设置数据库默认编码
docker run -p 3312:3306 --name mysql -v /docker/mysql/var/log:/var/log/mysql -v /docker/mysql/var/data:/var/lib/mysql -v /docker/mysql/var/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=mysql_password -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
6.查看mysql是否运行中
docker ps
[root@nt-web var]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fbf083b59b38 mysql:5.7 "docker-entrypoint.s…" 18 seconds ago Up 17 seconds 33060/tcp, 0.0.0.0:5306->3306/tcp, :::5306->3306/tcp mysql57
cf10e973d1db jumpserver/magnus:v2.27.0 "./entrypoint.sh" 3 days ago Up 3 days (healthy) 0-29999/tcp, 30101-65535/tcp, 0.0.0.0:30000-30100->30000-30100/tcp, :::30000-30100->3000/tcp jms_magnus
b0f57cee920e jumpserver/lion:v2.27.0 "./entrypoint.sh" 3 days ago Up 3 days (healthy) 4822/tcp, 8081/tcp
7.进入mysql容器
[root@nt-web var]# docker exec -it fbf083b59b38 bash
root@fbf083b59b38:/# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
root@fbf083b59b38:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, 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.
8.查看mysql字符集
mysql> show variables like 'character_set_%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
$\textcolor{RedOrange}{需注意的是,如果用sqlyog等客户端执行命令查询到的字符集不一定准确,可能由客户端转过编码} $
9.退出mysql
mysql> exit;
10.退出容器
root@fbf083b59b38:/# exit;
11.删除mysql镜像
[root@nt-web var]# docker rm -f 30b362f9e773
30b362f9e773为容器的CONTAINER ID,大家可填写各自具体的容器id删除
标签:set,--,Mysql5.7,character,mysql,var,docker,安装 From: https://www.cnblogs.com/deepsoul/p/17071985.html