Linux下搭建 redis


一、下載redis

這個可以到官網下載,直接在百度里面搜索redis

二、安裝redis

1、安裝gcc

1
yum install gcc - c + +

 2、解壓redis文件並且重命名

1
2
3
[root@localhost local] #tar -zxvf /root/redis-3.2.12.tar.gz -C /usr/local/
[root@localhost local] # cd /usr/local/
[root@localhost local] # mv redis-3.2.12/  mv redis

3、編譯redis

1
2
3
[root@localhost local] # cd redis
[root@localhost local] # make
[root@localhost local] # make PREFIX=/usr/local/redis install

4、測試一下是否已經安裝好

1
2
[root@localhost redis] # cd /usr/local/redis/bin
[root@localhost  bin ] # ./redis-server

顯示內容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
9190 :C  03  Sep  10 : 19 : 09.291  # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9190 :C  03  Sep  10 : 19 : 09.292  # Redis version=4.0.1, bits=32, commit=00000000, modified=0, pid=9190, just started
9190 :C  03  Sep  10 : 19 : 09.292  # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
9190 :M  03  Sep  10 : 19 : 09.295  *  Increased maximum number of  open  files to  10032  (it was originally  set  to  1024 ).
9190 :M  03  Sep  10 : 19 : 09.312  # Warning: 32 bit instance detected but no memory limit set. Setting 3 GB maxmemory limit with 'noeviction' policy now.
                 _._
            _. - ``__ '' - ._
       _. - ``    `.  `_.  '' - ._           Redis  4.0 . 1  ( 00000000 / 0 32  bit
   . - `` . - ```.  ```\ /     _.,_ '' - ._
  (    '      ,       . - `  | `,    )     Running  in  standalone mode
  |` - ._` - ... - ` __... - .`` - ._| '` _.-' |     Port:  6379
  |    ` - ._   `._     /      _. - '    |     PID:  9190
   ` - ._    ` - ._  ` - . /   _. - '    _.-'
  |` - ._` - ._    ` - .__. - '    _.-' _. - '|
  |    ` - ._` - ._        _. - '_.-'     |           http: / / redis.io
   ` - ._    ` - ._` - .__. - '_.-'     _. - '
  |` - ._` - ._    ` - .__. - '    _.-' _. - '|
  |    ` - ._` - ._        _. - '_.-'     |
   ` - ._    ` - ._` - .__. - '_.-'     _. - '
       ` - ._    ` - .__. - '    _.-'
           ` - ._        _. - '
               ` - .__. - '
 
9190 :M  03  Sep  10 : 19 : 09.316  # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
9190 :M  03  Sep  10 : 19 : 09.316  # Server initialized
9190 :M  03  Sep  10 : 19 : 09.318  # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition.
9190 :M  03  Sep  10 : 19 : 09.318  *  Ready to accept connections

三、加入開機啟動

1、加入開機啟動

1
2
[root@localhost local] # cd /usr/local/redis/utils
[root@localhost utils] #  cp redis_init_script /etc/init.d/redis

2、編輯啟動文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
[root@localhost utils] # vim  /etc/init.d/redis
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
 
REDISPORT = 6379
EXEC = / usr / local / redis / bin / redis - server    #指向正確的bin地址
CLIEXEC = / usr / local / redis / bin / redis - cli     #這邊同上
 
PIDFILE = / var / run / redis_${REDISPORT}.pid
CONF = "/etc/redis/${REDISPORT}.conf"
 
case  "$1"  in
     start)
         if  - f $PIDFILE ]
         then
                 echo  "$PIDFILE exists, process is already running or crashed"
         else
                 echo  "Starting Redis server..."
                 $EXEC $CONF
         fi
         ;;
     stop)
         if  [ !  - f $PIDFILE ]
         then
                 echo  "$PIDFILE does not exist, process is not running"
         else
                 PID = $(cat $PIDFILE)
                 echo  "Stopping ..."
                 $CLIEXEC  - p $REDISPORT shutdown
                 while  - / proc / ${PID} ]
                 do
                     echo  "Waiting for Redis to shutdown ..."
                     sleep  1
                 done
                 echo  "Redis stopped"
         fi
         ;;
     * )
         echo  "Please use start or stop as first argument"
         ;;
esac

3、創建配置文件

1
2
3
4
5
6
7
[root@localhost local] # mkdir /etc/redis
[root@localhost local] # cp /usr/local/redis/redis.conf  /etc/redis/6379.conf
[root@localhost local] # cd /etc/redis/
[root@localhost redis] # vim 6379.conf
#bind 127.0.0.1   #這一行注釋
protected - mode no   #改成no 表示無需密碼登錄
daemonize yes   #改成yes

4、啟動redis

1
2
3
4
[root@localhost local] # service redis start
[root@localhost local] # ps -ef|grep redis
root      60252      1   0  15 : 27  ?         00 : 00 : 00  / usr / local / redis / bin / redis - server  * : 6379
root      61165  48206   0  15 : 44  pts / 0     00 : 00 : 00  grep  - - color = auto redis

四、redis集群配置

 4.1、yum安裝下載依賴的插件

1
2
3
4
5
6
7
yum  install  ruby -y
yum  install  rubygems -y
yum -y  install  ruby ruby-devel rubygems rpm-build
ruby -version
yum  install  centos-release-scl-rh
scl   enable   rh-ruby23  bash
gem  install  redis

4.2、必須准備6個redis,因為是3主3從,所以不得小於6台:

安裝上述安裝redis的辦法,在裝5台,分別是,分別裝在:/usr/local目錄下:

1
2
3
4
5
6
redis_6379
redis_6380
redis_6381
redis_6382
redis_6383
redis_6384

同時啟動文件和配置文件也,配置文件在: /etc/redis/下:

1
2
[root@shtw-redisserver09 redis] # ls
6379.conf  6380.conf  6381.conf  6382.conf  6383.conf  6384.conf

 配置文件內容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#bind 127.0.0.1  #注釋掉
port 6380    #端口號需要修改
tcp-backlog 511
timeout 0
tcp-keepalive 300
supervised no
pidfile  /var/run/redis_6380 .pid    #進程號修改
loglevel notice
logfile  ""
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error  yes
rdbcompression  yes
rdbchecksum  yes
dbfilename dump.rdb
dir  ./
slave-serve-stale-data  yes
slave- read -only  yes
repl-diskless- sync  no
repl-diskless- sync -delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated  yes
lua- time -limit 5000
cluster-enabled  yes    #開啟集群模式
appendonly  yes
cluster-config- file  nodes-6380.conf
latency-monitor-threshold 0
notify-keyspace-events  ""
list-max-ziplist-size -2
list-compress-depth 0
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing  yes
hz 10
aof-rewrite-incremental-fsync  yes

完整的文件內容:

  6380.conf

上傳 redis-trib.rb 文件至/usr/local下:

1
2
3
4
[root@shtw-redisserver09  local ] # cd /usr/local/
[root@shtw-redisserver09  local ] # rz -y
[root@shtw-redisserver09  local ] # ll
-rwxr-xr-x  1 root root 60852 Aug 23  2019 redis-trib.rb

 redis-trib.rb下載地址:https://pan.baidu.com/s/1NqwED60Z9ggiZPOJIco7rA  提取碼:tn8d

然后執行如下命令:

1
2
3
4
5
[root@shtw-redisserver09  local ] #./redis-trib.rb create --replicas 1 10.10.202.236:6379  10.10.202.236:6380 10.10.202.236:6381
10.10.202.236:6382 10.10.202.236:6383 10.10.202.236:6384   #如果上述成功了之后,下面的就不要執行了
 
[root@shtw-redisserver09  local ] #./redis-trib.rb create --replicas 2 10.10.202.236:6379  10.10.202.236:6380 10.10.202.236:6381
10.10.202.236:6382 10.10.202.236:6383 10.10.202.236:6384

重啟報錯:

>>> Creating cluster

[ERR] Node 127.0.0.1:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

 

解決方法:刪除nodes-xxx.conf配置文件,刪除pid文件,刪除各節點aof,rdb文件,殺掉所有redis進程,然后重啟redis集群搞定 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM