Docker中安装mysql、redis

本文档详细介绍了如何在Docker环境下安装并配置MySQL 5.7和Redis。首先通过`docker pull`命令下载镜像,然后启动容器,并进行相应的配置,如设置MySQL的utf8字符集和初始化连接,以及Redis的appendonly持久化。接着,展示了如何通过`docker exec`进入容器检查配置,并使用`docker update`确保容器在启动时运行MySQL和Redis。
摘要由CSDN通过智能技术生成

一、安装mysql

1.1.docker中安装mysql

[vagrant@localhost /]$ sudo docker pull mysql:5.7
5.7: Pulling from library/mysql
123275d6e508: Already exists 
27cddf5c7140: Pull complete 
c17d442e14c9: Pull complete 
2eb72ffed068: Pull complete 
d4aa125eb616: Pull complete 
52560afb169c: Pull complete 
68190f37a1d2: Pull complete 
3fd1dc6e2990: Pull complete 
85a79b83df29: Pull complete 
35e0b437fe88: Pull complete 
992f6a10268c: Pull complete 
Digest: sha256:82b72085b2fcff073a6616b84c7c3bcbb36e2d13af838cec11a9ed1d0b183f5e
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

1.2.查看镜像

[vagrant@localhost /]$ sudo docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
mysql        5.7       f07dfa83b528   2 weeks ago   448MB

1.3.启动mysql

sudo 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

1.4.修改配置

[root@hadoop-104 conf]# pwd /mydata/mysql/conf
[root@localhost conf]# 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
#重启mysql
[root@localhost conf]# docker restart mysql



1.5. 进入容器查看配置:

[root@localhost conf]# docker exec -it mysql /bin/bash
root@b216be101add:/# whereis mysql
mysql: /usr/bin/mysql /usr/lib/mysql /etc/mysql /usr/share/mysql
root@b216be101add:/# ls /etc/mysql 
my.cnf
root@b216be101add:/# cat /etc/mysql/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
root@b216be101add:/# 
#退出交互模式
root@b216be101add:/# exit

1.6.设置启动docker时,即运行mysql

[root@localhost conf]# docker update mysql --restart=always
mysql
[root@localhost conf]# 

二、安装redis

2.1.docker中安装redis

[root@localhost /]# docker pull redis
Using default tag: latest
latest: Pulling from library/redis
6ec7b7d162b2: Already exists 
1f81a70aa4c8: Pull complete 
968aa38ff012: Pull complete 
884c313d5b0b: Pull complete 
6e858785fea5: Pull complete 
78bcc34f027b: Pull complete 
Digest: sha256:0f724af268d0d3f5fb1d6b33fc22127ba5cbca2d58523b286ed3122db0dc5381
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
[root@localhost /]# 

2.2.docker中启动redis

[root@localhost /]# mkdir -p /mydata/redis/conf
[root@localhost /]# touch /mydata/redis/conf/redis.conf
[root@localhost /]# echo "appendonly yes"  >> /mydata/redis/conf/redis.conf
[root@localhost /]# 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
ce7ae709711986e3f90c9278b284fe6f51f1c1102ba05f3692f0e934ceca1565
[root@localhost /]#

2.3.连接到docker的redis

[root@localhost /]# docker exec -it redis redis-cli
127.0.0.1:6379> set key1 v1
OK
127.0.0.1:6379> get key1
"v1"
127.0.0.1:6379> 

2.4.设置redis容器在docker启动的时候启动

[root@hadoop-104 ~]# docker update redis --restart=always
redis
[root@hadoop-104 ~]# 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值