Redis安装-CentOs7

官方地址

确保gcc已经安装

$ yum list installed | grep gcc 
$ yum install gcc

下载、提取和编辑Redis:

$ wget http://download.redis.io/releases/redis-4.0.8.tar.gz
$ tar xzf redis-4.0.8.tar.gz
$ cd redis-4.0.8
$ make MALLOC=libc
$ make test

运行Redis并放置到后台运行:

[root@localhost redis-4.0.8]# src/redis-server

6454:C 10 Mar 04:16:51.874 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6454:C 10 Mar 04:16:51.874 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=6454, just started
6454:C 10 Mar 04:16:51.874 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf
6454:M 10 Mar 04:16:51.875 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.8 (00000000/0) 64 bit
  .-`` .-```.  ```/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 6454
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

6454:M 10 Mar 04:16:51.877 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
6454:M 10 Mar 04:16:51.877 # Server initialized
6454:M 10 Mar 04:16:51.877 # 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.
6454:M 10 Mar 04:16:51.877 # 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.
6454:M 10 Mar 04:16:51.877 * Ready to accept connections
^Z       #Ctrl + Z
[1]+  Stopped                 src/redis-server
[root@localhost redis-4.0.8]# bg %1
[1]+ src/redis-server &
[root@localhost redis-4.0.8]# jobs
[1]+  Running                 src/redis-server &

使用内置客户端与Redis交互:

[root@localhost redis-4.0.8]# src/redis-cli
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
127.0.0.1:6379> 

为了使Java代码能够连接到Redis服务器需要设置Redis密码和bind:

编辑redis.conf文件:

[root@localhost redis-4.0.8]# find / -name redis.conf               #查找redis.conf文件
/opt/redis-4.0.8/redis.conf
[root@localhost redis-4.0.8]# vi redis.conf                         #编辑redis.conf文件
......
################################## SECURITY ###################################

# Require clients to issue AUTH <PASSWORD> before processing any other
# commands.  This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
# requirepass foobared
requirepass 123456                                                   #设置密码
......
################################## NETWORK #####################################

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 lookback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#bind 127.0.0.1                                                        #注释后redis会监听所有网络接口的连接请求

redis常用命令:

[root@localhost redis-4.0.8]# src/redis-server redis.conf &            #后台运行redis server

[root@localhost redis-4.0.8]# src/redis-cli                            #运行redis命令行接口
127.0.0.1:6379> auth 123456                                            #认证
OK
127.0.0.1:6379> keys *                                                 #列举
1) "foo"
2) "a"
3) "age"
4) "aaa"
127.0.0.1:6379> set qqq 12345                                          
OK
127.0.0.1:6379> get qqq
"12345"
127.0.0.1:6379> del qqq
(integer) 1127.0.0.1:6379> keys *
1) "aaa"
2) "foo"
3) "age"
4) "a"
127.0.0.1:6379> shutdown                                               #关闭redis服务端
15595:M 11 Mar 11:22:07.286 # User requested shutdown...
15595:M 11 Mar 11:22:07.286 * Saving the final RDB snapshot before exiting.
15595:M 11 Mar 11:22:07.291 * DB saved on disk
15595:M 11 Mar 11:22:07.291 * Removing the pid file.
15595:M 11 Mar 11:22:07.291 # Redis is now ready to exit, bye bye...
not connected> 

关联文档:

Redis-4.0.8 readme.md

原文地址:https://www.cnblogs.com/jimboi/p/8538768.html