1、處理超大量數據能力強 2、可以運行在便宜的PC伺服器集群上 4、對數據的高併發讀寫能力強(mysql在上萬數據的寫入對磁碟IO消耗很大) 5、對海量數據的高效存儲和訪問。 6、對數據的高可靠擴展性和高可用性 ...
該文使用centos6.5 64位 redis-3.2.8
NSQL:Not Only SQL 以key-value 形式存儲和傳統的關係型資料庫不一樣,不一定遵循傳統資料庫的一些基本要求,比如說遵循SQL標準、ACID屬性、表結構等,這類資料庫主要具有以下特點:非關係型的、分散式的、開源的、水平可擴展的。
名稱解釋:
1、key-value :js中的對象,json數據格式、java中的map,java中的javabean
2、SQL標準:insert、delete、update、select 等標準的SQL語句
3、ACID屬性:事務 ,redis中也存在事務,但很簡單。
4、表結構:通過已經建立的表向表中添加數據,redis可以在表不存在的情況下直接添加數據
NSQL 特點:
1、處理超大量數據能力強
2、可以運行在便宜的PC伺服器集群上
4、對數據的高併發讀寫能力強(mysql在上萬數據的寫入對磁碟IO消耗很大)
5、對海量數據的高效存儲和訪問。
6、對數據的高可靠擴展性和高可用性
一、redis簡介
Redis是一個開源的,先進的key-value存儲。它通常被稱為數據結構伺服器,因為鍵可以包含字元串、哈希、鏈表、集合和有序集合。
支持的數據類型:string(字元串)、list(集合)、set(集合)、zset(有序集合)。
支持的操作:這些數據類型支持push/pop、add/remove 等豐富的數據操作。支持不同方式的排序。
緩存:redis為了保證效率數據都是緩存在記憶體中的,為了防止系統突然崩潰從而導致記憶體中的數據丟失,它也可以周期性的把更新的數據寫入磁碟或者把修改操作寫入追加的記錄文件。
redis用戶:新浪微博是redis最大的用戶,200多台物理機。
redis在新浪微博中的使用場景:
1、應用程式直接訪問redis資料庫
該方式與傳統應用程式訪問mysql類似,但該方式不安全。
2、應用程式直接訪問Redis,只有當Redis訪問失敗後才訪問Mysql
a、應用程式先訪問redis server,redis與mysql集群進行數據同步
b、如果redis集群宕機後應用程式直接訪問mysql集群
3、redis使用場景:
a、區最新N個數據的操作
b、排行榜應用、取TOP N操作
c、需要精確設定過期時間的應用
d、計數器應用
e、uniq操作、獲取某段時間所有數據排重值
f、實時系統、反垃圾系統
g、Pub/Sub構建實時消息系統
h、構建隊列系統
i、redis緩存
三、redis的安裝
1、下載 https://redis.io/download 穩定版(stable)
a、解壓:[root@localhost tools]# tar -zxvf redis-3.2.8.tar.gz
b、編譯:[root@localhost redis-3.2.8]# make
如果出現錯誤 gcc:命令未找到 則安裝gcc yum install -y gcc g++ gcc-c++ make
如果出現 tcl 問題 則安裝tcl yum install tcl
c、安裝 [root@localhost src]# make install
在src下將出現redis的相關命令
-rwxr-xr-x. 1 root root 5707211 2月 17 22:33 redis-cli 進入redis客戶端命令
-rwxr-xr-x. 1 root root 7827978 2月 17 22:33 redis-server 啟動redis服務命令
2、配置redis
(1)[root@localhost src]# mkdir -p /usr/local/redis/bin 創建文件夾用戶存儲redis命令
(2)[root@localhost src]# mkdir -p /usr/local/redis/etc 創建文件夾用戶存儲redis配置文件
(3)將/tools/redis-3.2.8下的 redis.conf移動到/usr/local/redis/etc下
[root@localhost redis-3.2.8]# mv ./redis.conf /usr/local/redis/etc
(4)將/tools/redis-3.2.8/src下的 mkreleasehdr.sh、redis-benchmark、redis-check-aof、redis-check-rdb、redis-cli、redis-server移動到/usr/local/redis/bin下
(5)啟動redis服務:進入redis bin目錄下 /usr/local/redis/bin
[root@localhost bin]# ./redis-server
如果出現 -bash: ./redis-server: 許可權不夠
查看許可權:[root@localhost bin]# ls -l | grep -i redis-server
賦予xr許可權 [root@localhost bin]# chmod 755 redis-server
[root@localhost bin]# ls -l | grep -i redis-server
-rw-rw-r--. 1 root root 7827978 2月 17 22:52 redis-server
[root@localhost bin]# chmod 755 redis-server
[root@localhost bin]# ls -l | grep -i redis-server
-rwxr-xr-x. 1 root root 7827978 2月 17 22:52 redis-server
提示信息
[root@localhost bin]# ./redis-server
5322:C 17 Feb 23:18:39.086 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
5322:M 17 Feb 23:18:39.087 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.8 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 5322
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
5322:M 17 Feb 23:18:39.113 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
5322:M 17 Feb 23:18:39.113 # Server started, Redis version 3.2.8
5322:M 17 Feb 23:18:39.114 # 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.
5322:M 17 Feb 23:18:39.114 # 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 must be restarted after THP is disabled.
5322:M 17 Feb 23:18:39.114 * The server is now ready to accept connections on port 6379
安裝成功埠 6379
使用指定的配置文件啟動redis伺服器
[root@localhost bin]# ./redis-server /usr/local/redis/etc/redis.conf
四、redis後臺運行
################################# GENERAL #####################################
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize no
daemonize no:預設啟動方式該方式占用一個視窗,一旦ctrl+c redis伺服器就關閉
daemonize yes:將redis的啟動方式改為後臺運行
進入:redis 客戶端
[root@localhost bin]# pwd
/usr/local/redis/bin
[root@localhost bin]# ./redis-cli
127.0.0.1:6379>
關閉redis伺服器:
[root@localhost bin]# pkill redis-serve (通過進程的方式)
[root@localhost bin]# ./redis-cli shutdown(通過redis命令)