macbook 源码安装 redis

获取源码

编译安装

  • 解压后,进入 redis-6.2.6 目录。执行如下命令,查看编译步骤会如何执行

    [21:12:28 dongling@noah redis-6.2.6]106$ make -n
    cd src && /Library/Developer/CommandLineTools/usr/bin/make all
    /bin/sh: pkg-config: command not found
    echo ""
    echo "Hint: It's a good idea to run 'make test' ;)"
    echo "" 
    

    可以看到,命令行会进入 src 目录执行编译命令。在 src 目录下,果然找到一个 Makefile。打开 src/Makefile,可以看到结尾的 install 命令如下:

    install: all
    	@mkdir -p $(INSTALL_BIN)
    	$(call MAKE_INSTALL,$(REDIS_SERVER_NAME),$(INSTALL_BIN))
    	$(call MAKE_INSTALL,$(REDIS_BENCHMARK_NAME),$(INSTALL_BIN))
    	$(call MAKE_INSTALL,$(REDIS_CLI_NAME),$(INSTALL_BIN))
    	@ln -sf $(REDIS_SERVER_NAME) $(INSTALL_BIN)/$(REDIS_CHECK_RDB_NAME)
    	@ln -sf $(REDIS_SERVER_NAME) $(INSTALL_BIN)/$(REDIS_CHECK_AOF_NAME)
    	@ln -sf $(REDIS_SERVER_NAME) $(INSTALL_BIN)/$(REDIS_SENTINEL_NAME)
    
    uninstall:
        rm -f $(INSTALL_BIN)/{$(REDIS_SERVER_NAME),$(REDIS_BENCHMARK_NAME),$(REDIS_CLI_NAME),$(REDIS_CHECK_RDB_NAME),$(REDIS_CHECK_AOF_NAME),$(REDIS_SENTINEL_NAME)}
    

    可以知道 make install 命令会把文件安装到 $(INSTALL_BIN) 这个目录下。其定义如下:

    PREFIX?=/usr/local
    INSTALL_BIN=$(PREFIX)/bin
    INSTALL=install
    PKG_CONFIG?=pkg-config
    

    src/Makefile中的INSTALL_BIN 修改为 INSTALL_BIN=$(PREFIX)/redis,这样就可以将redis安装到我们想要的目录下了。此时创建 /usr/local/redis-6.2.6 目录,然后创建软连接 /usr/local/redis 到该目录:

    sudo mkdir -p /usr/loca/redis-6.2.6
    sudo ln -sf /usr/loca/redis-6.2.6 /usr/loca/redis
    sudo chown -R dongling:staff /usr/loca/redis-6.2.6
    sudo chown -R dongling:staff /usr/loca/redis
    
  • 编译并安装
    回到 redis-6.2.6 源码目录,执行如下命令:

    make  # 编译
    make install # 安装
    

    并将 redis-6.2.6/redis.conf 文件 copy 到 /usr/local/redis-6.2.6/conf 目录

配置环境变量

  • 将redis目录添加到PATH环境变量中

    REDIS_HOME=/usr/local/redis
    export PATH=$PATH:$REDIS_HOME
    alias startredis='nohup redis-server ${REDIS_HOME}/conf/redis.conf 2>&1 >> ${REDIS_HOME}/redis.log &'
    alias redis='redis-cli'
    

    环境变量生效后,直接在命令行执行 startredis 即可启动redis;
    在命令行执行 redis 即可使用客户端连接 redis-server

上一篇:关于MacBook拖放技巧


下一篇:M1 macbook安装jdk