centos7 安装redis4.0.2 配置设置开机服务

Redis在linux上的安装首先必须先安装gcc,这个是用来编译redis的源文件的。

yum install -y gcc-c++

解压redis的源文件

tar zxvf redis-4.0.2.tar.gz

进入redis的解压目录

cd redis-4.0.2/

使用make命令编译

make

进入解压的src目录下

cd src

运行make test测试是否可以安装

[root@localhost src]# make test
You need tcl 8.5 or newer in order to run the Redis test
make: *** [test] Error 1

在这里发现有错误

TCL(Tool Command Language)工具脚本语言,是Linux内的一种语言包。,这里需要先安装tcl。

wget 
tar zxvf tcl8.6.1-src.tar.gz 
cd tcl8.6.1/
cd unix/
./configure
make
make install

再次测试make test 

安装

[root@localhost src]# make PREFIX=/usr/local/redis install

把解压的redis路径下的redis.conf文件拷贝到安装路径下面

cp redis.conf /usr/local/redis

启动redis 
第一种方法:进入安装路径下的bin

[root@localhost redis]# cd bin
[root@localhost bin]# ./redis-server

这样其实已经启动成功了,但是这属于前端启动,启动redis之后,我们的控制台就不能进行任何操作了。只能ctrl+c停止启动。

第二种方法: 
后端启动 
1、首先编辑redis.conf

[root@localhost redis]# vim redis.conf

找到daemonize no将其改为yes(在vi中插入数据按键盘上的i或者insert) 

按esc退出insert模式,再按:,并且输入wq。代表保存并且退出

再次启动

[root@localhost redis]# ./bin/redis-server ./redis.conf

这样redis就启动了

可以通过 

ps -ef | grep -i redis

来查看是否启动

关闭redis

[root@localhost redis]# ./bin/redis-cli shutdown

添加开机启动服务

vim /etc/systemd/system/redis4.service

粘贴一下内容

[Unit]
Description=The redis-server Process Manager
After=syslog.target network.target

[Service]
Type=simple
PIDFile=/var/run/redis_6379.pid
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf         
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID

[Install]
WantedBy=multi-user.target

设置开机启动

systemctl daemon-reload 
systemctl start redis4.service 
systemctl enable redis4.service

创建redis命令软连接

ln -s /usr/local/redis/bin/redis-cli /usr/bin/redis-cli

测试redis

Redis默认配置是不需要密码认证的,也就是说只要连接的Redis服务器的host和port正确,就可以连接使用。这在安全性上会有一定的问题,所以需要启用Redis的认证密码,增加Redis服务器的安全性。

修改配置文件找到如下行:

#requirepass foobared

去掉前面的注释,并修改为所需要的密码:

requirepass myPassword (其中myPassword就是要设置的密码)

重启Redis

设置Redis认证密码后,客户端登录时需要使用-a参数输入认证密码,不添加该参数虽然也可以登录成功,但是没有任何操作权限。如下:

$ ./redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.

使用密码认证登录,并验证操作权限:

$ ./redis-cli -h 127.0.0.1 -p 6379 -a myPassword
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "myPassword"

看到类似上面的输出,说明Reids密码认证配置成功。

注意了局域网使用redis 必须修改bind 地址,默认只能本机使用.可以改为0.0.0.0 或者注释掉.

评论

还没有任何评论,你来说两句吧

发表评论