进入下载目录
下载redis
- wget http://download.redis.io/releases/redis-3.0.5.tar.gz
解压缩
- tar -zxvf redis-3.0.5.tar.gz
进入安装目录
编译安装
如何启动
检测客户端检测连接是否正常
停止服务
ctrl+C 跳出
如何开机自启动
启动脚本 redis_init_script 位于位于redis的 /utils/目录下
复制此脚本文件至/etc/init.d目录下
- cp /usr/local/redis-3.0.5/utils/redis_init_script /etc/init.d/redis
修改文件
- #!/bin/sh
- # chkconfig: 2345 80 90
- # Simple Redis init.d script conceived to work on Linux systems
- # as it does use of the /proc filesystem.
-
- REDISPORT=6379
- EXEC=/usr/local/redis-3.0.5/src/redis-server
- CLIEXEC=/usr/local/redis-3.0.5/src/redis-cli
-
- PIDFILE=/var/run/redis_${REDISPORT}.pid
- CONF="/etc/redis/${REDISPORT}.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 &
- 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
- ;;
- *)
- echo "Please use start or stop as first argument"
- ;;
- esac
和原配置文件相比:
1.原文件是没有以下第2行的内容的,
#chkconfig: 2345 80 90
我自己改写的文件
- #!/bin/bash
- # Startup script for the redis
- # chkconfig: 2345 80 90
- # description: The redis daemon is a network memory cache service.
- # processname: redis
- # pidfile: /var/run/redis.pid
- REDISPORT=6379
- EXEC=/usr/local/redis-3.0.5/src/redis-server
- CLIEXEC=/usr/local/redis-3.0.5/src/redis-cli
- PIDFILE=/var/run/redis_${REDISPORT}.pid
- CONF="/etc/redis/${REDISPORT}.conf"
- RETVAL=0
- prog="redis"
- # Source function library.
- . /etc/init.d/functions
- # Source networking configuration.
- . /etc/sysconfig/network
- # Check that networking is up.
- [ ${NETWORKING} = "no" ] && exit 0
- [ -x $EXEC ] || exit 0
- # Start redis daemons functions.
- start() {
- if [ -f $PIDFILE ];then
- echo "$PIDFILE exists, process is already running or crashed"
- exit 1
- fi
- echo $"Starting $prog: "
- $EXEC $CONF &
- RETVAL=$?
- return $RETVAL
- }
- # Stop redis daemons functions.
- stop() {
- if [ ! -f $PIDFILE ];then
- echo "$PIDFILE exists, process is already running or crashed"
- exit 1
- fi
- echo -n $"Stopping $prog: "
- PID=$(cat $PIDFILE)
- $CLIEXEC -p $REDISPORT shutdown
- while [ -x /proc/${PID} ]
- do
- echo "Waiting for Redis to shutdown ..."
- sleep 1
- done
- echo "redis stopped"
- RETVAL=$?
- }
- # See how we were called.
- case "$1" in
- start)
- start
- ;;
- stop)
- stop
- ;;
- restart|reload)
- stop
- start
- ;;
- *)
- echo $"Usage: $prog {start|stop|restart|reload}"
- exit 1
- esac
- exit $RETVAL
你喜欢的话,也可以用我改写的噢
2.原文件EXEC、CLIEXEC参数,也是有所更改。
1
2
|
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli
|
3.redis开启的命令,以后台运行的方式执行,加上 &
$EXEC $CONF &
ps:注意后面的那个“&”,即是将服务转到后面运行的意思,否则启动服务时,redis服务将 占据在前台,占用了主用户界面,造成其它的命令执行不了。
将redis配置文件拷贝到/etc/redis/${REDISPORT}.conf
- cp /usr/local/redis-3.0.5/redis.conf /etc/redis/6379.conf
修改此配置文件
- daemonize no //修改为yes 使redis以守护进程模式启动
-
- pidfile /var/run/redis.pid //修改为/var/run/redis_6379.pid 设置redis的PID文件的路径
赋予redis文件执行权限
- chmod +x /etc/init.d/redis
添加服务
设置开机启动
- chkconfig --level 35 redis on
查看是否设置成功
- chkconfig --list | grep redis
此状态下表面开机启动成功
service方式启动redis服务
service方式停止redis服务
将redis的命令所在目录添加到系统参数PATH中
修改profile文件:
追加
- export PATH=$PATH:$JAVA_HOME/bin:/usr/local/redis-3.0.5/src
使配置文件立即生效
这样就可以直接调用redis-cli的命令了,如下所示:
退出客户端输入quit即可
检测后台进程是否存在
检测6379端口是否在监听
- netstat -tunpl | grep 6379
密码认证
打开 /etc/redis.conf 修改 requirepass 配置项
- # vim /etc/redis.conf
-
- requirepass test123
- # service redis restart
- Stopping redis-server: [ OK ]
- Starting redis-server: [ OK ]
-
- # redis-cli
- redis 127.0.0.1:6379> set h helloworld
- (error) ERR operation not permitted
auth test123
- redis 127.0.0.1:6379> auth test123
- OK
- redis 127.0.0.1:6379> set h helloworld
- OK
- redis 127.0.0.1:6379> get h
- "helloworld"
- redis 127.0.0.1:6379> exit
redis-cli -a 参数指定密码
- # redis-cli -a test123
- redis 127.0.0.1:6379> set h helloworld
- OK
- redis 127.0.0.1:6379> get h
- "helloworld"
- redis 127.0.0.1:6379> exit
通过 config 动态改变密码,无需重新启动 redis 进程
- # redis-cli -a test123
- redis 127.0.0.1:6379> config get requirepass
- 1) "requirepass"
- 2) "test123"
- redis 127.0.0.1:6379> config set requirepass passabc
- OK
- redis 127.0.0.1:6379> auth passabc
- OK
- redis 127.0.0.1:6379> set h helloworld
- OK
- redis 127.0.0.1:6379> get h
- "helloworld"
- redis 127.0.0.1:6379> exit
master/slave 模式, master 有密码, slave 怎样配置?
redis的主从配置
1、选中一台服务器作为master
2、其它服务器作为slave
修改/etc/redis/6379.conf配置文件
找到# slaveof <masterip> <masterport>
把此配置注释放开,写上master的ip 端口号即可
如
- slaveof 192.168.1.101 6379
重启服务,即可发现master机子中的相关数据已经同步到所有的slave从机中
windows下下载redis安装包,可以到redis官网下载:http://redis.io或者github上面下载https://github.com/antirez/redis,然后解压:
如图
其实这2个版本是一样的,我用的是最后一个,把最后一个解压出来:
如图
redis.windows.conf这个就是redis在windows下面的配置文件,解压了redis不会把服务安装到计算机里面的服务列表里面,不安装在服务里面很蛋疼的,每次都要开一个黑窗口!所以我们得把redis安装到我们自己windows服务器里面,用如下命令把redis服务安装到计算机里面:
进入redis的安装目录运行 redis-server --service-install redis.windows.conf 把redis发布到windows服务里面
打开计算机服务列表就可以看到了:
这样我们在windows下就安装完成了。
接下来我们要安装一个客户端来连接redis,推荐使用
perfect!!!