The best way to learn Redis is to try it out while learning. Before learning about Redis's core content, familiarize yourself with the basics of how to install and run Redis, as well as the fundamentals of Redis, as described in practice.
One, Redis installation
To install Redis you need to understand the version of Redis rules to choose the most appropriate version, the Redis Convention minor version number (the number after the first decimal point) is even the stable version, such as 2.4, 2.6, etc., odd is a non-stable version, we recommend the use of stable versions of Redis.
1, source code compilation and installation (recommended)
: The installation of http://download.redis.io/redis-stable.tar.gz Redis is very simple, as follows:
tar zxvf redis-3.0. 2. Tar . GZCD Redis-3.0. 2 Make
2. Start and stop Redis
Before you start, you need to know what Redis executables are and what they do.
The most common is redis-server and redis-cli, and Redis-server is the Redis server, starting it is to start Redis. REDIS-CLI is the command-line client that it comes with.
1) direct Start (development environment)
Direct start for the development environment, relatively simple, run Redis-server, the default port is 6379, through the--port parameter can be customized port
[Email protected] src]# redis-Server26263: C ,Oct the:Ten:24.465# warning:no Configfilespecified, using the default Config. In order to specify a configfileUse redis-server/path/to/redis.conf26263: M ,Oct the:Ten:24.468* Increased maximum number of open files to10032(It is originally set to1024x768). _._ _.-``__"'-._ _.-`` `. `_."'-._ Redis3.0.2(00000000/0) -bit.-`` .-```. ```\/ _.,_"'-._ ( ' , .-` | `, ) Running in standalone mode|`-._`-...-` __...-.``-._|'` _.-'| Port:6379| `-._ `._ / _.-' | pid:26263`-._ `-._ `-./ _.-' _.-'|`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-'| http//Redis.io`-._ `-._`-.__.-'_.-'_.-'|`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-'| `-._ `-._`-.__.-'_.-'_.-'`-._ `-.__.-' _.-' `-._ _.-'`-.__.-' 26263: M ,Oct the:Ten:24.506# Server started, Redis version3.0.226263: M ,Oct the:Ten:24.508# WARNING Overcommit_memory is set to0! Background save may fail under low memory condition. To fix this issue add'vm.overcommit_memory = 1'To/etc/sysctl.conf and ThenReboot or run the command'sysctl Vm.overcommit_memory=1' forThis is take effect.26263: M ,Oct the:Ten:24.509# WARNING You has Transparent Huge Pages (THP) Support enabledinchYour kernel. This would 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.localinchorder to retain the setting after a reboot. Redis must be restarted after THP is disabled.26263: M ,Oct the:Ten:24.509# warning:the TCP Backlog setting of511Cannot be enforced because/proc/sys/net/core/somaxconn are set to the lower value of -.26263: M ,Oct the:Ten:24.510* The server is now a ready-to-accept connections on port6379
View Code
2) script start (production environment)
The production environment is recommended to start with an initial step, the main steps are as follows:
- To modify the initialization steps: /redis-3.0.2/utils/redis_init_script script is copied to the/ETC/INIT.D directory, the file name is Reids_ port number, modify the Red section below.
CP redis_init_script/etc/init.d/redis_6379
1#!/bin/SH 2 # 3# Simple Redis init.d script conceived to work on Linux systems4# as it does use of the/proc FileSystem. 5 6 redisport=6379 #端口号 7 exec=/usr/local/bin/redis-server #redis-server executable file directory 8 cliexec= /USR/LOCAL/BIN/REDIS-CLI #redis the directory where the-cli executable file resides9 Tenpidfile=/var/run/Redis_${redisport}.pid Oneconf="/etc/redis/${redisport}.conf" A - Case " $" inch -start) the if[ -F $PIDFILE] - Then - Echo "$PIDFILE exists, process is already running or crashed" - Else + Echo "starting Redis Server ..." -$EXEC $CONF + fi A ;; atstop) - if[ ! -F $PIDFILE] - Then - Echo "$PIDFILE does not exist, process was not running" - Else -pid=$ (Cat$PIDFILE) in Echo "stopping ..." -$CLIEXEC-p $REDISPORT shutdown to while[-x/proc/${pid}] + Do - Echo "waiting for Redis to shutdown ..." the Sleep 1 * Done $ Echo "Redis stopped" Panax Notoginseng fi - ;; the*) + Echo "Please use start or stop as first argument" A ;; the Esac
- Create the desired folder: Create the following two folders. /etc/reids: Storing redis configuration files;/var/redis/6379: Storing Persistent files
[[Email protected] utils]# CD/mkdirmkdir /var/redis/6379 -P
- Modify the configuration file: Copy the configuration file to the/etc/redis directory, named 6379.conf, and you need to modify the following red part parameters
[[email protected] Redis-]# CP redis.conf/etc/redis/6379 .conf[[email protected "Redis -. 2 ] # Vim/etc/redis/6379 . conf
PNS daemonize Yes
When the running daemonized, Redis writes a PID file in/var/run/redis.pid by
# Default. You can specify a custom PID file from here.
pidfile/var/run/redis_6379.pid
# Accept connections on the specified port, default is 6379.
The # If Port 0 is specified Redis would not be listen on a TCP socket.
Port 6379
187 dir/var/redis/6379 #持久化文件目录
Specific configuration parameters are described below:
daemonize: whether to run Pidfile in the future daemon mode: pid file location port: Listening Port number timeout: Request time- out loglevel: Log information level logfile: log file location databases: * *: The frequency of the snapshot is saved, the first * indicates how long, and the third * indicates how many times the write operation is performed. Snapshots are automatically saved when a certain number of writes are performed within a certain amount of time. You can set multiple conditions. Rdbcompression: whether to use compressed dbfilename: Data Snapshot file name (only file name, excluding directory)dir: Data snapshot of the Save directory (this is the directory) AppendOnly: whether to open the Appendonlylog, each write operation will remember a log, which will improve the data anti-risk ability, but affect efficiency. Appendfsync: appendonlylog How to sync to disk (three options, each write is forced to call Fsync, Fsync enabled once per second, do not call Fsync wait for the system to synchronize itself)
- Start:/etc/init.d/redis_6379 start
[Email protected] redis]#/etc/init.d/redis_6379 startstarting Redis server ... [Email protected] redis]#PSaxu|grepRedisroot26400 0.2 0.3 137440 7432? Ssl the: the 0:xx/home/software/redis-3.0.2/src/redis-server *:6379Root26404 0.0 0.0 103256 844pts/3S+ the: the 0:xx grepRedis[[email protected] redis]#
So Redis starts up.
3) Stop Redis
[Email protected] redis]#/home/software/redis-3.0. 2/src/redis--9 Redis
3. Redis Command Line Client
Go to the command line:
[[email protected] redis]# redis-CLI127.0. 0.1:6379> pingpong127.0. 0.1:6379
Getting Started with Redis