NO IMAGE

Redis是一個開源的使用ANSI C語言編寫、支援網路、可基於記憶體亦可持久化的日誌型、Key-Value資料庫,並提供多種語言的API.

這裡使用目的:需求是實時匹配一個號碼所屬地區,且併發量發。那就需要將號碼段對應的地區編碼表儲存到redis,大概30W條,如果實時連線資料庫不實際。

一.下載

官網下載,可自選版本,點選進入下載,這裡下載了redis-3.0.7

二.編譯

2.1執行make編譯redis

$ tar -zxzf redis-3.0.7.tar.gz
$ cd redis-3.0.7
$ make

編譯後在src下會生成6個執行檔案

redis-server(啟動服務端)、redis-cli(呼叫客戶端)、redis-benchmark、redis-check-aof、redis-check-dump、redis-sentinel

2.2 啟動redis服務,執行src/redis-server,服務埠預設:6379

[[email protected] redis-3.0.7]# src/redis-server
25810:C 04 Aug 13:00:26.992 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf
25810:M 04 Aug 13:00:26.993 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.0.7 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 25810
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
25810:M 04 Aug 13:00:26.995 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
25810:M 04 Aug 13:00:26.995 # Server started, Redis version 3.0.7
25810:M 04 Aug 13:00:26.995 # 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.
25810:M 04 Aug 13:00:26.995 * The server is now ready to accept connections on port 6379

2.3 設定、查詢redis值
開啟另一個客戶端,執行src/redis-cli啟動客戶端,set設定、get查詢 KEY-VALUE

[[email protected] redis-3.0.7]# src/redis-cli
127.0.0.1:6379> set name zhengzhenzhen
OK
127.0.0.1:6379> get name
"zhengzhenzhen"
127.0.0.1:6379>
[[email protected] redis-3.0.7]#

三.安裝redis服務

為了管理redis方便,使其開機自啟動,可以將redis安裝成系統服務。

3.1 執行make install

[[email protected] src]# cd ..
[[email protected] redis-3.0.7]# make install
cd src && make install
make[1]: Entering directory `/home/chenyihui/redis-3.0.7/src'
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: Leaving directory `/home/chenyihui/redis-3.0.7/src'

會將make編譯生成的6個可執行檔案拷貝到/usr/local/bin下

[[email protected] /]# cd usr/local/bin
[[email protected] bin]# ll
total 13780
-rwxr-xr-x 1 root root    2363 May  7  2014 pcre-config
-rwxr-xr-x 1 root root   86157 May  7  2014 pcregrep
-rwxr-xr-x 1 root root  161200 May  7  2014 pcretest
-rwxr-xr-x 1 root root 3987849 Aug  5 11:47 redis-benchmark
-rwxr-xr-x 1 root root   23082 Aug  5 11:47 redis-check-aof
-rwxr-xr-x 1 root root   45861 Aug  5 11:47 redis-check-dump
-rwxr-xr-x 1 root root 4078469 Aug  5 11:47 redis-cli
lrwxrwxrwx 1 root root      27 Aug  5 11:47 redis-sentinel -> /usr/local/bin/redis-server
-rwxr-xr-x 1 root root 5640961 Aug  5 11:47 redis-server

3.2 執行./utils/install_server.sh配置redis配置之後redis能開機啟動,最後successful!

[[email protected] redis-3.0.7]# ./utils/install_server.sh
Welcome to the redis service installer
This script 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!

檢視是否已設定開機啟動

[[email protected] redis-3.0.7]# chkconfig --list
...
redis_6379 0:off 1:off 2:on 3:on 4:on 5:on 6:off
...

3.3 redis服務的檢視、啟動、停止
檢視啟動情況:ps -ef|grep redis

[[email protected] redis-3.0.7]# ps -ef|grep redis
root 9009 1 0 13:19 ? 00:00:00 /usr/local/bin/redis-server *:6379
root 23907 14649 0 13:21 pts/2 00:00:00 grep redis

停止服務:/etc/init.d/redis_6379 stop

[[email protected] redis-3.0.7]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped

啟動服務:/etc/init.d/redis_6379 start

以上!