Redis安装及主从复制实战

本文涉及的产品
云数据库 Tair(兼容Redis),内存型 2GB
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
网络型负载均衡 NLB,每月750个小时 15LCU
简介:

      Redis 是一款开源的,使用C语言编写的、支持网络交互的、可基于内存也可持久化的高性能Key-Value存储系统(cache and store)。它通常被称为数据结构服务器,因为值(value)可以是 字符串(String)哈希(hashes)列表(list)集合(sets) 和 有序集合(sorted        sets)等类型。

Redis作者谈Redis应用场景:http://blog.nosqlfan.com/html/2235.html


1.redis安装部署:
wget http://download.redis.io/releases/redis-3.0.5.tar.gz
tar xf redis-3.0.5.tar.gz 
cd redis-3.0.5
less README   #(如何安装,查看README,INSTALL)
make MALLOC=jemalloc
make PREFIX=/application/redis-3.0.5 install
ln -s /application/redis-3.0.5/ /application/redis
echo "export PATH=/application/redis/bin/:$PATH" >>/etc/profile
source /etc/profile
[root@LB-111-06 ~]# which redis-server
/application/redis/bin/redis-server
[root@LB-111-06 redis-3.0.5]# ls
00-RELEASENOTES  INSTALL     runtest           tests
BUGS             Makefile    runtest-cluster   utils
CONTRIBUTING     MANIFESTO   runtest-sentinel
COPYING          README      sentinel.conf
deps             redis.conf  src

[root@LB-111-06 redis-3.0.5]# mkdir /application/redis/conf
[root@LB-111-06 redis-3.0.5]# cp redis.conf /application/redis/conf/
[root@LB-111-06 redis-3.0.5]# redis-server /application/redis/conf/redis.conf  &
[1] 5321
5321:M 01 Nov 21:38:48.524 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.0.5 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |      PID: 5321
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

5321:M 01 Nov 21:38:48.525 # Server started, Redis version 3.0.5
5321:M 01 Nov 21:38:48.525 # 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.
5321:M 01 Nov 21:38:48.525 * The server is now ready to accept connections on port 6379

[root@LB-111-06 redis-3.0.5]# killall redis-server
5321:signal-handler (1446385182) Received SIGTERM scheduling shutdown...
5321:M 01 Nov 21:39:42.523 # User requested shutdown...
5321:M 01 Nov 21:39:42.523 * Saving the final RDB snapshot before exiting.
5321:M 01 Nov 21:39:42.526 * DB saved on disk
5321:M 01 Nov 21:39:42.526 # Redis is now ready to exit, bye bye...
[1]+  Done                    redis-server /application/redis/conf/redis.conf

[root@LB-111-06 redis-3.0.5]#  sysctl vm.overcommit_memory=1
vm.overcommit_memory = 1
[root@LB-111-06 redis-3.0.5]# redis-server  /application/redis/conf/redis.conf  &
[1] 5334
5334:M 01 Nov 21:42:03.900 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.0.5 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |      PID: 5334
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

5334:M 01 Nov 21:42:03.900 # Server started, Redis version 3.0.5
5334:M 01 Nov 21:42:03.900 * DB loaded from disk: 0.000 seconds
5334:M 01 Nov 21:42:03.900 * The server is now ready to accept connections on port 6379

[root@LB-111-06 redis-3.0.5]# netstat -tunlp|grep 6379
tcp        0      0 0.0.0.0:6379                0.0.0.0:*                   LISTEN      5334/redis-server * 
tcp        0      0 :::6379                     :::*                        LISTEN      5334/redis-server * 

[root@LB-111-06 redis-3.0.5]# lsof -i:6379             
COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 5334 root    4u  IPv6  20820      0t0  TCP *:6379 (LISTEN)
redis-ser 5334 root    5u  IPv4  20822      0t0  TCP *:6379 (LISTEN)

[root@LB-111-06 redis-3.0.5]# redis-cli  shutdown     #关闭服务
5334:M 01 Nov 21:46:49.015 # User requested shutdown...
5334:M 01 Nov 21:46:49.015 * Saving the final RDB snapshot before exiting.
5334:M 01 Nov 21:46:49.017 * DB saved on disk
5334:M 01 Nov 21:46:49.017 # Redis is now ready to exit, bye bye...
[1]+  Done                    redis-server /application/redis/conf/redis.conf
[root@LB-111-06 redis-3.0.5]# lsof -i:6379    

[root@LB-111-06 redis-3.0.5]# ll /application/redis/
total 4
drwxr-xr-x 2 root root 4096 Nov  1 21:19 bin
[root@LB-111-06 redis-3.0.5]# tree /application/redis/bin
/application/redis/bin
├── redis-benchmark  #Redis性能测试工具,测试Redis在你的系统及你的配置下的读写性能
├── redis-check-aof  #更新日志检查
├── redis-check-dump #用于本地数据库检查
├── redis-cli        #Redis命令行操作工具。当然你也可以用telnet根据其纯文本协议来操作
├── redis-sentinel -> redis-server
└── redis-server     #Redis服务器的daemon启动程序

0 directories, 6 files

[root@LB-111-06 redis-3.0.5]# redis-cli --help
redis-cli 3.0.5

Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
  -h <hostname>      Server hostname (default: 127.0.0.1).
  -p <port>          Server port (default: 6379).
  -s <socket>        Server socket (overrides hostname and port).
  -a <password>      Password to use when connecting to the server.
  -r <repeat>        Execute specified command N times.
  -i <interval>      When -r is used, waits <interval> seconds per command.
                     It is possible to specify sub-second times like -i 0.1.
  -n <db>            Database number.
  -x                 Read last argument from STDIN.
  -d <delimiter>     Multi-bulk delimiter in for raw formatting (default: \n).
  -c                 Enable cluster mode (follow -ASK and -MOVED redirections).
  --raw              Use raw formatting for replies (default when STDOUT is
                     not a tty).
  --no-raw           Force formatted output even when STDOUT is not a tty.
  --csv              Output in CSV format.
  --stat             Print rolling stats about server: mem, clients, ...
  --latency          Enter a special mode continuously sampling latency.
  --latency-history  Like --latency but tracking latency changes over time.
                     Default time interval is 15 sec. Change it using -i.
  --latency-dist     Shows latency as a spectrum, requires xterm 256 colors.
                     Default time interval is 1 sec. Change it using -i.
  --lru-test <keys>  Simulate a cache workload with an 80-20 distribution.
  --slave            Simulate a slave showing commands received from the master.
  --rdb <filename>   Transfer an RDB dump from remote server to local file.
  --pipe             Transfer raw Redis protocol from stdin to server.
  --pipe-timeout <n> In --pipe mode, abort with error if after sending all data.
                     no reply is received within <n> seconds.
                     Default timeout: 30. Use 0 to wait forever.
  --bigkeys          Sample Redis keys looking for big keys.
  --scan             List all keys using the SCAN command.
  --pattern <pat>    Useful with --scan to specify a SCAN pattern.
  --intrinsic-latency <sec> Run a test to measure intrinsic system latency.
                     The test will run for the specified amount of seconds.
  --eval <file>      Send an EVAL command using the Lua script at <file>.
  --help             Output this help and exit.
  --version          Output version and exit.

Examples:
  cat /etc/passwd | redis-cli -x set mypasswd
  redis-cli get mypasswd
  redis-cli -r 100 lpush mylist x
  redis-cli -r 100 -i 1 info | grep used_memory_human:
  redis-cli --eval myscript.lua key1 key2 , arg1 arg2 arg3
  redis-cli --scan --pattern '*:12345*'
(Note: when using --eval the comma separates KEYS[] from ARGV[] items)
When no command is given, redis-cli starts in interactive mode.
Type "help" in interactive mode for information on available commands.

[root@LB-111-06 redis-3.0.5]# redis-cli 
127.0.0.1:6379> help
redis-cli 3.0.5
Type: "help @<group>" to get a list of commands in <group>
      "help <command>" for help on <command>
      "help <tab>" to get a list of possible help topics
      "quit" to exit
127.0.0.1:6379> help get

  GET key
  summary: Get the value of a key
  since: 1.0.0
  group: string

127.0.0.1:6379> help set

  SET key value [EX seconds] [PX milliseconds] [NX|XX]
  summary: Set the string value of a key
  since: 1.0.0
  group: string

127.0.0.1:6379> set no002 oldboy
OK
127.0.0.1:6379> get no002
"oldboy"
127.0.0.1:6379> quit

[root@LB-111-06 redis-3.0.5]# redis-cli  -h 10.0.0.6 -p 6379 set no001 zhangsan
OK
[root@LB-111-06 redis-3.0.5]# redis-cli  -h 10.0.0.6 -p 6379 get no001
"zhangsan"
[root@LB-111-06 redis-3.0.5]# redis-cli del no001
(integer) 1
[root@LB-111-06 redis-3.0.5]# redis-cli del no001
(integer) 0
[root@LB-111-06 redis-3.0.5]# telnet 127.0.0.1 6379
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
set no003 jisu
+OK
get no003
$4
jisu

2.redis的php客户端扩展安装
下载:https://github.com/nicolasff/phpredis/archive/phpredis-2.2.4.tar.gz
tar zxvf phpredis-2.2.4.tar.gz  #解压
cd phpredis-2.2.4                    #进入安装目录
/application/php/bin/phpize  #生成configure配置文件
./configure --with-php-config=/application/php/bin/php-config
make             #编译
make install  #安装

#安装完成之后,出现下面的安装路径
/application/php5.6.12/lib/php/extensions/no-debug-zts-20131226/

3.配置php支持
vi   #编辑配置文件,在最后一行添加以下内容
添加
extension = redis.so
extension_dir = "/application/php5.6.12/lib/php/extensions/no-debug-zts-20131226/"

4.redis配置文件介绍及主从同步配置:
a、slave配置:
 210 # slaveof <masterip> <masterport>
 211    slaveof  10.0.0.6   6379  #为master的IP及服务端口
[root@LNMP-07 conf]# redis-cli -h localhost -p 6379 monitor  #在slave库打开主从同步实时监控
OK
1448619846.561773 [0 10.0.0.6:6379] "PING"
1448619856.670778 [0 10.0.0.6:6379] "PING"
...............................
1448619947.631375 [0 10.0.0.6:6379] "PING"

b、master操作:
[root@LB-111-06 ~]# redis-cli
127.0.0.1:6379> set t1 oldboy01
OK
127.0.0.1:6379> get t1
"oldboy01"
127.0.0.1:6379> 

此时再查看从库:
。。。。。。。。。。。。。。。
1448619947.631375 [0 10.0.0.6:6379] "PING"
1448619955.222078 [0 10.0.0.6:6379] "SELECT" "0"
1448619955.222147 [0 10.0.0.6:6379] "set" "t1" "oldboy01"

主库连接到从库查看:
[root@LB-111-06 ~]# redis-cli -h 10.0.0.7 get t1
"oldboy01"
[root@LB-111-06 ~]# redis-cli -h localhost -p 6379 info       #查看redis服务的统计信息
# Server
redis_version:3.0.5
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:b358be82c008aa44
redis_mode:standalone
os:Linux 2.6.32-504.el6.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.4.7
process_id:5357
run_id:b93947dbd3db2bc911115f9187d3d3f176861c17
tcp_port:6379
uptime_in_seconds:5665
uptime_in_days:0
hz:10
lru_clock:3551121
config_file:/application/redis/conf/redis.conf

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

# Memory
used_memory:1884728
used_memory_human:1.80M
used_memory_rss:2523136
used_memory_peak:1920672
used_memory_peak_human:1.83M
used_memory_lua:36864
mem_fragmentation_ratio:1.34
mem_allocator:jemalloc-3.6.0

# Persistence
loading:0
rdb_changes_since_last_save:1
rdb_bgsave_in_progress:0
rdb_last_save_time:1446391003
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok

# Stats
total_connections_received:13
total_commands_processed:703
instantaneous_ops_per_sec:1
total_net_input_bytes:25030
total_net_output_bytes:1198
instantaneous_input_kbps:0.04
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:1
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:4
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:310
migrate_cached_sockets:0

# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.7,port=6379,state=online,offset=1011,lag=1
master_repl_offset:1011
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:1010

# CPU
used_cpu_sys:4.19
used_cpu_user:1.95
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Cluster
cluster_enabled:0

# Keyspace
db0:keys=3,expires=0,avg_ttl=0

[root@LB-111-06 ~]# redis-cli -h localhost -p 6379 info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.7,port=6379,state=online,offset=1277,lag=1
master_repl_offset:1277
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:1276

[root@LB-111-06 ~]# redis-cli
127.0.0.1:6379> help info
  INFO [section]
  summary: Get information and statistics about the server
  since: 1.0.0
  group: server

127.0.0.1:6379> info stats
# Stats
total_connections_received:15
total_commands_processed:952
instantaneous_ops_per_sec:1
total_net_input_bytes:34215
total_net_output_bytes:3844
instantaneous_input_kbps:0.04
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:1
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:4
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:162
migrate_cached_sockets:0
127.0.0.1:6379> help
redis-cli 3.0.5
Type: "help @<group>" to get a list of commands in <group>
      "help <command>" for help on <command>
      "help <tab>" to get a list of possible help topics
      "quit" to exit



      Redis 是一款开源的,使用C语言编写的、支持网络交互的、可基于内存也可持久化的高性能Key-Value存储系统(cache and store)。它通常被称为数据结构服务器,因为值(value)可以是 字符串(String)哈希(hashes)列表(list)集合(sets) 和 有序集合(sorted        sets)等类型。

Redis作者谈Redis应用场景:http://blog.nosqlfan.com/html/2235.html


1.redis安装部署:
wget http://download.redis.io/releases/redis-3.0.5.tar.gz
tar xf redis-3.0.5.tar.gz 
cd redis-3.0.5
less README   #(如何安装,查看README,INSTALL)
make MALLOC=jemalloc
make PREFIX=/application/redis-3.0.5 install
ln -s /application/redis-3.0.5/ /application/redis
echo "export PATH=/application/redis/bin/:$PATH" >>/etc/profile
source /etc/profile
[root@LB-111-06 ~]# which redis-server
/application/redis/bin/redis-server
[root@LB-111-06 redis-3.0.5]# ls
00-RELEASENOTES  INSTALL     runtest           tests
BUGS             Makefile    runtest-cluster   utils
CONTRIBUTING     MANIFESTO   runtest-sentinel
COPYING          README      sentinel.conf
deps             redis.conf  src

[root@LB-111-06 redis-3.0.5]# mkdir /application/redis/conf
[root@LB-111-06 redis-3.0.5]# cp redis.conf /application/redis/conf/
[root@LB-111-06 redis-3.0.5]# redis-server /application/redis/conf/redis.conf  &
[1] 5321
5321:M 01 Nov 21:38:48.524 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.0.5 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |      PID: 5321
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

5321:M 01 Nov 21:38:48.525 # Server started, Redis version 3.0.5
5321:M 01 Nov 21:38:48.525 # 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.
5321:M 01 Nov 21:38:48.525 * The server is now ready to accept connections on port 6379

[root@LB-111-06 redis-3.0.5]# killall redis-server
5321:signal-handler (1446385182) Received SIGTERM scheduling shutdown...
5321:M 01 Nov 21:39:42.523 # User requested shutdown...
5321:M 01 Nov 21:39:42.523 * Saving the final RDB snapshot before exiting.
5321:M 01 Nov 21:39:42.526 * DB saved on disk
5321:M 01 Nov 21:39:42.526 # Redis is now ready to exit, bye bye...
[1]+  Done                    redis-server /application/redis/conf/redis.conf

[root@LB-111-06 redis-3.0.5]#  sysctl vm.overcommit_memory=1
vm.overcommit_memory = 1
[root@LB-111-06 redis-3.0.5]# redis-server  /application/redis/conf/redis.conf  &
[1] 5334
5334:M 01 Nov 21:42:03.900 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.0.5 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |      PID: 5334
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

5334:M 01 Nov 21:42:03.900 # Server started, Redis version 3.0.5
5334:M 01 Nov 21:42:03.900 * DB loaded from disk: 0.000 seconds
5334:M 01 Nov 21:42:03.900 * The server is now ready to accept connections on port 6379

[root@LB-111-06 redis-3.0.5]# netstat -tunlp|grep 6379
tcp        0      0 0.0.0.0:6379                0.0.0.0:*                   LISTEN      5334/redis-server * 
tcp        0      0 :::6379                     :::*                        LISTEN      5334/redis-server * 

[root@LB-111-06 redis-3.0.5]# lsof -i:6379             
COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 5334 root    4u  IPv6  20820      0t0  TCP *:6379 (LISTEN)
redis-ser 5334 root    5u  IPv4  20822      0t0  TCP *:6379 (LISTEN)

[root@LB-111-06 redis-3.0.5]# redis-cli  shutdown     #关闭服务
5334:M 01 Nov 21:46:49.015 # User requested shutdown...
5334:M 01 Nov 21:46:49.015 * Saving the final RDB snapshot before exiting.
5334:M 01 Nov 21:46:49.017 * DB saved on disk
5334:M 01 Nov 21:46:49.017 # Redis is now ready to exit, bye bye...
[1]+  Done                    redis-server /application/redis/conf/redis.conf
[root@LB-111-06 redis-3.0.5]# lsof -i:6379    

[root@LB-111-06 redis-3.0.5]# ll /application/redis/
total 4
drwxr-xr-x 2 root root 4096 Nov  1 21:19 bin
[root@LB-111-06 redis-3.0.5]# tree /application/redis/bin
/application/redis/bin
├── redis-benchmark  #Redis性能测试工具,测试Redis在你的系统及你的配置下的读写性能
├── redis-check-aof  #更新日志检查
├── redis-check-dump #用于本地数据库检查
├── redis-cli        #Redis命令行操作工具。当然你也可以用telnet根据其纯文本协议来操作
├── redis-sentinel -> redis-server
└── redis-server     #Redis服务器的daemon启动程序

0 directories, 6 files

[root@LB-111-06 redis-3.0.5]# redis-cli --help
redis-cli 3.0.5

Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
  -h <hostname>      Server hostname (default: 127.0.0.1).
  -p <port>          Server port (default: 6379).
  -s <socket>        Server socket (overrides hostname and port).
  -a <password>      Password to use when connecting to the server.
  -r <repeat>        Execute specified command N times.
  -i <interval>      When -r is used, waits <interval> seconds per command.
                     It is possible to specify sub-second times like -i 0.1.
  -n <db>            Database number.
  -x                 Read last argument from STDIN.
  -d <delimiter>     Multi-bulk delimiter in for raw formatting (default: \n).
  -c                 Enable cluster mode (follow -ASK and -MOVED redirections).
  --raw              Use raw formatting for replies (default when STDOUT is
                     not a tty).
  --no-raw           Force formatted output even when STDOUT is not a tty.
  --csv              Output in CSV format.
  --stat             Print rolling stats about server: mem, clients, ...
  --latency          Enter a special mode continuously sampling latency.
  --latency-history  Like --latency but tracking latency changes over time.
                     Default time interval is 15 sec. Change it using -i.
  --latency-dist     Shows latency as a spectrum, requires xterm 256 colors.
                     Default time interval is 1 sec. Change it using -i.
  --lru-test <keys>  Simulate a cache workload with an 80-20 distribution.
  --slave            Simulate a slave showing commands received from the master.
  --rdb <filename>   Transfer an RDB dump from remote server to local file.
  --pipe             Transfer raw Redis protocol from stdin to server.
  --pipe-timeout <n> In --pipe mode, abort with error if after sending all data.
                     no reply is received within <n> seconds.
                     Default timeout: 30. Use 0 to wait forever.
  --bigkeys          Sample Redis keys looking for big keys.
  --scan             List all keys using the SCAN command.
  --pattern <pat>    Useful with --scan to specify a SCAN pattern.
  --intrinsic-latency <sec> Run a test to measure intrinsic system latency.
                     The test will run for the specified amount of seconds.
  --eval <file>      Send an EVAL command using the Lua script at <file>.
  --help             Output this help and exit.
  --version          Output version and exit.

Examples:
  cat /etc/passwd | redis-cli -x set mypasswd
  redis-cli get mypasswd
  redis-cli -r 100 lpush mylist x
  redis-cli -r 100 -i 1 info | grep used_memory_human:
  redis-cli --eval myscript.lua key1 key2 , arg1 arg2 arg3
  redis-cli --scan --pattern '*:12345*'
(Note: when using --eval the comma separates KEYS[] from ARGV[] items)
When no command is given, redis-cli starts in interactive mode.
Type "help" in interactive mode for information on available commands.

[root@LB-111-06 redis-3.0.5]# redis-cli 
127.0.0.1:6379> help
redis-cli 3.0.5
Type: "help @<group>" to get a list of commands in <group>
      "help <command>" for help on <command>
      "help <tab>" to get a list of possible help topics
      "quit" to exit
127.0.0.1:6379> help get

  GET key
  summary: Get the value of a key
  since: 1.0.0
  group: string

127.0.0.1:6379> help set

  SET key value [EX seconds] [PX milliseconds] [NX|XX]
  summary: Set the string value of a key
  since: 1.0.0
  group: string

127.0.0.1:6379> set no002 oldboy
OK
127.0.0.1:6379> get no002
"oldboy"
127.0.0.1:6379> quit

[root@LB-111-06 redis-3.0.5]# redis-cli  -h 10.0.0.6 -p 6379 set no001 zhangsan
OK
[root@LB-111-06 redis-3.0.5]# redis-cli  -h 10.0.0.6 -p 6379 get no001
"zhangsan"
[root@LB-111-06 redis-3.0.5]# redis-cli del no001
(integer) 1
[root@LB-111-06 redis-3.0.5]# redis-cli del no001
(integer) 0
[root@LB-111-06 redis-3.0.5]# telnet 127.0.0.1 6379
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
set no003 jisu
+OK
get no003
$4
jisu

2.redis的php客户端扩展安装
下载:https://github.com/nicolasff/phpredis/archive/phpredis-2.2.4.tar.gz
tar zxvf phpredis-2.2.4.tar.gz  #解压
cd phpredis-2.2.4                    #进入安装目录
/application/php/bin/phpize  #生成configure配置文件
./configure --with-php-config=/application/php/bin/php-config
make             #编译
make install  #安装

#安装完成之后,出现下面的安装路径
/application/php5.6.12/lib/php/extensions/no-debug-zts-20131226/

3.配置php支持
vi   #编辑配置文件,在最后一行添加以下内容
添加
extension = redis.so
extension_dir = "/application/php5.6.12/lib/php/extensions/no-debug-zts-20131226/"

4.redis配置文件介绍及主从同步配置:
a、slave配置:
 210 # slaveof <masterip> <masterport>
 211    slaveof  10.0.0.6   6379  #为master的IP及服务端口
[root@LNMP-07 conf]# redis-cli -h localhost -p 6379 monitor  #在slave库打开主从同步实时监控
OK
1448619846.561773 [0 10.0.0.6:6379] "PING"
1448619856.670778 [0 10.0.0.6:6379] "PING"
...............................
1448619947.631375 [0 10.0.0.6:6379] "PING"

b、master操作:
[root@LB-111-06 ~]# redis-cli
127.0.0.1:6379> set t1 oldboy01
OK
127.0.0.1:6379> get t1
"oldboy01"
127.0.0.1:6379> 

此时再查看从库:
。。。。。。。。。。。。。。。
1448619947.631375 [0 10.0.0.6:6379] "PING"
1448619955.222078 [0 10.0.0.6:6379] "SELECT" "0"
1448619955.222147 [0 10.0.0.6:6379] "set" "t1" "oldboy01"

主库连接到从库查看:
[root@LB-111-06 ~]# redis-cli -h 10.0.0.7 get t1
"oldboy01"
[root@LB-111-06 ~]# redis-cli -h localhost -p 6379 info       #查看redis服务的统计信息
# Server
redis_version:3.0.5
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:b358be82c008aa44
redis_mode:standalone
os:Linux 2.6.32-504.el6.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.4.7
process_id:5357
run_id:b93947dbd3db2bc911115f9187d3d3f176861c17
tcp_port:6379
uptime_in_seconds:5665
uptime_in_days:0
hz:10
lru_clock:3551121
config_file:/application/redis/conf/redis.conf

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

# Memory
used_memory:1884728
used_memory_human:1.80M
used_memory_rss:2523136
used_memory_peak:1920672
used_memory_peak_human:1.83M
used_memory_lua:36864
mem_fragmentation_ratio:1.34
mem_allocator:jemalloc-3.6.0

# Persistence
loading:0
rdb_changes_since_last_save:1
rdb_bgsave_in_progress:0
rdb_last_save_time:1446391003
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok

# Stats
total_connections_received:13
total_commands_processed:703
instantaneous_ops_per_sec:1
total_net_input_bytes:25030
total_net_output_bytes:1198
instantaneous_input_kbps:0.04
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:1
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:4
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:310
migrate_cached_sockets:0

# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.7,port=6379,state=online,offset=1011,lag=1
master_repl_offset:1011
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:1010

# CPU
used_cpu_sys:4.19
used_cpu_user:1.95
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Cluster
cluster_enabled:0

# Keyspace
db0:keys=3,expires=0,avg_ttl=0

[root@LB-111-06 ~]# redis-cli -h localhost -p 6379 info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.7,port=6379,state=online,offset=1277,lag=1
master_repl_offset:1277
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:1276

[root@LB-111-06 ~]# redis-cli
127.0.0.1:6379> help info
  INFO [section]
  summary: Get information and statistics about the server
  since: 1.0.0
  group: server

127.0.0.1:6379> info stats
# Stats
total_connections_received:15
total_commands_processed:952
instantaneous_ops_per_sec:1
total_net_input_bytes:34215
total_net_output_bytes:3844
instantaneous_input_kbps:0.04
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:1
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:4
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:162
migrate_cached_sockets:0
127.0.0.1:6379> help
redis-cli 3.0.5
Type: "help @<group>" to get a list of commands in <group>
      "help <command>" for help on <command>
      "help <tab>" to get a list of possible help topics
      "quit" to exit



本文转自 linuxzkq 51CTO博客,原文链接:http://blog.51cto.com/linuxzkq/1735468

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
2月前
|
NoSQL 安全 测试技术
Redis游戏积分排行榜项目中通义灵码的应用实战
Redis游戏积分排行榜项目中通义灵码的应用实战
71 4
|
14天前
|
NoSQL 关系型数据库 Redis
《docker高级篇(大厂进阶):1.Docker复杂安装详说》包括:安装mysql主从复制、安装redis集群
《docker高级篇(大厂进阶):1.Docker复杂安装详说》包括:安装mysql主从复制、安装redis集群
64 14
|
11天前
|
关系型数据库 MySQL 应用服务中间件
《docker基础篇:8.Docker常规安装简介》包括:docker常规安装总体步骤、安装tomcat、安装mysql、安装redis
《docker基础篇:8.Docker常规安装简介》包括:docker常规安装总体步骤、安装tomcat、安装mysql、安装redis
51 7
|
27天前
|
NoSQL 算法 Redis
docker高级篇(大厂进阶):安装redis集群
docker高级篇(大厂进阶):安装redis集群
100 24
|
20天前
|
NoSQL 关系型数据库 MySQL
Linux安装jdk、mysql、redis
Linux安装jdk、mysql、redis
151 7
|
2月前
|
NoSQL Linux PHP
如何在不同操作系统上安装 Redis 服务器,包括 Linux 和 Windows 的具体步骤
本文介绍了如何在不同操作系统上安装 Redis 服务器,包括 Linux 和 Windows 的具体步骤。接着,对比了两种常用的 PHP Redis 客户端扩展:PhpRedis 和 Predis,详细说明了它们的安装方法及优缺点。最后,提供了使用 PhpRedis 和 Predis 在 PHP 中连接 Redis 服务器及进行字符串、列表、集合和哈希等数据类型的基本操作示例。
86 4
|
2月前
|
存储 SQL 关系型数据库
2024Mysql And Redis基础与进阶操作系列(1)作者——LJS[含MySQL的下载、安装、配置详解步骤及报错对应解决方法]
Mysql And Redis基础与进阶操作系列(1)之[MySQL的下载、安装、配置详解步骤及报错对应解决方法]
|
3月前
|
NoSQL 关系型数据库 MySQL
MySQL与Redis协同作战:优化百万数据查询的实战经验
【10月更文挑战第13天】 在处理大规模数据集时,传统的关系型数据库如MySQL可能会遇到性能瓶颈。为了提升数据处理的效率,我们可以结合使用MySQL和Redis,利用两者的优势来优化数据查询。本文将分享一次实战经验,探讨如何通过MySQL与Redis的协同工作来优化百万级数据统计。
134 5
|
3月前
|
缓存 NoSQL Java
Spring Boot与Redis:整合与实战
【10月更文挑战第15天】本文介绍了如何在Spring Boot项目中整合Redis,通过一个电商商品推荐系统的案例,详细展示了从添加依赖、配置连接信息到创建配置类的具体步骤。实战部分演示了如何利用Redis缓存提高系统响应速度,减少数据库访问压力,从而提升用户体验。
192 2
|
2月前
|
NoSQL 编译器 Linux
【赵渝强老师】Redis的安装与访问
本文基于Redis 6.2版本,详细介绍了在CentOS 7 64位虚拟机环境中部署Redis的步骤。内容包括安装GCC编译器、创建安装目录、解压安装包、编译安装、配置文件修改、启动服务及验证等操作。视频讲解和相关图片帮助理解每一步骤。