search
HomeDatabaseRedisHow to install and configure Redis service under CentOS7

1. 安装依赖

➜  yum install -y gcc gcc-c++ kernel-devel

2. 下载源码包

# 推荐在这个目录存放各个软件的源码➜  cd /usr/local/src# 下载指定版本➜  wget http://download.redis.io/releases/redis-5.0.5.tar.gz# 下载最新稳定版➜  wget http://download.redis.io/redis-stable.tar.gz# 查看源码具体版本➜  cat redis-stable/src/version.h

3. 编译安装

➜  tar zxvf redis-5.0.5.tar.gz
➜  cd redis-5.0.5
➜  make# 安装到指定目录下➜  make PREFIX=/usr/local/redis-5.0.5 install# 拷贝默认配置文件到指定目录➜  mkdir /usr/local/redis-5.0.5/etc
➜  cp redis.conf /usr/local/redis-5.0.5/etc/# 创建程序软链接,以便后期版本升级cd /usr/local/
ln -s redis-5.0.5 redis# 配置环境变量,以便在全局使用 Redis 相关命令➜  echo 'export PATH="$PATH:/usr/local/redis/bin"' >> /etc/profile
➜  source /etc/profile# 验证➜  redis-cli -v
redis-cli 5.0.5

4. 修改默认配置文件

这里只是一些推荐的常用基本配置

➜  cd /usr/local/redis/etc# 为方便管理多个 Redis 服务,以版本号作为配置文件的名称后缀➜  mv redis.conf redis_6379.conf# 开始编辑配置文件➜  vi redis_6379.conf# --------------------# 以下是常用配置项# --------------------# 开启守护进程(后台)方式运行daemonize yes# 进程文件pidfile /var/redis/run/redis_6379.pid# 只允许指定主机连接,默认不限制bind 127.0.0.1# 端口号port 6379# 客户端闲置多长时间(单位:s)关闭连接# 默认 0 ,无限制timeout 300# 本地持久化数据文件名dbfilename dump_6379.rdb# 设置工作目录dir /var/redis/db/# 日志级别# - debug 适用于开发、测试,打印的信息较多# - verbose 比 debug 简洁一些# - notice 默认,普通的 verbose,用于生产环境# - warning 警告和一些比较严重的信息loglevel notice# 日志文件# 默认为空字符串,表示标准输出(stdout)# 如果以守护进程运行,并且此处采用标准输出,则日志发送给 /dev/nulllogfile /var/redis/log/redis_6379.log# 客户端连接密码# 为保证服务安全,建议开启并设置一个复杂的密码requirepass pwd2019# --------------------# 保存上面修改好的配置文件# --------------------# 创建配置中不存在的目录➜  mkdir -p /var/redis/{run,log,db}

5. 启动服务

基本启动方式

# 以默认配置启动➜  redis-server # 指定配置文件➜  redis-server /usr/local/redis/etc/redis_6379.conf# 查看更多使用参数➜  redis-server -h# 客户端连接测试➜  redis-cli127.0.0.1:6379> KEYS *(error) NOAUTH Authentication required.127.0.0.1:6379> auth pwd2019OK127.0.0.1:6379> KEYS *(empty list or set)127.0.0.1:6379> exit➜

使用脚本启动

➜  cd /usr/local/src/redis-5.0.5/utils/
➜  cp redis_init_script /etc/init.d/
➜  cd /etc/init.d/
➜  mv redis_init_script redis_6379
➜  vim redis_6379
#!/bin/sh## Simple Redis init.d script conceived to work on Linux systems# as it does use of the /proc filesystem.### BEGIN INIT INFO# Provides:     redis_6379# Default-Start:        2 3 4 5# Default-Stop:         0 1 6# Short-Description:    Redis data structure server# Description:          Redis data structure server. See https://redis.io### END INIT INFO# 根据实际安装情况修改这里的路径、端口、连接密码REDISPORT=6379
REDISPWD=pwd2019
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis/etc/redis_${REDISPORT}.conf"case "$1" instart)if [ -f $PIDFILE ]thenecho "$PIDFILE exists, process is already running or crashed"elseecho "Starting Redis server..."$EXEC $CONFfi;;
    stop)if [ ! -f $PIDFILE ]thenecho "$PIDFILE does not exist, process is not running"elsePID=$(cat $PIDFILE)echo "Stopping ..."if [ -n $REDISPWD ]; then$CLIEXEC -p $REDISPORT -a $REDISPWD shutdownelse$CLIEXEC -p $REDISPORT shutdownfiwhile [ -x /proc/${PID} ]doecho "Waiting for Redis to shutdown ..."sleep 1doneecho "Redis stopped"fi;;
    *)echo "Please use start or stop as first argument";;esac

使用脚本启动服务测试

➜  ./redis_6379 start# 检查是否启动成功➜  ps -ef | grep redisroot     19262     1  0 01:42 ?        00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:6379root     19267 19129  0 01:43 pts/0    00:00:00 grep --color=auto redis# 客户端连接测试➜  redis-cli127.0.0.1:6379> KEYS *(error) NOAUTH Authentication required.127.0.0.1:6379> auth pwd2019OK127.0.0.1:6379> KEYS *(empty list or set)127.0.0.1:6379> exit➜  # 停止服务➜  ./redis_6379 stop

6. 加入开机自启动

# 使用 root 用户操作# 添加到自启动列表# 这里的 redis_6379 与 /etc/init.d/redis_6379 文件名保持一致➜  chkconfig --add redis_6379# 将 2 3 4 5 级别设置为自启动➜  chkconfig --level 2345 redis_6379 on# 检查是否设置成功➜  chkconfig --list | grep redis# 重启检查自启动是否生效➜  reboot

在 CentOS7+ 建议使用 systemctl 命令对 Redis 服务进行统一管理,如下:

# 查看服务状态➜  systemctl status redis_6379● redis_6379.service - LSB: Redis data structure server
   Loaded: loaded (/etc/rc.d/init.d/redis_6379; bad; vendor preset: disabled)
   Active: active (running) since Mon 2019-11-11 02:21:03 UTC; 3s ago Docs: man:systemd-sysv-generator(8)
  Process: 1042 ExecStop=/etc/rc.d/init.d/redis_6379 stop (code=exited, status=0/SUCCESS)
  Process: 1056 ExecStart=/etc/rc.d/init.d/redis_6379 start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/redis_6379.service   └─1058 /usr/local/redis/bin/redis-server 127.0.0.1:6379Nov 11 02:21:03 cnetos7-localhost systemd[1]: Starting LSB: Redis data structure server...Nov 11 02:21:03 cnetos7-localhost redis_6379[1056]: Starting Redis server...Nov 11 02:21:03 cnetos7-localhost redis_6379[1056]: 1057:C 11 Nov 2019 02:21:03.594 # oO0OoO0OoO0Oo Re...0OoNov 11 02:21:03 cnetos7-localhost redis_6379[1056]: 1057:C 11 Nov 2019 02:21:03.594 # Redis version=5....tedNov 11 02:21:03 cnetos7-localhost redis_6379[1056]: 1057:C 11 Nov 2019 02:21:03.594 # Configuration loadedNov 11 02:21:03 cnetos7-localhost systemd[1]: Started LSB: Redis data structure server.Hint: Some lines were ellipsized, use -l to show in full.# 启动服务➜  systemctl start redis_6379# 关闭服务➜  systemctl stop redis_6379

The above is the detailed content of How to install and configure Redis service under CentOS7. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
Redis: Beyond SQL - The NoSQL PerspectiveRedis: Beyond SQL - The NoSQL PerspectiveMay 08, 2025 am 12:25 AM

Redis goes beyond SQL databases because of its high performance and flexibility. 1) Redis achieves extremely fast read and write speed through memory storage. 2) It supports a variety of data structures, such as lists and collections, suitable for complex data processing. 3) Single-threaded model simplifies development, but high concurrency may become a bottleneck.

Redis: A Comparison to Traditional Database ServersRedis: A Comparison to Traditional Database ServersMay 07, 2025 am 12:09 AM

Redis is superior to traditional databases in high concurrency and low latency scenarios, but is not suitable for complex queries and transaction processing. 1.Redis uses memory storage, fast read and write speed, suitable for high concurrency and low latency requirements. 2. Traditional databases are based on disk, support complex queries and transaction processing, and have strong data consistency and persistence. 3. Redis is suitable as a supplement or substitute for traditional databases, but it needs to be selected according to specific business needs.

Redis: Introduction to a Powerful In-Memory Data StoreRedis: Introduction to a Powerful In-Memory Data StoreMay 06, 2025 am 12:08 AM

Redisisahigh-performancein-memorydatastructurestorethatexcelsinspeedandversatility.1)Itsupportsvariousdatastructureslikestrings,lists,andsets.2)Redisisanin-memorydatabasewithpersistenceoptions,ensuringfastperformanceanddatasafety.3)Itoffersatomicoper

Is Redis Primarily a Database?Is Redis Primarily a Database?May 05, 2025 am 12:07 AM

Redis is primarily a database, but it is more than just a database. 1. As a database, Redis supports persistence and is suitable for high-performance needs. 2. As a cache, Redis improves application response speed. 3. As a message broker, Redis supports publish-subscribe mode, suitable for real-time communication.

Redis: Database, Server, or Something Else?Redis: Database, Server, or Something Else?May 04, 2025 am 12:08 AM

Redisisamultifacetedtoolthatservesasadatabase,server,andmore.Itfunctionsasanin-memorydatastructurestore,supportsvariousdatastructures,andcanbeusedasacache,messagebroker,sessionstorage,andfordistributedlocking.

Redis: Unveiling Its Purpose and Key ApplicationsRedis: Unveiling Its Purpose and Key ApplicationsMay 03, 2025 am 12:11 AM

Redisisanopen-source,in-memorydatastructurestoreusedasadatabase,cache,andmessagebroker,excellinginspeedandversatility.Itiswidelyusedforcaching,real-timeanalytics,sessionmanagement,andleaderboardsduetoitssupportforvariousdatastructuresandfastdataacces

Redis: A Guide to Key-Value Data StoresRedis: A Guide to Key-Value Data StoresMay 02, 2025 am 12:10 AM

Redis is an open source memory data structure storage used as a database, cache and message broker, suitable for scenarios where fast response and high concurrency are required. 1.Redis uses memory to store data and provides microsecond read and write speed. 2. It supports a variety of data structures, such as strings, lists, collections, etc. 3. Redis realizes data persistence through RDB and AOF mechanisms. 4. Use single-threaded model and multiplexing technology to handle requests efficiently. 5. Performance optimization strategies include LRU algorithm and cluster mode.

Redis: Caching, Session Management, and MoreRedis: Caching, Session Management, and MoreMay 01, 2025 am 12:03 AM

Redis's functions mainly include cache, session management and other functions: 1) The cache function stores data through memory to improve reading speed, and is suitable for high-frequency access scenarios such as e-commerce websites; 2) The session management function shares session data in a distributed system and automatically cleans it through an expiration time mechanism; 3) Other functions such as publish-subscribe mode, distributed locks and counters, suitable for real-time message push and multi-threaded systems and other scenarios.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft