Linux下編譯安裝Redis以及主從複製配置

來源:互聯網
上載者:User

Linux下編譯安裝Redis以及主從複製配置

Redis的安裝配置很簡單,而且很早之前就裝過Redis,可這幾天再次安裝時居然又遺忘了一些細節,看來好記性不如爛筆頭,還是在部落格記錄一下比較好,至少不用總是抱度娘大腿了。

今天編譯安裝了幾次,發現居然沒在prefix指定目錄組建檔案??看了半天結果發現PREFIX我用了小寫字母。。。

看來還是得記錄一次正確的操作步驟,免得再次出現這種窘迫。

一、選擇版本

前往官方網站:http://www.redis.io/download

選擇一個適合的穩定版本,比如最新的redis-3.0穩定版(stable),獲得:

http://download.redis.io/releases/redis-3.0.0.tar.gz

二、編譯安裝

cd /usr/local/src
wget http://download.redis.io/releases/redis-3.0.0.tar.gz
tar zxvf redis-3.0.0.tar.gz
cd redis-3.0.0
make
#可選執行命令:make test

#這裡記得PREFIX是大寫,小寫不生效!
make PREFIX=/usr/local/redis-3.0.0 install

#建立軟連結
ln -s /usr/local/redis-3.0.0 /usr/local/redis

#建立目錄並拷貝預設設定檔過去
mkdir -p /usr/local/redis/{etc,var}
cp redis.conf /usr/local/etc/

#如果是對其他機器提供服務,推薦在redis.conf中設定監聽IP為0.0.0.0,啟動就可以開一個進程,預設是2個,還有個127.0.0.1
bind 0.0.0.0

cd /usr/local/src

wget http://download.redis.io/releases/redis-3.0.0.tar.gz

tar zxvf redis-3.0.0.tar.gz

cd redis-3.0.0

make

#可選執行命令:make test

 

#這裡記得PREFIX是大寫,小寫不生效!

make PREFIX=/usr/local/redis-3.0.0 install

 

#建立軟連結

ln -s /usr/local/redis-3.0.0 /usr/local/redis

#建立目錄並拷貝預設設定檔過去

mkdir -p /usr/local/redis/{etc,var}

cp redis.conf /usr/local/etc/
 

#如果是對其他機器提供服務,推薦在redis.conf中設定監聽IP為0.0.0.0,啟動就可以開一個進程,預設是2個,還有個127.0.0.1

bind 0.0.0.0


安裝完成後,redis目錄結構如下:

[root@cache-ns-4 ~]# tree /usr/local/redis             
/usr/local/redis
├── bin              #bin下面存放各種執行檔案
│  ├── redis-benchmark
│  ├── redis-check-aof
│  ├── redis-check-dump
│  ├── redis-cli    #redis用戶端執行檔案
│  ├── redis-sentinel -> /usr/local/redis-3.0.0/bin/redis-server
│  └── redis-server  #redis服務端執行檔案
├── dump.rdb          #啟動後預設產生的資料檔案,可以在redis.conf中設定dir的路徑參數指定到其他目錄
├── etc
│  └── redis.conf
└── var

 

[root@cache-ns-4 ~]# tree /usr/local/redis             

/usr/local/redis

├── bin              #bin下面存放各種執行檔案

│  ├── redis-benchmark

│  ├── redis-check-aof

│  ├── redis-check-dump

│  ├── redis-cli    #redis用戶端執行檔案

│  ├── redis-sentinel -> /usr/local/redis-3.0.0/bin/redis-server

│  └── redis-server  #redis服務端執行檔案

├── dump.rdb          #啟動後預設產生的資料檔案,可以在redis.conf中設定dir的路徑參數指定到其他目錄

├── etc

│  └── redis.conf

└── var

 

三、註冊服務

①、編寫服務控制指令碼

vi /etc/init.d/redis

#!/bin/bash
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig:  - 80 12
# description: Redis is a persistent key-value database
# processname: redis-server
# config:  /usr/local/redis/etc/redis.conf
# pidfile:  /usr/local/redis/var/redis.pid
 
source /etc/init.d/functions
 
BIN="/usr/local/redis/bin"
CONFIG="/usr/local/redis/etc/redis.conf"
PIDFILE="/var/run/redis.pid"
 
 
### Read configuration
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"
 
RETVAL=0
prog="redis-server"
desc="Redis Server"
 
start() {
 
    if [ -e $PIDFILE ];then
      echo "$desc already running...."
      exit 1
    fi
 
    echo -n $"Starting $desc: "

    #使用中偶爾探索服務器啟動後居然不轉入後台,所以在最後加了一個“&”
    daemon $BIN/$prog $CONFIG &
 
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
    return $RETVAL
}
 
stop() {
    echo -n $"Stop $desc: "
    killproc $prog
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE
    return $RETVAL
}
 
restart() {
  stop
  start
}
 
case "$1" in
 start)
    start
    ;;
 stop)
    stop
    ;;
 restart)
    restart
    ;;
 condrestart)
    [ -e /var/lock/subsys/$prog ] && restart
    RETVAL=$?
    ;;
 status)
    status $prog
    RETVAL=$?
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart|condrestart|status}"
    RETVAL=1
esac
 
exit $RETVAL

 

 

#!/bin/bash

#

# redis - this script starts and stops the redis-server daemon

#

# chkconfig:  - 80 12

# description: Redis is a persistent key-value database

# processname: redis-server

# config:  /usr/local/redis/etc/redis.conf

# pidfile:  /usr/local/redis/var/redis.pid

 

source /etc/init.d/functions

 

BIN="/usr/local/redis/bin"

CONFIG="/usr/local/redis/etc/redis.conf"

PIDFILE="/var/run/redis.pid"

 

 

### Read configuration

[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"

 

RETVAL=0

prog="redis-server"

desc="Redis Server"

 

start() {

 

    if [ -e $PIDFILE ];then

      echo "$desc already running...."

      exit 1

    fi

 

    echo -n $"Starting $desc: "

 

    #使用中偶爾探索服務器啟動後居然不轉入後台,所以在最後加了一個“&”

    daemon $BIN/$prog $CONFIG &

 

    RETVAL=$?

    echo

    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog

    return $RETVAL

}

 

stop() {

    echo -n $"Stop $desc: "

    killproc $prog

    RETVAL=$?

    echo

    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE

    return $RETVAL

}

 

restart() {

  stop

  start

}

 

case "$1" in

start)

    start

    ;;

stop)

    stop

    ;;

restart)

    restart

    ;;

condrestart)

    [ -e /var/lock/subsys/$prog ] && restart

    RETVAL=$?

    ;;

status)

    status $prog

    RETVAL=$?

    ;;

  *)

    echo $"Usage: $0 {start|stop|restart|condrestart|status}"

    RETVAL=1

esac

 

exit $RETVAL


②、註冊服務與啟動

#給執行許可權
chmod +x /etc/init.d/redis

#開機啟動
chkconfig redis on

#啟動服務
service redis start

#下面是開機記錄
[root@localhost ~]# service redis start
Starting Redis Server:
[root@localhost ~]#                _._                                                 
          _.-``__ ''-._                                           
      _.-``    `.  `_.  ''-._          Redis 3.0.0 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                 
 (    '      ,      .-`  | `,    )    Running in stand alone mode
 |`-._`-...-` __...-.``-._|'` _.-'|    Port: 6379
 |    `-._  `._    /    _.-'    |    PID: 28584
  `-._    `-._  `-./  _.-'    _.-'                                 
 |`-._`-._    `-.__.-'    _.-'_.-'|                                 
 |    `-._`-._        _.-'_.-'    |          http://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                 
 |`-._`-._    `-.__.-'    _.-'_.-'|                                 
 |    `-._`-._        _.-'_.-'    |                                 
  `-._    `-._`-.__.-'_.-'    _.-'                                 
      `-._    `-.__.-'    _.-'                                     
          `-._        _.-'                                         
              `-.__.-'                                             

[28584] 16 Apr 23:48:55.614 # Server started, Redis version 3.0.0
[28584] 16 Apr 23:48:55.614 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
[28584] 16 Apr 23:48:55.614 # 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.
[28584] 16 Apr 23:48:55.614 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
[28584] 16 Apr 23:48:55.615 * DB loaded from disk: 0.000 seconds
[28584] 16 Apr 23:48:55.615 * The server is now ready to accept connections on port 6379


#給執行許可權

chmod +x /etc/init.d/redis

#開機啟動

chkconfig redis on

#啟動服務

service redis start


#下面是開機記錄

[root@localhost ~]# service redis start

Starting Redis Server:

[root@localhost ~]#                _._                                                 

          _.-``__ ''-._                                           

      _.-``    `.  `_.  ''-._          Redis 3.0.0 (00000000/0) 64 bit

  .-`` .-```.  ```\/    _.,_ ''-._                                 

(    '      ,      .-`  | `,    )    Running in stand alone mode

|`-._`-...-` __...-.``-._|'` _.-'|    Port: 6379

|    `-._  `._    /    _.-'    |    PID: 28584

  `-._    `-._  `-./  _.-'    _.-'                                 

|`-._`-._    `-.__.-'    _.-'_.-'|                                 

|    `-._`-._        _.-'_.-'    |          http://redis.io       

  `-._    `-._`-.__.-'_.-'    _.-'                                 

|`-._`-._    `-.__.-'    _.-'_.-'|                                 

|    `-._`-._        _.-'_.-'    |                                 

  `-._    `-._`-.__.-'_.-'    _.-'                                 

      `-._    `-.__.-'    _.-'                                     

          `-._        _.-'                                         

              `-.__.-'                                             

 

[28584] 16 Apr 23:48:55.614 # Server started, Redis version 3.0.0

[28584] 16 Apr 23:48:55.614 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

[28584] 16 Apr 23:48:55.614 # 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.

[28584] 16 Apr 23:48:55.614 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

[28584] 16 Apr 23:48:55.615 * DB loaded from disk: 0.000 seconds

[28584] 16 Apr 23:48:55.615 * The server is now ready to accept connections on port 6379

 

四、主從配置

①、配置

在從機上按照上面的步驟安裝redis,然後在從機的redis.conf裡面新增如下設定項目:


#指定redis主機的IP地址和連接埠(預設未設定密碼認證)
slaveof 192.168.10.124 6379


#指定redis主機的IP地址和連接埠(預設未設定密碼認證)

slaveof 192.168.10.124 6379


儲存後啟動redis即可完成簡單的主從配置。

 ②、測試

測試很簡單,先在主機上通過用戶端 redis-cli 執行新增索引值命令:


[root@localhost ~]# /usr/local/redis/bin/redis-cli
127.0.0.1:6379> set newkey slavetest
OK

 

[root@localhost ~]# /usr/local/redis/bin/redis-cli

127.0.0.1:6379> set newkey slavetest

OK


然後,登入從機上同樣執行 redis-cli 執行查詢命令:


[root@localhost ~]# /usr/local/redis/bin/redis-cli       
127.0.0.1:6379> get newkey
"slavetest"

[root@localhost ~]# /usr/local/redis/bin/redis-cli       

127.0.0.1:6379> get newkey

"slavetest"


很明顯主機上新增的索引值已經自動同步到了從機,主從同步成功!

本文只是記錄一下基本的編譯安裝和主從配置,當然,redis還有其可以繼續進行自訂設定或最佳化的項目,後續有機會再繼續整理補充一下。

Ubuntu 14.04下Redis安裝及簡單測試

Redis叢集明細文檔

Ubuntu 12.10下安裝Redis(圖文詳解)+ Jedis串連Redis

Redis系列-安裝部署維護篇

CentOS 6.3安裝Redis

Redis安裝部署學習筆記

Redis設定檔redis.conf 詳解

Redis 的詳細介紹:請點這裡
Redis 的:請點這裡

本文永久更新連結地址:

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.