相對(duì)于 MySQL娜扇,redis 基于內(nèi)存操作可提供較好的速度體驗(yàn),并且也可以提供持久化存儲(chǔ)栅组,數(shù)據(jù)類(lèi)型豐富雀瓢,存取方便,作為緩存玉掸,值得使用刃麸。
Redis is an in-memory database that persists on disk. The data model is key-value, but many different kind of values are supported: Strings, Lists, Sets, Sorted Sets, Hashes, HyperLogLogs, Bitmaps.
- Another Redis Desktop Manager: 這個(gè)桌面客戶(hù)端能用。似乎是個(gè)中國(guó)人司浪。
安裝
cd /home/tools
wget http://download.redis.io/releases/redis-3.2.4.tar.gz
tar xzf redis-3.2.4.tar.gz
cd redis-3.2.4
make
make PREFIX=/usr/local/redis install
make PREFIX=/usr/local install
make PREFIX=/usr install
cp redis.conf /usr/local/redis/redis_example.conf
- make 后租谈,即可運(yùn)行編譯好的文件篮奄;
src/redis-server
src/redis-client
- make [PREFIX=/some/other/directory] install 安裝到指定目錄;
默認(rèn)安裝在 /usr/local/bin 下割去;
redis.conf 配置 片段
dir /var/lib/redis/
slave-read-only yes
- dir 指定持久化數(shù)據(jù)存放目錄 /var/lib/redis/窟却;
- 配置
slaveof
項(xiàng),就表明這個(gè) redis 實(shí)例是一個(gè) slave呻逆,運(yùn)行在 slave 狀態(tài)夸赫; - 訪(fǎng)問(wèn) slave 時(shí),假如 slave 設(shè)置為 slave-read-only咖城,那么訪(fǎng)問(wèn)方式茬腿;
- 配置文件存放到 /etc/ 下呼奢,或者 /usr/local/redis/ 下;
- 配置文件命名 redis_6379.conf滓彰,6379 是服務(wù)端口控妻;
- bind 指令指定本機(jī)(127.0.0.1)和內(nèi)網(wǎng)地址(192.168.*.*,通常不要對(duì)外開(kāi)放 redis 服務(wù))
當(dāng)你沒(méi)有使用 bind 指令(也就是綁定所有網(wǎng)絡(luò)地址)揭绑,也沒(méi)有設(shè)認(rèn)證密碼時(shí)弓候,你只能在本機(jī)訪(fǎng)問(wèn)該服務(wù);
(error) DENIED. Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
- 機(jī)器變更內(nèi)網(wǎng) ip 地址時(shí)他匪,注意更改 redis.conf 文件菇存,并重啟服務(wù);
啟動(dòng)停止
- 啟動(dòng)
redis-server /usr/local/redis/redis_6379.conf
redis-server /usr/local/redis/redis_6380.conf
- 關(guān)閉
redis-cli
進(jìn)入交互界面邦蜜,shutdown
即可依鸥;
持久化
- 了解 CAP 定理(CAP theorem)
Consistence、Availability悼沈、Partition tolerance贱迟;
Master/Slave Replication 復(fù)制工作原理
- slave 啟動(dòng)時(shí),連接上 master絮供,發(fā)送 PSYNC 命令衣吠;
- master 接到命令,啟動(dòng)一個(gè)后臺(tái)進(jìn)程保存數(shù)據(jù)到 rdb 文件壤靶;
- master 和 slave 之間傳送 rdb 文件缚俏;
- slave 將文件加載到內(nèi)存;
- 初始文件數(shù)據(jù)傳送完畢之后的傳送就是命令流了贮乳;
- slave 支持重連忧换;重連之后就只傳送增量?jī)?nèi)容了(partial resynchronization);
slave configuration
- 選項(xiàng):
slaveof 192.168.1.1 6379
在 slave 的配置文件中向拆,使用 slaveof 指定 master 的 ip & port 即可亚茬;
通過(guò)命令行slaveof <ip> <port>
命令可臨時(shí)連接指定的 master; - 選項(xiàng):
slave-read-only yes
yes 是默認(rèn)設(shè)置亲铡; - masterauth <password>
如果 master 通過(guò) requirepass 設(shè)置了認(rèn)證才写,則 slave 可通過(guò) masterauth 給出認(rèn)證需要的密碼;
通過(guò)命令行:config set masterauth <password>
可臨時(shí)設(shè)置奖蔓;
redis-cli
redis-cli -p 6380
-
redis-cli --help
會(huì)打印redis-cli的用法,各個(gè)OPTIONS 的含義讹堤。 -
redis-cli -c
-c 參數(shù)指明 cluster 模式吆鹤。Enable cluster mode (follow -ASK and -MOVED redirections)。 - 查看庫(kù)2中那些沒(méi)有設(shè)置過(guò)期的keys洲守;
redis-cli -n 2 keys '*' | while read LINE; do TTL=
redis-cli -n 2 ttl $LINE
; if [ $TTL -eq -1 ]; then echo "$LINE"; fi; done;
Redis Administration
- set the Linux kernel overcommit memory setting to 1
echo vm.overcommit_memory = 1 >> /etc/sysctl.conf
sysctl vm.overcommit_memory=1
立即生效疑务;
redis命令介紹
-
redis-benchmark
:性能測(cè)試工具 -
redis-check-aof
:數(shù)據(jù)修復(fù) -
redis-check-dump
:檢查導(dǎo)出工具 -
redis-check-rdb
: -
redis-cli
:命令行工具沾凄;提供交互操作界面;
-n 指定 database number知允; redis-sentinel
-
redis-server
:服務(wù)程序撒蟀;