redis 基礎介紹

  1. 介紹與安裝
    • Redis 的解釋
      – Redis [Remote Directory Server] 遠程服務器字典
    • 下載安裝
      – wget redis.tar.gz
      – cd redis
      – make
      – make install
  2. 在bin下可執行的程序
    • redis-server Redis服務器
    • redis-cli: 命令行客戶端
    • redis-benchmark: redis的性能測試工具
    • redis-check-aof: AOF文件修復工具
    • redis-check-dump: RDB文件檢查工具
    • redis.conf 是redis的配置文件,將配置文件中daemonize yes 以守護進程的方式來使用
    • 啓動redis
      – 編譯安裝的: redis-server /etc/redis.conf
      – ubuntu :
      \cp 源碼安裝目錄/init.d/init.d.redis /etc/init.d/redis
      sudo /etc/init.d/redis start

源碼安裝腳本[摘取子lnmp-redis安裝腳本]

#!/bin/bash

Install_Redis()
{
    ver="1"
    echo "Which version do you want to install:"
    echo "Install Redis 3.0.1   Stable Version please type: 1"
    echo "Install Redis 2.8.20  Old Version please type: 2"
    read -p "Enter 1 or 2 (Default Stable version): " ver
    if [ "${ver}" = "" ]; then
        ver="1"
    fi

    if [ "${ver}" = "1" ]; then
        echo "You will install Redis 3.0.1   Stable Version"
    elif [ "${ver}" = "2" ]; then
        echo "You will install Redis 2.8.20  Old Version"
    else
        echo "Input error,please input 1 or 2 !"
        echo "Please Rerun $0"
        exit 1
    fi

    echo "====== Installing Redis ======"
    Press_Install

    sed -i '/redis.so/d' /usr/local/php/etc/php.ini
    Get_PHP_Ext_Dir
    zend_ext="${zend_ext_dir}redis.so"
    if [ -s "${zend_ext}" ]; then
        rm -f "${zend_ext}"
    fi

    cd ${cur_dir}/src
    if [ "${ver}" = "1" ]; then
        Download_Files http://download.redis.io/releases/${Redis_Stable_Ver}.tar.gz ${Redis_Stable_Ver}.tar.gz
        Tar_Cd ${Redis_Stable_Ver}.tar.gz ${Redis_Stable_Ver}
    else
        Download_Files http://download.redis.io/releases/${Redis_Old_Ver}.tar.gz ${Redis_Old_Ver}.tar.gz
        Tar_Cd ${Redis_Old_Ver}.tar.gz ${Redis_Old_Ver}
    fi

    if [ "${Is_64bit}" = "y" ] ; then
        make PREFIX=/usr/local/redis install
    else
        make CFLAGS="-march=i686" PREFIX=/usr/local/redis install
    fi
    mkdir -p /usr/local/redis/etc/
    \cp redis.conf  /usr/local/redis/etc/
    sed -i 's/daemonize no/daemonize yes/g' /usr/local/redis/etc/redis.conf
    sed -i 's/# bind 127.0.0.1/bind 127.0.0.1/g' /usr/local/redis/etc/redis.conf
    cd ../

    if [ -s /sbin/iptables ]; then
        /sbin/iptables -I INPUT -p tcp -s 127.0.0.1 --dport 6379 -j ACCEPT
        /sbin/iptables -A INPUT -p tcp --dport 6379 -j DROP
        if [ "$PM" = "yum" ]; then
            service iptables save
        elif [ "$PM" = "apt" ]; then
            iptables-save > /etc/iptables.rules
        fi
    fi

    if [ -s ${PHPRedis_Ver} ]; then
        rm -rf ${PHPRedis_Ver}
    fi

    Download_Files http://pecl.php.net/get/${PHPRedis_Ver}.tgz ${PHPRedis_Ver}.tgz
    Tar_Cd ${PHPRedis_Ver}.tgz ${PHPRedis_Ver}
    /usr/local/php/bin/phpize
    ./configure --with-php-config=/usr/local/php/bin/php-config
    make && make install
    cd ../

sed -i '/the dl()/i\
extension = "redis.so"' /usr/local/php/etc/php.ini

    \cp ${cur_dir}/init.d/init.d.redis /etc/init.d/redis
    chmod +x /etc/init.d/redis
    echo "Add to auto start..."
    StartUp redis
    Restart_PHP
    /etc/init.d/redis start

    echo "====== Redis install completed ======"
    echo "Redis installed successfully, enjoy it!"
}

Uninstall_Redis()
{
    echo "You will uninstall Redis..."
    Press_Start
    sed -i '/redis.so/d' /usr/local/php/etc/php.ini
    Restart_PHP
    Remove_StartUp redis
    echo "Delete Redis files..."
    rm -rf /usr/local/redis
    rm -rf /etc/init.d/redis
    if [ -s /sbin/iptables ]; then
        /sbin/iptables -D INPUT -p tcp -s 127.0.0.1 --dport 6379 -j ACCEPT
        /sbin/iptables -D INPUT -p tcp --dport 6379 -j DROP
        if [ "$PM" = "yum" ]; then
            service iptables save
        elif [ "$PM" = "apt" ]; then
            iptables-save > /etc/iptables.rules
        fi
    fi
    echo "Uninstall Redis completed."
}
  1. 命令返回值
    1. 狀態回覆
      • ping
      • SET test ‘this is a test’
    2. 錯誤回覆
      • 錯誤回覆以error開始
      • (error) ERR unknown command ‘TESTERROR’
    3. 整數回覆
      • 以interger 數值
      • (interger) 2
    4. 字符串回覆
      • GET test
      • test 存在 返回test的值
      • test 不存在 返回 (nil) 代表空的結果
    5. 多行字符串回覆
      • KEYS *
      • 得到當前數據庫中存在的鍵名
  2. Redis 配置選項相關的內容
    和鏈接有關的

    1. 動態獲取/設置Redis 的配置選項的值
      1. 獲取
        • CONFIG GET name
      2. 設置
        • CONFIG SET name value
    2. Redis配置文件redis.conf選項相關
      • port 6379 默認端口
      • bind 127.0.0.1 默認綁定的主機地址
      • timeout 0 當客戶端閒置多久後關閉鏈接,0代表沒有開啓這個選項
      • loglevel notice 日誌的記錄級別
        – debug: 很詳細的信息,適合開發和測試
        – verbose: 包含很多不太有用的信息
        – notice: 比較適合生產環境
        – warning: 警告信息
      • logfile stdout: 記錄日誌的方式, 默認爲標準輸出
      • databases 16, 默認數據庫的個數16個, 默認的數據編號從0開始

    和快照有關的

    • rdb模式:

      • save <seconds> <changes> 多少秒內有多少次更改將其同步到磁盤中的數據文件裏
      • save 900 1 900秒內有1次更改將其同步到磁盤中的數據文件裏
      • rdbcompression yes 存儲本地數據庫時是否啓用壓縮, 默認yes
      • dbfilename dump.rdb 指定本地數據庫文件名, 默認爲dump.rdb
    • aof模式:

      • appendonly yes|no 開啓和關閉
      • aof文件的保存位置和rdb文件的位置相同,都是dir參數設置的,默認的文件名是appendonly.aof,可以通過appendfilename參數修改
      • appendfilename appendonly.aof
        aof日誌文件重寫
      • auto-aof-rewrite-percentage 100
      • auto-aof-rewrite-min-size 64mb
        也可手動執行bgrewriteaof進行重寫
        redis寫命令同步的時機
      • appendfsync always 每次都會執行
      • appendfsync everysec 默認 每秒執行一次同步操作(推薦)
      • appendfsync no 不主動進行同步,由操作系統來做,30秒一次
      • redis-check-aof 文件修復
    • dir ./ 指定本地數據庫的存放目錄, 默認是當前目錄dump.rdb|*.aof都會存儲到這裏

    • hash-max-ziplist-entries 512 字節

    • hash-max-ziplist-value 64 字段數目
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章