从零开始搭建系统1.5——Redis安装及配置

1.在/usr/目录下创建redis目录

[root@localhost usr]# mkdir redis

2.下载安装包

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

3.安装gcc,由于编译需要gcc如果没有安装的话需要安装

yum install gcc
查看gcc版本,会看到详细的版本信息,安装GCC就完成了

4.解压安装包并安装

 tar -zxvf redis-4.0.8.tar.gz

cd redis-4.0.8

make MALLOC=libc

5.启动redis

先切换到redis src目录下

直接启动redis

./redis-server

如上图:redis启动成功,但是这种启动方式需要一直打开窗口,不能进行其他操作,不太方便。

按 ctrl + c可以关闭窗口。

6.将/usr/local/redis-4.0.6/src目录下的文件加到/usr/local/bin目录

cd src && make install

7.设置redis开机自启动

在/etc目录下新建redis目录

将/usr/redis/redis-4.0.8/redis.conf 文件复制一份到/etc/redis目录下,并命名为6379.conf

cp /usr/redis/redis-4.0.8/redis.conf /etc/redis/6379.conf

设置为后台方式运行,修改6379.conf文件

daemonize no  
修改为
daemonize yes

将redis的启动脚本复制一份放到/etc/init.d目录下

cp /usr/redis/redis-4.0.8/utils/redis_init_script /etc/init.d/redis

先切换到/etc/init.d目录下

然后执行自启命令

[root@iZwz991stxdwj560bfmadtZ init.d]# chkconfig redis on

看结果是redis不支持chkconfig

解决方法:

使用vim编辑redis文件,在第一行加入如下两行注释,保存退出

# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database

注释的意思是,redis服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10。

 

再次执行开机自启命令,成功

[root@iZwz991stxdwj560bfmadtZ init.d]# chkconfig redis on

 

现在可以直接已服务的形式启动和关闭redis了

启动:

service redis start  

service redis stop

ps:如果redis设置了密码的话直接stop的话会出现以下情况,关闭不掉

解决方法:
1.修改redis服务脚本,加入如下所示的信息即可:
vi /etc/init.d/redis
$CLIEXEC -a "password" -p $REDISPORT shutdown

2.也可以通过redis-cli登录,然后shutdown关闭

8.开启远程访问

修改6379.conf

注释掉bind 127.0.0.1可以使所有的ip访问redis
若是想指定多个ip访问,但并不是全部的ip访问,可以bind

注意

下面还有个说明
在redis3.2之后,redis增加了protected-mode,在这个模式下,即使注释掉了bind 127.0.0.1,再访问redisd时候还是报错,如下

(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 loopback 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 '--protected-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.

修改办法:protected-mode no

重新启动服务

service redis stop

service redis start

9.设置访问密码

修改6379.conf

打开配置文件找到,大概在500行,可以在命令行模式下进行搜索

[plain] view plain copy
#requirepass foobared
去掉行前的注释,并修改密码为所需的密码,保存文件
[plain] view plain copy
requirepass myRedis

重新启动服务

service redis stop

service redis start

10.开启6379端口

添加
firewall-cmd --zone=public --add-port=6379/tcp --permanent    (--permanent永久生效,没有此参数重启后失效)
重新载入
firewall-cmd --reload
查看
firewall-cmd --zone= public --query-port=6379/tcp
查看所有打开的端口
firewall-cmd --zone=public --list-ports
 
11.删除之前建的redis文件夹

rm -rf redis

原文地址:https://www.cnblogs.com/provence666/p/8638528.html