Redis     Remote  Dictionary Server(远程字典服务器)-www.redis.cn中文网站

高性能的分布式内存数据库,支持数据持久化(可以把内存保存到硬盘),list,hash,set,zset等数据类型和master-slave模式数据备份

Redis服务器搭建

安装源码包redis-4.0.8.tar.gz(基于c语言编译,需要先安装gcc)

[root@mysql50 ~]# yum -y install gcc
[root@mysql50 ~]# tar -zxf redis-4.0.8.tar.gz 
[root@mysql50 ~]# cd redis-4.0.8/
[root@mysql50 redis-4.0.8]# make
cd src && make all
make[1]: 进入目录“/root/redis-4.0.8/src”
    CC Makefile.dep
....
 
    INSTALL redis-check-rdb
    INSTALL redis-check-aof

Hint: It\'s a good idea to run \'make test\' ;)

make[1]: 离开目录“/root/redis-4.0.8/src”
[root@mysql50 redis-4.0.8]# make install
cd src && make install
     ...
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: 离开目录“/root/redis-4.0.8/src”

初始化配置 (端口,主配置文件,日志文件,数据库目录,启动服务程序,命令行链接工具)

  1. Port                            : 6379                                                                        
  2. Config file                 : /etc/redis/6379.conf              
  3. Log file                      : /var/log/redis_6379.log
  4. Data dir                     : /var/lib/redis/6379
  5. Executable               : /usr/local/bin/redis-server
  6. Cli Executable         : /usr/local/bin/redis-cli
[root@mysql50 redis-4.0.8]# ./utils/install_server.sh      //初始化
Welcome to the redis service installer
This   will help you easily set up a running redis server

Please select the redis port for this instance: [6379]       
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] 
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379           //服务启动脚本
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...                                  //服务已经开启
Installation successful!

查看服务状态信息及启动,停止

[root@mysql50 redis-4.0.8]# netstat -pantul | grep 6379
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN  5025/redis-server 1 
[root@mysql50 redis-4.0.8]# ps -C redis-server
  PID TTY          TIME CMD
 5025 ?        00:00:00 redis-server
[root@mysql50 redis-4.0.8]# /etc/init.d/redis_6379 status
Redis is running (5025)

[root@mysql50 redis-4.0.8]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@mysql50 redis-4.0.8]# /etc/init.d/redis_6379 status
cat: /var/run/redis_6379.pid: 没有那个文件或目录
Redis is running ()

[root@mysql50 redis-4.0.8]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@mysql50 redis-4.0.8]# /etc/init.d/redis_6379 status
Redis is running (5421)

链接redis

[root@mysql50 redis-4.0.8]# redis-cli 
127.0.0.1:6379> ping 
PONG                                       //返回值为PONG正常
127.0.0.1:6379> exit

数据管理命令

[root@mysql50 redis-4.0.8]# redis-cli 
127.0.0.1:6379> set key value [EX seconds] [PX milliseconds] [NX|XX]  //存储
127.0.0.1:6379[1]> set test 123
OK
127.0.0.1:6379[1]> get key             //获取键值
127.0.0.1:6379[1]> select index        //数据库编号0-15 切换数据库
127.0.0.1:6379[1]> keys pattern        //* 打印所有 te?? 打印指定变量,?通配符
127.0.0.1:6379[1]> EXISTS key [key ...]  //检查是否存在
127.0.0.1:6379[1]> ttl key                //查看生存时间
127.0.0.1:6379[1]> type key               //查看类型
127.0.0.1:6379[1]> move key db            //移动记录到某个库
127.0.0.1:6379> expire key seconds         //设置生存时间
127.0.0.1:6379> del key [key ...]         //删除键值
127.0.0.1:6379> fullshall                  //删除所有
127.0.0.1:6379> flushdb                    //删除当前库的内容
127.0.0.1:6379> save                       //保存
127.0.0.1:6379> SHUTDOWN [NOSAVE|SAVE]     //关闭服务,默认save

配置选项

[root@mysql50 ~]# vim /etc/redis/6379.conf   //进入打开行号
   ...
  12 # 1k => 1000 bytes                       //数据单位
  13 # 1kb => 1024 bytes              
  14 # 1m => 1000000 bytes
  15 # 1mb => 1024*1024 bytes
  16 # 1g => 1000000000 bytes
  17 # 1gb => 1024*1024*1024 bytes
    ...
 ################### NETWORK ###############
  70 bind 127.0.0.1                           //IP地址
  93 port 6379                                //端口
 102 tcp-backlog 511                          //tcp链接总数
 114 timeout 0                                //链接超时时间
 131 tcp-keepalive 300                        //长链接时间
 #################### GENERAL #################
 137 daemonize yes                             //守护进程方式运行
 172 logfile /var/log/redis_6379.log           //pid文件
 187 data s 16                              //数据库个数
 #################### SNAPSHOTTING  ########### 
 264 dir /var/lib/redis/6379                   //数据库目录
 ################### CLIENTS ################
 533 # maxclients 10000                        //并发链接数量
 ################## MEMORY MANAGEMENT ###########
 560 # maxmemory <bytes>                       //最大内存
 565 # volatile-lru -> Evict using approximated LRU among the keys with an expir     e set.                                         //最近最少使用(针对设置了ttl的key)
 566 # allkeys-lru -> Evict any key using approximated LRU.  //删除最少使用的key
 569 # volatile-random -> Remove a random key among the ones with an expire set. //在设置了ttl的key里随机删除
 570 # allkeys-random -> Remove a random key, any key.  //随机删除key
 571 # volatile-ttl -> Remove the key with the nearest expire time (minor TTL)  //移除最近过期的key
 572 # noeviction -> Don\'t evict anything, just return an error on write operati     ons.  //不删除,写满时报错
 591 # maxmemory-policy noeviction                   //定义使用策略
 602 # maxmemory-samples 5                           //选取模板数据的个数(针对lru和ttl策略

 

收藏 打印