【Linux学习】redis安装
redis单机安装
1、redis安装需要c语言的编译环境。如果没有gcc 需要在线安装
rmp -qa | grep gcc-c++
2、在/user/local/upload 下解压redis的软件包
tar -zxvf redis-3.0.0.tar.gz
3、得到解压后的目录 redis-3.0.0
进入 redis-3.0.0 ,执行make命令,进行编译
make
4、安装redis。
make install PREFIX=/usr/local/redis
5、启动redis。进入上面的安装目录/usr/local/redis的bin目录。
./redis-server
这种启动方式不能中断,不能关闭该窗口也不能ctrl+C退出之后redis就关闭了。
设置redis为后台启动
1、把解压后的redis-3.0.0/redis.conf复制到/usr/local/redis/bin目录下
cp redis.conf /usr/local/redis/bin/
2、在/usr/local/redis/bin目录下编辑redis.conf文件 daemonized(守护的)
3、后台启动redis
./redis-server redis.conf -->使用redis.conf配置启动redis
4、查看redis 进程
ps aux|grep redis
5、使用redis-cli 链接redis服务器
注意:默认链接的是localhost 运行在 6379 端口的 redis 服务
使用 redis-cli 的 -h (服务器地址) -p (端口)进行连接
./redis-cli -h 192.168.25.128 -p 6379
设置redis 为开机启动,避免每次开机都要重新开启redis。
1、编写脚本
vim /etc/init.d/redis
2、内容如下
#!/bin/bash # # chkconfig: 2345 10 90 # description: Start and Stop redis PATH=/usr/local/bin:/sbin:/usr/bin:/bin REDISPORT=6379 EXEC=/usr/local/redis/bin/redis-server REDIS_CLI=/usr/local/redis/bin/redis-cli PIDFILE=/var/run/redis.pid CONF="/usr/local/redis/bin/redis.conf" AUTH="1234" 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 if [ "$?"="0" ] then echo "Redis is running..." fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE exists, process is not running." else PID=$(cat $PIDFILE) echo "Stopping..." $REDIS_CLI -p $REDISPORT SHUTDOWN sleep 2 while [ -x $PIDFILE ] do echo "Waiting for Redis to shutdown..." sleep 1 done echo "Redis stopped" fi ;; restart|force-reload) ${0} stop ${0} start ;; *) echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2 exit 1 esac
3、设置redis 权限
chmod 755 /etc/init.d/redis
4、启动redis
/etc/init.d/redis start
5、设置开机启动
cd /etc/init.d/
chkconfig redis on
注意:
如果无法通过http协议访问虚拟,需要关闭防火墙,防火墙相关命令
service iptables stop 暂停
chkconfig iptables off 永久关闭
service iptables status 检查状态
后台启动也可以放入开机启动文件 [ etc]# cat /etc/rc.local #!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don‘t # want to do the full Sys V style init stuff. ulimit -n 25000 touch /var/lock/subsys/local /root/down/tomcat/bin/startup.sh /usr/local/mongodb/bin/start_mongod.sh /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf /etc/init.d/mongodbtest1026 restart You have mail in /var/spool/mail/root