1.下载Redis包
cd /usr/local
#官网获取redis源码包
wget http://download.redis.io/releases/redis-6.0.5.tar.gz
说明:如果尚未安装wget命令,请先安装wget命令
yum install -y wget
2.安装redis相关依赖
redis使用C语言写的,在编译源码的时候需要gcc,redis-6.x版本对gcc版本是有要求的,gcc版本不要低于5.3。
查看gcc版本
gcc -v
[root@localhost local]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-9/root/usr/libexec/gcc/x86_64-redhat-linux/9/lto-wrapper
Target: x86_64-redhat-linux
Configured with: …/configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-9/root/usr --mandir=/opt/rh/devtoolset-9/root/usr/share/man --infodir=/opt/rh/devtoolset-9/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-9.3.1-20200408/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 9.3.1 20200408 (Red Hat 9.3.1-2) (GCC)
尚未安装gcc的情况,可以执行下面的步骤去安装
yum安装gcc等依赖
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
#scl命令启用只是临时的,退出shell或重启就会恢复原系统gcc版本
scl enable devtoolset-9 bash
#写入系统执行脚本文件,永久生效
echo “source /opt/rh/devtoolset-9/enable” >>/etc/profile
3、编译安装redis
#进入源码压缩包所在目录
cd /usr/local/
#解压redis源码压缩包
tar -zxvf ./redis-6.0.5.tar.gz
#进入redis源码包目录
cd /usr/local/redis-6.0.5
#编译redis源码并安装
make && make install
编译安装其它命令
编译出错时,清出编译生成的文件
make distclean
编译安装到指定目录下
make PREFIX=/usr/local/redis install
卸载
make uninstall
4、配置redis自启动
1、通过systemctl配置自启动
修改redis.conf配置文件
vim /usr/local/redis-6.0.5/redis.conf
#默认绑定本地回环地址,可注释本配置,开放远程ip可以访问,或者指定对应的远程ip
#bind 127.0.0.1
#设置密码
requirepass 123456
#表示以后台守护进程方式启动服务,默认值为no
daemonize yes
#表示以 CentOS systemd 系统服务方式启动,默认值为no
supervised systemd
#指定日志文件目录,注意此目录需要真实存在,否则redis启动报错
logfile “/var/log/redis/redis-server.log”
创建 redis-server.service 文件
vim /etc/systemd/system/redis-server.service
redis-server.service文件配置内容
example systemd service unit file for redis-server
In order to use this as a template for providing a redis service in your
environment, at the very least make sure to adapt the redis configuration
file you intend to use as needed (make sure to set “supervised systemd”), and
to set sane TimeoutStartSec and TimeoutStopSec property values in the unit’s
“[Service]” section to fit your needs.
Some properties, such as User= and Group=, are highly desirable for virtually
all deployments of redis, but cannot be provided in a manner that fits all
expectable environments. Some of these properties have been commented out in
this example service unit file, but you are highly encouraged to set them to
fit your needs.
Please refer to systemd.unit(5), systemd.service(5), and systemd.exec(5) for
more information.
[Unit]
Description=Redis data structure server
Documentation=https://redis.io/documentation
#Before=your_application.service another_example_application.service
#AssertPathExists=/var/lib/redis
[Service]
#ExecStart=/usr/local/bin/redis-server --supervised systemd --daemonize yes
#Alternatively, have redis-server load a configuration file:
#ExecStart=/usr/local/bin/redis-server /path/to/your/redis.conf
ExecStart=/usr/local/bin/redis-server /usr/local/redis-6.0.5/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always
LimitNOFILE=10032
NoNewPrivileges=yes
#OOMScoreAdjust=-900
#PrivateTmp=yes
#Type=notify
#注意 notify 会失败,换成 forking 方式启动,让主进程复制一个子进程的方式执行
#Type=forking
#TimeoutStartSec=100
#TimeoutStopSec=100
#UMask=0077
User=root
#Group=root
#WorkingDirectory=/var/lib/redis
[Install]
WantedBy=multi-user.target
重新加载系统服务文件
systemctl daemon-reload
以系统服务方式启动 redis-server
systemctl enable redis-server.service // 配置守护进程,每次开机都会自行启动
启动服务进程
systemctl start redis-server.service
重启服务进程
systemctl restart redis-server.service
查看服务状态
systemctl status redis-server.service
查看redis默认的6379端口是否监听
netstat -tlnp | grep 6379
查看redis日志
cat /var/log/redis/redis-server.log
查找文件
find / -type f -name “*.rdb”
centos7.0 Redis持久化配置:
logfile “/var/log/redis/redis-server.log”
dir /var/lib/redis