CentOS7.6下安装Redis5.0.7

此次安装是在CentOS7下安装Redis5.0.7

一.首先准备Redis安装包

这里下载的是 redis-5.0.7.tar.gz 安装包,并将其直接放在了 root ⽬录下
    压缩包下载地址://files.cnblogs.com/files/blogs/726807/redis-5.0.7.tar.gz

二.解压安装包

2.1在/data下创建redis文件夹并进入

cd /data/
mkdir redis
cd redis

2.2将安装包解压到/data/redis/

tar zxvf /root/redis-5.0.7.tar.gz -C /data/redis

解压完之后会在/data/redis/下生成一个redis-5.0.7的文件夹

三.编译并安装

cd /data/redis/redis-5.0.7
make && make PREFIX=/data/redis install

注意:因为缺少相关的包导致编译失败

举例:
1.缺少gcc,则安装gcc

yum -y install gcc

2.make时报如下错误:
zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory
zmalloc.h:55:2: error: #error "Newer version of jemalloc required"
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/data0/src/redis-2.6.2/src'
make: *** [all] Error 2

原因是jemalloc重载了Linux下的ANSIC的malloc和free函数。解决办法:make时添加参数。

make MALLOC=libc
3.make之后,会出现一句提示Hint: To run ‘make test’ is a good idea ;

但是不测试,通常是可以使用的。若我们运行make test ,会有如下提示

[devnote@devnote src]$ make test
You need tcl 8.5 or newer in order to run the Redis test
make: ***[test] Error_1

解决办法是用yum安装tcl8.5(或去tcl的官方网站//www.tcl.tk/下载8.5版本,并参考官网介绍进行安装)

yum -y install tcl

补:问题解决完了最好再重新编译下。

四.将Redis安装为系统服务并后台启动

    [root@localhost redis-5.0.7]# cd utils/
	//运行服务脚本  脚本中redis安装路径为/data/redis

        //此处我全部选择的默认配置即可,有需要可以按需定制!!!
        //参考地址://files.cnblogs.com/files/blogs/726807/install_server.sh
        //可以直接粘贴上我的我把所有用到的Redis的所有配置都放在了/data/redis目录下
	
	说明:为了方便,最好自己配置一下install_server.sh中的路径,就比如后续启动需要的redis-server 和 配置文件6379.conf 把它俩放到建的redis文件夹下,方便查找
    [root@localhost utils]# ./install_server.sh

五.查看Redis服务启动情况,并停止Redis服务

	//查看服务状态	
        systemctl status redis
	//停止服务
	systemctl stop redis
	//结束进程
	ps -ef|grep redis
	kill -9 PID

六.修改配置文件和修改系统配置(保证redis的远程可以访问)

6.1.编辑redis配置文件

vi /etc/redis/6379.conf(默认配置文件位置,修改自己实际配置文件)

​ vi /data/redis/16379.conf

修改内容: 
1.将 bind 127.0.0.1 修改为 0.0.0.0 //修改IP
2.daemonize yes //在后台运行
3.protected-mode 设置成no(默认是设置成yes的, 防止了远程访问,在redis3.2.3版本后)
4.requirepass 123456 //设置密码 可不设置

6.2.关闭防火墙和SELINUX

6.2.1关闭防火墙

1、停止firewalld服务
systemctl stop firewalld
 

2、禁止firewalld开机启动
systemctl disable firewalld

6.2.2关闭SELINUX

vi /etc/selinux/config
将`SELINUX=enforcing`修改为`SELINUX=disabled`,保存退出

七.启动Redis并远程访问

7.1启动Redis (使用绝对路径启动,一劳永逸,免去一些找不到命令错误)

    /usr/local/bin/redis-server /etc/redis/6379.conf //此处使用的是默认路径
    /data/redis/bin/redis-server /data/redis/16379.conf //自己的路径

​ ps -ef|grep redis //查看是否启动成功

7.2远程访问Redis

​ 输入IP,默认端口号6379,密码,测试连接,搞定!

八.设置开机启动Redis(建议设置)

方式 1 vi /etc/rc.d/rc.local

添加启动命令到 /etc/rc.d/rc.local 中:
/usr/local/redis-5.0.7/bin/redis-server  /etc/redis/redis.conf //此处使用的是默认路径
/usr/local/redis/bin/redis-server /usr/local/redis/6379.conf //自己的路径

​ 方式 2 . vi /lib/systemd/system/redis.service

[Unit]
Description=Redis
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
#PIDFile=/data/redis/redis.pid
ExecStart=/data/redis/bin/redis-server /data/redis/16379.conf
ExecReload=/bin/kill -s HUP $MAINPID 
ExecStop=/bin/kill -s QUIT $MAINPID
ExecStartPost=/bin/sh -c "echo $MAINPID > /data/redis/redis.pid"
PrivateTmp=true

[Install]
WantedBy=multi-user.target
//重载系统服务
sudo systemctl daemon-reload
Tags: