一、redis 安装:
1)Redis介绍
Redis 是一个高性能的key-value数据库。
Redis本质上一个Key/Value数据库,与Memcached类似的NoSQL型数据库, 但是他的数据可以持久化的保存在磁盘上,解决了服务重启后数据不丢失的问题,它的值可以是string(字符串)、list(列表)、sets(集合)或者是ordered sets(被排序的集合),所有的数据类型都具有push/pop、add/remove、执行服务端的并集、交集、两个sets集中的差别等等操作,这些操作都是具有原子性的,Redis还支持各种不同的排序能力。
2)下载
wget http://redis.googlecode.com/files/redis-2.6.13.tar.gz
安装:
tar xf redis-2.6.13.tar.gz
cd redis-2.6.13
解决办法:
yum -y install jemalloc jemalloc-devel
make MALLOC=libc
即可解决
make install
cp redis.conf /etc/
cd src/
cp redis-benchmark redis-cli redis-server /usr/bin/
echo vm.overcommit_memory=1>>/etc/sysctl.conf 让内存自由分配
sysctl -p
redis-server /etc/redis.conf 启动redis服务器
尝试连接数据库
二、已安装redis,清空redis缓存及其他操作示例:
进入redis src目录:
./redis-cli -h 10.11.233.18 -p 7980 -n db
输入密码:auth 'pwd'
[root@iZ254lfyd6nZ ~]# cd /
[root@iZ254lfyd6nZ /]# ls
bin boot dev etc home lib lib64 lost+found media mnt opt proc root sbin selinux srv sys tmp usr var
[root@iZ254lfyd6nZ /]# cd usr
[root@iZ254lfyd6nZ usr]# ls
bin etc games include lib lib64 libexec local sbin share src tmp
[root@iZ254lfyd6nZ usr]# cd local
[root@iZ254lfyd6nZ local]# ls
aegis bin etc games include lib lib64 libexec redis redis-2.8.12.tar.gz sbin share src
[root@iZ254lfyd6nZ local]# cd redis
[root@iZ254lfyd6nZ redis]# ls
00-RELEASENOTES CONTRIBUTING deps INSTALL MANIFESTO redis.conf runtest-sentinel src utils
BUGS COPYING dump.rdb Makefile README runtest sentinel.conf tests
[root@iZ254lfyd6nZ redis]# cd src
[root@iZ254lfyd6nZ src]# ls
adlist.c aof.o db.o intset.h mkreleasehdr.sh rand.c redis-check-dump replication.o sha1.h t_list.o ziplist.o
adlist.h asciilogo.h debug.c intset.o multi.c rand.h redis-check-dump.c rio.c sha1.o t_set.c zipmap.c
adlist.o bio.c debug.o lzf_c.c multi.o rand.o redis-check-dump.o rio.h slowlog.c t_set.o zipmap.h
ae.c bio.h dict.c lzf_c.o networking.c rdb.c redis-cli rio.o slowlog.h t_string.c zipmap.o
ae_epoll.c bio.o dict.h lzf_d.c networking.o rdb.h redis-cli.c scripting.c slowlog.o t_string.o zmalloc.c
ae_evport.c bitops.c dict.o lzf_d.o notify.c rdb.o redis-cli.o scripting.o solarisfixes.h t_zset.c zmalloc.h
ae.h bitops.o endianconv.c lzf.h notify.o redisassert.h redis.h sds.c sort.c t_zset.o zmalloc.o
ae_kqueue.c config.c endianconv.h lzfP.h object.c redis-benchmark redis.o sds.h sort.o util.c
ae.o config.h endianconv.o Makefile object.o redis-benchmark.c redis-sentinel sds.o syncio.c util.h
ae_select.c config.o fmacros.h Makefile.dep pqsort.c redis-benchmark.o redis-server sentinel.c syncio.o util.o
anet.c crc64.c help.h memtest.c pqsort.h redis.c release.c sentinel.o testhelp.h valgrind.sup
anet.h crc64.h hyperloglog.c memtest.o pqsort.o redis-check-aof release.h setproctitle.c t_hash.c version.h
anet.o crc64.o hyperloglog.o migrate.c pubsub.c redis-check-aof.c release.o setproctitle.o t_hash.o ziplist.c
aof.c db.c intset.c migrate.o pubsub.o redis-check-aof.o replication.c sha1.c t_list.c ziplist.h
[root@iZ254lfyd6nZ src]# ./redis-cli
127.0.0.1:6379> get name
"tanglei1234"
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) ""
127.0.0.1:6379> config set requirepass newpsw
OK
127.0.0.1:6379> config get requirepass
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth newpsw
OK
127.0.0.1:6379> get name
"tanglei1234"
127.0.0.1:6379>
Connection closed by foreign host.
三、其他配置:
#直接make 编译
make
#可使用root用户执行`make install`,将可执行文件拷贝到/usr/local/bin目录下。这样就可以直接敲名字运行程序了。
make install
启动
#加上`&`号使redis以后台程序方式运行
./redis-server &
检测
#检测后台进程是否存在
ps -ef |grep redis
#检测6379端口是否在监听
netstat -lntp | grep 6379
#使用`redis-cli`客户端检测连接是否正常
./redis-cli
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set key "hello world"
OK
127.0.0.1:6379> get key
"hello world"
停止
#使用客户端
redis-cli shutdown
#因为Redis可以妥善处理SIGTERM信号,所以直接kill -9也是可以的
kill -9 PID
通过指定配置文件启动
配置文件
可为redis服务启动指定配置文件,配置文件 redis.conf 在Redis根目录下。
#修改daemonize为yes,即默认以后台程序方式运行(还记得前面手动使用&号强制后台运行吗)。
daemonize no
#可修改默认监听端口
port 6379
#修改生成默认日志文件位置
logfile "/home/futeng/logs/redis.log"
#配置持久化文件存放位置
dir /home/futeng/data/redisData
启动时指定配置文件
redis-server ./redis.conf
#如果更改了端口,使用`redis-cli`客户端连接时,也需要指定端口,例如:
redis-cli -p 6380
其他启停同 直接启动 方式。配置文件是非常重要的配置工具,随着使用的逐渐深入将显得尤为重要,推荐在一开始就使用配置文件。
使用Redis启动脚本设置开机自启动
启动脚本
推荐在生产环境中使用启动脚本方式启动redis服务。启动脚本 redis_init_script 位于位于Redis的 /utils/ 目录下。
#大致浏览下该启动脚本,发现redis习惯性用监听的端口名作为配置文件等命名,我们后面也遵循这个约定。
#redis服务器监听的端口
REDISPORT=6379
#服务端所处位置,在make install后默认存放与`/usr/local/bin/redis-server`,如果未make install则需要修改该路径,下同。
EXEC=/usr/local/bin/redis-server
#客户端位置
CLIEXEC=/usr/local/bin/redis-cli
#Redis的PID文件位置
PIDFILE=/var/run/redis_${REDISPORT}.pid
#配置文件位置,需要修改
CONF="/etc/redis/${REDISPORT}.conf"
配置环境
1. 根据启动脚本要求,将修改好的配置文件以端口为名复制一份到指定目录。需使用root用户。
mkdir /etc/redis
cp redis.conf /etc/redis/6379.conf
2. 将启动脚本复制到/etc/init.d目录下,本例将启动脚本命名为redisd(通常都以d结尾表示是后台自启动服务)。
cp redis_init_script /etc/init.d/redisd
3. 设置为开机自启动
此处直接配置开启自启动 chkconfig redisd on 将报错误: service redisd does not support chkconfig
参照 此篇文章 ,在启动脚本开头添加如下两行注释以修改其运行级别:
#!/bin/sh
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
#
再设置即可成功。
#设置为开机自启动服务器
chkconfig redisd on
#打开服务
service redisd start
#关闭服务
service redisd stop