Redis 单节点安装

1.Redis官网

https://redis.io/download  选择5.0.10版本

2.规划目录

mkdir -p /data/soft                      #下载⽬录
mkdir -p /opt/redis_6379/{conf,logs,pid} #安装⽬录,⽇志⽬录,pid⽬录,配置⽬录
mkdir -p /data/redis_6379/               #数据⽬录

3.安装命令 (编译安装)

编译安装解释
./config             指定编译参数
make                按照要求编译生成可执行程序
make install      把生成的可执行文件复制到/usr/local/bin/1.直接下载
cd /data/soft/
wget http://download.redis.io/releases/redis-5.0.7.tar.gz

2.上传rz 
cd /data/soft/
redis-5.0.7.tar.gz

3.解压到/opt/ 目录下
tar zxf redis-5.0.7.tar.gz -C /opt/

4.做个软链接
ln -s /opt/redis-5.0.7 /opt/redis

5.安装依赖
yum install gcc make -y

6.进入redis 目录,编译
cd /opt/redis
make && make install    如果安装make 时报错,执行 make MALLOC=libc

4.编写配置文档

1.创建数据和日志目录
mkdir -p /opt/redis_6379/{conf,logs,pid}
mkdir -p /data/redis_6379

2.编写配置文档
cat >/opt/redis_6379/conf/redis_6379.conf<<EOF
daemonize yes
bind 127.0.0.1 10.0.0.51
port 6379
pidfile /opt/redis_6379/pid/redis_6379.pid
logfile /opt/redis_6379/logs/redis_6379.log
EOF

5.启动redis

redis-server /opt/redis_6379/conf/redis_6379.conf

为什么不用将redis写入环境变量中,因为PATH 本身就是/usr/local/bin 下,因为make install 将生成的可执行文件复制到/usr/local/bin/下
[root@db01 /opt/redis_6379/logs23:15:19]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/mysql/bin:/root/bin

6.检查是否启动

ps -ef|grep redis
netstat -lntup|grep 6379

7.连接redis

[root@db01 ~]# redis-cli      
127.0.0.1:6379> set k1 v1       #设置K1 values 为v1
OK
127.0.0.1:6379> get k1          #查k1 的values 
"v1"
可以显示以上说明redis 连接上,而且正常

8.关闭命令

方法一: 在redis 中输入shutdown 再输入exit 或者ctrl +c
       [root@db01 ~]# redis-cli
       127.0.0.1:6379> SHUTDOWN
       
方法二: 在虚拟机机输入 redis-cli shutdown
       [root@db01 ~]# redis-cli shutdown
       
方法三: kill redis
       pkill redis

9 .system启动配置

1.先关闭redis
  redis-cli shutdown
  
2.创建redis虚拟用户和授权
  groupadd redis -g 2000
  useradd redis -u 2000 -g 2000 -M -s /bin/nologin
  chown -R redis.redis /opt/redis*
  chown -R redis.redis /data/redis*
  
 3. 编写配置档
 cat >/usr/lib/systemd/system/redis.service<<EOF
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/local/bin/redis-server /opt/redis_6379/conf/redis_6379.conf --supervised systemd
ExecStop=/usr/local/bin/redis-cli shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target
EOF

4.执行启动命令
systemctl daemon-reload   每次redis 断开后需要重行执行命令,systemctl start redis 才可以执行,要不然报错
systemctl start redis

10.优化警告

用systemctl start redis 后redis 日志会错以下四个警告错误

1. maximum open files过低
17068:M 23 Jun 2020 10:23:55.707 # You requested maxclients of 10000
requiring at least 10032 max file descriptors.
17068:M 23 Jun 2020 10:23:55.707 # Server can't set maximum open files
to 10032 because of OS error: Operation not permitted.
17068:M 23 Jun 2020 10:23:55.707 # Current maximum open files is 4096.
maxclients has been reduced to 4064 to compensate for low ulimit. If you
need higher maxclients increase 'ulimit -n'

解决方法: systemd启动⽂件添加参数
vim /usr/lib/systemd/system/redis.service
[Service]
..............
LimitNOFILE=65536

[root@db01 /data/soft22:55:03]# cat /usr/lib/systemd/system/redis.service
[Service]
ExecStart=/usr/local/bin/redis-server /opt/redis_6379/conf/redis_6379.conf --supervised systemd
ExecStop=/usr/local/bin/redis-cli shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=65536                              *****

2.WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

解决方法: 执行以下命令
echo "511" > /proc/sys/net/core/somaxconn 

3.overcommit_memory设置 虚拟内存相关
WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

 解决方法:
 sysctl vm.overcommit_memory=1
 
 4.关闭THP⼤内存⻚
 WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
 
 解决方法:
 临时解决: 虚拟机重起后就没有了
 echo never > /sys/kernel/mm/transparent_hugepage/enabled
 
 永久解决: 写入开机启动文件中
 vim /etc/rc.d/rc.local
 echo never > /sys/kernel/mm/transparent_hugepage/enabled
 
 赋予开机启动文件运行权限
 chmod +x /etc/rc.d/rc.local

11.配置文件解释

1.daemonize yes
# daemonize no 默认情况下, redis 不是在后台运⾏的,如果需要在后台运⾏,把该项的值更改为 yes

2.bind 127.0.0.1 10.0.0.51
# 指定 redis 只接收来⾃于该 IP 地址的请求,如果不进⾏设置,那么将处理所有请求

3.port 6379
# 指定redis运⾏的端⼝,默认是 6379

4.pidfile /opt/redis_6379/pid/redis_6379.pid
# 当redis在后台运⾏的时候, Redis默认会把pid⽂件放在 /var/run/redis.pid ,你可以配置到其他地址。
# 当运⾏多个redis服务时,需要指定不同的 pid ⽂件和端⼝

5.logfile /opt/redis_6379/logs/redis_6379.log
# 配置 log ⽂件地址

12.  redis    yum 直接安装相关配置和启动

#1.安装
   yum install -y redis   
#2.查看默认配置档和相关文件
  rpm -ql redis 
  
 [root@shell /var/log/redis22:23:13]# rpm -ql redis
/etc/logrotate.d/redis
/etc/redis-sentinel.conf
/etc/redis.conf
/etc/systemd/system/redis-sentinel.service.d
/etc/systemd/system/redis-sentinel.service.d/limit.conf
/etc/systemd/system/redis.service.d
/etc/systemd/system/redis.service.d/limit.conf
/usr/bin/redis-benchmark
/usr/bin/redis-check-aof
/usr/bin/redis-check-rdb
/usr/bin/redis-cli
/usr/bin/redis-sentinel
/usr/bin/redis-server
/usr/lib/systemd/system/redis-sentinel.service
/usr/lib/systemd/system/redis.service
/usr/libexec/redis-shutdown
/usr/share/doc/redis-3.2.12
/usr/share/doc/redis-3.2.12/00-RELEASENOTES
/usr/share/doc/redis-3.2.12/BUGS
/usr/share/doc/redis-3.2.12/CONTRIBUTING
/usr/share/doc/redis-3.2.12/MANIFESTO
/usr/share/doc/redis-3.2.12/README.md
/usr/share/licenses/redis-3.2.12
/usr/share/licenses/redis-3.2.12/COPYING
/usr/share/man/man1/redis-benchmark.1.gz
/usr/share/man/man1/redis-check-aof.1.gz
/usr/share/man/man1/redis-check-rdb.1.gz
/usr/share/man/man1/redis-cli.1.gz
/usr/share/man/man1/redis-sentinel.1.gz
/usr/share/man/man1/redis-server.1.gz
/usr/share/man/man5/redis-sentinel.conf.5.gz
/usr/share/man/man5/redis.conf.5.gz
/var/lib/redis
/var/log/redis
/var/run/redis
#3.启动redis
 /usr/bin/redis-server  /etc/redis.conf     #这样启动会在前台夯住,可以放后台启动
 nohup /usr/bin/redis-server  /etc/redis.conf &
#4.查看是否启动OK
[root@shell /var/log/redis22:23:26]# ps -ef |grep redis
root       7718   7624  0 22:16 pts/1    00:00:01 /usr/bin/redis-server 127.0.0.1:6379
root       7741   7443  0 22:25 pts/0    00:00:00 grep --color=auto redis
#5.连接redis
 redis-cli 或redis-cli -h ip #如果是多端口需要写明ip 端口,如果是单节点一个端口就是用这个命令redis-cli
#6.查看redis 版本信息
 连接上redis后输入info 即可看到版本信息
 [root@shell /var/log/redis22:19:02]# redis-cli
127.0.0.1:6379> info
# Server
redis_version:3.2.12
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:7897e7d0e13773f
redis_mode:standalone
os:Linux 3.10.0-957.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.8.5
process_id:7718
run_id:c4c51ede67e3e8baf06fc60bbbabe34a9f5d9611
tcp_port:6379
uptime_in_seconds:183
uptime_in_days:0
hz:10
lru_clock:6571998
executable:/usr/bin/redis-server
config_file:/etc/redis.conf

 

Do everything well
原文地址:https://www.cnblogs.com/linuxmysql/p/15440652.html