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

版权声明: https://blog.csdn.net/weixin_40521823/article/details/85328056

一、redis下载

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

二、redis在Linux系统安装

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

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

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

三、向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> 

猜你喜欢

转载自blog.csdn.net/weixin_40521823/article/details/85328056