Centos7下的Redis安装

解压安装

一、下载Redis

手动下载地址:http://download.redis.io/releases/

1
2
mkdir tools && cd tools
wget http://download.redis.io/releases/redis-7.0.4.tar.gz

二、解压Redis

1
2
cd ../ 
tar -zxvf redis-7.0.4.tar.gz

三、编译安装

1
2
3
4
5
#下面操作如果报gcc错误,需要先安装gcc,
yum install -y gcc
cd redis-7.0.4/
make PREFIX=/home/redis/redis MALLOC=libc
cd src && make PREFIX=/home/redis/redis install

成功安装的提示:

1
2
3
4
5
6
7
8
9
    CC Makefile.dep

Hint: It's a good idea to run 'make test' ;)

INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install

四 开机自启

1
chkconfig redis on

另外: yum 安装

安装库

1
yum install epel-release

安装redis

1
yum install redis

服务操作

1
2
3
4
systemctl status redis # redis 状态
systemctl entable redis # 开机自启
systemctl start redis #启动
systemctl restart redis # 重启

启动操作

直接启动

1
2
3
#如果当前模式安装,会自动添加全局快捷方式
#直接执行下面命令可以启动redis
redis-server

后台方式启动

方式一:

  • 修改redis.conf文件
    daemonize no
    修改为
    daemonize yes
  • 指定redis配置文件启动
    1
    redis-server /home/redis/redis.conf 
    方式二:
    1
    nohup redis-server &

后台方式启动的关闭

1
2
3
ps -ef|grep redis
#root 7767 5357 0 10:31 pts/1 00:00:00 redis-server *:6379
kill -9 7767
链接

redis-cli -h yourIp-p yourPort -a youPassword

设置远程连接

编辑 redis.conf文件
bind 127.0.0.1这一行注释掉,或者 bind 0.0.0.0
protected-mode 要设置成 no
重启redis

防火墙配置

参考https://blog.csdn.net/irokay/article/details/72717132
切记:ssh远程访问时,不要把22端口删掉,22是ssh连接的端口。
各种数据库需要的默认端口:https://www.jianshu.com/p/50f15b3d0de6

1
2
#清除所有规则,开放所有端口
iptables -F

下面是我的一个iptables的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
#-A INPUT -p tcp -m state --state NEW -m multiport tcp --dport 1433,1521,3306,5000,5432,6379,9092,11211,27017 -j ACCEPT
-A INPUT -m state --state NEW -p tcp -m multiport --dport 1433,1521,3306,5000,5432,6379,9092,11211,27017 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8000:8888 -j ACCEPT

-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

配置文件

获取默认配置文件

1
2
3
4
daemonize no # 后台运行
bind 0.0.0.0 # 访问地址
port 6379 #端口
requirepass # 密码

其他操作

1
2
3
4
5
6
7
8
9
10
11
12
# 启动redis
service redis start
# 停止redis
service redis stop
# 查看redis运行状态
service redis status
# 查看redis进程
ps -ef | grep redis
# 进入本机redis
redis-cli
# 列出所有key
keys *

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# start
# ~/redis/redis-server ~/redis/redis.conf

bind 127.0.0.1 -::1

protected-mode yes

port 56379

tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
pidfile ~/redis/redis_56379.pid
loglevel notice
logfile ""
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync yes
repl-diskless-sync-delay 5
repl-diskless-sync-max-replicas 0
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128

requirepass Skynj@123

lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly no
appendfilename "appendonly.aof"
appenddirname "appendonlydir"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
aof-timestamp-enabled no
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-listpack-entries 512
hash-max-listpack-value 64
list-max-listpack-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-listpack-entries 128
zset-max-listpack-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes

本文地址 Centos7下的Redis安装

本文地址: https://github.com/maxzhao-it/blog/post/30297/