redis 源码编译安装

https://db-engines.com/en/ranking  数据库排名

redis.io   官网                         reids.cn  中文社区,版本相差一个大本版号

Redis 版本号第 2 位,如果是奇数,则为非稳定版本(例如 2.7、2.9、3.1)如果是偶数为稳定版本(例如 2.8、3.0、3.2)

# redis-cli -v  查看版本号
redis-cli 6.0.6   稳定版

redis指定实例进行连接(需要在实例的配置文件中bind ip才可以登录,绑定多个,空格隔开,重启)
./bin/redis-cli -h 192.168.154.128 -p 6379

1、安装gcc。

yum -y install gcc  CentOS 7默认安装的gcc版本为4.8.5,最高支持到Redis 5.x.x版本,Redis 6.x.x版本需要升级gcc。

2、升级gcc(Redis 5.x.x及以下版本可跳过此步骤)。

yum -y install centos-release-scl
yum -y install devtoolset-9-gcc*

// 切换gcc版本有2种方式
// a.永久生效(推荐)
echo "source /opt/rh/devtoolset-9/enable" >> /etc/profile
source /etc/profile

// b.临时生效(重启之类的会恢复到原gcc版本)
scl enable devtoolset-9 bash

3、下载并解压Redis源码包。

wget http://download.redis.io/releases/redis-6.0.6.tar.gz
tar xzf redis-6.0.6.tar.gz -C /usr/local/src

4、编译安装

cd /usr/local/src/redis-6.0.6
make  (若编译失败,执行make distclean)
make install
注:没有执行./configure文件,因为已经有,Makefile文件了

a  ./configure是用来检测你的安装平台的目标特征的。比如它会检测你是不是有CC或GCC,并不是需要CC或GCC,它是个shell脚本生成Makefile文件,为下一步的编译做准备。
b  make是用来编译的,它从Makefile中读取指令,然后编译。
c  make install是用来安装的,它也从Makefile中读取指令,安装到指定的位置。
d  make install 就是一个移动二进制文件的过程。
cd /usr/local/bin  (自定义路径/bin/)
ls
redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli  redis-sentinel  redis-server

5、使用官方提供的脚本将Redis注册为系统服务。

cd /usr/local/src/redis-6.0.6/utils
./install_server.sh

如果遇到:
This systems seems to use systemd.
Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!

则需编辑install_server.sh脚本文件:
vi /usr/local/src/redis-6.0.6/utils/install_server.sh
注释以下几行代码后,再次执行./install_server.sh:
#bail if this system is managed by systemd
#_pid_1_exe="$(readlink -f /proc/1/exe)"
#if [ "${_pid_1_exe##*/}" = systemd ]
#then
#       echo "This systems seems to use systemd."
#       echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
#       exit 1
#fi

// 上面步骤中输入./install_server.sh启动脚本后,按照脚本提示信息自定义几项配置(看不懂英文请善用翻译工具),不输入直接按回车则是设为[]内的默认配置
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] /etc/redis/redis.conf
Please select the redis log file name [/var/log/redis_6379.log] /var/log/redis.log
Please select the data directory for this instance [/var/lib/redis/6379] /var/lib/redis
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port           : 6379
Config file    : /etc/redis/redis.conf
Log file       : /var/log/redis.log
Data dir       : /var/lib/redis
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!

注:一台主机装好redis服务后,可以启动多个redis实例,通过./install_server.sh来实现。
# ps -ef | grep redis
root       6668      1  0 14:33 ?        00:00:13 /usr/local/bin/redis-server 127.0.0.1:6379
root       7123      1  0 14:44 ?        00:00:11 /usr/local/bin/redis-server 127.0.0.1:6380
root       7458      1  0 15:53 ?        00:00:01 /usr/local/bin/redis-server 127.0.0.1:6381

若不执行第五步,则自己写入环境变量
通用格式:
export REDIS_HOME=安装路径
export PATH=....:$REDIS_HOME/bin(有多个软件路径时,用:分开,$安装目录/bin  bin下有二进制可执行文件)

在一个装有docker的环境遇到报错(干净环境无报错)

Failed at step EXEC spawning /etc/rc.d/init.d/redis_6379: Exec format error  

6、编辑配置文件

vi /etc/redis/redis.conf
默认情况下,如果没有指定"bind"配置指令,Redis将侦听服务器上所有可用网络接口的连接。使用"bind"配置指令,后跟一个或多个IP地址,可以只监听一个或多个选定的接口。如果确定希望实例监听所有接口,只需注释以下一行。
注释掉就是允许所有ip访问此Redis服务,没注释就是配一个允许一个。
# bind 127.0.0.1

保护模式默认是启用的。只有当您确定希望其他主机的客户机连接到Redis,且没有配置身份验证,也没有使用"bind"指令显式列出一组特定的接口时,才应该禁用它。
说人话就是,你没配监听地址bind xxx.xxx.xxx.xxx,且,没配密码requirepass password,才要设为no。
protected-mode yes

默认情况下,Redis不作为守护进程运行。如果需要,请使用"yes"。
daemonize yes

配置身份验证密码。
requirepass password

7、启动Redis。

systemctl start redis_6379
systemctl status redis_6379

8、开放firewall防火墙6379端口。

// 开放6379端口
firewall-cmd --add-port=6379/tcp --zone=public --permanent
// 重启防火墙
firewall-cmd --reload

 9、redis线程问题(理解不到位,有待整理)

redis的work线程是单线程的

redis6.x版本开始使用  io多线程,突破主要的性能瓶颈(网络)

追踪redis进程和内核的调用 yum install -y strace strace -ff -o ~/ooxx/out ./redis-server # redis-cli 127.0.0.1:6379> BGSAVE Background saving started # ll 总用量 1812 -rw-r--r-- 1 root root 1416199 8月 30 18:47 out.7958 -rw-r--r-- 1 root root 178 8月 30 18:35 out.7959 -rw-r--r-- 1 root root 178 8月 30 18:35 out.7960 -rw-r--r-- 1 root root 178 8月 30 18:35 out.7961 -rw-r--r-- 1 root root 1443 8月 30 18:43 out.7962 -rw-r--r-- 1 root root 2793 8月 30 18:43 out.8006 out.7958是干活的线程,派生出下面的线程,vim out.7958可以查看它们的关系

10、redis-cli使用密码

登录后 auth pass

或者 redis-cli -h xxx -p 6379 -a xxx    docker exec -it redis  redis-cli -h xxx  -p xxx  -a xxx
原文地址:https://www.cnblogs.com/zjz20/p/13514979.html