Centos下Redis安装与配置,亲测实用

1、安装redis及下载redis

1.1、可去官网http://redis.io ,也可通过wget命令

$ cd /opt/sofware  #切换目录
$ wget http://download.redis.io/releases/redis-4.0.10.tar.gz

1.2、解压
解压到/opt/module目录下

tar -zxvf redis-4.0.10.tar.gz -C /opt/module/ 

1.3、yum安装gcc依赖

yum install gcc tcl -y

1.4、切换到redis解压目录下

cd /opt/module/redis-4.0.10

1.5、编译安装

make MALLOC=libc
cd src && make install

#输出以下内容
Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make: warning:  Clock skew detected.  Your build may be incomplete.

1.6、测试是否安装成功
1)先切换到redis src目录下

cd /opt/module/redis-4.0.10/src

2)启动redis服务
输入./redis-server指令

[root@node3 src]# ./redis-server 
2812:C 11 Jun 16:23:12.402 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2812:C 11 Jun 16:23:12.402 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=2812, just started
2812:C 11 Jun 16:23:12.402 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.10 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 2812
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

2812:M 11 Jun 16:23:12.411 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2812:M 11 Jun 16:23:12.411 # Server initialized
2812:M 11 Jun 16:23:12.411 # 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.
2812:M 11 Jun 16:23:12.412 # 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.
2812:M 11 Jun 16:23:12.412 * Ready to accept connections

如上图:redis启动成功,但是这种启动方式需要一直打开窗口,不能进行其他操作,不太方便。 按 ctrl + c可以关闭窗口。

3)修改配置文件,以后台进程方式启动redis
第一步:修改redis.conf文件

cd /opt/module/redis-4.0.10
vim redis.conf

3.1)修改daemonize

daemonize no #默认为no

修改为

daemonize yes #后台进程方式改为yes

3.2)修改bind 配置

bind 127.0.0.1 #默认只有本机才能够连接

修改为

bind 192.168.8.94 #改为本机ip地址

3.3)修改protected-mode配置

protected-mode yes #在默认保护模式下启用

修改为

protected-mode no #禁用它,任何client不用认证即可连接

3.4)修改redis的密码

vim  /etc/redis.conf

#requirepass foobared去掉注释,foobared改为自己的密码,我在这里改为

requirepass 123456

然后保存,重启服务
第二步:指定redis.conf文件启动

cd /opt/module/redis-4.0.10/src

输入以下指令./redis-server …/redis.conf 后台进程启动redis

[root@node3 src]# ./redis-server ../redis.conf 
2851:C 11 Jun 16:45:16.619 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2851:C 11 Jun 16:45:16.619 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=2851, just started
2851:C 11 Jun 16:45:16.619 # Configuration loaded

第三步:测试redis

[root@node3 src]# ./redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> set ko 'ok'
OK
127.0.0.1:6379> keys *
1) "ko"
127.0.0.1:6379> get ko
"ok"
127.0.0.1:6379>

第四步:关闭redis进程
首先使用ps -aux | grep redis查看redis进程

[root@node3 src]# ps aux|grep redis
root       2852  0.1  0.2 141832  2028 ?        Ssl  16:45   0:00 ./redis-server 127.0.0.1:6379
root       2857  0.0  0.0 112704   976 pts/0    S+   16:46   0:00 grep --color=auto redis

使用kill命令杀死进程

kill -9 2852

2、设置redis开机自启动

1、在/etc目录下新建redis目录

mkdir redis

2、将/opt/module/redis-4.0.10/redis.conf 文件复制一份到/etc/redis目录下,并命名为6379.conf

 cp /opt/module/redis-4.0.10/redis.conf /etc/redis/6379.conf

3、将redis的启动脚本复制一份放到/etc/init.d目录下

 cp /opt/module/redis-4.0.10/utils/redis_init_script  /etc/init.d/redisd

4、设置redis开机自启动
先切换到/etc/init.d目录下
然后执行自启命令

chkconfig redisd on

service redisd does not support chkconfig 
看结果是redisd不支持chkconfig
解决方法:

使用vim编辑redisd文件,在第一行加入如下两行注释,保存退出

# chkconfig:   2345 90 10
# description:  Redis is a persistent key-value database

再次执行开机自启命令,成功

chkconfig redisd on

现在可以直接已服务的形式启动和关闭redis了

启动:
service redisd start
关闭:
service redisd stop

备注:这里如果启动redis报错,可能是上面已经启动了,需要到/var/run下删除6379.conf文件,在使用service redisd start即可

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值