1.从官网下载redis-6.0.5.tar.gz
https://download.redis.io/releases/
2.使用root创建redis用户和目录
[root@t3-dtpoc-dtpoc-web06 home]# useradd -d /home/redis -m redis
使用redis用户创建redis目录包括 data logs conf文件夹
[root@t3-dtpoc-dtpoc-web06 home]# su - redis
[redis@t3-dtpoc-dtpoc-web06 ~]$ pwd
/home/redis
[redis@t3-dtpoc-dtpoc-web06 ~]$ mkdir redis
[redis@t3-dtpoc-dtpoc-web06 ~]$ cd redis/
[redis@t3-dtpoc-dtpoc-web06 redis]$ mkdir data
[redis@t3-dtpoc-dtpoc-web06 redis]$ mkdir logs
[redis@t3-dtpoc-dtpoc-web06 redis]$ pwd
/home/redis/redis
[redis@t3-dtpoc-dtpoc-web06 redis]$ ls
conf data logs
3.将redis-6.0.5.tar.gz上传到/home/redis,并使用redis用户解压
[redis@t3-dtpoc-dtpoc-web06 ~]$ tar -xvf redis-6.0.5.tar.gz
.....
redis-6.0.5/utils/systemd-redis_server.service
redis-6.0.5/utils/tracking_collisions.c
redis-6.0.5/utils/whatisdoing.sh
[redis@t3-dtpoc-dtpoc-web06 ~]$ ls
redis redis-6.0.5 redis-6.0.5.tar.gz
4.使用redis用户编译并安装redis
[redis@t3-dtpoc-dtpoc-web06 ~]$cd redis-6.0.5
[redis@t3-dtpoc-dtpoc-web06 redis-6.0.5]$make
...
LINK redis-server
INSTALL redis-sentinel
CC redis-cli.o
LINK redis-cli
CC redis-benchmark.o
LINK redis-benchmark
INSTALL redis-check-rdb
INSTALL redis-check-aof
Hint: It's a good idea to run 'make test' ;)
make[1]: Leaving directory '/home/redis/redis-6.0.5/src'
安装redis,PREFIX=/home/redis/redis 设置安装目录
[redis@t3-dtpoc-dtpoc-web06 redis-6.0.5]$ make PREFIX=/home/redis/redis install
cd src && make install
make[1]: Entering directory '/home/redis/redis-6.0.5/src'
CC Makefile.dep
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: Leaving directory '/home/redis/redis-6.0.5/src'
可以看到自动产生了安装目录bin文件夹:
[redis@t3-dtpoc-dtpoc-web06 redis]$ ls
bin conf data logs
5.设置redis.conf配置文件,设置daemonize yes和protected-mode no
方法1:直接copy已有的redis.conf文件然后修改
[redis@t3-dtpoc-dtpoc-web06 redis-6.0.5]$ pwd
/home/redis/redis-6.0.5
[redis@t3-dtpoc-dtpoc-web06 redis-6.0.5]$ cp redis.conf /home/redis/redis/conf
[redis@t3-dtpoc-dtpoc-web06 redis-6.0.5]$ vi /home/redis/redis/conf/redis.conf
方法2:一般直接新建一个redis.conf文件,然后插入配置项,这里我们写入如下配置项
注意:pidfile很重要,默认是/var/run/redis_6379.pid,如果不更改成/home/redis目录下,redis可能没有权限,无法生成pid文件,而稍后我们配置service服务脚本判断redis是否启动的依据就是是否存在Pid文件
port 6379
daemonize yes
pidfile "/home/redis/redis/logs/redis_6379.pid"
logfile "/home/redis/redis/logs/redis_6379.log"
dir "/home/redis/redis/data"
6.启动redis server
[redis@t3-dtpoc-dtpoc-web06 bin]$ pwd
/home/redis/redis/bin
[redis@t3-dtpoc-dtpoc-web06 bin]$ ls
redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server
[redis@t3-dtpoc-dtpoc-web06 bin]$ ./redis-server ../conf/redis.conf
3138733:C 08 Oct 2023 17:23:11.393 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3138733:C 08 Oct 2023 17:23:11.393 # Redis version=6.0.5, bits=64, commit=00000000, modified=0, pid=3138733, just started
3138733:C 08 Oct 2023 17:23:11.393 # Configuration loaded
可以看到redis进程已经启动和端口监听
[redis@t3-dtpoc-dtpoc-web06 bin]$ ps -elf |grep -i redis
4 S root 3132139 3117780 0 80 0 - 59706 - 17:04 pts/0 00:00:00 su - redis
4 S redis 3132140 3132139 0 80 0 - 55895 - 17:04 pts/0 00:00:00 -bash
1 S redis 3138734 1 0 80 0 - 60985 do_epo 17:23 ? 00:00:00 ./redis-server 127.0.0.1:6379
0 R redis 3138765 3132140 0 80 0 - 56256 - 17:23 pts/0 00:00:00 ps -elf
0 S redis 3138766 3132140 0 80 0 - 53327 - 17:23 pts/0 00:00:00 grep -i redis
[redis@t3-dtpoc-dtpoc-web06 bin]$ netstat -tulnp
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN -
tcp 0 0 10.153.119.7:15400 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:15400 0.0.0.0:* LISTEN -
tcp 0 0 10.153.119.7:15401 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:15401 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 3138734/./redis-ser
tcp 0 0 10.153.119.7:15405 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
tcp6 0 0 :::9090 :::* LISTEN -
tcp6 0 0 ::1:15400 :::* LISTEN -
tcp6 0 0 ::1:15401 :::* LISTEN -
tcp6 0 0 :::3306 :::* LISTEN -
tcp6 0 0 :::4236 :::* LISTEN -
tcp6 0 0 :::111 :::* LISTEN -
tcp6 0 0 :::22 :::* LISTEN -
udp 0 0 0.0.0.0:111 0.0.0.0:* -
udp 0 0 127.0.0.1:323 0.0.0.0:* -
udp6 0 0 :::111 :::* -
udp6 0 0 ::1:323 :::* -
7.配置redis service服务
cp /home/redis/redis-6.0.5/utils/redis_init_script /etc/init.d/redis
chown redis /etc/init.d/redis
修改其中:
REDISPORT=6379
EXEC=/home/redis/redis/bin/redis-server
CLIEXEC=/home/redis/redis/bin/redis-cli
PIDFILE=/home/redis/redis/logs/redis_${REDISPORT}.pid
CONF="/home/redis/redis/conf/redis.conf"
测试:
[redis@t3-dtpoc-dtpoc-web06 data]$ service redis stop
Stopping ...
Redis stopped
[redis@t3-dtpoc-dtpoc-web06 data]$ service redis start
Starting Redis server...
[redis@t3-dtpoc-dtpoc-web06 data]$ ps -elf |grep -i redis
4 S root 3132139 3117780 0 80 0 - 59706 - Oct08 pts/0 00:00:00 su - redis
4 S redis 3132140 3132139 0 80 0 - 55895 - Oct08 pts/0 00:00:00 -bash
4 S root 3262862 3254597 0 80 0 - 59706 - 10:28 pts/1 00:00:00 su - redis
4 S redis 3262863 3262862 0 80 0 - 55864 - 10:28 pts/1 00:00:00 -bash
0 S redis 3265945 3262863 0 80 0 - 55837 core_s 10:52 pts/1 00:00:00 vi /etc/init.d/redis
1 S redis 3266216 1 0 80 0 - 8456 do_epo 10:54 ? 00:00:00 /home/redis/redis/bin/redis-server *:6379
0 R redis 3266306 3132140 0 80 0 - 56256 - 10:55 pts/0 00:00:00 ps -elf
0 S redis 3266307 3132140 0 80 0 - 53327 - 10:55 pts/0 00:00:00 grep -i redis
8.查看配置
127.0.0.1:6379> CONFIG GET *
-
"rdbchecksum"
-
"yes"
-
"daemonize"
-
"yes"
-
"io-threads-do-reads"
-
"no"
测试最基础的命令,重启redis进程后,输入的key没有保存,说明没有进行持久化,我们打开data目录也没有看到数据文件,说明哪里的设置有问题,继续研究
[redis@t3-dtpoc-dtpoc-web06 redis]$ ls
bin conf data logs
[redis@t3-dtpoc-dtpoc-web06 redis]$ cd bin
[redis@t3-dtpoc-dtpoc-web06 bin]$ ls
dump.rdb redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server
[redis@t3-dtpoc-dtpoc-web06 bin]$ ./redis-cli
127.0.0.1:6379> get read
(nil)
127.0.0.1:6379> set read 1000
OK
127.0.0.1:6379> get read
"1000"
127.0.0.1:6379> shutdown
not connected> exit
[redis@t3-dtpoc-dtpoc-web06 bin]$ ./redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected> exit
[redis@t3-dtpoc-dtpoc-web06 bin]$ service redis start
Starting Redis server...
[redis@t3-dtpoc-dtpoc-web06 bin]$ ./redis-cli
127.0.0.1:6379> get read
(nil)
127.0.0.1:6379>
然后在redis.conf文件加上如下配置后,重启redis服务,发现重启redis服务,数据也已经持久化了
注意:appendfilename会在dir参数的目录下创建data文件。appendfsync everysec是每秒进行持久化
appendonly yes
appendfilename "appendonly_6379.aof"
appendfsync everysec
[redis@t3-dtpoc-dtpoc-web06 bin]$ ./redis-cli
127.0.0.1:6379> get read
(nil)
127.0.0.1:6379> set read 10000
OK
127.0.0.1:6379> set read2 20000
OK
127.0.0.1:6379> shutdown
not connected> exit
[redis@t3-dtpoc-dtpoc-web06 bin]$ service redis start
Starting Redis server...
[redis@t3-dtpoc-dtpoc-web06 bin]$ ./redis-cli
127.0.0.1:6379> get read
"10000"
127.0.0.1:6379> get read2
"20000"
127.0.0.1:6379>
查看appendonly_6379.aof文件
[redis@t3-dtpoc-dtpoc-web06 data]$ pwd
/home/redis/redis/data
[redis@t3-dtpoc-dtpoc-web06 data]$ ls
appendonly_6379.aof