0. 镜像加速(如果没有设置会下载很慢)
这里是 centos的加速
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://f8fei4ql.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
1. 下载镜像
$ docker pull mysql:5.7
这里:5.7
是指mysql的版本,版本地址查看https://hub.docker.com/
2. 启动镜像
$ docker run -p 3306:3306 --name mysql \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7
-p 3306:3306
: 将容器的3306端映射到主机的3306端口-v /mydata/mysal/conf:/etc/mysgl
: 将配置文件夹挂载到主机-v/mydata/mysal/log:/var/log/mysal
: 将日志文件夹挂载到主机-v/mydata/mysal/data:/var/lib/mysql/
: 将配置文件夹挂载到主机-e MYSQL_ROOT_PASSWORD=root
: 初始化root用户的密码
3. 添加配置
$ vim /mydata/mysql/conf/my.cnf
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
$ docker restart mysql
4. 测试结果
$ docker exec -it mysql /bin/bash
$ cat /etc/mysql/my.cnf
$ docker ps