CentOS 编译安装 redis (1734 views)

gHOST

2019-08-14 12:05:17

安装编译所需依赖

安装 gcc

[root@localhost ~]# yum -y install gcc bzip2

https://github.com/jemalloc/jemalloc/releases/ 下载最新的 jemalloc

[root@localhost ~]# wget https://github.com/jemalloc/jemalloc/releases/download/5.2.1/jemalloc-5.2.1.tar.bz2

解压、安装 jemalloc

[root@localhost ~]# tar xjvf jemalloc-5.2.1.tar.bz2
[root@localhost ~]# cd jemalloc-5.2.1
[root@localhost jemalloc-5.2.1]# ./configure
[root@localhost jemalloc-5.2.1]# make && make install
[root@localhost jemalloc-5.2.1]# cd ..

下载 redis

https://redis.io/download 下载最新 redis 源码包

[root@localhost ~]# wget http://download.redis.io/releases/redis-5.0.5.tar.gz

解压、编译 redis

[root@localhost ~]# tar xzvf redis-5.0.5.tar.gz
[root@localhost ~]# cd redis-5.0.5
[root@localhost redis-5.0.5]# make

如果要 make test, 需要安装 tcl, 不需要运行 test 的话,可以跳过这一步

[root@localhost redis-5.0.5]# yum -y install tcl
[root@localhost redis-5.0.5]# make test

将 redis 相关文件拷贝到目标目录

/usr/local/redis

[root@localhost redis-5.0.5]# mkdir -p /usr/local/redis/{bin,etc,var}
[root@localhost redis-5.0.5]# cp src/{redis-benchmark,redis-check-aof,redis-check-rdb,redis-cli,redis-sentinel,redis-server} /usr/local/redis/bin/
[root@localhost redis-5.0.5]# cp redis.conf /usr/local/redis/etc/
[root@localhost redis-5.0.5]# ln -s /usr/local/redis/bin/* /usr/local/bin/

修改配置文件

/usr/local/redis/etc/redis.conf

[root@localhost redis-5.0.5]# cd /usr/local/redis/etc
[root@localhost etc]# sed -i 's@pidfile.*@pidfile /var/run/redis/redis.pid@' redis.conf                   # 守护进程 pid
[root@localhost etc]# sed -i "s@logfile.*@logfile /usr/local/redis/var/redis.log@" redis.conf             # 日志记录文件
[root@localhost etc]# sed -i "s@^dir.*@dir /usr/local/redis/var@" redis.conf                              # 本地数据库存放目录
[root@localhost etc]# sed -i 's@daemonize no@daemonize yes@' redis.conf                                   # 启用守护进程
[root@localhost etc]# sed -i "s@^# bind 127.0.0.1@bind 127.0.0.1@" redis.conf                             # 绑定的主机地址
[root@localhost etc]# sed -i "s@maxmemory <bytes>@maxmemory <bytes>\nmaxmemory 7516192768@" redis.conf    # redis 可使用最大内存限制, 字节为单位, 这里设置的是 7G

添加 redis 用户,修改 redis 目录权限

[root@localhost etc]# useradd -M -s /sbin/nologin redis
[root@localhost etc]# chown -R redis:redis /usr/local/redis/{var,etc}

创建 redis 启动脚本

[root@localhost etc]# vi /lib/systemd/system/redis-server.service

贴入以下内容

[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
Type=forking
PIDFile=/var/run/redis/redis.pid
User=redis
Group=redis

Environment=statedir=/var/run/redis
PermissionsStartOnly=true
ExecStartPre=/bin/mkdir -p ${statedir}
ExecStartPre=/bin/chown -R redis:redis ${statedir}
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
ExecStop=/bin/kill -s TERM $MAINPID
Restart=always
LimitNOFILE=1000000
LimitNPROC=1000000
LimitCORE=1000000

[Install]
WantedBy=multi-user.target

修改权限,允许跟系统自动启动

[root@localhost etc]# chmod 754 /lib/systemd/system/redis-server.service
[root@localhost etc]# systemctl enable redis-server

启动、停止、重启服务,查看服务状态

[root@localhost etc]# systemctl start redis-server
[root@localhost etc]# systemctl stop redis-server
[root@localhost etc]# systemctl restart redis-server
[root@localhost etc]# systemctl status redis-server

参考

https://blog.csdn.net/Cooder_SXK/article/details/80837063

https://www.cnblogs.com/zhang0807/p/10153618.html