安全相关:
    创建一个用户 useradd user
    修改密码 passwd user
    然后用这个用户进行登录
    用这个用户启动redis
    用root账号将这个用户的权限修改为/sbin/nologin
    修改文件是:/etc/passwd 对应的名字:/sbin/nologin
    redis的关于save的给注掉,关于config的也给注掉
    redis设置登录密码 requirepass xxxx

    1.Windows下
    下载安装redis
    redis-server.exe redis.windows.conf
    启动
    如果启动失败,
    打开redis.windows.conf
    搜索max
    在 # maxheap 下面添加
    maxheap 1024000000
    再次启动即可正常启动

    2.linux下
    下载最新稳定版redis
    wget http://download.redis.io/redis-stable.tar.gz
    解压
    tar -zxvf redis-stable.tar.gz
    进入redis目录
    编译
    make

    1. 这里可能会报 cc 命令找不到,需要安装gcc:
    2. yum -y install gcc automake autoconf libtool make
    3. 再次执行make命令,会报错,需要把文件删掉,重新解压,再运行make命令
    4. make命令成功后,执行安装命令

    cd src
    make install PREFIX=/usr/local/redis
    将配置文件移动到redis目录
    mv redis.conf /usr/local/redis/etc/
    vim /usr/local/redis/etc/redis.conf
    使redis后台运行:
    将daemonize的值改为yes
    首先将配置文件中的bind注释掉(局域网内就可以连接了)
    如果需要在外网连接,将protected-model改为no

    –启动服务端
    ./redis-server ../etc/redis.config (启动redis服务要将redis.conf也写进去,同时redis.conf在etc下)
    –启动客户端
    ./redis-cli

    安装好redis后如何查看redis 是否启动
    先启动redis客户端redis-cli(redis客户端):
    redis 127.0.0.1:6379> PING
    返回
    PONG

    (error) NOAUTH Authentication required.
    例如密码是‘root’,当出现认证问题时候,输入“auth ‘root’”即可

    1. 开机启动:
    2. redis配置为随机启动,类似于windows的服务,开机启动。
    3. centos下配置随机启动需要在目录/etc/init.d中添加启动脚本,启动脚本的模板在redis源代码目录的utils文件夹中:redis_init_script
    4. 我们把这个文件复制到/etc/init.d文件夹中,并重命名为redis_6379, 我们这个服务名也就为redis_6379了,
    5. 再来看下这个文件的内容:
    1. #!/bin/sh
    2. #
    3. # Simple Redis init.d script conceived to work on Linux systems
    4. # chkconfig: 2345 90 10
    5. # description: Redis is a persistent key-value database
    6. # as it does use of the /proc filesystem.
    7. REDISPORT=6379
    8. EXEC=/usr/local/bin/redis-server
    9. CLIEXEC=/usr/local/bin/redis-cli
    10. PIDFILE=/var/run/redis_${REDISPORT}.pid
    11. CONF="/etc/redis/${REDISPORT}.conf"
    12. case "$1" in
    13. start)
    14. if [ -f $PIDFILE ]
    15. then
    16. echo "$PIDFILE exists, process is already running or crashed"
    17. else
    18. echo "Starting Redis server..."
    19. $EXEC $CONF
    20. fi
    21. ;;
    22. stop)
    23. if [ ! -f $PIDFILE ]
    24. then
    25. echo "$PIDFILE does not exist, process is not running"
    26. else
    27. PID=$(cat $PIDFILE)
    28. echo "Stopping ..."
    29. $CLIEXEC -p $REDISPORT shutdown
    30. while [ -x /proc/${PID} ]
    31. do
    32. echo "Waiting for Redis to shutdown ..."
    33. sleep 1
    34. done
    35. echo "Redis stopped"
    36. fi
    37. ;;
    38. *)
    39. echo "Please use start or stop as first argument"
    40. ;;
    41. esac
    1. 注意:
    2. 第四行和第五行,是我们后加入的。
    3. $EXEC $CONF 代码中变量赋值,execredis-server命令路径,conf为配置文件,配置文件为/etc/redis/6379.conf,这个文件还没有,我们下面来配置它:看到里面的start stop命令,应该就非常熟悉了。注意start命令中,执行的启动命令为
    4. 创建目录/etc/redis 配置文件的模板还在在redis源码中找:redis-4.0.2/redis.conf
    5. 将这个配置文件复制到/etc/redis目录 ,并重命名为6379.conf
    6. 打开这个文件并修改:
    7. 这里贴出关键修改代码:
    8. # 注释掉它,以便让外网访问
    9. # bind 127.0.0.1
    10. # 关闭保护模式
    11. protected-mode no
    12. # Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程
    13. # 启用守护进程后,Redis会把pid写到一个pidfile中,在/var/run/redis.pid
    14. daemonize yes
    15. # Redis以守护进程方式运行时,Redis默认会把pid写入/var/run/redis.pid文件,可以通过pidfile指定
    16. pidfile /var/run/redis_6379.pid
    17. # 指定Redis监听端口,默认端口为6379
    18. # 如果指定0端口,表示Redis不监听TCP连接
    19. port 6379
    20. #requirepass配置可以让用户使用AUTH命令来认证密码,才能使用其他命令。这让redis可以使用在不受信任的
    21. 网络中。为了保持向后的兼容性,可以注释该命令,因为大部分用户也不需要认证。使用requirepass的时候需要
    22. 注意,因为redis太快了,每秒可以认证15w次密码,简单的密码很容易被攻破,所以最好使用一个更复杂的密码
    23. # requirepass foobared
    24. # 工作目录.
    25. # 指定本地数据库存放目录,文件名由上一个dbfilename配置项指定
    26. #
    27. # Also the Append Only File will be created inside this directory.
    28. #
    29. # 注意,这里只能指定一个目录,不能指定文件名
    30. dir /var/redis/6379
    31. 最后一行 /var/redis/6379 这个目录还没有,需要我们创建,用于存放redis的持久化文件。
    32. 然后执行命令:
    33. 通过上面的操作后,我们就可以通过 如下命令启动,停止redis
    34. service redis_6379 stop
    35. service redis_6379 start