1. 程式人生 > 程式設計 >Redis的概述、搭建及簡單使用(基於CentOS 6.5 Linux)

Redis的概述、搭建及簡單使用(基於CentOS 6.5 Linux)

1、Redis 簡介

Redis 是完全開源免費的,遵守BSD協議,是一個高效能的key-value資料庫。

Redis 與其他 key – value 快取產品有以下三個特點:

  • Redis支援資料的持久化,可以將記憶體中的資料儲存在磁碟中,重啟的時候可以再次載入進行使用。
  • Redis不僅僅支援簡單的key-value型別的資料,同時還提供list,set,zset,hash等資料結構的儲存。
  • Redis支援資料的備份,即master-slave模式的資料備份。

2、Redis 優勢

  • 效能極高 – Redis能讀的速度是110000次/s,寫的速度是81000次/s 。 豐富的資料型別 – Redis支援二進位制案例的 Strings,Lists,Hashes,Sets 及 Ordered Sets 資料型別操作。
  • 原子 – Redis的所有操作都是原子性的,意思就是要麼成功執行要麼失敗完全不執行。單個操作是原子性的。多個操作也支援事務,即原子性,通過MULTI和EXEC指令包起來。
  • 豐富的特性 – Redis還支援 publish/subscribe,通知,key 過期等等特性。

3、Redis與其他key-value儲存有什麼不同?

  • Redis有著更為複雜的資料結構並且提供對他們的原子性操作,這是一個不同於其他資料庫的進化路徑。Redis的資料型別都是基於基本資料結構的同時對程式設計師透明,無需進行額外的抽象。

  • Redis執行在記憶體中但是可以持久化到磁碟,所以在對不同資料集進行高速讀寫時需要權衡記憶體,因為資料量不能大於硬體記憶體。在記憶體資料庫方面的另一個優點是,相比在磁碟上相同的複雜的資料結構,在記憶體中操作起來非常簡單,這樣Redis可以做很多內部複雜性很強的事情。同時,在磁碟格式方面他們是緊湊的以追加的方式產生的,因為他們並不需要進行隨機訪問。

4、Redis 安裝

wget下載:

[root@10 opt]# wget http://download.redis.io/releases/redis-4.0.12.tar.gz
複製程式碼

tar解壓:

[root@10 opt]# tar -zxvf redis-4.0.12.tar.gz
複製程式碼

重新命名:

[root@10 opt]# mv redis-4.0.12 redis
複製程式碼

進入目錄 redis 並 make :

[root@10 opt]# cd redis
[root@10 redis]# make
複製程式碼

進入src 啟動:

[root@10 redis]# cd src
[root@10 src]# ./redis-server 複製程式碼

提示:

[root@10 src]# ./redis-server 
30902:C 26 May 05:40:00.253 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
30902:C 26 May 05:40:00.254 # Redis version=4.0.12,bits=64,commit=00000000,modified=0,pid=30902,just started
30902:C 26 May 05:40:00.254 # Warning: no config file specified,using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
30902:M 26 May 05:40:00.255 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.12 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    ',.-`  | `,)     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 30902
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

30902:M 26 May 05:40:00.257 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
30902:M 26 May 05:40:00.257 # Server initialized
30902:M 26 May 05:40:00.257 # 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.
30902:M 26 May 05:40:00.259 # 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 # Redis configuration file example.
must be restarted after THP is disabled.
30902:M 26 May 05:40:00.259 * Ready to accept connections
複製程式碼

需要配置 redis.conf:

[root@10 src]# vi ../redis.conf
複製程式碼

找到更改以下內容,開啟守護程式

# no 改為 yes
daemonize yes

# 日誌可以自行新增,不做要求
logfile ./logs/redis.log 
複製程式碼

啟動redis並應用redis.conf配置檔案:

[root@10 src]# ./redis-server ../redis.conf
複製程式碼

開啟客戶端並進行簡單測試:

[root@10 src]# ./redis-cli
127.0.0.1:6379> EXISTS mykey
(integer) 0 
127.0.0.1:6379> append mykey hello
(integer) 5
127.0.0.1:6379> append mykey world
(integer) 10
127.0.0.1:6379> get mykey
"helloworld"
127.0.0.1:6379> 
複製程式碼

基本配置到此結束。

5、定義全域性命令

[root@10 src]# vi /etc/profile
# 增加
export REDIS_HOME=/opt/mongodb
export PATH=$REDIS_HOME/src:$PATH

# 生效
[root@10 src]# source /etc/profile
複製程式碼

設定完,就可以在任何目錄下,執行:

redis-cli
複製程式碼

檢視和操作資料庫啦。