Redis 安装使用

1、下载
wget http://download.redis.io/releases/redis-4.0.10.tar.gz

2、解压

[root@localhost redis]# tar xzf redis-4.0.10.tar.gz
[root@localhost redis]# ll
总用量 1700
drwxrwxr-x 6 root root     309 6月  13 2018 redis-4.0.10
-rw-r--r-- 1 root root 1738465 6月  14 2018 redis-4.0.10.tar.gz
[root@localhost redis]# 

3、安装

[root@localhost redis]# cd redis-4.0.10/
[root@localhost redis-4.0.10]#make

4、启动服务
make完后 redis-2.8.17目录下会出现编译后的redis服务程序redis-server,还有用于测试的客户端程序redis-cli,两个程序位于安装目录 src 目录下:
下面启动redis服务:

[root@localhost redis-4.0.10]# cd src/
[root@localhost src]# ./redis-server
110270:C 23 May 16:31:45.683 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
110270:C 23 May 16:31:45.683 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=110270, just started
110270:C 23 May 16:31:45.683 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
110270:M 23 May 16:31:45.684 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.10 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 110270
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

110270:M 23 May 16:31:45.685 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
110270:M 23 May 16:31:45.685 # Server initialized
110270:M 23 May 16:31:45.685 # 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.
110270:M 23 May 16:31:45.685 # 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.
110270:M 23 May 16:31:45.685 * Ready to accept connections

以上报错关键信息:

‘vm.overcommit_memory = 1’ to /etc/sysctl.conf and then reboot or run
the command ‘sysctl vm.overcommit_memory=1’

按照提示,修改/etc/sysctl.conf 文件,执行 sysctl vm.overcommit_memory=1,重启后再次启动 redis:

 [root@localhost src]# ./redis-server
    8044:C 23 May 16:41:17.630 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    8044:C 23 May 16:41:17.630 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=8044, just started
    8044:C 23 May 16:41:17.630 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
    8044:M 23 May 16:41:17.630 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                    _._                                                  
               _.-``__ ''-._                                             
          _.-``    `.  `_.  ''-._           Redis 4.0.10 (00000000/0) 64 bit
      .-`` .-```.  ```\/    _.,_ ''-._                                   
     (    '      ,       .-`  | `,    )     Running in standalone mode
     |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
     |    `-._   `._    /     _.-'    |     PID: 8044
      `-._    `-._  `-./  _.-'    _.-'                                   
     |`-._`-._    `-.__.-'    _.-'_.-'|                                  
     |    `-._`-._        _.-'_.-'    |           http://redis.io        
      `-._    `-._`-.__.-'_.-'    _.-'                                   
     |`-._`-._    `-.__.-'    _.-'_.-'|                                  
     |    `-._`-._        _.-'_.-'    |                                  
      `-._    `-._`-.__.-'_.-'    _.-'                                   
          `-._    `-.__.-'    _.-'                                       
              `-._        _.-'                                           
                  `-.__.-'                                               
    
    8044:M 23 May 16:41:17.635 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    8044:M 23 May 16:41:17.635 # Server initialized
    8044:M 23 May 16:41:17.635 # 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.
    8044:M 23 May 16:41:17.637 * DB loaded from disk: 0.001 seconds
    8044:M 23 May 16:41:17.637 * Ready to accept connections

本次报错关键信息:

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.

根据提示,在系统中执行命令:echo never >/sys/kernel/mm/transparent_hugepage/enabled,接着启动 redis:

[root@localhost src]# ./redis-server
8048:C 23 May 16:43:05.241 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8048:C 23 May 16:43:05.241 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=8048, just started
8048:C 23 May 16:43:05.241 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
8048:M 23 May 16:43:05.242 * Increased maximum number of open files to 10032 (it was originally set to 1024).
8048:M 23 May 16:43:05.242 # Creating Server TCP listening socket *:6379: bind: Address already in use

注意这种方式启动redis 使用的是默认配置。也可以通过启动参数告诉redis使用指定配置文件使用下面命令启动:

$ cd src
$ ./redis-server ../redis.conf  #实际配置文件所在位置

redis.conf 是一个默认的配置文件。我们可以根据需要使用自己的配置文件。

5、启动后可以通过命令查看到 redis 进程情况

[root@localhost ~]# ps axu |grep redis
root       7672  0.1  0.7 145308  7596 ?        Ssl  10:24   0:00 /data/redis/redis-4.0.10/src/redis-server 192.168.225.129:6379
root       8151  0.0  0.0 112724   984 pts/0    R+   10:27   0:00 grep --color=auto redis

启动redis服务进程后,就可以使用测试客户端程序redis-cli和redis服务交互了。
比如:

[root@localhost src]# ./redis-cli -h 192.168.225.131 -p 6379  #登入 redis
192.168.225.131:6379> set iris '123'  
(error) NOAUTH Authentication required.
192.168.225.131:6379> auth 'redis1'   #输入验证密码,设置了 requirepass 才需要
OK
192.168.225.131:6379> set iris '123'  #生成 key,value为”123“
OK
192.168.225.131:6379>

6、修改后台进程
默认情况,Redis不是在后台运行,如果需要把redis放在后台运行
vim ./redis.conf # 配置文件实际路径
将daemonize的值改为yes(默认是 no,关闭)

7、停止redis实例

/usr/local/redis/bin/redis-cli shutdown
# 或者
pkill redis-server

8、配置 Redis 开机启动
想让redis在开机的时候自动启动:

 vim /etc/rc.local

添加如下内容:

/data/redis/redis-4.0.10/src/redis-server /data/redis/redis-4.0.10/redis.conf

注意:以上方法,有可能不生效,需要以下方式检查:
1.查看/etc/rc.local 文件权限:

[root@localhost redis-4.0.10]# ll /etc/rc.local 
lrwxrwxrwx. 1 root root 13 4月  11 15:39 /etc/rc.local -> rc.d/rc.local

2.查看链接文件./rc.d/rc.local的权限,

[root@localhost redis-4.0.10]# ll /etc/rc.d/rc.local
-rw-r--r--. 1 root root 551 5月  23 16:59 /etc/rc.d/rc.local

3.需执行以下命令,才能让系统再启动的时候,自动启动 redis

chmod +x /etc/rc.d/rc.local

9、客户端连接 redis验证

[root@localhost src]# ./redis-cli -h 192.168.225.131 -p 6379  #登入 redis
192.168.225.131:6379> set iris '123'
(error) NOAUTH Authentication required.
192.168.225.131:6379> auth 'redis1'
OK
192.168.225.131:6379> set iris '123'
OK
192.168.225.131:6379> info   #查看 redis配置信息& 资源情况
# Server
redis_version:4.0.10
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:4dceaced14d685c9
redis_mode:standalone
os:Linux 3.10.0-957.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:7607
run_id:d6a5653596055ace77661a721c5a6402c1c53ecd
tcp_port:6379
uptime_in_seconds:846
uptime_in_days:0
hz:10
lru_clock:3656086
executable:/data/redis/redis-4.0.10/src/redis-server
config_file:/data/redis/redis-4.0.10/redis.conf

# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

# Memory
used_memory:849592
used_memory_human:829.68K
used_memory_rss:7688192
used_memory_rss_human:7.33M
used_memory_peak:849592
used_memory_peak_human:829.68K
used_memory_peak_perc:100.12%
used_memory_overhead:836366
……

10、redis 的主要文件
接下来我们看看./redis/bin目录下的几个文件:

  • redis-benchmark:redis性能测试工具
  • redis-check-aof:aof日志检查工具
  • redis-check-dump:RDB日志检查工具
  • redis-cli:连接用的客户端
  • redis-server:redis服务进程

11、Redis的配置参数

  • daemonize:如需要在后台运行,把该项的值改为yes
  • pdifile:把pid文件放在/var/run/redis.pid,可以配置到其他地址
  • bind:指定redis只接收来自该IP的请求,如果不设置,那么将处理所有请求,在生产环节中最好设置该项
  • port:监听端口,默认为6379 timeout:设置客户端连接时的超时时间,单位为秒
  • loglevel:等级分为4级,debug,revbose,notice和warning。生产环境下一般开启notice
  • logfile:配置log文件地址,默认使用标准输出,即打印在命令行终端的端口上 database:设置数据库的个数,默认使用的数据库是0
  • save:设置redis进行数据库镜像的频率 rdbcompression:在进行镜像备份时,是否进行压缩
  • dbfilename:镜像备份文件的文件名 dir:数据库镜像备份的文件放置的路径 slaveof:设置该数据库为其他数据库的从数据库
  • masterauth:当主数据库连接需要密码验证时,在这里设定 requirepass:设置客户端连接后进行任何其他指定前需要使用的密码
  • maxclients:限制同时连接的客户端数量 maxmemory:设置redis能够使用的最大内存
  • appendonly:开启appendonly模式后,redis会把每一次所接收到的写操作都追加到appendonly.aof文件中,当redis重新启动时,会从该文件恢复出之前的状态
  • appendfsync:设置appendonly.aof文件进行同步的频率 vm_enabled:是否开启虚拟内存支持
  • vm_swap_file:设置虚拟内存的交换文件的路径
  • vm_max_momery:设置开启虚拟内存后,redis将使用的最大物理内存的大小,默认为0
  • vm_page_size:设置虚拟内存页的大小 vm_pages:设置交换文件的总的page数量 vm_max_thrrads:设置vm
  • IO同时使用的线程数量
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值