一、环境
系统 CentOS6.4x64最小化安装
redis-m 192.168.1.13
redis-s 192.168.1.14
二、安装redis
下载软件
[[email protected] ~]# wget http://download.redis.io/releases/redis-2.8.9.tar.gz
安装
[[email protected] ~]# tar xf redis-2.8.9.tar.gz [[email protected] ~]# cd redis-2.8.9 [[email protected] redis-2.8.9]# make [[email protected] redis-2.8.9]# make PREFIX=/usr/local/redis-2.8.9 install [[email protected] redis-2.8.9]# ln -s /usr/local/redis-2.8.9/ /usr/local/redis #默认文件 [[email protected] ~]# ll /usr/local/redis/bin total 13912 -rwxr-xr-x 1 root root 4172184 Jul 26 18:08 redis-benchmark #redis性能测试工具 -rwxr-xr-x 1 root root 22177 Jul 26 18:08 redis-check-aof #对更新日志appendonly.aof检查,是否可用 -rwxr-xr-x 1 root root 45411 Jul 26 18:08 redis-check-dump #用于本地数据库rdb文件的检查 -rwxr-xr-x 1 root root 4265375 Jul 26 18:08 redis-cli #redis命令行操作工具 -rwxr-xr-x 1 root root 5728711 Jul 26 18:08 redis-server #服务器的daemon启动程序
启动redis
#增加环境变量 [[email protected] ~]# echo ‘PATH=$PATH:/usr/local/redis/bin‘ >>/etc/profile [[email protected] ~]# source /etc/profile [[email protected] ~]# which redis-server /usr/local/redis/bin/redis-server [[email protected] ~]# redis-server -h Usage: ./redis-server [/path/to/redis.conf] [options] ./redis-server - (read config from stdin) ./redis-server -v or --version ./redis-server -h or --help ./redis-server --test-memory <megabytes> Examples: ./redis-server (run the server with default conf) ./redis-server /etc/redis/6379.conf ./redis-server --port 7777 ./redis-server --port 7777 --slaveof 127.0.0.1 8888 ./redis-server /etc/myredis.conf --loglevel verbose Sentinel mode: ./redis-server /etc/sentinel.conf --sentinel #启动服务,由于默认是没有配置文件,所以我们从安装目录cp一个配置文件 [[email protected] ~]# mkdir /usr/local/redis/conf [[email protected] ~]# cp /root/redis-2.8.9/redis.conf /usr/local/redis/conf/ #在后台启动 [[email protected] ~]# redis-server /usr/local/redis/conf/redis.conf & [1] 5870 [[email protected] ~]# [5870] 26 Jul 18:24:24.771 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ‘‘-._ _.-`` `. `_. ‘‘-._ Redis 2.8.9 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ‘‘-._ ( ‘ , .-` | `, ) Running in stand alone mode |`-._`-...-` __...-.``-._|‘` _.-‘| Port: 6379 | `-._ `._ / _.-‘ | PID: 5870 `-._ `-._ `-./ _.-‘ _.-‘ |`-._`-._ `-.__.-‘ _.-‘_.-‘| | `-._`-._ _.-‘_.-‘ | http://redis.io `-._ `-._`-.__.-‘_.-‘ _.-‘ |`-._`-._ `-.__.-‘ _.-‘_.-‘| | `-._`-._ _.-‘_.-‘ | `-._ `-._`-.__.-‘_.-‘ _.-‘ `-._ `-.__.-‘ _.-‘ `-._ _.-‘ `-.__.-‘ [5870] 26 Jul 18:24:24.774 # Server started, Redis version 2.8.9 [5870] 26 Jul 18:24:24.774 # 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. [5870] 26 Jul 18:24:24.774 * The server is now ready to accept connections on port 6379 [[email protected] ~]# netstat -tunlp |grep redis tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 5870/redis-server * tcp 0 0 :::6379 :::* LISTEN 5870/redis-server * #解决warning警告 [[email protected] ~]# sysctl vm.overcommit_memory=1 vm.overcommit_memory = 1 [[email protected] ~]# echo "vm.overcommit_memory = 1" >>/etc/sysctl.conf [[email protected] ~]# sysctl -p #关闭redis服务 [[email protected] ~]# redis-cli shutdown [5870] 26 Jul 18:32:13.908 # User requested shutdown... [5870] 26 Jul 18:32:13.908 * Saving the final RDB snapshot before exiting. [5870] 26 Jul 18:32:13.926 * DB saved on disk [5870] 26 Jul 18:32:13.926 # Redis is now ready to exit, bye bye... [1]+ Done redis-server /usr/local/redis/conf/redis.conf [[email protected] ~]# netstat -tunlp |grep redis
redis启动脚本
[[email protected] ~]# cat redis.sh #!/bin/bash # chkconfig: 2345 50 30 # # description: Redis service # #Script:Redis command Redisserver=/usr/local/redis/bin/redis-server Rediscli=/usr/local/redis/bin/redis-cli Redisconf=/usr/local/redis/conf/redis.conf function_start() { printf "start redis-server..." $Redisserver $Redisconf &>/dev/null & if [ $? -eq 0 ];then echo "runing" fi } function_stop() { printf "stop redis-server..." $Rediscli -p 6379 shutdown if [ $? -eq 0 ];then echo "stop" fi } function_restart() { function_start function_stop } function_kill() { killall redis-server } function_status() { a=`ps -A|grep "redis-server\>" -c` if [ $a -ge 1 ];then echo -e "The Redis is [\e[0;32;5m runing \e[0m]" else echo -e "The Redis is [\e[0;31;5m not run \e[0m]" fi } case "$1" in start) function_start ;; stop) function_stop ;; restart) function_stop function_start ;; kill) function_kill ;; status) function_status ;; *) echo "Usage: /etc/init.d/redis {start|stop|restart|kill|status}" esac exit
三、操作redis
[[email protected] ~]# redis-cli #添加一个值set key value 127.0.0.1:6379> set id 001 OK 127.0.0.1:6379> get id "001" 127.0.0.1:6379> del id (integer) 1 #返回1,表示操作成功 127.0.0.1:6379> get id (nil) 127.0.0.1:6379> exists id #判断一个key是否存在 (integer) 0 #返回0表示不存在 #切换数据库,默认有16个库,标号从0-15,这里我们切换到1号库 127.0.0.1:6379> select 1 OK 127.0.0.1:6379[1]> keys * (empty list or set) 127.0.0.1:6379[1]> set name lyao OK 127.0.0.1:6379[1]> get name "lyao" #切回0号库 127.0.0.1:6379[1]> select 0 OK 127.0.0.1:6379> get name #这个key只在1号库中才有 (nil) #其它的连接方法 [[email protected] ~]# redis-cli -h 192.168.1.13 -p 6379 192.168.1.13:6379> -h #指定主机地址 -p #指定端口号 #redis使用帮助 [[email protected] ~]# redis-cli 127.0.0.1:6379> help redis-cli 2.8.9 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 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> help @string APPEND key value summary: Append a value to a key since: 2.0.0 BITCOUNT key [start] [end] summary: Count set bits in a string since: 2.6.0 BITOP operation destkey key [key ...] summary: Perform bitwise operations between strings since: 2.6.0 DECR key summary: Decrement the integer value of a key by one since: 1.0.0 DECRBY key decrement summary: Decrement the integer value of a key by the given number since: 1.0.0 GET key summary: Get the value of a key since: 1.0.0 GETBIT key offset summary: Returns the bit value at offset in the string value stored at key since: 2.2.0 GETRANGE key start end summary: Get a substring of the string stored at a key since: 2.4.0 GETSET key value summary: Set the string value of a key and return its old value since: 1.0.0 INCR key summary: Increment the integer value of a key by one since: 1.0.0 INCRBY key increment summary: Increment the integer value of a key by the given amount since: 1.0.0 INCRBYFLOAT key increment summary: Increment the float value of a key by the given amount since: 2.6.0 MGET key [key ...] summary: Get the values of all the given keys since: 1.0.0 MSET key value [key value ...] summary: Set multiple keys to multiple values since: 1.0.1 MSETNX key value [key value ...] summary: Set multiple keys to multiple values, only if none of the keys exist since: 1.0.1 PSETEX key milliseconds value summary: Set the value and expiration in milliseconds of a key since: 2.6.0 SET key value [EX seconds] [PX milliseconds] [NX|XX] summary: Set the string value of a key since: 1.0.0 SETBIT key offset value summary: Sets or clears the bit at offset in the string value stored at key since: 2.2.0 SETEX key seconds value summary: Set the value and expiration of a key since: 2.0.0 SETNX key value summary: Set the value of a key, only if the key does not exist since: 1.0.0 SETRANGE key offset value summary: Overwrite part of a string at key starting at the specified offset since: 2.2.0 STRLEN key summary: Get the length of the value stored in a key since: 2.2.0
四、redis权限
redis为客户端设置外部连接密码
[[email protected] ~]# vim /usr/local/redis/conf/redis.conf requirepass lyao36843 #相关说明大概在326行,每秒能进行150k次数的密码尝试 #重启redis服务 [[email protected] ~]# redis-cli shutdown [5892] 26 Jul 19:49:03.756 # User requested shutdown... [5892] 26 Jul 19:49:03.756 * Saving the final RDB snapshot before exiting. [5892] 26 Jul 19:49:03.789 * DB saved on disk [5892] 26 Jul 19:49:03.789 # Redis is now ready to exit, bye bye... [1]+ Done redis-server /usr/local/redis/conf/redis.conf [[email protected] ~]# redis-server /usr/local/redis/conf/redis.conf & [[email protected] ~]# redis-cli 127.0.0.1:6379> set k v (error) NOAUTH Authentication required. #提示权限不够 #设置密码后,在进行命令操作之前,需要进行auth认证 127.0.0.1:6379> auth lyao36843 OK 127.0.0.1:6379> set k v OK 127.0.0.1:6379> get k "v" #能够正常增加值 #另外一种连接方式 [[email protected] ~]# redis-cli -a lyao36843 127.0.0.1:6379> set k1 v1 OK 127.0.0.1:6379> get k1 "v1"
五、redis数据类型
数据类型有string,list,set,hash这几种
string类型
127.0.0.1:6379> set mykey "my binary safe value" OK 127.0.0.1:6379> get mykey "my binary safe value" #用来计数 127.0.0.1:6379> set counter 1 OK 127.0.0.1:6379> incr counter #自增 (integer) 2 127.0.0.1:6379> incr counter (integer) 3 127.0.0.1:6379> incr counter (integer) 4 127.0.0.1:6379> decr counter #自减 (integer) 3 127.0.0.1:6379> decr counter (integer) 2 #getset用法 127.0.0.1:6379> set name lyao OK 127.0.0.1:6379> getset name lyao36843 #先获取name的值,然后将name的值更新为lyao36843 "lyao" 127.0.0.1:6379> get name "lyao36843" #批量操作 127.0.0.1:6379> mset name1 tom age 26 sex male OK 127.0.0.1:6379> mget name1 age sex 1) "tom" 2) "26" 3) "male" #给值追加内容 127.0.0.1:6379> get name1 "tom" 127.0.0.1:6379> append name1 "teacher" (integer) 10 127.0.0.1:6379> get name1 "tomteacher"
六、redis多实例
[[email protected] ~]# mkdir -p /data/6380/data [[email protected] ~]# mkdir -p /data/6381/data [[email protected] ~]# cp /usr/local/redis/conf/redis.conf /data/6380 [[email protected] ~]# cp /usr/local/redis/conf/redis.conf /data/6381 #编辑6380的配置文件 [[email protected] ~]# vim /data/6380/redis.conf dir /data/6380/data #修改dump文件的数据目录 port 6380 #修改端口号 pidfile /data/6380/redis.pid #修改pid文件路径 #编辑6381的配置文件 [[email protected] ~]# vim /data/6381/redis.conf pidfile /data/6381/redis.pid port 6381 dir /data/6381/data #启动多实例 [[email protected] ~]# redis-server /data/6380/redis.conf & [[email protected] ~]# redis-server /data/6381/redis.conf & #查看端口 [[email protected] ~]# netstat -tunlp | grep redis tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 5956/redis-server * tcp 0 0 0.0.0.0:6380 0.0.0.0:* LISTEN 6156/redis-server * tcp 0 0 0.0.0.0:6381 0.0.0.0:* LISTEN 6161/redis-server * tcp 0 0 :::6379 :::* LISTEN 5956/redis-server * tcp 0 0 :::6380 :::* LISTEN 6156/redis-server * tcp 0 0 :::6381 :::* LISTEN 6161/redis-server * #从上面结果我们能看到,6379,6380,6381这3个redis实例都正常运行起来了 #连接到6380实例 [[email protected] ~]# redis-cli -p 6380 127.0.0.1:6380> auth lyao36843 OK 127.0.0.1:6380> set name k1 OK 127.0.0.1:6380> get name "k1" 127.0.0.1:6380> quit #连接到6381实例 [[email protected] ~]# redis-cli -p 6381 127.0.0.1:6381> auth lyao36843 OK 127.0.0.1:6381> set web httpd OK 127.0.0.1:6381> get web "httpd" 127.0.0.1:6381> quit
时间: 2024-10-18 20:00:50