Redis 入門 - 0(安裝、遠端訪問)

Chinkiver發表於2018-02-13

構建獨立 Redis 伺服器,並可遠端訪問和使用

Redis 4.0.8 is the latest stable version.

前提:

  1. Linux CentOS 7 虛擬機器
  2. yum -y install gcc, tcl, wget, iptables*,已成功安裝

安裝:

  1. 設定安裝執行目錄,例如:cd ~
  2. 獲取,wget http://download.redis.io/releases/redis-4.0.8.tar.gz
  3. 解壓縮,tar -xvf redis-4.0.8.tar.gz
  4. 進入目錄,cd redis-4.0.8
  5. 執行,make && make install
  6. 首先會編輯 Redis,隨後會將 Redis bin 服務,複製到 /usr/local/bin 目錄下,具體如下:
    -rwxr-xr-x. 1 root root 2451440 2月  12 20:30 redis-benchmark    // 效能測試工具
    -rwxr-xr-x. 1 root root 5763360 2月  12 20:30 redis-check-aof    // AOF檔案修復工具
    -rwxr-xr-x. 1 root root 5763360 2月  12 20:30 redis-check-rdb    // RDB檔案檢查工具
    -rwxr-xr-x. 1 root root 2616600 2月  12 20:30 redis-cli  // 命令列客戶端
    lrwxrwxrwx. 1 root root      12 2月  12 20:30 redis-sentinel -> redis-server     // Sentinel 服務
    -rwxr-xr-x. 1 root root 5763360 2月  12 20:30 redis-server   // 服務
  7. 安裝成功後,最好執行次make test。如果有報錯,對錯誤進行修正

常見錯誤

  1. 執行make test時,報: You need tcl 8.5 or newer in order to run the Redis test錯誤時,可安裝並升級yum -y install tcl即可解決
  2. 編譯時報:jemalloc/jemalloc.h: No such file or directory,可單獨執行:make MALLOC=libc,再執行:make install

service 配置

  1. 複製:cp -p ./redis-4.0.8/utils/redis_init_script /etc/init.d/redisd
  2. 編輯vim /etc/init.d/redisd,注意:REDISPORT、EXEC、CLIEXEC、PIDFILE、CONF,這幾個變數的設定
REDISPORT=6379  // 為埠號,你可以修改為不同的
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/redis_${REDISPORT}.conf"
// 我偏好將 Reids config 檔案命名為 redis_6379.conf
  1. 至此,後續可以使用service start/stop/restart redisd等命令來控制 Redis 服務

conf 配置

  1. 複製:cp -p ./redis-4.0.8/redis.conf /etc/redis/redis_6379.conf下(無此目錄時,請建立)
  2. 編輯:vim /etc/redis/redis_6379.conf
// 為了能夠遠端訪問,讓此伺服器作為 Redis 專用伺服器來使用,註釋掉
#bind 127.0.0.1

// 同樣,為了能夠遠端訪問,由 yes 更改為 no
protected-mode no

// 設定的值參考 /proc/sys/net/core/somaxconn 下的數值,否則會報警
tcp-backlog 128

// 啟用後臺模式,由 no 改為 yes
daemonize yes

// 最好驗證下,是否和 redisd 中的配置一致
pidfile /var/run/redis_6379.pid

// 修改日誌等級,設定日誌路徑
loglevel warning
logfile "/data/redis/log/redis.log"

// 設定訪問密碼(我這裡設定為 redis)
requirepass redis

// 配置持久化檔案存放位置
dir /data/redis
  1. 儲存退出,完成 conf 檔案的修改

啟動/停止

  1. 啟動:service redisd start
  2. 驗證(注意:因為設定了密碼,所以需要執行auth [password]進行驗證)
    [root@centos-linux bin]# ./redis-cli
    127.0.0.1:6379> ping
    (error) NOAUTH Authentication required.
    127.0.0.1:6379> auth redis
    OK
    127.0.0.1:6379> ping
    PONG
    127.0.0.1:6379>

    當然也可以執行:./redis-cli -a [password]來登入,省去輸入密碼

  3. 停止
    [root@centos-linux ~]# cd /usr/local/bin/
    [root@centos-linux bin]# ./redis-cli
    127.0.0.1:6379> auth redis
    OK
    127.0.0.1:6379> shutdown
    not connected>
    not connected> exit

Redis 埠為 6379,如果需要從外部進行訪問,則需要更改防火牆為:允許外部訪問 6379 埠

  1. 編輯 vim /etc/sysconfig/iptables 檔案,新增如下命令:
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 6379 -j ACCEPT

如果發現開啟的檔案 /etc/sysconfig/iptables 為空時,請執行:

yum -y install iptables*    // 安裝 iptables
  1. 重啟防火牆
    service iptables restart
11133:M 22 Feb 16:41:52.540 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.

按照警告提示資訊中的解決方法,即可消除警告

安裝 Redis

  1. 使用 brew 安裝 Redis,brew install redis
  2. 啟動 Redis,brew services start redis
  3. 配置 Redis,redis-server /usr/local/etc/redis.conf(無必要,因為我們僅需要 redis-cli)

遠端訪問

  1. 遠端訪問之前配置的 Reids 伺服器,redis-cli -h 10.211.55.3 -p 6379
  2. 驗證
    ➜  ~ redis-cli -h 10.211.55.3 -p 6379
    10.211.55.3:6379> auth redis
    OK
    10.211.55.3:6379> ping
    PONG
    10.211.55.3:6379>
  3. redis-cli -h 10.211.55.3 -p 6379 -a redis

注意:IP 10.211.55.3 是我虛擬機器的 IP,會和你的不同

相關文章