redis在Linux下的安装、配置以及问题处理

本文详细介绍了在Linux系统上安装Redis的过程,包括下载、编译、配置及启动服务的方法,并演示了基本的Redis命令操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、redis下载

  去redis官网进行下载,进入页面如下图所示:

二、redis在Linux系统安装

              虚拟机:vmware 14 pro    镜像:centos 7.4

1、新建soft文件,上传下载的redis-5.0.3.tar.gz到Linux下的soft目录下;

上传可以使用rz命令上传:

输入rz命令发现,未找到命令:

解决办法,输入以下命令: yum install lrzsz -y

再次输入rz命令,即可上传文件到该目录下。

2、安装redis编译的c环境,命令:yum install gcc-c++

            出现这个表示,编译成功:

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

make[1]: 离开目录“/usr/local/redis/src”

3、解压到/usr/local下,命令:tar -xvf redis-5.0.3.tar.gz -C/usr/local

4、进入redis-5.0.3目录 ,使用make命令编译redis,命令:make

这一步注意:

若出现以下错误:

cd src && make all
make[1]: Entering directory `/root/redis-5.0.3/src‘
    CC adlist.o
In file included from adlist.c:34:
zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory
zmalloc.h:55:2: error: #error "Newer version of jemalloc required"
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/root/redis-5.0.3/src‘
make: *** [all] Error 2

解决办法:输入命令:make all

5、在redis-5.0.3目录中 ,安装 redis到/usr/local/redis中。

         使用命令:make PREFIX=/usr/local/redis install

注意这里的redis目录是安装目录,redis-5.0.3目录是解压目录

6、前端模式启动redis服务:

        进入redis/bin目录下,输入命令:./redis-server,若出现以下页面代表启动成功

              _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.3 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 8626
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

8626:M 28 Dec 2018 16:41:06.715 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

7、进入redis-5.0.3目录中,拷贝redis-5.0.3中的redis.conf到安装目录redis中,

   命令:cp redis.conf ../redis/bin/

8、后端模式启动redis服务:

         进入redis/bin目录下,修改redis.conf的配置文件,daemonize yes以后,后端模式启动

          命令:vi /usr/local/redis/bin/redis.conf

################################# GENERAL #####################################

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes

# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:

9、后端模式启动redis服务:

   进入redis/bin目录下,

    输入命令:./redis-server redis.conf

 启动客户端:输入命令:./redis-cli 

   如下所示,表示启动成功:

[root@localhost bin]# ls
dump.rdb         redis-check-aof  redis-cli   redis-sentinel
redis-benchmark  redis-check-rdb  redis.conf  redis-server
[root@localhost bin]# ./redis-server redis.conf 
8779:C 28 Dec 2018 17:21:17.347 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8779:C 28 Dec 2018 17:21:17.347 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=8779, just started
8779:C 28 Dec 2018 17:21:17.347 # Configuration loaded
[root@localhost bin]# ./redis-cli 
127.0.0.1:6379> set username zhangsan
OK
127.0.0.1:6379> get username
"zhangsan"
127.0.0.1:6379> 

10、关闭redis服务

    关闭方式一:(杀死进程  kill -9 pid)

     出现如下所示表示关闭成功:

127.0.0.1:6379> exit
[root@localhost bin]# ps -ef | grep redis
root       8780      1  0 17:21 ?        00:00:00 ./redis-server 127.0.0.1:6379
root       8788   7355  0 17:26 pts/0    00:00:00 grep --color=auto redis
[root@localhost bin]# kill -9 8780
[root@localhost bin]# ./redis-c
redis-check-aof  redis-check-rdb  redis-cli        
[root@localhost bin]# ./redis-cli 
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected> 

关闭方式二:(shutdown)

如下图所示表示关闭成功:

not connected> 
not connected> exit
[root@localhost bin]# ./redis-server redis.conf 
8790:C 28 Dec 2018 17:31:05.380 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8790:C 28 Dec 2018 17:31:05.380 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=8790, just started
8790:C 28 Dec 2018 17:31:05.380 # Configuration loaded
[root@localhost bin]# ./redis-cli 
127.0.0.1:6379> exit
[root@localhost bin]# ./redis-cli shutdown
[root@localhost bin]# ./redis-cli 
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected> 

11、如需远程连接redis,需配置redis端口6379在linux防火墙中开放

              /sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT

              /etc/rc.d/init.d/iptables save

使用上面命令会报错:-bash: /etc/rc.d/init.d/iptables: 没有那个文件或目录

查阅资料,发现一遍博客很有用,原文:

https://blog.youkuaiyun.com/seudongnan/article/details/78350832

按照博主方法,最后将 /etc/rc.d/init.d/iptables save 修改为: /etc/sysconfig/iptables save

三、向redis服务器发送命令

  redis-cli连上redis服务后,可以在redis命令行发送命令

1、ping  ,测试客户端与redis的连接是否正常,如果连接正常会收到回复PONG

[root@localhost bin]# ./redis-server redis.conf 
8826:C 28 Dec 2018 17:37:15.587 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8826:C 28 Dec 2018 17:37:15.587 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=8826, just started
8826:C 28 Dec 2018 17:37:15.587 # Configuration loaded
[root@localhost bin]# ./redis-cli 
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> 

2、set/get,使用set和get可以向redis设置数据、获取数据

127.0.0.1:6379> set name zhangsan
OK
127.0.0.1:6379> get name
"zhangsan"
127.0.0.1:6379> 

3、del  删除指定key的内容

127.0.0.1:6379> set name zhangsan
OK
127.0.0.1:6379> del name
(integer) 1
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> 

也可使用命令行登录:

./redis-cli -h 127.0.0.1 -p 6379

登录后如下所示:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值