1.redis简介
REmote DIctionary Server(Redis)是一个几乎key-value键值对的持久化数据库存储系统。redis和大名鼎鼎的Memcached缓存服务很像,但是redis支持的数据存储类型更丰富,包括string(字符串)、list(链表)、set(集合)和zset(有序集合)等。
这些数据类型都支持push/pop、add/remove及取交集、并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached缓存服务一样,为了保证效率,数据都是缓存在内存中提供服务。和memcached不同的是,redis持久化服务还会周期的把更新的数据写入到磁盘以及把修改的操作记录追加到文件里记录下来,比memcached更有优势的是,redis还支持master-slave(主从)同步,这点很类似关系型数据库MySQL。
redis的出现,再一定程度上弥补了memcached这类key-value内存缓存的不足。在部分场合可以对关系型数据库起到很好的补充作用,redis提供了Python、Ruby、Erlang、PHP客户端,使用很方便。redis官方文档如下:
http://www.redis.io/documentation
2 Redis 的特点
1)性能很高:redis能支持超过100K+每秒的读写频率
2)丰富的数据类型:redis支持二进制的Strings、Lists、Hashes、Sets及Ordered Sets数据类型操作
3)原子:Redis的所有操作都是原子性的,同时redis还支持对几个操作合并后的原子执行
4)丰富的特性:Redis还支持publish/subscribe,通知,key过期等特性
5)redis支持异机主从复制
6)可以持久化存储数据,与memcached不同
3 Redis的数据类型
Redis最为常见的数据类型主要为有以下五种:
1)String
2)Hash
3)List
4)Set
5)Sorted set
4 redis的应用场景
传统的MySQL + Memcached 的网站架构遇到的问题:
MySQL数据库实际上适合进行海量数据存储的,加上通过Memcached将热点数据存放到内存cache里,达到加速数据访问的目的,绝大部分公司曾经使用过这样的架构,但随着业务数据的量的不断增加,和访问量的持续增长,很多问题就会暴露出来:
1)需要不断的对MySQL进行拆库拆表,Memcached也需要不断跟着扩容,扩容和维护工作占据大量开发运维时间
2)memcached与MySQL数据库一直性问题是个老大难。
3)memcached数据命中率低或者down机,会导致大量访问直接穿透到数据库,导致MySQL无法支撑访问
4)跨机房cache同步一致性问题
redis 的最佳应用场景:
1)Redis 最佳使用场景是全部数据in-memory
2)Redis更多场景是作为Memcached的替代品来使用
3)当需要除key/value之外的更多数据类型支持时,使用Redis更合适。
4)当存储的数据不能被剔除时,使用Redis更合适。
5) 需要负载均衡的场景(主从同步)
更多Redis作者谈Redis应用场景:http://blog.nosqlfan.com/html/2235.html
业务场景:
1、使用Redis bitmap进行活跃用户统计
http://blog.nosqlfan.com/html/3501.html
这里对Redis数据库做个小结
1)提高了DB的可扩展性,只需要将新加的数据放到新加的服务器上就开了
2)提高了DB的可用性,只影响到需要访问的shard服务器上的数据用户
3)提高了DB的可维护性,对系统的升级和配置可以按shard一个个来搞,对服务产生的影响较小
4)小的数据库的查询压力小,查询更快,性能更好
使用过程中的一些经验与教训,做个小结:
1)要进行Master-slave配置,出现服务器故障时可以直接切换。
2)在master侧禁用数据持久化,只需要在slave上配置数据持久化
3)物理内存+虚拟内存不足,这个时候dump一直死着,时间久了机器挂掉。这个情况就是灾难
4)当Redis物理内存使用超过内存总量的3/5时就会开始比较危险了,就开始做swap,内存碎片大
5)当达到最大内存时,会清空带有过期时间的key,即使key未到过期时间
6)redis与DB同步写的问题,先写DB,然后再写redis,因为写内存基本上没问题
操作系统:
1 [root@localhost ~]# cat /etc/redhat-release 2 CentOS Linux release 7.2.1511 (Core) 3 [root@localhost ~]# uname -a 4 Linux localhost.localdomain 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux 5 [root@localhost ~]#
下载redis安装包,这里用redis-3.2.12:
1 [root@localhost tools]# ls -ald redis-3.2.12* 2 -rw-r--r-- 1 root root 1551468 Jul 24 20:29 redis-3.2.12.tar.gz 3 [root@localhost tools]#
解压编译安装
1 [root@localhost tools]# tar xf redis-3.2.12.tar.gz 2 [root@localhost tools]# cd redis-3.2.12/ 3 [root@localhost redis-3.2.12]# cat README.md 4 This README is just a fast *quick start* document. You can find more detailed documentation at http://redis.io. 5 6 What is Redis? 7 -------------- 8 9 Redis is often referred as a *data structures* server. What this means is that Redis provides access to mutable data structures via a set of commands, which are sent using a *server-client* model with TCP sockets and a simple protocol. So different processes can query and modify the same data structures in a shared way. 10 11 Data structures implemented into Redis have a few special properties: 12 13 * Redis cares to store them on disk, even if they are always served and modified into the server memory. This means that Redis is fast, but that is also non-volatile. 14 * Implementation of data structures stress on memory efficiency, so data structures inside Redis will likely use less memory compared to the same data structure modeled using an high level programming language. 15 * Redis offers a number of features that are natural to find in a database, like replication, tunable levels of durability, cluster, high availability. 16 17 Another good example is to think of Redis as a more complex version of memcached, where the operations are not just SETs and GETs, but operations to work with complex data types like Lists, Sets, ordered data structures, and so forth. 18 19 If you want to know more, this is a list of selected starting points: 20 21 * Introduction to Redis data types. http://redis.io/topics/data-types-intro 22 * Try Redis directly inside your browser. http://try.redis.io 23 * The full list of Redis commands. http://redis.io/commands 24 * There is much more inside the Redis official documentation. http://redis.io/documentation 25 26 Building Redis 27 -------------- 28 29 Redis can be compiled and used on Linux, OSX, OpenBSD, NetBSD, FreeBSD. 30 We support big endian and little endian architectures, and both 32 bit 31 and 64 bit systems. 32 33 It may compile on Solaris derived systems (for instance SmartOS) but our 34 support for this platform is *best effort* and Redis is not guaranteed to 35 work as well as in Linux, OSX, and *BSD there. 36 37 It is as simple as: 38 39 % make 40 41 You can run a 32 bit Redis binary using: 42 43 % make 32bit 44 45 After building Redis is a good idea to test it, using: 46 47 % make test 48 49 Fixing build problems with dependencies or cached build options 50 --------- 51 52 Redis has some dependencies which are included into the `deps` directory. 53 `make` does not rebuild dependencies automatically, even if something in the 54 source code of dependencies is changed. 55 56 When you update the source code with `git pull` or when code inside the 57 dependencies tree is modified in any other way, make sure to use the following 58 command in order to really clean everything and rebuild from scratch: 59 60 make distclean 61 62 This will clean: jemalloc, lua, hiredis, linenoise. 63 64 Also if you force certain build options like 32bit target, no C compiler 65 optimizations (for debugging purposes), and other similar build time options, 66 those options are cached indefinitely until you issue a `make distclean` 67 command. 68 69 Fixing problems building 32 bit binaries 70 --------- 71 72 If after building Redis with a 32 bit target you need to rebuild it 73 with a 64 bit target, or the other way around, you need to perform a 74 `make distclean` in the root directory of the Redis distribution. 75 76 In case of build errors when trying to build a 32 bit binary of Redis, try 77 the following steps: 78 79 * Install the packages libc6-dev-i386 (also try g++-multilib). 80 * Try using the following command line instead of `make 32bit`: 81 `make CFLAGS="-m32 -march=native" LDFLAGS="-m32"` 82 83 Allocator 84 --------- 85 86 Selecting a non-default memory allocator when building Redis is done by setting 87 the `MALLOC` environment variable. Redis is compiled and linked against libc 88 malloc by default, with the exception of jemalloc being the default on Linux 89 systems. This default was picked because jemalloc has proven to have fewer 90 fragmentation problems than libc malloc. 91 92 To force compiling against libc malloc, use: 93 94 % make MALLOC=libc 95 96 To compile against jemalloc on Mac OS X systems, use: 97 98 % make MALLOC=jemalloc 99 100 Verbose build 101 ------------- 102 103 Redis will build with a user friendly colorized output by default. 104 If you want to see a more verbose output use the following: 105 106 % make V=1 107 108 Running Redis 109 ------------- 110 111 To run Redis with the default configuration just type: 112 113 % cd src 114 % ./redis-server 115 116 If you want to provide your redis.conf, you have to run it using an additional 117 parameter (the path of the configuration file): 118 119 % cd src 120 % ./redis-server /path/to/redis.conf 121 122 It is possible to alter the Redis configuration passing parameters directly 123 as options using the command line. Examples: 124 125 % ./redis-server --port 9999 --slaveof 127.0.0.1 6379 126 % ./redis-server /etc/redis/6379.conf --loglevel debug 127 128 All the options in redis.conf are also supported as options using the command 129 line, with exactly the same name. 130 131 Playing with Redis 132 ------------------ 133 134 You can use redis-cli to play with Redis. Start a redis-server instance, 135 then in another terminal try the following: 136 137 % cd src 138 % ./redis-cli 139 redis> ping 140 PONG 141 redis> set foo bar 142 OK 143 redis> get foo 144 "bar" 145 redis> incr mycounter 146 (integer) 1 147 redis> incr mycounter 148 (integer) 2 149 redis> 150 151 You can find the list of all the available commands at http://redis.io/commands. 152 153 Installing Redis 154 ----------------- 155 156 In order to install Redis binaries into /usr/local/bin just use: 157 158 % make install 159 160 You can use `make PREFIX=/some/other/directory install` if you wish to use a 161 different destination. 162 163 Make install will just install binaries in your system, but will not configure 164 init scripts and configuration files in the appropriate place. This is not 165 needed if you want just to play a bit with Redis, but if you are installing 166 it the proper way for a production system, we have a script doing this 167 for Ubuntu and Debian systems: 168 169 % cd utils 170 % ./install_server.sh 171 172 The script will ask you a few questions and will setup everything you need 173 to run Redis properly as a background daemon that will start again on 174 system reboots. 175 176 You'll be able to stop and start Redis using the script named 177 `/etc/init.d/redis_<portnumber>`, for instance `/etc/init.d/redis_6379`. 178 179 Code contributions 180 --- 181 182 Note: by contributing code to the Redis project in any form, including sending 183 a pull request via Github, a code fragment or patch via private email or 184 public discussion groups, you agree to release your code under the terms 185 of the BSD license that you can find in the [COPYING][1] file included in the Redis 186 source distribution. 187 188 Please see the [CONTRIBUTING][2] file in this source distribution for more 189 information. 190 191 Enjoy! 192 193 [1]: https://github.com/antirez/redis/blob/unstable/COPYING 194 [2]: https://github.com/antirez/redis/blob/unstable/CONTRIBUTING 195 [root@localhost redis-3.2.12]#
开始编译:centos7系统,编译参数“make MALLOC=jemalloc”
1 make MALLOC=jemalloc 2 [root@localhost redis-3.2.12]# make MALLOC=jemalloc 3 cd src && make all 4 make[1]: Entering directory `/root/tools/redis-3.2.12/src' 5 rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html 6 (cd ../deps && make distclean) 7 make[2]: Entering directory `/root/tools/redis-3.2.12/deps' 8 (cd hiredis && make clean) > /dev/null || true 9 (cd linenoise && make clean) > /dev/null || true 10 (cd lua && make clean) > /dev/null || true 11 (cd geohash-int && make clean) > /dev/null || true 12 (cd jemalloc && [ -f Makefile ] && make distclean) > /dev/null || true 13 (rm -f .make-*) 14 make[2]: Leaving directory `/root/tools/redis-3.2.12/deps' 15 (rm -f .make-*) 16 echo STD=-std=c99 -pedantic -DREDIS_STATIC='' >> .make-settings 17 echo WARN=-Wall -W >> .make-settings 18 echo OPT=-O2 >> .make-settings 19 echo MALLOC=jemalloc >> .make-settings 20 echo CFLAGS= >> .make-settings 21 echo LDFLAGS= >> .make-settings 22 echo REDIS_CFLAGS= >> .make-settings 23 echo REDIS_LDFLAGS= >> .make-settings 24 echo PREV_FINAL_CFLAGS=-std=c99 -pedantic -DREDIS_STATIC='' -Wall -W -O2 -g -ggdb -I../deps/geohash-int -I../deps/hiredis -I../deps/linenoise -I../deps/lua/src -DUSE_JEMALLOC -I../deps/jemalloc/include >> .make-settings 25 echo PREV_FINAL_LDFLAGS= -g -ggdb -rdynamic >> .make-settings 26 (cd ../deps && make hiredis linenoise lua geohash-int jemalloc) 27 make[2]: Entering directory `/root/tools/redis-3.2.12/deps' 28 (cd hiredis && make clean) > /dev/null || true 29 (cd linenoise && make clean) > /dev/null || true 30 (cd lua && make clean) > /dev/null || true 31 (cd geohash-int && make clean) > /dev/null || true 32 (cd jemalloc && [ -f Makefile ] && make distclean) > /dev/null || true 33 (rm -f .make-*) 34 (echo "" > .make-cflags) 35 (echo "" > .make-ldflags) 36 MAKE hiredis 37 cd hiredis && make static 38 make[3]: Entering directory `/root/tools/redis-3.2.12/deps/hiredis' 39 cc -std=c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb net.c 40 cc -std=c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb hiredis.c 41 cc -std=c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb sds.c 42 cc -std=c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb async.c 43 ar rcs libhiredis.a net.o hiredis.o sds.o async.o 44 make[3]: Leaving directory `/root/tools/redis-3.2.12/deps/hiredis' 45 MAKE linenoise 46 cd linenoise && make 47 make[3]: Entering directory `/root/tools/redis-3.2.12/deps/linenoise' 48 cc -Wall -Os -g -c linenoise.c 49 make[3]: Leaving directory `/root/tools/redis-3.2.12/deps/linenoise' 50 MAKE lua 51 cd lua/src && make all CFLAGS="-O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' " MYLDFLAGS="" AR="ar rcu" 52 make[3]: Entering directory `/root/tools/redis-3.2.12/deps/lua/src' 53 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o lapi.o lapi.c 54 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o lcode.o lcode.c 55 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o ldebug.o ldebug.c 56 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o ldo.o ldo.c 57 ldo.c: In function ‘f_parser’: 58 ldo.c:496:7: warning: unused variable ‘c’ [-Wunused-variable] 59 int c = luaZ_lookahead(p->z); 60 ^ 61 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o ldump.o ldump.c 62 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o lfunc.o lfunc.c 63 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o lgc.o lgc.c 64 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o llex.o llex.c 65 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o lmem.o lmem.c 66 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o lobject.o lobject.c 67 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o lopcodes.o lopcodes.c 68 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o lparser.o lparser.c 69 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o lstate.o lstate.c 70 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o lstring.o lstring.c 71 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o ltable.o ltable.c 72 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o ltm.o ltm.c 73 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o lundump.o lundump.c 74 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o lvm.o lvm.c 75 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o lzio.o lzio.c 76 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o strbuf.o strbuf.c 77 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o fpconv.o fpconv.c 78 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o lauxlib.o lauxlib.c 79 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o lbaselib.o lbaselib.c 80 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o ldblib.o ldblib.c 81 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o liolib.o liolib.c 82 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o lmathlib.o lmathlib.c 83 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o loslib.o loslib.c 84 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o ltablib.o ltablib.c 85 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o lstrlib.o lstrlib.c 86 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o loadlib.o loadlib.c 87 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o linit.o linit.c 88 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o lua_cjson.o lua_cjson.c 89 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o lua_struct.o lua_struct.c 90 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o lua_cmsgpack.o lua_cmsgpack.c 91 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o lua_bit.o lua_bit.c 92 ar rcu liblua.a lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o lundump.o lvm.o lzio.o strbuf.o fpconv.o lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o lstrlib.o loadlib.o linit.o lua_cjson.o lua_struct.o lua_cmsgpack.o lua_bit.o # DLL needs all object files 93 ranlib liblua.a 94 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o lua.o lua.c 95 cc -o lua lua.o liblua.a -lm 96 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o luac.o luac.c 97 cc -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -c -o print.o print.c 98 cc -o luac luac.o print.o liblua.a -lm 99 make[3]: Leaving directory `/root/tools/redis-3.2.12/deps/lua/src' 100 MAKE geohash-int 101 cd geohash-int && make 102 make[3]: Entering directory `/root/tools/redis-3.2.12/deps/geohash-int' 103 cc -Wall -O2 -g -c geohash.c 104 cc -Wall -O2 -g -c geohash_helper.c 105 make[3]: Leaving directory `/root/tools/redis-3.2.12/deps/geohash-int' 106 MAKE jemalloc 107 cd jemalloc && ./configure --with-lg-quantum=3 --with-jemalloc-prefix=je_ --enable-cc-silence CFLAGS="-std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops " LDFLAGS="" 108 checking for xsltproc... /usr/bin/xsltproc 109 checking for gcc... gcc 110 checking whether the C compiler works... yes 111 checking for C compiler default output file name... a.out 112 checking for suffix of executables... 113 checking whether we are cross compiling... no 114 checking for suffix of object files... o 115 checking whether we are using the GNU C compiler... yes 116 checking whether gcc accepts -g... yes 117 checking for gcc option to accept ISO C89... none needed 118 checking how to run the C preprocessor... gcc -E 119 checking for grep that handles long lines and -e... /usr/bin/grep 120 checking for egrep... /usr/bin/grep -E 121 checking for ANSI C header files... yes 122 checking for sys/types.h... yes 123 checking for sys/stat.h... yes 124 checking for stdlib.h... yes 125 checking for string.h... yes 126 checking for memory.h... yes 127 checking for strings.h... yes 128 checking for inttypes.h... yes 129 checking for stdint.h... yes 130 checking for unistd.h... yes 131 checking whether byte ordering is bigendian... no 132 checking size of void *... 8 133 checking size of int... 4 134 checking size of long... 8 135 checking size of intmax_t... 8 136 checking build system type... x86_64-unknown-linux-gnu 137 checking host system type... x86_64-unknown-linux-gnu 138 checking whether pause instruction is compilable... yes 139 checking for ar... ar 140 checking malloc.h usability... yes 141 checking malloc.h presence... yes 142 checking for malloc.h... yes 143 checking whether malloc_usable_size definition can use const argument... no 144 checking whether __attribute__ syntax is compilable... yes 145 checking whether compiler supports -fvisibility=hidden... yes 146 checking whether compiler supports -Werror... yes 147 checking whether tls_model attribute is compilable... yes 148 checking whether compiler supports -Werror... yes 149 checking whether alloc_size attribute is compilable... yes 150 checking whether compiler supports -Werror... yes 151 checking whether format(gnu_printf, ...) attribute is compilable... yes 152 checking whether compiler supports -Werror... yes 153 checking whether format(printf, ...) attribute is compilable... yes 154 checking for a BSD-compatible install... /usr/bin/install -c 155 checking for ranlib... ranlib 156 checking for ld... /usr/bin/ld 157 checking for autoconf... /usr/bin/autoconf 158 checking for memalign... yes 159 checking for valloc... yes 160 checking configured backtracing method... N/A 161 checking for sbrk... yes 162 checking whether utrace(2) is compilable... no 163 checking whether valgrind is compilable... no 164 checking whether a program using __builtin_ffsl is compilable... yes 165 checking LG_PAGE... 12 166 checking pthread.h usability... yes 167 checking pthread.h presence... yes 168 checking for pthread.h... yes 169 checking for pthread_create in -lpthread... yes 170 checking for library containing clock_gettime... none required 171 checking for secure_getenv... yes 172 checking for issetugid... no 173 checking for _malloc_thread_cleanup... no 174 checking for _pthread_mutex_init_calloc_cb... no 175 checking for TLS... yes 176 checking whether C11 atomics is compilable... no 177 checking whether atomic(9) is compilable... no 178 checking whether Darwin OSAtomic*() is compilable... no 179 checking whether madvise(2) is compilable... yes 180 checking whether to force 32-bit __sync_{add,sub}_and_fetch()... no 181 checking whether to force 64-bit __sync_{add,sub}_and_fetch()... no 182 checking for __builtin_clz... yes 183 checking whether Darwin OSSpin*() is compilable... no 184 checking whether glibc malloc hook is compilable... yes 185 checking whether glibc memalign hook is compilable... yes 186 checking whether pthreads adaptive mutexes is compilable... yes 187 checking for stdbool.h that conforms to C99... yes 188 checking for _Bool... yes 189 configure: creating ./config.status 190 config.status: creating Makefile 191 config.status: creating jemalloc.pc 192 config.status: creating doc/html.xsl 193 config.status: creating doc/manpages.xsl 194 config.status: creating doc/jemalloc.xml 195 config.status: creating include/jemalloc/jemalloc_macros.h 196 config.status: creating include/jemalloc/jemalloc_protos.h 197 config.status: creating include/jemalloc/jemalloc_typedefs.h 198 config.status: creating include/jemalloc/internal/jemalloc_internal.h 199 config.status: creating test/test.sh 200 config.status: creating test/include/test/jemalloc_test.h 201 config.status: creating config.stamp 202 config.status: creating bin/jemalloc-config 203 config.status: creating bin/jemalloc.sh 204 config.status: creating bin/jeprof 205 config.status: creating include/jemalloc/jemalloc_defs.h 206 config.status: creating include/jemalloc/internal/jemalloc_internal_defs.h 207 config.status: creating test/include/test/jemalloc_test_defs.h 208 config.status: executing include/jemalloc/internal/private_namespace.h commands 209 config.status: executing include/jemalloc/internal/private_unnamespace.h commands 210 config.status: executing include/jemalloc/internal/public_symbols.txt commands 211 config.status: executing include/jemalloc/internal/public_namespace.h commands 212 config.status: executing include/jemalloc/internal/public_unnamespace.h commands 213 config.status: executing include/jemalloc/internal/size_classes.h commands 214 config.status: executing include/jemalloc/jemalloc_protos_jet.h commands 215 config.status: executing include/jemalloc/jemalloc_rename.h commands 216 config.status: executing include/jemalloc/jemalloc_mangle.h commands 217 config.status: executing include/jemalloc/jemalloc_mangle_jet.h commands 218 config.status: executing include/jemalloc/jemalloc.h commands 219 =============================================================================== 220 jemalloc version : 4.0.3-0-ge9192eacf8935e29fc62fddc2701f7942b1cc02c 221 library revision : 2 222 223 CONFIG : --with-lg-quantum=3 --with-jemalloc-prefix=je_ --enable-cc-silence 'CFLAGS=-std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops ' LDFLAGS= 224 CC : gcc 225 CFLAGS : -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -fvisibility=hidden 226 CPPFLAGS : -D_GNU_SOURCE -D_REENTRANT 227 LDFLAGS : 228 EXTRA_LDFLAGS : 229 LIBS : -lpthread 230 TESTLIBS : 231 RPATH_EXTRA : 232 233 XSLTPROC : /usr/bin/xsltproc 234 XSLROOT : 235 236 PREFIX : /usr/local 237 BINDIR : /usr/local/bin 238 DATADIR : /usr/local/share 239 INCLUDEDIR : /usr/local/include 240 LIBDIR : /usr/local/lib 241 MANDIR : /usr/local/share/man 242 243 srcroot : 244 abs_srcroot : /root/tools/redis-3.2.12/deps/jemalloc/ 245 objroot : 246 abs_objroot : /root/tools/redis-3.2.12/deps/jemalloc/ 247 248 JEMALLOC_PREFIX : je_ 249 JEMALLOC_PRIVATE_NAMESPACE 250 : je_ 251 install_suffix : 252 autogen : 0 253 cc-silence : 1 254 debug : 0 255 code-coverage : 0 256 stats : 1 257 prof : 0 258 prof-libunwind : 0 259 prof-libgcc : 0 260 prof-gcc : 0 261 tcache : 1 262 fill : 1 263 utrace : 0 264 valgrind : 0 265 xmalloc : 0 266 munmap : 0 267 lazy_lock : 0 268 tls : 1 269 cache-oblivious : 1 270 =============================================================================== 271 cd jemalloc && make CFLAGS="-std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops " LDFLAGS="" lib/libjemalloc.a 272 make[3]: Entering directory `/root/tools/redis-3.2.12/deps/jemalloc' 273 gcc -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -c -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -o src/jemalloc.o src/jemalloc.c 274 gcc -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -c -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -o src/arena.o src/arena.c 275 gcc -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -c -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -o src/atomic.o src/atomic.c 276 gcc -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -c -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -o src/base.o src/base.c 277 gcc -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -c -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -o src/bitmap.o src/bitmap.c 278 gcc -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -c -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -o src/chunk.o src/chunk.c 279 gcc -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -c -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -o src/chunk_dss.o src/chunk_dss.c 280 gcc -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -c -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -o src/chunk_mmap.o src/chunk_mmap.c 281 gcc -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -c -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -o src/ckh.o src/ckh.c 282 gcc -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -c -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -o src/ctl.o src/ctl.c 283 gcc -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -c -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -o src/extent.o src/extent.c 284 gcc -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -c -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -o src/hash.o src/hash.c 285 gcc -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -c -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -o src/huge.o src/huge.c 286 gcc -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -c -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -o src/mb.o src/mb.c 287 gcc -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -c -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -o src/mutex.o src/mutex.c 288 gcc -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -c -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -o src/pages.o src/pages.c 289 gcc -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -c -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -o src/prof.o src/prof.c 290 gcc -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -c -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -o src/quarantine.o src/quarantine.c 291 gcc -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -c -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -o src/rtree.o src/rtree.c 292 gcc -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -c -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -o src/stats.o src/stats.c 293 gcc -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -c -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -o src/tcache.o src/tcache.c 294 gcc -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -c -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -o src/util.o src/util.c 295 gcc -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -c -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -o src/tsd.o src/tsd.c 296 ar crus lib/libjemalloc.a src/jemalloc.o src/arena.o src/atomic.o src/base.o src/bitmap.o src/chunk.o src/chunk_dss.o src/chunk_mmap.o src/ckh.o src/ctl.o src/extent.o src/hash.o src/huge.o src/mb.o src/mutex.o src/pages.o src/prof.o src/quarantine.o src/rtree.o src/stats.o src/tcache.o src/util.o src/tsd.o 297 make[3]: Leaving directory `/root/tools/redis-3.2.12/deps/jemalloc' 298 make[2]: Leaving directory `/root/tools/redis-3.2.12/deps' 299 CC adlist.o 300 CC quicklist.o 301 CC ae.o 302 CC anet.o 303 CC dict.o 304 CC server.o 305 CC sds.o 306 CC zmalloc.o 307 CC lzf_c.o 308 CC lzf_d.o 309 CC pqsort.o 310 CC zipmap.o 311 CC sha1.o 312 CC ziplist.o 313 CC release.o 314 CC networking.o 315 CC util.o 316 CC object.o 317 CC db.o 318 CC replication.o 319 CC rdb.o 320 CC t_string.o 321 CC t_list.o 322 CC t_set.o 323 CC t_zset.o 324 CC t_hash.o 325 CC config.o 326 CC aof.o 327 CC pubsub.o 328 CC multi.o 329 CC debug.o 330 CC sort.o 331 CC intset.o 332 CC syncio.o 333 CC cluster.o 334 CC crc16.o 335 CC endianconv.o 336 CC slowlog.o 337 CC scripting.o 338 CC bio.o 339 CC rio.o 340 CC rand.o 341 CC memtest.o 342 CC crc64.o 343 CC bitops.o 344 CC sentinel.o 345 CC notify.o 346 CC setproctitle.o 347 CC blocked.o 348 CC hyperloglog.o 349 CC latency.o 350 CC sparkline.o 351 CC redis-check-rdb.o 352 CC geo.o 353 LINK redis-server 354 INSTALL redis-sentinel 355 CC redis-cli.o 356 LINK redis-cli 357 CC redis-benchmark.o 358 LINK redis-benchmark 359 INSTALL redis-check-rdb 360 CC redis-check-aof.o 361 LINK redis-check-aof 362 363 Hint: It's a good idea to run 'make test' ;) 364 365 make[1]: Leaving directory `/root/tools/redis-3.2.12/src' 366 [root@localhost redis-3.2.12]#
提示:“It's a good idea to run 'make test'”, 可以make test看看结果
1 [root@localhost redis-3.2.12]# make test 2 cd src && make test 3 make[1]: Entering directory `/root/tools/redis-3.2.12/src' 4 You need tcl 8.5 or newer in order to run the Redis test 5 make[1]: *** [test] Error 1 6 make[1]: Leaving directory `/root/tools/redis-3.2.12/src' 7 make: *** [test] Error 2 8 [root@localhost redis-3.2.12]#
提示cd src && make test,好我们就直接切换到目录src下执行make test
1 [root@localhost redis-3.2.12]# cd src/ 2 [root@localhost src]# make test 3 You need tcl 8.5 or newer in order to run the Redis test 4 make: *** [test] Error 1 5 [root@localhost src]#
提示需要tcl 8.5或者更高版本,这里我们先查看下该软件的具体名称版本
1 [root@localhost ~]# yum provides tcl 2 Loaded plugins: fastestmirror 3 Loading mirror speeds from cached hostfile 4 * base: mirrors.aliyun.com 5 * extras: mirrors.aliyun.com 6 * updates: mirrors.aliyun.com 7 1:tcl-8.5.13-8.el7.i686 : Tool Command Language, pronounced tickle 8 Repo : base 9 10 11 12 1:tcl-8.5.13-8.el7.x86_64 : Tool Command Language, pronounced tickle 13 Repo : base 14 15 16 17 [root@localhost ~]# 18 [root@localhost src]# yum info tcl 19 Loaded plugins: fastestmirror 20 Loading mirror speeds from cached hostfile 21 * base: mirrors.aliyun.com 22 * extras: mirrors.aliyun.com 23 * updates: mirrors.aliyun.com 24 Available Packages 25 Name : tcl 26 Arch : i686 27 Epoch : 1 28 Version : 8.5.13 29 Release : 8.el7 30 Size : 1.9 M 31 Repo : base/7/x86_64 32 Summary : Tool Command Language, pronounced tickle 33 URL : http://tcl.sourceforge.net/ 34 License : TCL 35 Description : The Tcl (Tool Command Language) provides a powerful platform for 36 : creating integration applications that tie together diverse 37 : applications, protocols, devices, and frameworks. When paired with the 38 : Tk toolkit, Tcl provides a fastest and powerful way to create 39 : cross-platform GUI applications. Tcl can also be used for a variety 40 : of web-related tasks and for creating powerful command languages for 41 : applications. 42 43 Name : tcl 44 Arch : x86_64 45 Epoch : 1 46 Version : 8.5.13 47 Release : 8.el7 48 Size : 1.9 M 49 Repo : base/7/x86_64 50 Summary : Tool Command Language, pronounced tickle 51 URL : http://tcl.sourceforge.net/ 52 License : TCL 53 Description : The Tcl (Tool Command Language) provides a powerful platform for 54 : creating integration applications that tie together diverse 55 : applications, protocols, devices, and frameworks. When paired with the 56 : Tk toolkit, Tcl provides a fastest and powerful way to create 57 : cross-platform GUI applications. Tcl can also be used for a variety 58 : of web-related tasks and for creating powerful command languages for 59 : applications. 60 61 [root@localhost src]#
从上面发现当前最新的tcl版本时8.5.13,下面执行安装操作:
1 [root@localhost src]# yum install tcl-8.5.13 2 Loaded plugins: fastestmirror 3 Loading mirror speeds from cached hostfile 4 * base: mirrors.aliyun.com 5 * extras: mirrors.aliyun.com 6 * updates: mirrors.aliyun.com 7 Resolving Dependencies 8 --> Running transaction check 9 ---> Package tcl.x86_64 1:8.5.13-8.el7 will be installed 10 --> Finished Dependency Resolution 11 12 Dependencies Resolved 13 14 ======================================================================================================================================================================================================================================== 15 Package Arch Version Repository Size 16 ======================================================================================================================================================================================================================================== 17 Installing: 18 tcl x86_64 1:8.5.13-8.el7 base 1.9 M 19 20 Transaction Summary 21 ======================================================================================================================================================================================================================================== 22 Install 1 Package 23 24 Total download size: 1.9 M 25 Installed size: 4.4 M 26 Is this ok [y/d/N]: y 27 Downloading packages: 28 tcl-8.5.13-8.el7.x86_64.rpm | 1.9 MB 00:00:00 29 Running transaction check 30 Running transaction test 31 Transaction test succeeded 32 Running transaction 33 Installing : 1:tcl-8.5.13-8.el7.x86_64 1/1 34 Verifying : 1:tcl-8.5.13-8.el7.x86_64 1/1 35 36 Installed: 37 tcl.x86_64 1:8.5.13-8.el7 38 39 Complete! 40 [root@localhost src]#
继续执行make test
1 !!! WARNING The following tests failed: 2 3 *** [err]: Slave should be able to synchronize with the master in tests/integration/replication-psync.tcl 4 Replication not started. 5 *** [err]: Cant' start the Redis server 6 CONFIGURATION:notify-keyspace-events KEA 7 daemonize no 8 pidfile /var/run/redis.pid 9 port 21224 10 timeout 0 11 bind 127.0.0.1 12 loglevel verbose 13 logfile '' 14 databases 16 15 latency-monitor-threshold 1 16 save 60 10000 17 rdbcompression yes 18 dbfilename dump.rdb 19 dir ./tests/tmp/server.9677.26 20 slave-serve-stale-data yes 21 appendonly yes 22 appendfsync everysec 23 no-appendfsync-on-rewrite no 24 activerehashing yes 25 ERROR: 26 Cleanup: may take some time... OK 27 make: *** [test] Error 1 28 [root@localhost src]#
继续执行make PREEFIX path install
1 [root@localhost src]# make PREFIX=/application/redis-3.2.12 install 2 3 Hint: It's a good idea to run 'make test' ;) 4 5 INSTALL install 6 INSTALL install 7 INSTALL install 8 INSTALL install 9 INSTALL install 10 [root@localhost src]# 11 [root@localhost src]# tree /application/redis-3.2.12/ 12 /application/redis-3.2.12/ 13 └── bin 14 ├── redis-benchmark 15 ├── redis-check-aof 16 ├── redis-check-rdb 17 ├── redis-cli 18 ├── redis-sentinel -> redis-server 19 └── redis-server 20 21 1 directory, 6 files 22 [root@localhost src]#
创建软连接和配置文件目录,拷贝配置文件到指定路径
1 [root@localhost src]# ln -sv /application/redis-3.2.12/ /application/redis 2 ‘/application/redis’ -> ‘/application/redis-3.2.12/’ 3 [root@localhost src]# echo 'vm.overcommit_memory = 1' >>/etc/sysctl.conf 4 [root@localhost src]# sysctl -p 5 vm.overcommit_memory = 1 6 [root@localhost src]# echo "export PATH=/application/redis/bin:$PATH" >>/etc/profile 7 [root@localhost src]# source /etc/profile 8 [root@localhost src]# mkdir -p /application/redis/conf 9 [root@localhost src]# cd .. 10 [root@localhost redis-3.2.12]# pwd 11 /root/tools/redis-3.2.12 12 [root@localhost redis-3.2.12]# cp redis.conf /application/redis/conf/ 13 [root@localhost redis-3.2.12]#
拷贝后目录结构如下:
1 [root@localhost redis-3.2.12]# tree /application/redis 2 /application/redis 3 ├── bin 4 │ ├── redis-benchmark 5 │ ├── redis-check-aof 6 │ ├── redis-check-rdb 7 │ ├── redis-cli 8 │ ├── redis-sentinel -> redis-server 9 │ └── redis-server 10 └── conf 11 └── redis.conf 12 13 2 directories, 7 files 14 [root@localhost redis-3.2.12]#
根据README.md提示,启动redis直接使用命令:
./redis-server /path/to/redis.conf
或者
./redis-server --port 9999 --slaveof 127.0.0.1 6379
./redis-server /etc/redis/6379.conf --loglevel debug
1 [root@localhost redis]# /application/redis/bin/redis-server /application/redis/conf/redis.conf 2 13308:M 24 Jul 21:42:03.329 * Increased maximum number of open files to 10032 (it was originally set to 1024). 3 _._ 4 _.-``__ ''-._ 5 _.-`` `. `_. ''-._ Redis 3.2.12 (00000000/0) 64 bit 6 .-`` .-```. ```/ _.,_ ''-._ 7 ( ' , .-` | `, ) Running in standalone mode 8 |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 9 | `-._ `._ / _.-' | PID: 13308 10 `-._ `-._ `-./ _.-' _.-' 11 |`-._`-._ `-.__.-' _.-'_.-'| 12 | `-._`-._ _.-'_.-' | http://redis.io 13 `-._ `-._`-.__.-'_.-' _.-' 14 |`-._`-._ `-.__.-' _.-'_.-'| 15 | `-._`-._ _.-'_.-' | 16 `-._ `-._`-.__.-'_.-' _.-' 17 `-._ `-.__.-' _.-' 18 `-._ _.-' 19 `-.__.-' 20 21 13308:M 24 Jul 21:42:03.354 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 22 13308:M 24 Jul 21:42:03.354 # Server started, Redis version 3.2.12 23 13308:M 24 Jul 21:42:03.354 # 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. 24 13308:M 24 Jul 21:42:03.354 * The server is now ready to accept connections on port 6379
查看启动状态:
1 [root@localhost ~]# netstat -lnupt|grep redis 2 tcp 0 0 127.0.0.1:21224 0.0.0.0:* LISTEN 11906/src/redis-ser 3 tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 13308/redis-server 4 [root@localhost ~]# ps xua|grep redis 5 root 11906 0.6 0.4 136920 7916 pts/3 Sl 21:10 0:13 src/redis-server 127.0.0.1:21224 6 root 13308 0.4 0.4 136920 7920 pts/1 Sl+ 21:42 0:00 /application/redis/bin/redis-server 127.0.0.1:6379 7 root 13321 0.0 0.0 112648 952 pts/3 S+ 21:43 0:00 grep --color=auto redis 8 [root@localhost ~]#
redis监听端口本地6379,说明redis已经启动成功。
以上启动都是直接在前台,即当前shell环境下,如果退出shell redis服务就直接退出,这对我们使用不是肯定不合理,redis配置文件有个选项直接将启动设置为daemon模式
1 root@localhost conf]# cat redis.conf |grep "daemonize" 2 # Note that Redis will write a pid file in /var/run/redis.pid when daemonized. 3 daemonize no 4 # When the server runs non daemonized, no pid file is created if none is 5 # specified in the configuration. When the server is daemonized, the pid file 6 # output for logging but daemonize, logs will be sent to /dev/null 7 [root@localhost conf]#
修改该参数值yes后,启动
1 [root@localhost redis]# /application/redis/bin/redis-server /application/redis/conf/redis.conf 2 [root@localhost redis]# 3 [root@localhost redis]# 4 [root@localhost redis]# ps xua|grep redis 5 root 11906 0.6 0.4 136920 7916 pts/3 Sl 21:10 0:17 src/redis-server 127.0.0.1:21224 6 root 13412 0.1 0.4 136920 7540 ? Rsl 21:54 0:00 /application/redis/bin/redis-server 127.0.0.1:6379 7 root 13416 0.0 0.0 112648 952 pts/1 R+ 21:54 0:00 grep --color=auto redis 8 [root@localhost redis]#
登陆redis服务:
1 [root@localhost ~]# /application/redis/bin/redis-cli 2 127.0.0.1:6379> help 3 redis-cli 3.2.12 4 To get help about Redis commands type: 5 "help @<group>" to get a list of commands in <group> 6 "help <command>" for help on <command> 7 "help <tab>" to get a list of possible help topics 8 "quit" to exit 9 10 To set redis-cli perferences: 11 ":set hints" enable online hints 12 ":set nohints" disable online hints 13 Set your preferences in ~/.redisclirc 14 127.0.0.1:6379>
客户端命令子命令通过--help即可显示
1 [root@localhost ~]# /application/redis/bin/redis-cli --help 2 redis-cli 3.2.12 3 4 Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]] 5 -h <hostname> Server hostname (default: 127.0.0.1). 6 -p <port> Server port (default: 6379). 7 -s <socket> Server socket (overrides hostname and port). 8 -a <password> Password to use when connecting to the server. 9 -r <repeat> Execute specified command N times. 10 -i <interval> When -r is used, waits <interval> seconds per command. 11 It is possible to specify sub-second times like -i 0.1. 12 -n <db> Database number. 13 -x Read last argument from STDIN. 14 -d <delimiter> Multi-bulk delimiter in for raw formatting (default: ). 15 -c Enable cluster mode (follow -ASK and -MOVED redirections). 16 --raw Use raw formatting for replies (default when STDOUT is 17 not a tty). 18 --no-raw Force formatted output even when STDOUT is not a tty. 19 --csv Output in CSV format. 20 --stat Print rolling stats about server: mem, clients, ... 21 --latency Enter a special mode continuously sampling latency. 22 --latency-history Like --latency but tracking latency changes over time. 23 Default time interval is 15 sec. Change it using -i. 24 --latency-dist Shows latency as a spectrum, requires xterm 256 colors. 25 Default time interval is 1 sec. Change it using -i. 26 --lru-test <keys> Simulate a cache workload with an 80-20 distribution. 27 --slave Simulate a slave showing commands received from the master. 28 --rdb <filename> Transfer an RDB dump from remote server to local file. 29 --pipe Transfer raw Redis protocol from stdin to server. 30 --pipe-timeout <n> In --pipe mode, abort with error if after sending all data. 31 no reply is received within <n> seconds. 32 Default timeout: 30. Use 0 to wait forever. 33 --bigkeys Sample Redis keys looking for big keys. 34 --scan List all keys using the SCAN command. 35 --pattern <pat> Useful with --scan to specify a SCAN pattern. 36 --intrinsic-latency <sec> Run a test to measure intrinsic system latency. 37 The test will run for the specified amount of seconds. 38 --eval <file> Send an EVAL command using the Lua script at <file>. 39 --ldb Used with --eval enable the Redis Lua debugger. 40 --ldb-sync-mode Like --ldb but uses the synchronous Lua debugger, in 41 this mode the server is blocked and script changes are 42 are not rolled back from the server memory. 43 --help Output this help and exit. 44 --version Output version and exit. 45 46 Examples: 47 cat /etc/passwd | redis-cli -x set mypasswd 48 redis-cli get mypasswd 49 redis-cli -r 100 lpush mylist x 50 redis-cli -r 100 -i 1 info | grep used_memory_human: 51 redis-cli --eval myscript.lua key1 key2 , arg1 arg2 arg3 52 redis-cli --scan --pattern '*:12345*' 53 54 (Note: when using --eval the comma separates KEYS[] from ARGV[] items) 55 56 When no command is given, redis-cli starts in interactive mode. 57 Type "help" in interactive mode for information on available commands 58 and settings. 59 60 [root@localhost ~]#
直接运行redis-cli 即可登陆到redis常用命令使用运行help command
1 127.0.0.1:6379> help 2 redis-cli 3.2.12 3 To get help about Redis commands type: 4 "help @<group>" to get a list of commands in <group> 5 "help <command>" for help on <command> 6 "help <tab>" to get a list of possible help topics 7 "quit" to exit 8 9 To set redis-cli perferences: 10 ":set hints" enable online hints 11 ":set nohints" disable online hints 12 Set your preferences in ~/.redisclirc 13 127.0.0.1:6379> help set 14 15 SET key value [EX seconds] [PX milliseconds] [NX|XX] 16 summary: Set the string value of a key 17 since: 1.0.0 18 group: string 19 20 127.0.0.1:6379> help get 21 22 GET key 23 summary: Get the value of a key 24 since: 1.0.0 25 group: string 26 27 127.0.0.1:6379>
如果要查看所有信息运行info
1 [root@localhost ~]# /application/redis/bin/redis-cli 2 127.0.0.1:6379> info 3 # Server 4 redis_version:3.2.12 5 redis_git_sha1:00000000 6 redis_git_dirty:0 7 redis_build_id:13a68ce18431225 8 redis_mode:standalone 9 os:Linux 3.10.0-327.el7.x86_64 x86_64 10 arch_bits:64 11 multiplexing_api:epoll 12 gcc_version:4.8.5 13 process_id:13412 14 run_id:bc69db78d60419ec1b04bfd282d4bd29e9a3587d 15 tcp_port:6379 16 uptime_in_seconds:602 17 uptime_in_days:0 18 hz:10 19 lru_clock:5757621 20 executable:/application/redis/bin/redis-server 21 config_file:/application/redis/conf/redis.conf 22 23 # Clients 24 connected_clients:1 25 client_longest_output_list:0 26 client_biggest_input_buf:0 27 blocked_clients:0 28 29 # Memory 30 used_memory:822696 31 used_memory_human:803.41K 32 used_memory_rss:7720960 33 used_memory_rss_human:7.36M 34 used_memory_peak:822696 35 used_memory_peak_human:803.41K 36 total_system_memory:1921671168 37 total_system_memory_human:1.79G 38 used_memory_lua:37888 39 used_memory_lua_human:37.00K 40 maxmemory:0 41 maxmemory_human:0B 42 maxmemory_policy:noeviction 43 mem_fragmentation_ratio:9.38 44 mem_allocator:jemalloc-4.0.3 45 46 # Persistence 47 loading:0 48 rdb_changes_since_last_save:0 49 rdb_bgsave_in_progress:0 50 rdb_last_save_time:1532483675 51 rdb_last_bgsave_status:ok 52 rdb_last_bgsave_time_sec:-1 53 rdb_current_bgsave_time_sec:-1 54 aof_enabled:0 55 aof_rewrite_in_progress:0 56 aof_rewrite_scheduled:0 57 aof_last_rewrite_time_sec:-1 58 aof_current_rewrite_time_sec:-1 59 aof_last_bgrewrite_status:ok 60 aof_last_write_status:ok 61 62 # Stats 63 total_connections_received:5 64 total_commands_processed:4 65 instantaneous_ops_per_sec:0 66 total_net_input_bytes:162 67 total_net_output_bytes:32084 68 instantaneous_input_kbps:0.00 69 instantaneous_output_kbps:0.00 70 rejected_connections:0 71 sync_full:0 72 sync_partial_ok:0 73 sync_partial_err:0 74 expired_keys:0 75 evicted_keys:0 76 keyspace_hits:0 77 keyspace_misses:0 78 pubsub_channels:0 79 pubsub_patterns:0 80 latest_fork_usec:0 81 migrate_cached_sockets:0 82 83 # Replication 84 role:master 85 connected_slaves:0 86 master_repl_offset:0 87 repl_backlog_active:0 88 repl_backlog_size:1048576 89 repl_backlog_first_byte_offset:0 90 repl_backlog_histlen:0 91 92 # CPU 93 used_cpu_sys:3.15 94 used_cpu_user:0.10 95 used_cpu_sys_children:0.00 96 used_cpu_user_children:0.00 97 98 # Cluster 99 cluster_enabled:0 100 101 # Keyspace 102 127.0.0.1:6379>
如果查看某一项信息可以通过info args,例如查看CPU、 Server、Clients等
1 127.0.0.1:6379> info CPU 2 # CPU 3 used_cpu_sys:3.16 4 used_cpu_user:0.10 5 used_cpu_sys_children:0.00 6 used_cpu_user_children:0.00 7 127.0.0.1:6379> info Clients 8 # Clients 9 connected_clients:1 10 client_longest_output_list:0 11 client_biggest_input_buf:0 12 blocked_clients:0 13 127.0.0.1:6379> info Server 14 # Server 15 redis_version:3.2.12 16 redis_git_sha1:00000000 17 redis_git_dirty:0 18 redis_build_id:13a68ce18431225 19 redis_mode:standalone 20 os:Linux 3.10.0-327.el7.x86_64 x86_64 21 arch_bits:64 22 multiplexing_api:epoll 23 gcc_version:4.8.5 24 process_id:13412 25 run_id:bc69db78d60419ec1b04bfd282d4bd29e9a3587d 26 tcp_port:6379 27 uptime_in_seconds:642 28 uptime_in_days:0 29 hz:10 30 lru_clock:5757661 31 executable:/application/redis/bin/redis-server 32 config_file:/application/redis/conf/redis.conf 33 127.0.0.1:6379> info Clients 34 # Clients 35 connected_clients:1 36 client_longest_output_list:0 37 client_biggest_input_buf:0 38 blocked_clients:0 39 127.0.0.1:6379>
比如可以在命令行查看配置文件信息,可以使用CONFIG GET CONFIG_SETTING_NAME
1 127.0.0.1:6379> CONFIG GET loglevel 2 1) "loglevel" 3 2) "notice" 4 127.0.0.1:6379> 5 127.0.0.1:6379> CONFIG GET loglevel 6 1) "loglevel" 7 2) "notice" 8 127.0.0.1:6379> CONFIG GET port 9 1) "port" 10 2) "6379" 11 127.0.0.1:6379> CONFIG GET timeout 12 1) "timeout" 13 2) "0" 14 127.0.0.1:6379> CONFIG GET pidfile 15 1) "pidfile" 16 2) "/var/run/redis_6379.pid" 17 127.0.0.1:6379>
如果查看所有配置文件所有信息 CONFIG GET '*'
1 127.0.0.1:6379> CONFIG GET * 2 1) "dbfilename" 3 2) "dump.rdb" 4 3) "requirepass" 5 4) "" 6 5) "masterauth" 7 6) "" 8 7) "unixsocket" 9 8) "" 10 9) "logfile" 11 10) "" 12 11) "pidfile" 13 12) "/var/run/redis_6379.pid" 14 13) "slave-announce-ip" 15 14) "" 16 15) "maxmemory" 17 16) "0" 18 17) "maxmemory-samples" 19 18) "5" 20 19) "timeout" 21 20) "0" 22 21) "auto-aof-rewrite-percentage" 23 22) "100" 24 23) "auto-aof-rewrite-min-size" 25 24) "67108864" 26 25) "hash-max-ziplist-entries" 27 26) "512" 28 27) "hash-max-ziplist-value" 29 28) "64" 30 29) "list-max-ziplist-size" 31 30) "-2" 32 31) "list-compress-depth" 33 32) "0" 34 33) "set-max-intset-entries" 35 34) "512" 36 35) "zset-max-ziplist-entries" 37 36) "128" 38 37) "zset-max-ziplist-value" 39 38) "64" 40 39) "hll-sparse-max-bytes" 41 40) "3000" 42 41) "lua-time-limit" 43 42) "5000" 44 43) "slowlog-log-slower-than" 45 44) "10000" 46 45) "latency-monitor-threshold" 47 46) "0" 48 47) "slowlog-max-len" 49 48) "128" 50 49) "port" 51 50) "6379" 52 51) "tcp-backlog" 53 52) "511" 54 53) "databases" 55 54) "16" 56 55) "repl-ping-slave-period" 57 56) "10" 58 57) "repl-timeout" 59 58) "60" 60 59) "repl-backlog-size" 61 60) "1048576" 62 61) "repl-backlog-ttl" 63 62) "3600" 64 63) "maxclients" 65 64) "10000" 66 65) "watchdog-period" 67 66) "0" 68 67) "slave-priority" 69 68) "100" 70 69) "slave-announce-port" 71 70) "0" 72 71) "min-slaves-to-write" 73 72) "0" 74 73) "min-slaves-max-lag" 75 74) "10" 76 75) "hz" 77 76) "10" 78 77) "cluster-node-timeout" 79 78) "15000" 80 79) "cluster-migration-barrier" 81 80) "1" 82 81) "cluster-slave-validity-factor" 83 82) "10" 84 83) "repl-diskless-sync-delay" 85 84) "5" 86 85) "tcp-keepalive" 87 86) "300" 88 87) "cluster-require-full-coverage" 89 88) "yes" 90 89) "no-appendfsync-on-rewrite" 91 90) "no" 92 91) "slave-serve-stale-data" 93 92) "yes" 94 93) "slave-read-only" 95 94) "yes" 96 95) "stop-writes-on-bgsave-error" 97 96) "yes" 98 97) "daemonize" 99 98) "yes" 100 99) "rdbcompression" 101 100) "yes" 102 101) "rdbchecksum" 103 102) "yes" 104 103) "activerehashing" 105 104) "yes" 106 105) "protected-mode" 107 106) "yes" 108 107) "repl-disable-tcp-nodelay" 109 108) "no" 110 109) "repl-diskless-sync" 111 110) "no" 112 111) "aof-rewrite-incremental-fsync" 113 112) "yes" 114 113) "aof-load-truncated" 115 114) "yes" 116 115) "maxmemory-policy" 117 116) "noeviction" 118 117) "loglevel" 119 118) "notice" 120 119) "supervised" 121 120) "no" 122 121) "appendfsync" 123 122) "everysec" 124 123) "syslog-facility" 125 124) "local0" 126 125) "appendonly" 127 126) "no" 128 127) "dir" 129 128) "/application/redis-3.2.12" 130 129) "save" 131 130) "900 1 300 10 60 10000" 132 131) "client-output-buffer-limit" 133 132) "normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60" 134 133) "unixsocketperm" 135 134) "0" 136 135) "slaveof" 137 136) "" 138 137) "notify-keyspace-events" 139 138) "" 140 139) "bind" 141 140) "127.0.0.1" 142 127.0.0.1:6379>
redis操作:
a、模拟写入数据:
1 127.0.0.1:6379> set redis 3.2.12 2 OK 3 127.0.0.1:6379> get redis 4 "3.2.12" 5 127.0.0.1:6379> set python 3.5.12 6 OK 7 127.0.0.1:6379> get python 8 "3.5.12" 9 127.0.0.1:6379>
b、删除数据
1 127.0.0.1:6379> del redis 2 (integer) 1 3 127.0.0.1:6379> get redis 4 (nil) 5 127.0.0.1:6379> del python 6 (integer) 1 7 127.0.0.1:6379> get python 8 (nil) 9 127.0.0.1:6379>
redis主从复制
假设当前主机redis为主,将配置文件中修改bind 127.0.0.1 为bind 192.168.112.140 127.0.0.1
在另外一台主机上同样部署redis-3.2.12,配置文件同主redis,修改slaveof内容为: slaveof 192.168.112.140 6379
1 [root@firewall-node01 conf]# cat redis.conf |grep slaveof 2 # Master-Slave replication. Use slaveof to make a Redis instance a copy of 3 slaveof 192.168.112.140 6379 4 [root@firewall-node01 conf]#
启动redis服务:
1 [root@firewall-node01 ~]# /application/redis/bin/redis-server /application/redis/conf/redis.conf 2 [root@firewall-node01 ~]# ps xua|grep redis 3 root 11526 0.2 0.4 136920 7608 ? Ssl 23:03 0:00 /application/redis/bin/redis-server 192.168.112.200:6379 4 root 11530 0.0 0.0 112648 948 pts/1 S+ 23:03 0:00 grep --color=auto redis 5 [root@firewall-node01 ~]# netstat -lnupt 6 Active Internet connections (only servers) 7 Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name 8 tcp 0 0 192.168.112.200:6379 0.0.0.0:* LISTEN 11526/redis-server 9 tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1157/sshd 10 tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2281/master 11 tcp6 0 0 :::22 :::* LISTEN 1157/sshd 12 tcp6 0 0 ::1:25 :::* LISTEN 2281/master 13 [root@firewall-node01 ~]#
在主redis上模拟写入:
1 127.0.0.1:6379> set redis 3.2.12 2 OK 3 127.0.0.1:6379> set centos 7.2.1511 4 OK 5 127.0.0.1:6379> set python 3.5.7 6 OK 7 127.0.0.1:6379> get redis 8 "3.2.12" 9 127.0.0.1:6379> get python 10 "3.5.7" 11 127.0.0.1:6379> get centos 12 "7.2.1511" 13 127.0.0.1:6379>
切换到从redis上查询:
1 127.0.0.1:6379> get redis 2 "3.2.12" 3 127.0.0.1:6379> get python 4 "3.5.7" 5 127.0.0.1:6379> get centos 6 "7.2.1511" 7 127.0.0.1:6379>
模拟删除操作:
主redis删除刚才写入的内容:
1 127.0.0.1:6379> del redis 2 (integer) 1 3 127.0.0.1:6379> del python 4 (integer) 1 5 127.0.0.1:6379> del centos 6 (integer) 1 7 127.0.0.1:6379> get redis 8 (nil) 9 127.0.0.1:6379> get python 10 (nil) 11 127.0.0.1:6379> get centos 12 (nil) 13 127.0.0.1:6379>
从redis上查看:
1 127.0.0.1:6379> 2 127.0.0.1:6379> 3 127.0.0.1:6379> get redis 4 (nil) 5 127.0.0.1:6379> get python 6 (nil) 7 127.0.0.1:6379> get centos 8 (nil) 9 127.0.0.1:6379>
如果想看更详细过程,我们可以在主从上分别开两个窗口监听,监听命令:redis-cli -h ip -p port monitor
主redis上:
1 [root@localhost ~]# redis-cli monitor 2 OK
从redis上
1 [root@firewall-node01 ~]# redis-cli monitor 2 OK 3 1532488634.079511 [0 192.168.112.140:6379] "PING" 4 1532488644.323362 [0 192.168.112.140:6379] "PING" 5 1532488654.591374 [0 192.168.112.140:6379] "PING" 6 1532488665.103119 [0 192.168.112.140:6379] "PING" 7 1532488675.441193 [0 192.168.112.140:6379] "PING" 8 1532488685.729128 [0 192.168.112.140:6379] "PING" 9 1532488696.052144 [0 192.168.112.140:6379] "PING" 10 1532488706.272656 [0 192.168.112.140:6379] "PING" 11 1532488716.508952 [0 192.168.112.140:6379] "PING" 12 1532488726.785865 [0 192.168.112.140:6379] "PING" 13 1532488737.030902 [0 192.168.112.140:6379] "PING" 14 1532488747.330386 [0 192.168.112.140:6379] "PING" 15 1532488757.639611 [0 192.168.112.140:6379] "PING" 16 1532488767.866261 [0 192.168.112.140:6379] "PING" 17 1532488778.153609 [0 192.168.112.140:6379] "PING" 18 1532488788.507588 [0 192.168.112.140:6379] "PING" 19 1532488798.776813 [0 192.168.112.140:6379] "PING"
在主redis执行添加内容操作
1 127.0.0.1:6379> set web001 www.baidu.com 2 OK 3 127.0.0.1:6379> set web002 www.tmall.com 4 OK 5 127.0.0.1:6379> set web003 www.163.com 6 OK 7 127.0.0.1:6379> set web004 www.sina.com.cn 8 OK 9 127.0.0.1:6379>
主redis监控窗口:
1 [root@localhost ~]# redis-cli monitor 2 OK 3 1532488868.015675 [0 127.0.0.1:60068] "set" "web001" "www.baidu.com" 4 1532488877.247735 [0 127.0.0.1:60068] "set" "web002" "www.tmall.com" 5 1532488887.446223 [0 127.0.0.1:60068] "set" "web003" "www.163.com" 6 1532488897.523142 [0 127.0.0.1:60068] "set" "web004" "www.sina.com.cn"
从redis上查看:
1 127.0.0.1:6379> get web001 2 "www.baidu.com" 3 127.0.0.1:6379> get web002 4 "www.tmall.com" 5 127.0.0.1:6379> get web003 6 "www.163.com" 7 127.0.0.1:6379> get web004 8 "www.sina.com.cn" 9 127.0.0.1:6379>
监控界面内容:
1 [root@firewall-node01 ~]# redis-cli monitor 2 OK 3 1532488634.079511 [0 192.168.112.140:6379] "PING" 4 1532488644.323362 [0 192.168.112.140:6379] "PING" 5 1532488654.591374 [0 192.168.112.140:6379] "PING" 6 1532488665.103119 [0 192.168.112.140:6379] "PING" 7 1532488675.441193 [0 192.168.112.140:6379] "PING" 8 1532488685.729128 [0 192.168.112.140:6379] "PING" 9 1532488696.052144 [0 192.168.112.140:6379] "PING" 10 1532488706.272656 [0 192.168.112.140:6379] "PING" 11 1532488716.508952 [0 192.168.112.140:6379] "PING" 12 1532488726.785865 [0 192.168.112.140:6379] "PING" 13 1532488737.030902 [0 192.168.112.140:6379] "PING" 14 1532488747.330386 [0 192.168.112.140:6379] "PING" 15 1532488757.639611 [0 192.168.112.140:6379] "PING" 16 1532488767.866261 [0 192.168.112.140:6379] "PING" 17 1532488778.153609 [0 192.168.112.140:6379] "PING" 18 1532488788.507588 [0 192.168.112.140:6379] "PING" 19 1532488798.776813 [0 192.168.112.140:6379] "PING" 20 1532488809.066915 [0 192.168.112.140:6379] "PING" 21 1532488819.414117 [0 192.168.112.140:6379] "PING" 22 1532488829.743688 [0 192.168.112.140:6379] "PING" 23 1532488840.054360 [0 192.168.112.140:6379] "PING" 24 1532488849.729390 [0 192.168.112.140:6379] "set" "web001" "www.baidu.com" 25 1532488850.251853 [0 192.168.112.140:6379] "PING" 26 1532488858.961381 [0 192.168.112.140:6379] "set" "web002" "www.tmall.com" 27 1532488860.463012 [0 192.168.112.140:6379] "PING" 28 1532488869.159778 [0 192.168.112.140:6379] "set" "web003" "www.163.com" 29 1532488870.850704 [0 192.168.112.140:6379] "PING" 30 1532488879.237154 [0 192.168.112.140:6379] "set" "web004" "www.sina.com.cn" 31 1532488881.098155 [0 192.168.112.140:6379] "PING" 32 1532488891.272511 [0 192.168.112.140:6379] "PING" 33 1532488901.528316 [0 192.168.112.140:6379] "PING" 34 1532488912.010221 [0 192.168.112.140:6379] "PING" 35 1532488922.381975 [0 192.168.112.140:6379] "PING" 36 1532488931.274611 [0 127.0.0.1:42876] "get" "web01" 37 1532488932.899151 [0 192.168.112.140:6379] "PING" 38 1532488941.722834 [0 127.0.0.1:42876] "get" "web001" 39 1532488943.583479 [0 192.168.112.140:6379] "PING" 40 1532488944.538151 [0 127.0.0.1:42876] "get" "web002" 41 1532488946.210489 [0 127.0.0.1:42876] "get" "web003" 42 1532488948.482047 [0 127.0.0.1:42876] "get" "web004" 43 1532488953.917618 [0 192.168.112.140:6379] "PING" 44 1532488964.223460 [0 192.168.112.140:6379] "PING" 45 1532488974.667318 [0 192.168.112.140:6379] "PING"
未完待续............