Docker安装MySQL、Redis
in with 0 comment

Docker安装MySQL、Redis

in with 0 comment

Docker 入门 中已经讲解了Docker的安装以及简单使用,本文主要讲解MySQL、Redis的详细安装过程。

安装MySQL

1、搜索镜像
#docker search mysql
NAME      DESCRIPTION              STARS      OFFICIAL         AUTOMATED
mysql    MySQL is a widely  used…   8679      [OK]         
mariadb  MariaDB is a community-…   3023      [OK]         
mysql/mysql-server  Optimized    …   642                          [OK]
centos/mysql-57-centos7  MySQL 5.7 SQL  63     
centurylink/mysql   Image containing…   61                        [OK]

2、拉取最新的镜像
# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
8f91359f1fff: Pull complete 
6bbb1c853362: Pull complete 
e6e554c0af6f: Pull complete 
f391c1a77330: Pull complete 
414a8a88eabc: Pull complete 
fee78658f4dd: Pull complete 
9568f6bff01b: Pull complete 
5a026d8bbe50: Pull complete 
07f193b54ae1: Pull complete 
1e404375a275: Pull complete 
b81b2ef0e430: Pull complete 
2f499f36bd40: Pull complete 
Digest: sha256:6d95fa56e008425121e24d2c01b76ebbf51ca1df0bafb1edbe1a46937f4a149d
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

3、在宿主机创建持久化 mysql data 及mysql.cnf 
[root@localhost ~]# mkdir -p /opt/docker/mysql
[root@localhost ~]# cd /opt/docker/mysql/
[root@localhost mysql]# mkdir conf
[root@localhost mysql]# mkdir data
[root@localhost mysql]# cd conf
[root@localhost conf]# vi mysql.cnf
[root@localhost conf]# ll
总用量 0 
-rw-r--r--. 1 root root 0 10月 20 13:15 mysql.cnf

4.添加操作权限
chmod 777 /opt/docker/mysql/data
备注:挂载时权限验证(操作权限)

5、运行镜像
#docker run -itd -p 3306:3306 --name mysql -v /opt/docker/mysql/conf:/etc/mysql -v /opt/docker/mysql/data:/var/lib/mysql-files -e MYSQL_ROOT_PASSWORD=123456 mysql
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
a5446329a199        mysql               "docker-entrypoint.s…"   16 seconds ago      Up 15 seconds       0.0.0.0:3306->3306/tcp, 33060/tcp   mysql


4、启动成功,查看日志[如果报错,也可以在日志中查看报错原因]
[root@localhost ~]# docker logs -f mysql

第五步中启动命令说明:

5、参考

docker安装mysql8.0 -- 在虚拟机中安装未遇到文章中的问题,但是云主机中安装也遇到文章中的问题,解决方式相同

Navicat 连接数据库成功

![Navicat连接成功](docker-mysql

安装Redis

1、搜索镜像
#docker search redis

2、拉取最新的镜像
# docker pull redis
Using default tag: latest
latest: Pulling from library/redis
8d691f585fa8: Pull complete 
8ccd02d17190: Pull complete 
4719eb1815f2: Pull complete 
200531706a7d: Pull complete 
eed7c26916cf: Pull complete 
e1285fcc6a46: Pull complete 
Digest: sha256:fe80393a67c7058590ca6b6903f64e35b50fa411b0496f604a85c526fb5bd2d2
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest

3、下载 redis.conf 官网地址:http://download.redis.io/redis-stable/redis.conf 修改后,现成的:redis.conf

bind 127.0.0.1 #注释掉这部分,用来限制redis只能本地访问
protected-mode no #默认yes表示开启保护模式,用来限制redis只能本地访问
daemonize no #默认no,改为yes意为以守护进程方式启动,可后台运行,除非kill进程(可选);改为yes会使配置文件方式启动redis失败
dir ./ #输入本地redis数据库存放文件夹(可选)
appendonly yes #redis持久化(可选)
databases 20 #数据库个数,这里设置redis最多有20个数据库

4、用命令启动Redis

# mkdir -p /opt/docker/redis/conf
将第三步的redis.conf放在/opt/docker/redis/conf中
# mkdir -p /opt/docker/redis/data
# docker run -p 6379:6379 --name redis -v /opt/docker/redis/conf:/etc/redis/redis.conf -v /opt/docker/redis/data:/data -d redis redis-server /etc/redis/redis.conf --appendonly yes --requirepass "123456"

# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
572df6bc1977        redis               "docker-entrypoint.s…"   12 seconds ago      Up 11 seconds       0.0.0.0:6379->6379/tcp              redis

--appendonly yes:开启数据持久化

5、验证Redis

# docker exec -it redis redis-cli
127.0.0.1:6379> info
NOAUTH Authentication required. // 提示需要密码
127.0.0.1:6379> Auth 123456     // 输入刚才设置的密码
OK
127.0.0.1:6379> info
# Server
redis_version:5.0.6
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:24cefa6406f92a1f
redis_mode:standalone
os:Linux 3.10.0-1062.1.1.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:8.3.0
process_id:1
run_id:e68f46ef296b8de98ce5dfc71b616d376ac62873
tcp_port:6379
uptime_in_seconds:302
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:12946792
executable:/data/redis-server
config_file:/etc/redis/redis.conf
# Clients
....

RedisDesktopManager登录

docker-redis

6、参考

Docker安装Redis