基于Linux环境安装Redis 6.2.9

基于Linux环境安装Redis 6.2.9

安装Redis

下载

找到对应的版本

https://download.redis.io/releases/

安装依赖

yum install -y gcc tcl jemalloc-devel

解压

tar -zxvf redis-6.2.9.tar.gz -C /opt

运行编译命令

cd redis-6.2.9
make && make install

耐心等待一段时间,没有明显的出错的话应该就安装成功了,其默认安装路径是/usr/local/bin/目录,所以我们需要到该目录检查一下

[root@localhost redis-6.2.9]# cd /usr/local/bin/
[root@localhost bin]# ll
total 18928
-rwxr-xr-x. 1 root root 4830064 Jun  7 23:04 redis-benchmark
lrwxrwxrwx. 1 root root      12 Jun  7 23:04 redis-check-aof -> redis-server
lrwxrwxrwx. 1 root root      12 Jun  7 23:04 redis-check-rdb -> redis-server
-rwxr-xr-x. 1 root root 5004176 Jun  7 23:04 redis-cli
lrwxrwxrwx. 1 root root      12 Jun  7 23:04 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 9542008 Jun  7 23:04 redis-server

该目录已经默认配置到环境变量,因此可以在任意目录下运行这些命令,其中:

  • redis-cli: 是redis提供的命令行客户端
  • redis-server: 是redis的服务端启动脚本
  • redis-sentinel: 是redis的哨兵启动脚本

启动Redis

启动Redis有很多种方式,在这里介绍以下三种:

  • 默认启动
  • 指定配置启动
  • 开机自启

默认启动:

安装完成后,可在任意目录下运行redis-server命令启动Redis:

redis-server

出现redis的日志界面,运行成功:

[root@localhost bin]# redis-server
26071:C 07 Jun 2024 23:08:23.763 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
26071:C 07 Jun 2024 23:08:23.763 # Redis version=6.2.9, bits=64, commit=00000000, modified=0, pid=26071, just started
26071:C 07 Jun 2024 23:08:23.763 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
26071:M 07 Jun 2024 23:08:23.767 * Increased maximum number of open files to 10032 (it was originally set to 1024).
26071:M 07 Jun 2024 23:08:23.767 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.2.9 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 26071
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

26071:M 07 Jun 2024 23:08:23.769 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
26071:M 07 Jun 2024 23:08:23.769 # Server initialized
26071:M 07 Jun 2024 23:08:23.769 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. 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.
26071:M 07 Jun 2024 23:08:23.769 * Ready to accept connections

这种方式为前台启动,会阻塞整个会话窗口,此时需要打开另一个窗口进行连接,窗口关闭或者按下【ctrl+c】则Redis停止。此种方式不推荐使用。

指定配置文件启动

若要让Redis以后台方式启动,则需要修改redis的配置文件,复制文件至redis-server命令下

cd /usr/local/bin
cp /opt/redis-6.2.9/redis.conf ./
修改配置文件
vi redis.conf
  • 监听的地址,默认是127.0.0.1,会导致只能在本地访问,改为0.0.0.0表示任何ip都可以访问,生产环境不要设置成0.0.0.0
# 原本是bind 127.0.0.1 -::1, 需要注释掉,加上bind 0.0.0.0
bind 0.0.0.0  
  • 守护进程,修改为yes后可后台进行
daemonize yes
  • 密码,设置后访问redis必须输入密码
requirepass 123321(密码可以随意)
Redis的其他常用配置
# 设置redis的日志文件,有错误会往里面放,没有设置路径,所以会放在打开redis服务的文件夹下
logfile "redis.log" 

# 监听的端口
port 6379

# 工作目录,默认是当前目录,也就是运行redis-server时的命令,日志、持久化等文件会保存在这个目录
dir .

# 数据库数量,设置为1,代表只使用1个库,默认有16个库,编号0~15
databases 1

# 设置redis能够使用的最大内存
maxmemory 512mb

修改保存后,启动Redis,加上配置文件的全路径,这里若是在redis安装目录下,可省去全路径。

redis-server redis.config

可查看redis 的进程。

ps -ef | grep redis

开机自启:

1、将redis加入开机自启服务,在/etc/systemd/system创建redis.service文件并加入内容

内容如下

[Unit]
Description=redis-server
After=network.target
 
[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /usr/local/bin/redis.conf
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

2、重新加载配置

systemctl daemon-reload

3、启动Redis

systemctl start redis

4、查看Redis状态

systemctl status redis

5、设置开机自启

systemctl enable redis
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值