博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Redis 缓存服务配置与使用
阅读量:7247 次
发布时间:2019-06-29

本文共 6953 字,大约阅读时间需要 23 分钟。

缓存服务器另外一种选择Redis

documentation
Redis 命令参考

$ mkdir
data $ yum install
tcl $ wget http://download.redis.io/releases/redis-3.0.2.tar.gz $ tar xzf redis-3.0.2.tar
.gz $ cd redis-3.0.2
$ make
make test

You need 'tclsh8.5' in order to run the Redis test

yum install tcl

$ src/redis-server

[root@localhost redis-3.0.2]# src/redis-server
25946:C 02 Jul 11:02:26.749 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf
                _._                                                 
           _.-``__ ''-._                                            
      _.-``    `.  `_.  ''-._           Redis 3.0.2 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
(    '      ,       .-`  | `,    )     Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
|    `-._   `._    /     _.-'    |     PID: 25946
  `-._    `-._  `-./  _.-'    _.-'                                  
|`-._`-._    `-.__.-'    _.-'_.-'|                                 
|    `-._`-._        _.-'_.-'    |                  
  `-._    `-._`-.__.-'_.-'    _.-'                                  
|`-._`-._    `-.__.-'    _.-'_.-'|                                 
|    `-._`-._        _.-'_.-'    |                                 
  `-._    `-._`-.__.-'_.-'    _.-'                                  
      `-._    `-.__.-'    _.-'                                      
          `-._        _.-'                                          
              `-.__.-'                                              

25946:M 02 Jul 11:02:26.751 # Server started, Redis version 3.0.2

25946:M 02 Jul 11:02:26.751 # 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.
25946:M 02 Jul 11:02:26.751 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
25946:M 02 Jul 11:02:26.751 * The server is now ready to accept connections on port 6379

警告:过量使用内存设置为0!在低内存环境下,后台保存可能失败。为了修正这个问题,请在/etc/sysctl.conf 添加一项 'vm.overcommit_memory = 1' ,然后重启(或者运行命令'sysctl vm.overcommit_memory=1' )使其生效。

$ vi /etc/
sysctl.conf vm.overcommit_memory = 1
:wq$ sysctl vm.overcommit_memory
=1
$
echo never > /sys/kernel/mm/transparent_hugepage/
enabled $ echo 511 > /proc/sys/net/core/somaxconn

 

 #查看进程

ps -ef | grep redis
#杀死进程
kill -9 25946

redis.conf 配置

#守护进程启动

daemonize yes
#数据文件路径
dir /opt/data/
#日志文件路径
logfile "/opt/data/redis.log"
#缓存数据名称
dbfilename dump.rdb

 #启动服务

src/redis-server /opt/redis-3.0.2/redis.conf
$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"
redis> exit

#关闭服务

$ src/redis-cli shutdown

持久化(persistence)

RDB 持久化可以在指定的时间间隔内生成数据集的时间点快照(point-in-time snapshot)。

AOF 持久化记录服务器执行的所有写操作命令,并在服务器启动时,通过重新执行这些命令来还原数据集。 AOF 文件中的命令全部以 Redis 协议的格式来保存,新命令会被追加到文件的末尾。 Redis 还可以在后台对 AOF 文件进行重写(rewrite),使得 AOF 文件的体积不会超出保存数据集状态所需的实际大小。
Redis 还可以同时使用 AOF 持久化和 RDB 持久化。 在这种情况下, 当 Redis 重启时, 它会优先使用 AOF 文件来还原数据集, 因为 AOF 文件保存的数据集通常比 RDB 文件所保存的数据集更完整。

复制(Replication)

配置一个从服务器非常简单,只要在配置文件中增加以下的这一行就可以了:

slaveof 192.168.1.1 6379
另外一种方法是调用 SLAVEOF 命令输入主服务器的 IP 和端口,然后同步就会开始:
127.0.0.1:6379> SLAVEOF 172.23.100.220 6379
OK
#salve日志
redisdemo@021rjsh17217s:/opt/redis-3.0.2$ sudo src/redis-server /opt/redis-3.0.2/redis.conf
28971:M 02 Jul 13:51:57.192 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                 
           _.-``__ ''-._                                            
      _.-``    `.  `_.  ''-._           Redis 3.0.2 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
(    '      ,       .-`  | `,    )     Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
|    `-._   `._    /     _.-'    |     PID: 28971
  `-._    `-._  `-./  _.-'    _.-'                                  
|`-._`-._    `-.__.-'    _.-'_.-'|                                 
|    `-._`-._        _.-'_.-'    |                  
  `-._    `-._`-.__.-'_.-'    _.-'                                  
|`-._`-._    `-.__.-'    _.-'_.-'|                                 
|    `-._`-._        _.-'_.-'    |                                 
  `-._    `-._`-.__.-'_.-'    _.-'                                  
      `-._    `-.__.-'    _.-'                                      
          `-._        _.-'                                          
              `-.__.-'                                              

28971:M 02 Jul 13:51:57.193 # Server started, Redis version 3.0.2

28971:M 02 Jul 13:51:57.194 # 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.
28971:M 02 Jul 13:51:57.194 # 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.
28971:M 02 Jul 13:51:57.194 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
28971:M 02 Jul 13:51:57.194 * The server is now ready to accept connections on port 6379
28971:S 02 Jul 13:56:04.572 * SLAVE OF 172.23.100.220:6379 enabled (user request)
28971:S 02 Jul 13:56:04.960 * Connecting to MASTER 172.23.100.220:6379
28971:S 02 Jul 13:56:04.960 * MASTER <-> SLAVE sync started
28971:S 02 Jul 13:56:04.961 * Non blocking connect for SYNC fired the event.
28971:S 02 Jul 13:56:04.962 * Master replied to PING, replication can continue...
28971:S 02 Jul 13:56:04.962 * Partial resynchronization not possible (no cached master)
28971:S 02 Jul 13:56:04.963 * Full resync from master: 3179db3e14a2f3abc534bc0dc28a7c025d3dd259:1
28971:S 02 Jul 13:56:05.070 * MASTER <-> SLAVE sync: receiving 40 bytes from master
28971:S 02 Jul 13:56:05.071 * MASTER <-> SLAVE sync: Flushing old data
28971:S 02 Jul 13:56:05.071 * MASTER <-> SLAVE sync: Loading DB in memory
28971:S 02 Jul 13:56:05.071 * MASTER <-> SLAVE sync: Finished with success

#master日志

                _._                                                 
           _.-``__ ''-._                                            
      _.-``    `.  `_.  ''-._           Redis 3.0.2 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
(    '      ,       .-`  | `,    )     Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
|    `-._   `._    /     _.-'    |     PID: 26132
  `-._    `-._  `-./  _.-'    _.-'                                  
|`-._`-._    `-.__.-'    _.-'_.-'|                                 
|    `-._`-._        _.-'_.-'    |                  
  `-._    `-._`-.__.-'_.-'    _.-'                                  
|`-._`-._    `-.__.-'    _.-'_.-'|                                 
|    `-._`-._        _.-'_.-'    |                                 
  `-._    `-._`-.__.-'_.-'    _.-'                                  
      `-._    `-.__.-'    _.-'                                      
          `-._        _.-'                                          
              `-.__.-'                                              

26132:M 02 Jul 11:23:14.445 # Server started, Redis version 3.0.2

26132:M 02 Jul 11:23:14.445 * DB loaded from disk: 0.000 seconds
26132:M 02 Jul 11:23:14.445 * The server is now ready to accept connections on port 6379
26132:M 02 Jul 13:43:39.602 * 1 changes in 900 seconds. Saving...
26132:M 02 Jul 13:43:39.602 * Background saving started by pid 26692
26692:C 02 Jul 13:43:39.608 * DB saved on disk
26692:C 02 Jul 13:43:39.608 * RDB: 0 MB of memory used by copy-on-write
26132:M 02 Jul 13:43:39.704 * Background saving terminated with success
26132:M 02 Jul 14:10:20.543 * Slave 172.23.100.217:6379 asks for synchronization
26132:M 02 Jul 14:10:20.543 * Full resync requested by slave 172.23.100.217:6379
26132:M 02 Jul 14:10:20.543 * Starting BGSAVE for SYNC with target: disk
26132:M 02 Jul 14:10:20.544 * Background saving started by pid 26797
26797:C 02 Jul 14:10:20.549 * DB saved on disk
26797:C 02 Jul 14:10:20.550 * RDB: 0 MB of memory used by copy-on-write
26132:M 02 Jul 14:10:20.650 * Background saving terminated with success
26132:M 02 Jul 14:10:20.650 * Synchronization with slave 172.23.100.217:6379 succeeded

集群

客户端

j2cache

StackExchange.Redis

REFER:

Redis 启动警告错误解决
Redis (二) 配置
Redis Sentinel集群方案--单机测试
redis sentinel 主从切换(failover)解决方案,详细配置
Redis.conf 配置文件详解
谈Twitter的百TB级Redis缓存实践

转载地址:http://gfnbm.baihongyu.com/

你可能感兴趣的文章
smarty if 操作符
查看>>
python 自定义异常
查看>>
nullnullAS 数组
查看>>
linux命令综合
查看>>
Xcode 调试技巧
查看>>
Clojure常用模块
查看>>
会计基础第二次模拟试题(1)
查看>>
C#线程间同步无法关闭
查看>>
Win7与Ubuntu双系统安装过程
查看>>
HBase之五:hbase的region分区
查看>>
Android 多用户多缓存的简单处理方案
查看>>
Linux&shell 之Linux文件权限
查看>>
oracle expdp和impdp使用例子
查看>>
JavaScript —— 局部变量和全局变量
查看>>
通过qsort(void * lineptr[], int left, int rifht, int (*comp)(void *, void *))解读指针函数和void指针...
查看>>
大学该怎么度过
查看>>
Windows服务创建及安装
查看>>
C# 网络编程之网页自动登录 (一).使用WebBrower控件模仿登录
查看>>
VS2012 编译GDAL
查看>>
腾讯笔试题
查看>>