Redis Quick Start
This is a quick start document that targets people without prior experience with Redis. Reading this document will help you:
- Download and compile Redis to start hacking.
- Use redis-cli to access the server.
- Use Redis from your application.
- Understand how Redis persistence works.
- Install Redis more properly.
- Find out what to read next to understand more about Redis.
Installing Redis
The suggested way of installing Redis is compiling it from sources as Redis has no dependencies other than a working GCC compiler and libc. Installing it using the package manager of your Linux distribution is somewhat discouraged as usually the available version is not the latest.
You can either download the latest Redis tar ball from the redis.io web site, or you can alternatively use this special URL that always points to the latest stable Redis version, that is, http://download.redis.io/redis-stable.tar.gz.
In order to compile Redis follow this simple steps:
wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make
At this point you can try if your build works correctly by typing make test, but this is an optional step. After the compilation thesrc directory inside the Redis distribution is populated with the different executables that are part of Redis:
- redis-server is the Redis Server itself.
- redis-sentinel is the Redis Sentinel executable (monitoring and failover).
- redis-cli is the command line interface utility to talk with Redis.
- redis-benchmark is used to check Redis performances.
- redis-check-aof and redis-check-dump are useful in the rare event of corrupted data files.
It is a good idea to copy both the Redis server and the command line interface in proper places, either manually using the following commands:
- sudo cp src/redis-server /usr/local/bin/
- sudo cp src/redis-cli /usr/local/bin/
Or just using make install
.
In the following documentation we assume that /usr/local/bin is in your PATH environment variable so that you can execute both the binaries without specifying the full path.
Starting Redis
The simplest way to start the Redis server is just executing the redis-server binary without any argument.
$ redis-server
[28550] 01 Aug 19:29:28 # Warning: no config file specified, using the default config. In order to specify a config file use 'redis-server /path/to/redis.conf'
[28550] 01 Aug 19:29:28 * Server started, Redis version 2.2.12
[28550] 01 Aug 19:29:28 * The server is now ready to accept connections on port 6379
... more logs ...
In the above example Redis was started without any explicit configuration file, so all the parameters will use the internal default. This is perfectly fine if you are starting Redis just to play a bit with it or for development, but for production environments you should use a configuration file.
In order to start Redis with a configuration file use the full path of the configuration file as first argument, like in the following example: redis-server /etc/redis.conf. You should use the redis.conf
file included in the root directory of the Redis source code distribution as a template to write your configuration file.
Check if Redis is working
External programs talk to Redis using a TCP socket and a Redis specific protocol. This protocol is implemented in the Redis client libraries for the different programming languages. However to make hacking with Redis simpler Redis provides a command line utility that can be used to send commands to Redis. This program is called redis-cli.
The first thing to do in order to check if Redis is working properly is sending a PING command using redis-cli:
$ redis-cli ping
PONG
Running redis-cli followed by a command name and its arguments will send this command to the Redis instance running on localhost at port 6379. You can change the host and port used by redis-cli, just try the --help option to check the usage information.
Another interesting way to run redis-cli is without arguments: the program will start in interactive mode, you can type different commands and see their replies.
$ redis-cli
redis 127.0.0.1:6379> ping
PONG
redis 127.0.0.1:6379> set mykey somevalue
OK
redis 127.0.0.1:6379> get mykey
"somevalue"
At this point you are able to talk with Redis. It is the right time to pause a bit with this tutorial and start the fifteen minutes introduction to Redis data types in order to learn a few Redis commands. Otherwise if you already know a few basic Redis commands you can keep reading.
Securing Redis
By default Redis binds to all the interfaces and has no authentication at all. If you use Redis into a very controlled environment, separated from the external internet and in general from attackers, that's fine. However if Redis without any hardening is exposed to the internet, it is a big security concern. If you are not 100% sure your environment is secured properly, please check the following steps in order to make Redis more secure, which are enlisted in order of increased security.
- Make sure the port Redis uses to listen for connections (by default 6379 and additionally 16379 if you run Redis in cluster mode, plus 26379 for Sentinel) is firewalled, so that it is not possible to contact Redis from the outside world.
- Use a configuration file where the
bind
directive is set in order to guarantee that Redis listens just in as little network interfaces you are using. For example only the loopback interface (127.0.0.1) if you are accessing Redis just locally from the same computer, and so forth. - Use the
requirepass
option in order to add an additional layer of security so that clients will require to authenticate using theAUTH
command. - Use spiped or another SSL tunnelling software in order to encrypt traffic between Redis servers and Redis clients if your environment requires encryption.
Note that a Redis exposed to the internet without any security is very simple to exploit, so make sure you understand the above and apply at least a firewalling layer. After the firewalling is in place, try to connect with redis-cli
from an external host in order to prove yourself the instance is actually not reachable.
Using Redis from your application
Of course using Redis just from the command line interface is not enough as the goal is to use it from your application. In order to do so you need to download and install a Redis client library for your programming language. You'll find a full list of clients for different languages in this page.
For instance if you happen to use the Ruby programming language our best advice is to use the Redis-rb client. You can install it using the command gem install redis (also make sure to install the SystemTimer gem as well).
These instructions are Ruby specific but actually many library clients for popular languages look quite similar: you create a Redis object and execute commands calling methods. A short interactive example using Ruby:
>> require 'rubygems'
=> false
>> require 'redis'
=> true
>> r = Redis.new
=> #<Redis client v2.2.1 connected to redis://127.0.0.1:6379/0 (Redis v2.3.8)>
>> r.ping
=> "PONG"
>> r.set('foo','bar')
=> "OK"
>> r.get('foo')
=> "bar"
Redis persistence
You can learn how Redis persistence works on this page, however what is important to understand for a quick start is that by default, if you start Redis with the default configuration, Redis will spontaneously save the dataset only from time to time (for instance after at least five minutes if you have at least 100 changes in your data), so if you want your database to persist and be reloaded after a restart make sure to call the SAVE command manually every time you want to force a data set snapshot. Otherwise make sure to shutdown the database using the SHUTDOWN command:
$ redis-cli shutdown
This way Redis will make sure to save the data on disk before quitting. Reading the persistence page is strongly suggested in order to better understand how Redis persistence works.
Installing Redis more properly
Running Redis from the command line is fine just to hack a bit with it or for development. However at some point you'll have some actual application to run on a real server. For this kind of usage you have two different choices:
- Run Redis using screen.
- Install Redis in your Linux box in a proper way using an init script, so that after a restart everything will start again properly.
A proper install using an init script is strongly suggested. The following instructions can be used to perform a proper installation using the init script shipped with Redis 2.4 in a Debian or Ubuntu based distribution.
We assume you already copied redis-server and redis-cli executables under /usr/local/bin.
-
Create a directory where to store your Redis config files and your data:
sudo mkdir /etc/redis sudo mkdir /var/redis
-
Copy the init script that you'll find in the Redis distribution under the utils directory into /etc/init.d. We suggest calling it with the name of the port where you are running this instance of Redis. For example:
sudo cp utils/redis_init_script /etc/init.d/redis_6379
-
Edit the init script.
sudo vi /etc/init.d/redis_6379
Make sure to modify REDISPORT accordingly to the port you are using. Both the pid file path and the configuration file name depend on the port number.
-
Copy the template configuration file you'll find in the root directory of the Redis distribution into /etc/redis/ using the port number as name, for instance:
sudo cp redis.conf /etc/redis/6379.conf
-
Create a directory inside /var/redis that will work as data and working directory for this Redis instance:
sudo mkdir /var/redis/6379
-
Edit the configuration file, making sure to perform the following changes:
- Set daemonize to yes (by default it is set to no).
- Set the pidfile to
/var/run/redis_6379.pid
(modify the port if needed). - Change the port accordingly. In our example it is not needed as the default port is already 6379.
- Set your preferred loglevel.
- Set the logfile to
/var/log/redis_6379.log
- Set the dir to /var/redis/6379 (very important step!)
-
Finally add the new Redis init script to all the default runlevels using the following command:
sudo update-rc.d redis_6379 defaults
You are done! Now you can try running your instance with:
/etc/init.d/redis_6379 start
Make sure that everything is working as expected:
- Try pinging your instance with redis-cli.
- Do a test save with redis-cli save and check that the dump file is correctly stored into /var/redis/6379/ (you should find a file called dump.rdb).
- Check that your Redis instance is correctly logging in the log file.
- If it's a new machine where you can try it without problems make sure that after a reboot everything is still working.
Note: In the above instructions we skipped many Redis configuration parameters that you would like to change, for instance in order to use AOF persistence instead of RDB persistence, or to setup replication, and so forth. Make sure to read the example redis.conf
file (that is heavily commented) and the other documentation you can find in this web site for more information.
/*
* Copyright 2001-2010 Georges Menie (www.menie.org)
* Copyright 2010 Salvatore Sanfilippo (adapted to Redis coding style)
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the University of California, Berkeley nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* CRC16 implementation according to CCITT standards.
*
* Note by @antirez: this is actually the XMODEM CRC 16 algorithm, using the
* following parameters:
*
* Name : "XMODEM", also known as "ZMODEM", "CRC-16/ACORN"
* Width : 16 bit
* Poly : 1021 (That is actually x^16 + x^12 + x^5 + 1)
* Initialization : 0000
* Reflect Input byte : False
* Reflect Output CRC : False
* Xor constant to output CRC : 0000
* Output for "123456789" : 31C3
*/
static const uint16_t crc16tab[256]= {
0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,
0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6,
0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de,
0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485,
0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d,
0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4,
0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc,
0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823,
0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b,
0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12,
0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a,
0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41,
0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49,
0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70,
0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78,
0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f,
0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067,
0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e,
0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256,
0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d,
0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405,
0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c,
0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634,
0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab,
0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3,
0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a,
0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92,
0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9,
0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1,
0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8,
0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0
};
uint16_t crc16(const char *buf, int len) {
int counter;
uint16_t crc = 0;
for (counter = 0; counter < len; counter++)
crc = (crc<<8) ^ crc16tab[((crc>>8) ^ *buf++)&0x00FF];
return crc;
}
相关推荐
本文将详细讲解Redis的安装配置过程,帮助你快速搭建自己的Redis服务器。 一、安装Redis 1. 下载Redis源码:首先,你需要访问Redis官网(https://redis.io/download)下载最新版本的Redis源码。通常,你会找到一个...
Linux 系统安装 Redis 及 Tomcat 配置 本文档将详细介绍 Linux 系统安装 Redis 及 ...本文档提供了 Linux 系统安装 Redis 及 Tomcat 的详细步骤和配置方法,旨在帮助读者快速掌握相关知识点,并应用于实际项目中。
以上就是在Windows上安装Redis的完整流程,包括启用必要的Windows功能,安装WSL2,设置Linux发行版,以及在Linux环境中安装和配置Redis服务器。这个过程需要一定的计算机操作基础,但按照上述步骤操作,应该能顺利...
总结来说,这个压缩包提供了一种快速安装和配置Redis及集群的解决方案。通过`go_redis`脚本,你可以自动化完成依赖安装和Redis服务启动。如果你需要搭建高可用的Redis环境,Sentinel配置和主从节点的设置也是必不可...
本文将详细介绍Redis的配置和安装过程,帮助你快速上手这个强大的内存数据存储系统。 ### 安装Redis #### 1. 下载Redis源码 首先,访问Redis官方网站(https://redis.io/download)下载最新版本的源代码。你可以...
在本文中,我们将深入探讨如何在Linux系统上快速安装Redis 2.6.14版本,以及相关的配置和使用方法。 首先,让我们来看两种不同的安装模式。在提供的压缩包中,有两个tar文件:"make-install.tar"和"copy-install....
8. **配置Redis**:Redis的配置文件通常位于安装目录下的`redis.windows.conf`。您可能需要根据实际需求修改配置,例如设置最大内存、调整端口号、开启日志记录等。 9. **防火墙设置**:如果需要远程访问Redis,...
### Redis安装与主从配置详解 #### 一、Redis简介 Redis是一种开源的键值对存储系统,它属于NoSQL数据库的一种,与Memcached类似但更加强大。不同于Memcached只支持内存存储,Redis的数据可以持久化存储在磁盘上,...
内容概要:本文详细讲解了在Linux环境中安装和配置Redis 5.0.4的具体步骤,从下载到编译安装,再到启动服务及配置文件的修改方法,并解决了一些常见的配置问题。文中不仅介绍了基本的命令用法,还解释了各配置项的...
本文将详细介绍Redis的安装配置过程,帮助你快速上手。 一、Redis简介 Redis(Remote Dictionary Server)是一个开源的、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。其特点是...
Ubuntu环境下手动安装与配置Redis涉及到多个步骤,包括下载、解压、编译、安装以及配置主从复制等。下面详细介绍这些知识点。 首先,Redis安装与主从配置的目的是确保数据的安全性。在主从配置中,从服务器(Slave...
- **简单易用:**安装配置简单,同时提供了多种编程语言的API接口,便于开发者快速集成到现有项目中。 #### 二、Redis应用场景 Redis因其独特的特性,被广泛应用于以下场景: - **缓存系统:**利用Redis的高速...
Redis是一款开源、高性能的键值对存储系统,常被用于...这个压缩包提供了从安装到使用的全套示例,适合初学者快速上手Redis。通过学习和实践,你可以更好地理解Redis的工作原理和使用方法,为实际项目开发打下基础。
Redis开发环境配置 Redis是一个完全开源免费的key-value数据库,遵守BSD协议,具有高性能。Redis与其他key-value缓存...通过本文的讲解,希望读者能够快速搭建Redis开发环境,并掌握Redis的基本使用方法和配置技巧。
1. **Redis快速入门**:这部分通常会介绍如何安装Redis,包括在各种操作系统(如Linux、Windows、macOS)上的安装步骤。还会讲解Redis服务器的启动与停止,以及配置文件的基本设置。此外,客户端工具的使用,如`...
redis快速入门详解,包括主从备份、详细配置说明~
docker 离线 redis 快速安装配置脚本
在本套资源中,我们有Redis 3.2.0的安装文件和对应的安装文档,这将帮助你在Linux环境下快速搭建Redis服务器。下面将详细介绍Redis 3.2.0的安装步骤以及一些关键知识点。 首先,确保你的系统满足安装Redis的基本...
- **后端启动**:需要将redis.conf文件复制到安装目录,并进行相应配置修改。 1. **复制配置文件**: ```bash [root@zhangchaoyang ~]# cp redis.conf /usr/redis-4.0.6/bin ``` 2. **修改配置文件**:为了使...
redis 详细介绍 配置安装以及常用命令