- 介绍与安装
- Redis 的解释
– Redis [Remote Directory Server] 远程服务器字典 - 下载安装
– wget redis.tar.gz
– cd redis
– make
– make install
- Redis 的解释
- 在bin下可执行的程序
- redis-server Redis服务器
- redis-cli: 命令行客户端
- redis-benchmark: redis的性能测试工具
- redis-check-aof: AOF文件修复工具
- redis-check-dump: RDB文件检查工具
- redis.conf 是redis的配置文件,将配置文件中daemonize yes 以守护进程的方式来使用
启动redis
– 编译安装的: redis-server /etc/redis.conf
– ubuntu :
\cp 源码安装目录/init.d/init.d.redis /etc/init.d/redis
sudo /etc/init.d/redis start
源码安装脚本[摘取子lnmp-redis安装脚本]
#!/bin/bash
Install_Redis()
{
ver="1"
echo "Which version do you want to install:"
echo "Install Redis 3.0.1 Stable Version please type: 1"
echo "Install Redis 2.8.20 Old Version please type: 2"
read -p "Enter 1 or 2 (Default Stable version): " ver
if [ "${ver}" = "" ]; then
ver="1"
fi
if [ "${ver}" = "1" ]; then
echo "You will install Redis 3.0.1 Stable Version"
elif [ "${ver}" = "2" ]; then
echo "You will install Redis 2.8.20 Old Version"
else
echo "Input error,please input 1 or 2 !"
echo "Please Rerun $0"
exit 1
fi
echo "====== Installing Redis ======"
Press_Install
sed -i '/redis.so/d' /usr/local/php/etc/php.ini
Get_PHP_Ext_Dir
zend_ext="${zend_ext_dir}redis.so"
if [ -s "${zend_ext}" ]; then
rm -f "${zend_ext}"
fi
cd ${cur_dir}/src
if [ "${ver}" = "1" ]; then
Download_Files http://download.redis.io/releases/${Redis_Stable_Ver}.tar.gz ${Redis_Stable_Ver}.tar.gz
Tar_Cd ${Redis_Stable_Ver}.tar.gz ${Redis_Stable_Ver}
else
Download_Files http://download.redis.io/releases/${Redis_Old_Ver}.tar.gz ${Redis_Old_Ver}.tar.gz
Tar_Cd ${Redis_Old_Ver}.tar.gz ${Redis_Old_Ver}
fi
if [ "${Is_64bit}" = "y" ] ; then
make PREFIX=/usr/local/redis install
else
make CFLAGS="-march=i686" PREFIX=/usr/local/redis install
fi
mkdir -p /usr/local/redis/etc/
\cp redis.conf /usr/local/redis/etc/
sed -i 's/daemonize no/daemonize yes/g' /usr/local/redis/etc/redis.conf
sed -i 's/# bind 127.0.0.1/bind 127.0.0.1/g' /usr/local/redis/etc/redis.conf
cd ../
if [ -s /sbin/iptables ]; then
/sbin/iptables -I INPUT -p tcp -s 127.0.0.1 --dport 6379 -j ACCEPT
/sbin/iptables -A INPUT -p tcp --dport 6379 -j DROP
if [ "$PM" = "yum" ]; then
service iptables save
elif [ "$PM" = "apt" ]; then
iptables-save > /etc/iptables.rules
fi
fi
if [ -s ${PHPRedis_Ver} ]; then
rm -rf ${PHPRedis_Ver}
fi
Download_Files http://pecl.php.net/get/${PHPRedis_Ver}.tgz ${PHPRedis_Ver}.tgz
Tar_Cd ${PHPRedis_Ver}.tgz ${PHPRedis_Ver}
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
cd ../
sed -i '/the dl()/i\
extension = "redis.so"' /usr/local/php/etc/php.ini
\cp ${cur_dir}/init.d/init.d.redis /etc/init.d/redis
chmod +x /etc/init.d/redis
echo "Add to auto start..."
StartUp redis
Restart_PHP
/etc/init.d/redis start
echo "====== Redis install completed ======"
echo "Redis installed successfully, enjoy it!"
}
Uninstall_Redis()
{
echo "You will uninstall Redis..."
Press_Start
sed -i '/redis.so/d' /usr/local/php/etc/php.ini
Restart_PHP
Remove_StartUp redis
echo "Delete Redis files..."
rm -rf /usr/local/redis
rm -rf /etc/init.d/redis
if [ -s /sbin/iptables ]; then
/sbin/iptables -D INPUT -p tcp -s 127.0.0.1 --dport 6379 -j ACCEPT
/sbin/iptables -D INPUT -p tcp --dport 6379 -j DROP
if [ "$PM" = "yum" ]; then
service iptables save
elif [ "$PM" = "apt" ]; then
iptables-save > /etc/iptables.rules
fi
fi
echo "Uninstall Redis completed."
}
- 命令返回值
- 状态回复
- ping
- SET test ‘this is a test’
- 错误回复
- 错误回复以error开始
- (error) ERR unknown command ‘TESTERROR’
- 整数回复
- 以interger 数值
- (interger) 2
- 字符串回复
- GET test
- test 存在 返回test的值
- test 不存在 返回 (nil) 代表空的结果
- 多行字符串回复
- KEYS *
- 得到当前数据库中存在的键名
- 状态回复
Redis 配置选项相关的内容
和链接有关的- 动态获取/设置Redis 的配置选项的值
- 获取
- CONFIG GET name
- 设置
- CONFIG SET name value
- 获取
- Redis配置文件redis.conf选项相关
port
6379 默认端口bind
127.0.0.1 默认绑定的主机地址timeout
0 当客户端闲置多久后关闭链接,0代表没有开启这个选项loglevel notice
日志的记录级别
– debug: 很详细的信息,适合开发和测试
– verbose: 包含很多不太有用的信息
– notice: 比较适合生产环境
– warning: 警告信息logfile
stdout: 记录日志的方式, 默认为标准输出databases
16, 默认数据库的个数16个, 默认的数据编号从0开始
和快照有关的
rdb模式:
- save
<seconds>
<changes>
多少秒内有多少次更改将其同步到磁盘中的数据文件里 - save 900 1
900秒内有1次更改将其同步到磁盘中的数据文件里
- rdbcompression yes 存储本地数据库时是否启用压缩, 默认yes
- dbfilename dump.rdb 指定本地数据库文件名, 默认为dump.rdb
- save
aof模式:
- appendonly yes|no 开启和关闭
- aof文件的保存位置和rdb文件的位置相同,都是dir参数设置的,默认的文件名是appendonly.aof,可以通过appendfilename参数修改
appendfilename appendonly.aof
aof日志文件重写auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
也可手动执行bgrewriteaof
进行重写
redis写命令同步的时机appendfsync always
每次都会执行appendfsync everysec
默认 每秒执行一次同步操作(推荐)appendfsync no
不主动进行同步,由操作系统来做,30秒一次redis-check-aof
文件修复
dir ./ 指定本地数据库的存放目录, 默认是当前目录
dump.rdb|*.aof都会存储到这里
hash-max-ziplist-entries 512 字节
- hash-max-ziplist-value 64 字段数目
- 动态获取/设置Redis 的配置选项的值