[root@iZ8hmg40voakzkZ ~]# yum install gcc* tcl tcl-devel make -y
[root@iZ8hmg40voakzkZ ~]# wget http://download.redis.io/releases/redis-6.2.4.tar.gz
[root@iZ8hmg40voakzkZ ~]# tar xzf redis-6.2.4.tar.gz
[root@iZ8hmg40voakzkZ ~]# cd redis-6.2.4/src/
[root@iZ8hmg40voakzkZ src]# make && make install
通过上图,我们可以很容易的看出,redis安装到了/usr/local,/usr/local/bin,/usr/local/share,/usr/local/include,/usr/local/lib,/usr/local/share/man目录下。
[root@iZ8hmg40voakzkZ src]# make test
[root@redis redis-6.2.4]# cd utils/
[root@redis utils]# ./install_server.sh
编辑文件,注释76-83行内容
[root@redis utils]# vim install_server.sh
[root@redis utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
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!
[root@redis utils]# vim install_server.sh
[root@redis utils]# ./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]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
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配置文件为/etc/redis/6379.conf
日志文件为/var/log/redis_6379.log
数据文件dump.rdb存放到/var/lib/redis/6379目录下
启动脚本为/etc/init.d/redis_6379
[root@redis utils]# vim /etc/systemd/system/redis_6379.service
[Unit]
Description=Redis on port 6379
[Service]
Type=forking
ExecStart=/etc/init.d/redis_6379 start
ExecStop=/etc/init.d/redis_6379 stop
[Install]
WantedBy=multi-user.target
注释:
[Unit] 表示这是基础信息
Description 是描述
[Service] 表示这里是服务信息
Type=forking是后台运行的形式
ExecStart 是启动服务的命令
ExecStop 是停止服务的指令
[Install] 表示这是是安装相关信息
WantedBy 是以哪种方式启动:multi-user.target表明当系统以多用户方式(默认的运行级别)启动时,这个服务需要被自动运行。
[root@redis ~]# systemctl daemon-reload
[root@redis ~]# systemctl enable redis_6379.service
Created symlink from /etc/systemd/system/multi-user.target.wants/redis_6379.service to /etc/systemd/system/redis_6379.service.
[root@redis ~]# systemctl start redis_6379.service
[root@redis ~]# systemctl status redis_6379.service
● redis_6379.service - Redis on port 6379
Loaded: loaded (/etc/systemd/system/redis_6379.service; enabled; vendor preset: disabled)
Active: active (running) since 日 2023-01-29 17:32:36 CST; 17s ago
Process: 2641 ExecStart=/etc/init.d/redis_6379 start (code=exited, status=0/SUCCESS)
Main PID: 2643 (redis-server)
Tasks: 5
Memory: 1.3M
CGroup: /system.slice/redis_6379.service
└─2643 /usr/local/bin/redis-server 127.0.0.1:6379
1月 29 17:32:36 redis systemd[1]: Starting Redis on port 6379...
1月 29 17:32:36 redis redis_6379[2641]: Starting Redis server...
1月 29 17:32:36 redis systemd[1]: Started Redis on port 6379.
[root@redis ~]# redis-cli --version
redis-cli 6.2.4
设置redis监听的地址,添加监听redis主机的ip
考虑到安全性,我们需要启用peiredis的密码验证功能requirepass参数
[root@redis redis]# grep -Ev '^#|^$' /etc/redis/6379.conf
bind 127.0.0.1 172.17.1.13
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis_6379.log
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 /var/lib/redis/6379
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
requirepass Qaz@2023Wzx
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"
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
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-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
[root@redis redis]# systemctl restart redis_6379.service
[root@redis redis]# netstat -anptu | grep redis
tcp 0 0 172.17.1.13:6379 0.0.0.0:* LISTEN 32358/redis-server
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 32358/redis-server
[root@redis ~]# redis-cli -h 172.17.1.13 -p 6379 -a Qaz@2023Wzx
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.17.1.13:6379> keys *
(empty array)
172.17.1.13:6379> set name list
OK
172.17.1.13:6379> get name
"list"
172.17.1.13:6379>
-h:指定连接地址 -p:指定端口号 -a:指定密码
keys *是查看redis所有的键值对
set namelisi添加一个键值name,内容为lisi
get name查看name这个键值的内容