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