【Redis1】Redis数据库的学习与实践—安装与部署
Redis数据库的学习与实践安装与部署 一:Redis数据库简介: RedisRemote DictionaryServer,可以直接理解为远程字典服务。 这里摘抄一段Redis官网上的话似乎更能准确的概述Redis的含义:Redis is an open source,BSD licensed, advanced key-valuecache and
Redis数据库的学习与实践—安装与部署
一:Redis数据库简介:
Redis—Remote DictionaryServer,可以直接理解为远程字典服务。
这里摘抄一段Redis官网上的话似乎更能准确的概述Redis的含义:Redis is an open source,BSD licensed, advanced key-valuecache and store.It is often referred to as a datastructure server since keys can contain strings,hashes,lists,sets,sorted sets,bitmapsand hyperloglogs.
Redis数据库是一个高性能的key-value数据库,Redis 是完全开源免费的,遵守BSD协议,先进的key - value持久化产品。它通常被称为数据结构服务器,因为值(value)可以是字符串(String), 哈希(Map),列表(list), 集合(sets)和有序集合(sorted sets)等类型。这些数据类型都支持push/pop,add/remove及取交集和并集及更丰富的操作,Redis支持各种不同方式的排序。为了保证效率,数据都是缓存在内存中,它也可以周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件。
二:Redis数据库使用场合:
目前全球最大的Redis用户是新浪微博,在新浪有200多台物理机,400多个端口正在运行这Redis,有+4G的数据在Redis上来为新浪微博用户提供服务。
在新浪微博Redis的部署场景很多,大概分为如下的两种:
1:应用程序直接访问Redis数据库;
2:应用程序直接访问Redis,只有Redis访问失败的时候才会访问MySQL。
Redis数据库提供多种灵活的数据结构和数据操作,为不同的大数据构建不同的数据结构。
具体的适用场景:
1:取最新N个数据的请求;
2:排行榜应用,取TOP N操作;
3:需要精确设定过期时间的应用;
4:计数器应用;
5:Uniq操作,获取某段时间所有数据排重值;
6:实时系统,反垃圾系统;
7:Pub/Sub(订阅/发布)构建实时消息系统;
8:构建队列系统;
9:缓存。
Redis,Mysql,MongoDB三种数据库的比较:
不同\数据库 |
Redis |
Mysql |
MongoDB |
1 |
库 |
库 |
库 |
2 |
无 |
表 |
集合 |
3 |
无 |
字段/行/列 |
无 |
三:Redis的安装与部署:
1:下载,解压和安装:
在官网下载编译后的,适用于64位Linux系统的redis-2.8.13.tar.gz,然后拷贝至Linux系统下,并执行下列命令:
#step1 $ tar zxvf redis-2.8.13.tar.gz $ mv redis-2.8.13 redis $ cd redis $ make #step2 $ cd redis/src $ make install |
2:启动Redis:
Redis的默认端口是:6379
#启动redis服务 $ cd redis $ cd redis/src $ redis-server #设置redis配置文件 $ cd redis $ sudo vim redis.conf |
3:修改配置文件和设置后台运行:
首先修改redis.conf文件:
$ sudo vim reids.conf |
然后修改配置选项daemonize为yes,然后保存退出
启动redis,并设置配置文件:
$ redis-server /home/djy/toolkit/redis/redis.conf |
设置成功之后,验证是否成功:
$ ps –ef | grep redis |
运行结果显示如下:
djy 6262 2972 0 01:37 pts/1 00:00:00 redis-server *:6379 djy 9539 2972 0 02:00 pts/1 00:00:00 grep redis |
查看6379端口的状态:
$ netstat –tunpl | grep 6379 |
4:进入Redis的客户端redis-cli
$ cd src $ redis-cli |
运行后的结果如下,则表示进入客户端成功:
djy@ubuntu:~/toolkit/redis/src$ redis-cli 127.0.0.1:6379> 127.0.0.1:6379> 127.0.0.1:6379> 127.0.0.1:6379> |
5:关闭redis-server
#第一种方式 $pkill redis-server #第二种方式 $redis-cli shutdown |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.
