linux下redis的安装与使用

发布于2017-08-10  585次阅读


先下载redis包:

wget http://download.redis.io/releases/redis-3.2.8.tar.gz

下载的是3.2.8的版本!

解压:

tar zxvf redis-3.2.8.tar.gz

解压完成后进入目录:

cd redis-3.2.8/

编译:

make

编译完成后创建文件夹:

mkdir -p /usr/local/redis

进入文件夹:

cd /usr/local/redis/

再创建两个文件夹:

mkdir -p bin conf

bin是放执行文件,conf是放配置文件

返回刚才redis的编译目录:cd 你的编译目录

进入src目录:

cd src

把这些文件移到刚刚创建的bin目录:

mv redis-cli /usr/local/redis/bin/
mv redis-sentinel /usr/local/redis/bin/
mv redis-check-aof /usr/local/redis/bin/
mv redis-benchmark /usr/local/redis/bin/
mv redis-server /usr/local/redis/bin/
mv redis-trib.rb /usr/local/redis/bin/
mv mkreleasehdr.sh /usr/local/redis/bin/
mv redis-check-rdb /usr/local/redis/bin/

然后返回上层目录:

把redis.conf移到conf目录

mv redis.conf /usr/local/redis/conf/

然后到bin目录:

cd /usr/local/redis/bin/

执行:

./redis-server

运行成功,但是这时候还 不是后台运行!

创建shell;

创建文件:

vi redis

然后把以下内容复制到文件内:

#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
# chkconfig: 2345 90 10
# description: redis is a persistent key-value database
REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis/conf/redis.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 1>/var/log/redis/infolog.log 2>/var/log/redis/errlog.log &
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
;;
restart)
stop
start
;;
*)
echo "Please use start or stop as first argument"
;;
esac

 

保存文件!

移动文件:

mv redis /etc/init.d/

进入目录:

cd /etc/init.d/

添加执行权限:

chmod a+x redis

好了返回到根目录让我来执行看看:

启动:

service redis start

报错提示:

是不存在文件夹

就创建个文件夹:

mkdir -p /var/log/redis

然后我们再才执行:

service redis start

运行成功!

让我们来看看:

 

【开放redis端口】

  1. #关闭防火墙
  2. service iptables stop
  3. vi /etc/sysconfig/iptables
  4. #添加
  5. -A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
  6. #重启防火墙
  7. service iptables restart

3>【安装phpredis扩展】

  1. tar -zxvf owlient-phpredis-2.1.1-1-g90ecd17.tar.gz
  2. cd owlient-phpredis-90ecd17
  3. /usr/local/php/bin/phpize
  4. ./configure --with-php-config=/usr/local/php/bin/php-config
  5. make
  6. make install
  7. #执行完make install后会生成
  8. #Installing shared extensions:     /usr/local/php//lib/php/extensions/no-debug-non-zts-20060613/
  9. #修改php.ini
  10. vi /usr/local/php/etc/php.ini
  11. #查找extension_dir,修改为
  12. extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
  13. #添加redis
  14. extension = redis.so
  15. #重启nginx
  16. service nginx restart

  • 微信或支付宝

道,可道,非常道;名,可名,非常名。