Linux操作系统安装redis

时间:2021-05-26 09:43:40

进入下载目录

  
  
 
 
  1. cd /usr/local

下载redis

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

解压缩

  
  
 
 
  1. tar -zxvf redis-3.0.5.tar.gz

进入安装目录

  
  
 
 
  1. cd redis-3.0.5/src


编译安装

  
  
 
 
  1. make install

如何启动

  
  
 
 
  1. redis-server &

检测客户端检测连接是否正常

  
  
 
 
  1. redis-cli
停止服务

Linux操作系统安装redis

ctrl+C 跳出


如何开机自启动

启动脚本 redis_init_script 位于位于redis的 /utils/目录下

Linux操作系统安装redis

复制此脚本文件至/etc/init.d目录下

  
  
 
 
  1. cp /usr/local/redis-3.0.5/utils/redis_init_script /etc/init.d/redis

修改文件

  
  
 
 
  1. vi /etc/init.d/redis

  
  
 
 
  1. #!/bin/sh
  2. # chkconfig: 2345 80 90
  3. # Simple Redis init.d script conceived to work on Linux systems
  4. # as it does use of the /proc filesystem.
  5.  
  6. REDISPORT=6379
  7. EXEC=/usr/local/redis-3.0.5/src/redis-server
  8. CLIEXEC=/usr/local/redis-3.0.5/src/redis-cli
  9.  
  10. PIDFILE=/var/run/redis_${REDISPORT}.pid
  11. CONF="/etc/redis/${REDISPORT}.conf"
  12.  
  13. case "$1" in
  14. start)
  15. if [ -f $PIDFILE ]
  16. then
  17. echo "$PIDFILE exists, process is already running or crashed"
  18. else
  19. echo "Starting Redis server..."
  20. $EXEC $CONF &
  21. fi
  22. ;;
  23. stop)
  24. if [ ! -f $PIDFILE ]
  25. then
  26. echo "$PIDFILE does not exist, process is not running"
  27. else
  28. PID=$(cat $PIDFILE)
  29. echo "Stopping ..."
  30. $CLIEXEC -p $REDISPORT shutdown
  31. while [ -x /proc/${PID} ]
  32. do
  33. echo "Waiting for Redis to shutdown ..."
  34. sleep 1
  35. done
  36. echo "Redis stopped"
  37. fi
  38. ;;
  39. *)
  40. echo "Please use start or stop as first argument"
  41. ;;
  42. esac

和原配置文件相比: 

1.原文件是没有以下第2行的内容的,

#chkconfig: 2345 80 90 


我自己改写的文件

  
  
 
 
  1. #!/bin/bash
  2. # Startup script for the redis
  3. # chkconfig: 2345 80 90
  4. # description: The redis daemon is a network memory cache service.
  5. # processname: redis
  6. # pidfile: /var/run/redis.pid
  7. REDISPORT=6379
  8. EXEC=/usr/local/redis-3.0.5/src/redis-server
  9. CLIEXEC=/usr/local/redis-3.0.5/src/redis-cli
  10. PIDFILE=/var/run/redis_${REDISPORT}.pid
  11. CONF="/etc/redis/${REDISPORT}.conf"
  12. RETVAL=0
  13. prog="redis"
  14. # Source function library.
  15. . /etc/init.d/functions
  16. # Source networking configuration.
  17. . /etc/sysconfig/network
  18. # Check that networking is up.
  19. [ ${NETWORKING} = "no" ] && exit 0
  20. [ -x $EXEC ] || exit 0
  21. # Start redis daemons functions.
  22. start() {
  23. if [ -f $PIDFILE ];then
  24. echo "$PIDFILE exists, process is already running or crashed"
  25. exit 1
  26. fi
  27. echo $"Starting $prog: "
  28. $EXEC $CONF &
  29. RETVAL=$?
  30. return $RETVAL
  31. }
  32. # Stop redis daemons functions.
  33. stop() {
  34. if [ ! -f $PIDFILE ];then
  35. echo "$PIDFILE exists, process is already running or crashed"
  36. exit 1
  37. fi
  38. echo -n $"Stopping $prog: "
  39. PID=$(cat $PIDFILE)
  40. $CLIEXEC -p $REDISPORT shutdown
  41. while [ -x /proc/${PID} ]
  42. do
  43. echo "Waiting for Redis to shutdown ..."
  44. sleep 1
  45. done
  46. echo "redis stopped"
  47. RETVAL=$?
  48. }
  49. # See how we were called.
  50. case "$1" in
  51. start)
  52. start
  53. ;;
  54. stop)
  55. stop
  56. ;;
  57. restart|reload)
  58. stop
  59. start
  60. ;;
  61. *)
  62. echo $"Usage: $prog {start|stop|restart|reload}"
  63. exit 1
  64. esac
  65. exit $RETVAL

你喜欢的话,也可以用我改写的噢

2.原文件EXEC、CLIEXEC参数,也是有所更改

  
  
 
 
  1. EXEC=/usr/local/redis/bin/redis-server
  2. CLIEXEC=/usr/local/redis/bin/redis-cli

3.redis开启的命令,以后台运行的方式执行,加上 &

  
  
 
 
  1. $EXEC $CONF &

ps:注意后面的那个“&”,即是将服务转到后面运行的意思,否则启动服务时,redis服务将 占据在前台,占用了主用户界面,造成其它的命令执行不了。


将redis配置文件拷贝到/etc/redis/${REDISPORT}.conf  

  
  
 
 
  1. mkdir /etc/redis

  
  
 
 
  1. cp /usr/local/redis-3.0.5/redis.conf /etc/redis/6379.conf

修改此配置文件


  
  
 
 
  1. daemonize no //修改为yes 使redis以守护进程模式启动
  2.  
  3. pidfile /var/run/redis.pid //修改为/var/run/redis_6379.pid 设置redis的PID文件的路径

赋予redis文件执行权限

  
  
 
 
  1. chmod +x /etc/init.d/redis


添加服务

  
  
 
 
  1. chkconfig --add redis


设置开机启动

  
  
 
 
  1. chkconfig --level 35 redis on


查看是否设置成功

  
  
 
 
  1. chkconfig --list | grep redis

Linux操作系统安装redis

此状态下表面开机启动成功

service方式启动redis服务

  
  
 
 
  1. service redis start

Linux操作系统安装redis


service方式停止redis服务

  
  
 
 
  1. service redis stop

Linux操作系统安装redis


将redis的命令所在目录添加到系统参数PATH中

修改profile文件:

  
  
 
 
  1. vi /etc/profile

追加


  
  
 
 
  1. export PATH=$PATH:$JAVA_HOME/bin:/usr/local/redis-3.0.5/src


使配置文件立即生效


  
  
 
 
  1. source /etc/profile

这样就可以直接调用redis-cli的命令了,如下所示: 

Linux操作系统安装redis

退出客户端输入quit即可


检测后台进程是否存在

  
  
 
 
  1. ps -ef|grep redis


检测6379端口是否在监听

  
  
 
 
  1. netstat -tunpl | grep 6379

密码认证

打开 /etc/redis.conf 修改 requirepass 配置项

  
  
 
 
  1. # vim /etc/redis.conf
  2.  
  3. requirepass test123

  
  
 
 
  1. # service redis restart
  2. Stopping redis-server: [ OK ]
  3. Starting redis-server: [ OK ]
  4.  
  5. # redis-cli
  6. redis 127.0.0.1:6379> set h helloworld
  7. (error) ERR operation not permitted

auth test123

  
  
 
 
  1. redis 127.0.0.1:6379> auth test123
  2. OK
  3. redis 127.0.0.1:6379> set h helloworld
  4. OK
  5. redis 127.0.0.1:6379> get h
  6. "helloworld"
  7. redis 127.0.0.1:6379> exit


redis-cli -a 参数指定密码

  
  
 
 
  1. # redis-cli -a test123
  2. redis 127.0.0.1:6379> set h helloworld
  3. OK
  4. redis 127.0.0.1:6379> get h
  5. "helloworld"
  6. redis 127.0.0.1:6379> exit


通过 config 动态改变密码,无需重新启动 redis 进程

  
  
 
 
  1. # redis-cli -a test123
  2. redis 127.0.0.1:6379> config get requirepass
  3. 1) "requirepass"
  4. 2) "test123"
  5. redis 127.0.0.1:6379> config set requirepass passabc
  6. OK
  7. redis 127.0.0.1:6379> auth passabc
  8. OK
  9. redis 127.0.0.1:6379> set h helloworld
  10. OK
  11. redis 127.0.0.1:6379> get h
  12. "helloworld"
  13. redis 127.0.0.1:6379> exit

master/slave 模式, master 有密码, slave 怎样配置?

  
  
 
 
  1. masterauth password

redis的主从配置

1、选中一台服务器作为master

2、其它服务器作为slave

       修改/etc/redis/6379.conf配置文件

       找到# slaveof <masterip> <masterport>

把此配置注释放开,写上master的ip 端口号即可

  
  
 
 
  1. slaveof 192.168.1.101 6379

重启服务,即可发现master机子中的相关数据已经同步到所有的slave从机中