redis安装

1. 下载redis安装包

wget http://download.redis.io/releases/redis-4.0.6.tar.gz
//版本可以自己去官网找,下载的路径一般是放在了  /usr/local/  下
//也可以自己选择路径,自己记住存放的路径,方便以后卸载的时候直接把redis相关的文件删除
[rootxxxxx local]# wget http://download.redis.io/releases/redis-4.0.6.tar.gz
--2017-12-13 12:35:12--  http://download.redis.io/releases/redis-4.0.6.tar.gz
Resolving download.redis.io (download.redis.io)... 109.74.203.151
Connecting to download.redis.io (download.redis.io)|109.74.203.151|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1723533 (1.6M) [application/x-gzip]
Saving to: ‘redis-4.0.6.tar.gz’

100%[==========================================================================================================>] 1,723,533    608KB/s   in 2.8s   

2017-12-13 12:35:15 (608 KB/s) - ‘redis-4.0.6.tar.gz’ saved [1723533/1723533]

2. 解压压缩包

tar -zxvf redis-4.0.6.tar.gz
//这里就在 /usr/local/ 下解压了,把它放到一个独立的包里 /usr/local/redis-4.0.6

3. yum安装gcc依赖

yum install gcc
//如果遇到选择,输入y即可

4. 进入redis解压目录下并编译安装

这一步会自动将 /usr/local/redis-4.0.6/src 目录下的一些文件加到 /usr/local/bin 目录下;

[rootxxxxx redis-4.0.6]# make MALLOC=libc
..........//一些编译内容
//进入src目录并安装
[rootxxxxx redis-4.0.6]# cd src && make install
    CC Makefile.dep

Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install

redis的三种启动方式及其设置

1. 直接启动redis启动脚本

进入 /usr/local/redis-4.0.6/src 目录下运行 redis-server 脚本;

[root@VM-24-4-centos src]# ./redis-server
20279:C 11 Aug 15:23:46.485 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
20279:C 11 Aug 15:23:46.485 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=20279, just started
20279:C 11 Aug 15:23:46.485 # 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.6 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 20279
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

20279:M 11 Aug 15:23:46.487 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
20279:M 11 Aug 15:23:46.487 # Server initialized
20279:M 11 Aug 15:23:46.487 # 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.
20279:M 11 Aug 15:23:46.487 # 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.
20279:M 11 Aug 15:23:46.487 * DB loaded from disk: 0.000 seconds
20279:M 11 Aug 15:23:46.487 * Ready to accept connections

如上redis就启动成功了,但是这种启动方式只能一直在这个界面,不能去做其他操作;
按 ctrl+c 可以退出;

2. 以后台进程方式启动redis

  • 修改 /usr/local/redis-4.0.6 目录下的 redis.conf 文件;
    vim redis.conf
    daemonize no 修改为 daemonize yes

  • 指定redis.conf文件启动(指令在src目录下执行);

[root@VM-24-4-centos src]# ./redis-server /usr/local/redis-4.0.6/redis.conf
22159:C 11 Aug 15:32:25.008 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
22159:C 11 Aug 15:32:25.008 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=22159, just started
22159:C 11 Aug 15:32:25.009 # Configuration loaded
  • 查看并关闭进程
    ps -ef|grep redis :查看进程命令;
    使用kill命令杀死进程:kill -9 进程号
[root@VM-24-4-centos src]# ps -ef|grep redis
root     22160     1  0 15:32 ?        00:00:00 ./redis-server 127.0.0.1:6379
root     22444 14479  0 15:33 pts/0    00:00:00 grep --color=auto redis

[root@VM-24-4-centos src]# kill -9 22160

3. 设置redis开机自启动

  • 在 /etc 目录下新建 redis 目录
[root@VM-24-4-centos etc]# mkdir redis
  • 将 /usr/local/redis-4.0.6/redis.conf 文件复制一份到 /etc/redis 目录下,并命名为 6379.conf;
[root@VM-24-4-centos redis]# cp /usr/local/redis-4.0.6/redis.conf /etc/redis/6379.conf
  • 将 redis 的启动脚本复制一份放到 /etc/init.d 目录下,并命名为redisd;
[root@VM-24-4-centos init.d]# cp /usr/local/redis-4.0.6/utils/redis_init_script /etc/init.d/redisd
  • 设置redis开机自启动
    先切换到/etc/init.d目录下,然后执行自启命令 chkconfig redisd on
[root@VM-24-4-centos init.d]# chkconfig redisd on
service redisd does not support chkconfig

出现上面是指 redisd 不支持 chkconfig,解决方法:编辑 redisd 文件,在第一行下面加入下面两行注释;
注释的意思是,redis服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10;

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

请添加图片描述

  • 再次执行开机自启命令,chkconfig redisd on

以上操作完成就可以以服务的形式启动和关闭redis了;

启动:service redisd start

[root@VM-24-4-centos src]# service redisd start
Starting Redis server...
1922:C 11 Aug 16:29:07.876 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1922:C 11 Aug 16:29:07.876 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=1922, just started
1922:C 11 Aug 16:29:07.876 # Configuration loaded

关闭:service redisd stop

[root@VM-24-4-centos src]# service redisd stop
Stopping ...
Redis stopped
  • 服务命令出现的问题:
  1. 启动时提示已经有进程或者崩溃了
[root@VM-24-4-centos src]# service redisd start
/var/run/redis_6379.pid exists, process is already running or crashed

/var/run/ 目录下已经有了 redis_6379.pid 文件,直接删除再启动就可以了;
[root@VM-24-4-centos src]# rm -rf /var/run/redis_6379.pid

  1. 关闭服务时连接超时、启动超时或者无法进入redis

记得把服务器的防火墙端口 6379 开放,如果你自己启用了iptable防火墙,记得添加了端口开放规则之后一定要保存规则!!!

请添加图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐