centos虚拟机部署redis踩坑

以前都是用的云服务器或windows本地启动的redis,想在本地linux虚拟机部署一个,踩了一些坑,记录一下。

部署流程:

整个redis部署流程就是:github官网下载源码,进入redis源码目录make命令编译,再进入src目录make install命令安装。安装成功后会将得到的文件默认放在/usr/local/bin目录下:
在这里插入图片描述
每个文件的具体功能说明:

  1. redis-server

    • 用途:这是 Redis 服务器的主要程序。运行这个程序可以启动一个 Redis 数据库实例。

    • 使用示例

      ./redis-server
      
    • 可以指定配置文件来定制 Redis 的运行参数:

      ./redis-server /path/to/redis.conf
      
  2. redis-cli

    • 用途:Redis 命令行客户端,用于与 Redis 服务器进行交互,发送命令并接收回复。

      ./redis-cli
      
    • 也可以直接在命令后跟上要执行的命令:

      ./redis-cli PING
      
  3. redis-benchmark

    • 用途:性能测试工具,用来模拟 N 个客户端同时发送 M 个请求来评估 Redis 的性能。

      ./redis-benchmark -h localhost -p 6379 -c 50 -n 100000
      
    • 上述命令表示使用 50 个客户端连接到本地 Redis 服务,并发送总共 100,000 个请求。

  4. redis-check-aof

    • 用途:AOF(Append Only File)文件检查工具,用于修复可能损坏的 AOF 文件。当 Redis 无法加载 AOF 文件时,可以使用此工具尝试恢复数据。

    • 使用示例

      ./redis-check-aof --fix /path/to/appendonly.aof
      
  5. redis-check-rdb

    • 用途:RDB(Redis DataBase file)文件检查工具,用于检查 RDB 文件是否损坏。这对于确保备份文件的有效性很有帮助。

    • 使用示例

      ./redis-check-rdb /path/to/dump.rdb
      
  6. redis-sentinel

    • 用途:Redis Sentinel 是 Redis 的高可用性解决方案。它监控主从实例的状态,并在检测到故障时自动进行故障转移。

    • 使用示例

      ./redis-sentinel /path/to/sentinel.conf
      
    • 注意:实际上,你可以通过 redis-server 来运行 Sentinel 配置文件,因为 redis-sentinelredis-server 的一个符号链接,专门用于运行 Sentinel 模式。

修改redis.conf配置:

将daemonize属性改为yes,代表以后台服务的方式启动Redis。

requirepass 更改密码

注释掉 bind 127.0.0.1 -::1 ,否则只能本地连接redis

部署中遇到的问题:

1.安装虚拟机时选择的最小安装,导致没有自带wget,ifconfig,vim等。

解决:选择更复杂的安装,例如基本网页服务器,右边勾选系统管理工具。
在这里插入图片描述
2.yum安装gcc时yum源报错:

已加载插件:fastestmirror, langpacks
Determining fastest mirrors

One of the configured repositories failed (未知),
and yum doesn’t have enough cached data to continue. At this point the only
safe thing yum can do is fail. There are a few ways to work “fix” this:

 1. Contact the upstream for the repository and get them to fix the problem.

 2. Reconfigure the baseurl/etc. for the repository, to point to a working
    upstream. This is most often useful if you are using a newer
    distribution release than is supported by the repository (and the
    packages for the previous distribution release still work).

 3. Run the command with the repository temporarily disabled
        yum --disablerepo=<repoid> ...

 4. Disable the repository permanently, so yum won't use it by default. Yum
    will then just ignore the repository until you permanently enable it
    again or use --enablerepo for temporary usage:

        yum-config-manager --disable <repoid>
    or
        subscription-manager repos --disable=<repoid>

 5. Configure the failing repository to be skipped, if it is unavailable.
    Note that yum will try to contact the repo. when it runs most commands,
    so will have to try and fail each time (and thus. yum will be be much
    slower). If it is a very temporary problem though, this is often a nice
    compromise:

        yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true

Cannot find a valid baseurl for repo: base/7/x86_64

解决:更换yum源,将/etc/yum.repos.d目录下的文件全删掉:

rm -rf /etc/yum.repos.d

更换为其它源,例如华为云:repo.huaweicloud.com/repository/conf/
在这里插入图片描述

找到对应版本,wget下载到/etc/yum.repos.d目录下,清楚yum缓存

cd /etc/yum.repos.d
wget https://repo.huaweicloud.com/repository/conf/CentOS-7-reg.repo
yum clean all

3.编译redis时,cd src && make all which: no python3 in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin) make[1]: 进入目录“/software/redis-7.4.2/src” CC threads_mngr.o In file included from server.h:58:0, from threads_mngr.c:15: zmalloc.h:29:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录 #include <jemalloc/jemalloc.h> ^ 编译中断。 make[1]: * [threads_mngr.o] 错误 1 make[1]: 离开目录“/software/redis-7.4.2/src” make: * [all] 错误 2

新版redis默认用的jemalloc,但系统中没有。

解决:禁用jemalloc来编译:

make MALLOC=libc

或者安装jemalloc后编译

4.启动报错:

[root@localhost bin]# redis-server redis.conf
49754:C 14 Feb 2025 00:48:03.092 # WARNING Memory overcommit must be enabled! Without it, a background save or replication 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

因为redis需要更多的内存

解决:在/etc/sysctl.conf文件中添加一行:vm.overcommit_memory = 1,然后重启机器或者
直接执行命令:

sysctl vm.overcommit_memory=1

(就是报错里说的解决办法)

### 安装 Redis虚拟机 为了在虚拟机中成功部署和配置 Redis,需先准备环境并安装必要的依赖项。通过命令 `yum install -y gcc tcl` 可以完成这些软件包的安装[^1]。 接着应将 Redis安装文件上传至虚拟机内,并解压该压缩包以便后续操作。通常情况下,建议创建专门目录用于存放 Redis 文件,这有助于保持系统的整洁有序。 #### 配置 Redis 启动参数 进入 `/usr/local/redis` 目录之后,利用 Vim 编辑器打开 `redis.conf` 文件进行编辑[^3]: ```bash cd /usr/local/redis vim redis.conf ``` 在此过程中可以调整一些默认设置来满足特定需求,比如绑定 IP 地址、端口号以及密码保护等选项。 #### 设置 Redis 开机自动启动 为了让 Redis 能够随系统一同启动,在 CentOS 或其他基于 Red Hat 的发行版上可以通过编写 Systemd 服务单元文件实现这一功能。新建名为 `redis.service` 的文件并将相应内容写入其中: ```ini [Unit] Description=Redis In-Memory Data Store After=network.target [Service] User=root ExecStart=/usr/local/redis/src/redis-server /usr/local/redis/redis.conf ExecStop=/usr/local/redis/src/redis-cli shutdown Restart=always [Install] WantedBy=multi-user.target ``` 保存上述修改后,使用如下指令使新建立的服务生效: ```bash systemctl enable redis.service systemctl start redis.service ``` 此时已经完成了 RedisLinux 虚拟机上的基本安装与配置工作。如果想要远程连接到服务器,则可借助于客户端工具执行类似下面这样的命令来进行测试[^2]: ```bash redis-cli -h <host> -p <port> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值