Redis之Centos與Docker安裝配置

Redis之環境安裝

一、Redis簡介

Redis是一個開放源代碼(BSD許可)的內存中數據結構存儲,用作數據庫,緩存和消息代理。它支持數據結構,例如字符串,哈希,列表,集合,帶範圍查詢的排序集合,位圖,超日誌,帶有半徑查詢和流的地理空間索引。Redis具有內置的複製,Lua腳本,LRU逐出,事務和不同級別的磁盤持久性,並通過Redis Sentinel和Redis Cluster自動分區提供了高可用性。

二、Centos下安裝Redis

2.1. 基礎安裝
# 下載Redis版本
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
# 解壓並創建安裝目錄
tar -zxvf redis-5.0.5.tar.gz && mkdir redis-service
# 進入redis解壓後的目錄進行編譯安裝  *prefix需要大寫*
make install PREFIX=/opt/redis-service
# 將解壓保重的配置文件複製到安裝目錄
cp redis.conf /opt/redis-service/
# 啓動
cd /opt/redis-service/bin && redis-server

2.2. 相關配置
# 69行, 將ip監控設置爲 0.0.0.0
bind 0.0.0.0
# 88行 設置允許外界訪問
protected-mode no
# 136行 yes 將用守護線程運行,即後臺運行
daemonize yes
# 172行  設置日誌文件
logfile /opt/redis-service/redis.log
# 508行 設置密碼
requirepass 123456
# 700行, 設置AOF持久化, 將no改爲yes
appendonly yes
2.3. 重新啓動redis
# 需要進入安裝目錄的bin目錄
bin/redis-server redis.conf
# 查看是否執行成功
ps -ef | grep -v grep | grep redis
root      65864      1  0 13:08 ?        00:00:00 bin/redis-server 0.0.0.0:6379
2.4. 使用Redis
root@long: bin/redis-cli
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> set name zhangsan
OK
127.0.0.1:6379> get name
"zhangsan"
127.0.0.1:6379> exit
2.5. 將redis-cli添加到環境變量
# 進入/etc/profile 這是文件是對所有用戶都生效的
vim /etc/profile
# 添加
export PATH=/opt/redis-service/bin:$PATH
# 刷新環境變量讓其生效
source /etc/profile
2.6. 設置redis爲服務
# 進入systemctl目錄
cd /lib/systemd/system
# 添加redis.service服務文件
vim redis.service

文件內容

[Unit]
Description=redis
After=network.target

[Service]
Type=forking
PIDFile=/var/run/redis_6379.pid
ExecStart=/opt/redis-service/bin/redis-server /opt/redis-service/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

使用服務啓動redis

# 重新刷新systemctl
systemctl daemon-reload
# 啓動redis
systemctl start redis
# 關閉redis
systemctl stop redis
# 設置開機自啓
systemctl enable redis.service
# 關閉開機自啓
systemctl disable redis.service
2.7. 卸載redis

刪除安裝目錄
刪除所有redis相關命令腳本
刪除redis解壓文件夾

2.8. 如果無法訪問,關閉防火牆
root@long:/opt/redis-service# systemctl status firewalld  # 查看防火牆狀態 running 說明運行
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2019-11-25 14:45:23 UTC; 10h ago
     Docs: man:firewalld(1)
 Main PID: 876 (firewalld)
    Tasks: 2 (limit: 2276)
   Memory: 25.2M
   CGroup: /system.slice/firewalld.service
           └─876 /usr/bin/python3 /usr/sbin/firewalld --nofork --nopid

root@long:/opt/redis-service# systemctl stop firewalld   # 關閉防火牆 就可以訪問了
root@long:/opt/redis-service# systemctl startfirewalld   # 啓動防火牆
root@long:/opt/redis-service# systemctl status firewalld  # 再次查看防火牆 狀態爲dead 死亡狀態
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Tue 2019-11-26 01:28:46 UTC; 1s ago
     Docs: man:firewalld(1)
  Process: 876 ExecStart=/usr/sbin/firewalld --nofork --nopid (code=exited, status=0/SUCCESS)
 Main PID: 876 (code=exited, status=0/SUCCESS)

三、Docker下安裝Redis

創建docker-compose容器目錄,並創建yaml文件

version: '3'
services:
  redis:
    restart: always
    image: redis:latest
    container_name: redis-service
    ports:
      - "6379:6379"
    command: redis-server --port 6379 --requirepass 123456 --appendonly yes
    volumes:
      - /home/docker-service/redis-service/data:/data
    networks:
      - esnet
networks:
  esnet:
    driver: bridge

將redis數據持久化文件掛載到本地/home/docker-service/redis-service/data

port 6379 設置端口
requirepass 123456 設置密碼
appendonly yes 設置持久化爲AOF

運行docker-compose

docker-compose up 
docker-compose up -d  # 後臺運行
docker-compose down   # 移除容器
docker-compose logs -f redis-compose # 查看redis容器運行日誌 

運行連接:


其他容器部署的yml文件可以看我的碼雲項目:docker-compose-service: docker-compose 容器服務彙總!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章