Docker安装MySQL和Redis

作者: JBryan | 来源:发表于2021-01-24 16:26 被阅读0次

Docker安装请参考:https://www.jianshu.com/p/64de448b637c

1、Docker安装MySQL

1.1、Docker仓库中拉取MySQL镜像
[root@localhost ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
a076a628af6f: Pull complete 
f6c208f3f991: Pull complete 
88a9455a9165: Pull complete 
406c9b8427c6: Pull complete 
7c88599c0b25: Pull complete 
25b5c6debdaf: Pull complete 
43a5816f1617: Pull complete 
1831ac1245f4: Pull complete 
37677b8c1f79: Pull complete 
27e4ac3b0f6e: Pull complete 
7227baa8c445: Pull complete 
Digest: sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@localhost ~]#
1.2、启动MySQL容器

--name指定容器名字 -v目录挂载 -p指定端口映射 -e设置mysql参数 -d后台运行

docker run -p 3306:3306 --name mysql \
-v /usr/local/app/mysql/log:/var/log/mysql \
-v /usr/local/app/mysql/data:/var/lib/mysql \
-v /usr/local/app/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7

运行结果:

[root@localhost ~]# docker run -p 3306:3306 --name mysql \
> -v /usr/local/app/mysql/log:/var/log/mysql \
> -v /usr/local/app/mysql/data:/var/lib/mysql \
> -v /usr/local/app/mysql/conf:/etc/mysql \
> -e MYSQL_ROOT_PASSWORD=root \
> -d mysql:5.7
645dab4cb60caa19225dbc73660b969c15f5d45841e5222db738504a9f4bd169
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED         STATUS         PORTS                                            NAMES
645dab4cb60c   mysql:5.7              "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes   0.0.0.0:3306->3306/tcp, 33060/tcp                mysql

MySQL用到的日志、配置、数据文件都会挂载到下面文件夹中

[root@localhost ~]# cd /usr/local/app/mysql
[root@localhost mysql]# ll
total 4
drwxr-xr-x. 2 root    root    6 Jan 24 12:57 conf
drwxr-xr-x. 5 polkitd root 4096 Jan 24 12:58 data
drwxr-xr-x. 2 root    root    6 Jan 24 12:57 log
[root@localhost mysql]# 
1.3、修改MySQL配置文件

进入/usr/local/app/mysql/conf,创建my.conf文件

[root@localhost mysql]# cd /usr/local/app/mysql/conf
[root@localhost conf]# ll
total 0
[root@localhost conf]# vi my.conf

复制下面内容到my.conf,保存退出

[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
mysql
[root@localhost conf]# docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED          STATUS          PORTS                                            NAMES
645dab4cb60c   mysql:5.7              "docker-entrypoint.s…"   10 minutes ago   Up 7 seconds    0.0.0.0:3306->3306/tcp, 33060/tcp                mysql
1.4、设置MySQL自动启动
[root@localhost conf]# docker update mysql --restart=always
mysql
[root@localhost conf]# 

此时即可使用Navicat等连接工具连接MySQL了


MySQL.jpg

2、Docker安装Redis

2.1、创建Redis配置文件

mkdir -p表示递归创建目录。

[root@localhost /]# mkdir -p /usr/local/app/redis/conf
[root@localhost /]# cd /usr/local/app/redis/conf
[root@localhost conf]# vi redis.conf

在redis.conf中添加appendonly yes,开启持久化。

appendonly yes

然后保存退出

2.2、拉取Redis镜像
docker pull redis

启动Redis

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

设置Redis容器自动启动

docker update redis --restart=always
2.3、安装Redis客户端

官网下载地址:https://redisdesktop.com/pricing,但是官网下载需要付费,百度网盘下载:https://pan.baidu.com/s/1K5Yd1OQ8nAofCl79Hp8r1A,提取码:528k。
下载、安装完成以后,运行Redis Desktop Manager

redis1.png
点击Connect to Redis Server,然后输入IP、端口,有设置密码的话,输入密码。
redis3.jpg
连接成功。
redis4.jpg

相关文章

网友评论

    本文标题:Docker安装MySQL和Redis

    本文链接:https://www.haomeiwen.com/subject/twlmzktx.html