Linux下redis搭建与配置
一、安装依赖环境
yum install -y wget gcc make tcl
二、下载源码包
wget http://download.redis.io/releases/redis-3.2.3.tar.gz
三、编译安装redis
tar -xvzf redis-3.2.3.tar.gz
cd redis-3.2.3
make
make install prefix=/usr/local
四、创建配置文件
mkdir /etc/redis
cp redis.conf /etc/redis/redis.conf
mkdir /data/redis
五、修改redis配置文件
vi /etc/redis/redis.conf
修改以下内容:
daemonize yes
timeout 300
dir /data/redis/
六、设置redis启动
配置内核参数
vi /etc/sysctl.conf
在文件中写入
vm.overcommit_memory = 1
保存后更新配置
sysctl –p
编写启动脚本
vi /etc/init.d/redis
#!/bin/bash
#
# redis - this script starts andstops the redis-server daemon
#
# chkconfig: - 80 12
# description: Redis is apersistent key-value database
# processname: redis-server
# config: /etc/redis/redis.conf
# pidfile: /var/run/redis.pid
source /etc/init.d/functions
BIN="/usr/local/bin"
CONFIG="/etc/redis/redis.conf"
PIDFILE="/var/run/redis.pid"
### Read configuration
[ -r "$SYSCONFIG" ]&& source "$SYSCONFIG"
RETVAL=0
prog="redis-server"
desc="Redis Server"
start() {
if [ -e $PIDFILE ];then
echo "$desc already running...."
exit 1
fi
echo -n $"Starting $desc: "
daemon $BIN/$prog $CONFIG
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
return $RETVAL
}
stop() {
echo -n $"Stop $desc: "
killproc $prog
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog$PIDFILE
return $RETVAL
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
condrestart)
[ -e /var/lock/subsys/$prog ] && restart
RETVAL=$?
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0{start|stop|restart|condrestart|status}"
RETVAL=1
esac
exit $RETVAL
修改脚本权限
chmod 755 /etc/init.d/redis
添加开机自启动
chkconfig --add redis
chkconfig --level 345 redis on
验证
chkconfig --list redis
[size=14px]七、开启服务
service redis start
[size=14px]八、设置redis密码
vim /etc/redis/redis.conf
在这个配置文件中找到
# requirepass foobared
在这个参数的下面添加密码
requirepass 密码(此处密码指定密文密码)
保存退出
重启服务
service redis restart
注意:
如果某些业务和redis有关联,需要
cd /data/www/application/站点名称/config
Vim config.php
找到redis设置,然后添加ip和密码即可
|--------------------------------------------------------------------------
| Redis配置
|--------------------------------------------------------------------------
|
*/
$config['redis_host'] = '127.0.0.1';
$config['redis_port'] = '6379';
$config['redis_auth'] ='dab783839e4dc38b0f0bb98fa6fa03ce';
完成之后和相关的业务负责人进行确认
[size=14px]八、redis测试
[size=14px]1.登录redis
redis-cli -h 127.0.0.1 -aad837d8610fbd7780786869fbba97566
注释:
-h 后面是redis 的ip如果是本地redis 写127.0.0.1即可
-a 后面是在redis.conf里设置的密文密码
2.查询键
keys *键*
[size=14px]3.获取键值
get 键
3.如果可以成功获得便测试成功