本文介绍使用docker安装mysql和redis,通过这两个的实战,了解一般的安装容器化服务的流程,体会服务容器化的好处
docker pull mysql:5.7
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端口
● --name:给容器命名
● -v /mydata/mysql/log:/var/log/mysql:将配置文件挂载到主机/mydata/…
● -e MYSQL_ROOT_PASSWORD=root:初始化root用户的密码为root
docker ps
# 查看所有历史 docker 容器
docker ps -a
# 进入挂载的mysql配置目录
cd /mydata/mysql/conf
# 修改配置文件 my.cnf
vi 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
# Esc
# :wq
docker restart mysql
docker pull redis
mkdir -p /mydata/redis/conf
touch /mydata/redis/conf/redis.conf
docker run -p 6379:6379 --name redis \
-v /mydata/redis/data:/data \
-v /mydata/redis/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf
4.配置 redis 持久化
echo "appendonly yes" >> /mydata/redis/conf/redis.conf
# 重启生效
docker restart redis
5.容器随 docker 启动自动运行
# mysql
docker update mysql --restart=always
# redis
docker update redis --restart=always