欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CentOS7安装redis

程序员文章站 2022-05-31 11:04:05
...

1、安装redis
1.1下载redis

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

 $ cd /opt/sofware  #切换目录  
 $ wget  http://download.redis.io/releases/redis-*.tar.gz --这里建议去官网看下最新版本

1.2解压

解压到/opt/module目录下

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

1.3yum安装gcc依赖

输入:y

yum install gcc tcl
Total download size: 23 M
Is this ok [y/d/N]: 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指令

[[email protected] 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修改port端口号

port 6379 #默认为6379端口

修改为其他端口

port 6380  #可根据实际情况配置

第二步:指定redis.conf文件启动

cd /opt/module/redis-4.0.10/src

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

[[email protected] 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

[[email protected] 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进程

[[email protected] 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