基本
內存 + 持久化數據庫
數據類型:字符串,列表,集合,散列表,有序集合
應用場景:發布/訂閱,隊列,主從復制,動態擴容,腳本操作,持久化
優點:沒有Schame約束,數據結構變更容易,抗壓能力強,性能極高
缺點:沒有索引,沒有外鍵,缺少int/date等基本數據類型,多條件查詢需要通過內聯集合
Redis安裝
下載:
# 下載
wget http://download.redis.io/releases/redis-5.0.4.tar.gz
# 解壓
tar -zxvf redis-5.0.4.tar.gz -C /usr/local
# 編譯
cd /usr/local/redis-5.0.4
make MALLOC=libc && make install
如果不能編譯,嘗試安裝依賴的庫:yum -y install gcc automake autoconf libtool make
三種啟動方式
1、直接啟動
./src/redis-server # 關閉會話后進程退出
[root@localhost src]# ./redis-server
24725:C 21 Mar 2019 10:24:52.670 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
24725:C 21 Mar 2019 10:24:52.671 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=24725, just started
24725:C 21 Mar 2019 10:24:52.671 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
24725:M 21 Mar 2019 10:24:52.672 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.4 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 24725
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
24725:M 21 Mar 2019 10:24:52.675 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
24725:M 21 Mar 2019 10:24:52.675 # Server initialized
24725:M 21 Mar 2019 10:24:52.675 # 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.
24725:M 21 Mar 2019 10:24:52.676 # 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.
24725:M 21 Mar 2019 10:24:52.676 * Ready to accept connections
2、守護進程啟動
redis.conf
vim redis.conf
# daemonize 修改為 yes
daemonize yes
./src/redis-server redis.conf
[root@localhost redis-5.0.4]# vim redis.conf
[root@localhost redis-5.0.4]# ./src/redis-server redis.conf
24731:C 21 Mar 2019 10:27:11.770 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
24731:C 21 Mar 2019 10:27:11.770 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=24731, just started
24731:C 21 Mar 2019 10:27:11.770 # Configuration loaded
3、配置腳本開機啟動
創建目錄
mkdir /etc/redis
cp redis.conf /etc/redis/6379.conf
# 開機啟動腳本通常以d結尾
cp utils/redis_init_script /etc/init.d/redisd
cd /etc/init.d
編輯腳本
- 添加代碼 [chkconfig: 2345 90 10]
#!/bin/sh
#
# chkconfig: 2345 90 10
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
### BEGIN INIT INFO
# Provides: redis_6379
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Redis data structure server
# Description: Redis data structure server. See https://redis.io
### END INIT INFO
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac
- 執行命令
chkconfig redisd on
# 啟動redis
service redisd start
出現的問題
/var/run/redis_6379.pid exists, process is already running or crashed
# 解決方法,刪除對應pid
rm -rf /var/run/redis_6379.pid
/etc/init.d/redisd: line 29: /usr/local/bin/redis-server: No such file or directory
# 解決1:將redis-server和redis-cli拷貝到/usr/local/bin目錄下
# 解決2:修改腳本,指定命令的路徑
EXEC=/usr/local/redis-5.0.4/src/redis-server
CLIEXEC=/usr/local/redis-5.0.4/src/redis-cli
測試啟動
[root@localhost soft]# redis-cli
127.0.0.1:6379> ping
PONG
redis的通用命令
- keys pattern:模式匹配key(慎用慎用慎用)
- dbsize:key總數
- exists key:key是否存在
- del key [key...]:刪除key
- expire key seconds:設置過期時間
- ttl key:查看還剩多長時間過期(-1為永久不過期;-2為已刪除)
- persist key:取消過期時間
- type key:返回key的類型
127.0.0.1:6379> keys *
1) "name2"
2) "name"
127.0.0.1:6379> set age 12
OK
127.0.0.1:6379> keys nam*
1) "name2"
2) "name"
127.0.0.1:6379> exists name
(integer) 1
127.0.0.1:6379> del key a b c d e
(integer) 4
127.0.0.1:6379> exists name2
(integer) 1
127.0.0.1:6379> del name2
(integer) 1
127.0.0.1:6379> exists name2
(integer) 0
127.0.0.1:6379> expire name 10
(integer) 1
127.0.0.1:6379> ttl name
(integer) 5
127.0.0.1:6379> ttl name
(integer) 2
127.0.0.1:6379> ttl name
(integer) -2
127.0.0.1:6379> expire name 10
(integer) 1
127.0.0.1:6379> ttl name
(integer) 8
127.0.0.1:6379> persist name
(integer) 1
127.0.0.1:6379> ttl name
(integer) -1
127.0.0.1:6379> type name
string
