centos7 安装 redis5

文档

安装开发工具

1yum groupinstall 'Development Tools' -y

下载 redis

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

解压到 /data 目录下

1tar -zxf redis-5.0.9.tar.gz -C /app

编译

1cd /app/redis-5.0.9
2make

安装

1make install

上述为默认安装,将会把编译得到的可执行文件拷贝到/usr/local/bin目录下。
如果要指定安装目录,可指定PREFIX参数,如:

1make PREFIX=/app/redis-5.0.9 install

如果上一步安装指定了安装目录,强烈建议设置环境变量

在 /etc/profile 末尾添加:

1export PATH=$PATH:/app/redis-5.0.9/bin

运行 redis

1redis-server /app/redis-5.0.9/redis.conf

在没有修改 redis.conf 的情况下,redis-cli 只能本地 localhost (127.0.0.1)访问,而不能远程访问:

1(error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the lookback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the --portected-mode no option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

上面说是说处于保护模式,只能本地链接。

所以我们需要修改配置文件 redis.conf

去掉IP绑定

1bind 127.0.0.1

改为

1# bind 127.0.0.1

以守护进程的方式运行 Redis

1daemonize no

改为

1daemonize yes

关闭保护模式

1protected-mode yes

改为 

1protected-mode no

设置密码(如果需要)

找到

1# requirepass foobared

将注释去掉,修改 foobared 为自己想要的密码,如:

1requirepass 123456

修改后,重新运行 redis-server 命令即可远程访问。

停止 redis 服务

1redis-cli -p 3306 shutdown

设置 redis 作为系统服务并开机启动

开机启动脚本

1vim /usr/lib/systemd/system/redis.service

内容如下:

 1[Unit]
 2Description=Redis Server
 3After=network.target
 4
 5[Service]
 6Type=forking
 7PIDFile=/var/run/redis_6379.pid
 8ExecStart=/usr/local/redis/src/redis-server /usr/local/redis/redis.conf
 9ExecReload=/bin/kill -s HUP $MAINPID 
10ExecStop=/bin/kill -s QUIT $MAINPID 
11PrivateTmp=true
12 
13[Install]
14WantedBy=multi-user.target

重载系统服务

1systemctl daemon-reload

启动 redis

1systemctl start redis

开机自启 redis

1systemctl enable redis