99-redis_install

0. 停启

  1. linux
    • service redis_6379 start/stop/restart/status
  2. mac
    • sudo sh ~/shell/redis/redis_6379.sh

1. download

# 源码下载
yum install wget https://download.redis.io/releases/redis-6.2.6.tar.gz

2. make

  • make编译后,src下便出现可执行程序,即可直接启动
F8E692FC-4D80-4E49-9602-87BC20E5FB54
[root@hecs-168322 redis-6.2.6]# pwd
/root/soft/redis6/redis-6.2.6
[root@hecs-168322 redis-6.2.6]# make
cd src && make all
make[1]: Entering directory `/root/soft/redis6/redis-6.2.6/src'
    CC Makefile.dep
make[1]: Leaving directory `/root/soft/redis6/redis-6.2.6/src'
make[1]: Entering directory `/root/soft/redis6/redis-6.2.6/src'
...

[root@hecs-168322 redis-6.2.6]# cd src/
[root@hecs-168322 src]# ll | grep redis-
-rwxr-xr-x 1 root root 5003808 Apr  2 17:30 redis-cli
-rw-rw-r-- 1 root root  318821 Oct  4  2021 redis-cli.c
-rw-r--r-- 1 root root     363 Apr  2 17:30 redis-cli.d
-rw-r--r-- 1 root root 1106976 Apr  2 17:30 redis-cli.o
-rwxr-xr-x 1 root root 9518928 Apr  2 17:30 redis-sentinel
-rwxr-xr-x 1 root root 9518928 Apr  2 17:30 redis-server
  1. README.md
  2. make命令为linux、mac自带的编译命令。具体怎样编译依赖Makefile文件
    • nginx源码是没有Makefile文件,需要configuration命令生成

0E0237CE-2D05-47A6-9F15-EF6D85B81BE1
C977918B-7A23-430A-91FA-BDDA7F0DB9A1

1. gcc

435E5A8B-29C9-4B04-AAC4-22C1C413710A
# 安装c环境
yum install gcc

# 第一次安装,一些条件不具备,可是已经make了一部分
# 清除make,把不具备的条件都具备下,再make
make distclean
DACD9EC9-13FB-4A83-9856-8719D2E16E9F

3. linux_install

1. make install

  • 通过 make install 。相当于创建redis-server快捷方式
[root@hecs-168322 redis-6.2.6]# make install PREFIX=/usr/local/redis
cd src && make install
make[1]: Entering directory `/root/soft/redis-6.2.6/src'
    CC Makefile.dep
make[1]: Leaving directory `/root/soft/redis-6.2.6/src'
make[1]: Entering directory `/root/soft/redis-6.2.6/src'

Hint: It's a good idea to run 'make test' ;)

    INSTALL redis-server
    INSTALL redis-benchmark
    INSTALL redis-cli
make[1]: Leaving directory `/root/soft/redis-6.2.6/src'
[root@hecs-168322 bin]# pwd
/usr/local/redis/bin
[root@hecs-168322 bin]# ll
total 18904
-rwxr-xr-x 1 root root 4829520 Apr  2 17:48 redis-benchmark
lrwxrwxrwx 1 root root      12 Apr  2 17:48 redis-check-aof -> redis-server
lrwxrwxrwx 1 root root      12 Apr  2 17:48 redis-check-rdb -> redis-server
-rwxr-xr-x 1 root root 5003808 Apr  2 17:48 redis-cli
lrwxrwxrwx 1 root root      12 Apr  2 17:48 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 9518928 Apr  2 17:48 redis-server

2. 环境变量

# 将下面的内容加到文件尾
vi /etc/profile

# 重启profile
source /etc/profile

# 验证环境变量的添加情况
echo $PATH


# profile文件末尾添加
# REDIS_HOME目录位置即install目录
export REDIS_HOME=/usr/local/redis
export PATH=$REDIS_HOME/bin:$PATH
# 新开窗口。验证环境变量的添加情况
[root@hecs-168322 ~]# echo $PATH
/usr/local/redis/bin:/usr/local/jdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

3. install_server.sh

[root@hecs-168322 utils]# pwd
/root/soft/redis-6.2.6/utils
[root@hecs-168322 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!

1. 修改

C4755B53-67EF-47F9-8F6C-03900E37A0BF

2. 创建instance

[root@hecs-168322 utils]# pwd
/root/soft/redis-6.2.6/utils
[root@hecs-168322 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@hecs-168322 utils]# vim install_server.sh
[root@hecs-168322 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]      # 1. 不断交互,进行redis实例配置
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/redis/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/redis/bin/redis-server
Cli Executable : /usr/local/redis/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             # 2. 开机启动脚本(停启脚本)
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!















 
















 





4. verify

# 停启脚本
service redis_6379 start/stop/restart/status

[root@hecs-168322 init.d]# service redis_6379 status
Redis is running (17999)

# redis_6379停启脚本存放目录
cd /etc/init.d/

# 查看redis进程
ps -ef | grep redis

4. mac_install

1. 环境变量

B8A3801C-EF61-4F3E-9AC0-E7DDE2BBDE3E
# 修改完了,强制保存:wq!。立即生效
# 'readonly' option is set (add ! to override)
vim /etc/zshrc

# zshrc文件最后加上
export REDIS_HOME=/usr/local/redis
PATH=$REDIS_HOME/bin:${PATH}

# 配置后,一定要新开个窗口,测试下
echo $path
➜  redis-6.2.6# echo $path
/usr/local/redis/bin ...

2. install

  • 安装到指定目录
# 指定目录编译
make install PREFIX=/usr/local/redis

# 清空编译
make distclean
➜  redis-6.2.6# make install PREFIX=/usr/local/redis
cd src && /Library/Developer/CommandLineTools/usr/bin/make install
/bin/sh: pkg-config: command not found
    CC Makefile.dep
/bin/sh: pkg-config: command not found

Hint: It's a good idea to run 'make test' ;)

mkdir: /usr/local/redis/bin: Permission denied
make[1]: *** [install] Error 1
make: *** [install] Error 2
#➜  redis-6.2.6 sudo make install PREFIX=/usr/local/redis
Password:
cd src && /Library/Developer/CommandLineTools/usr/bin/make install
/bin/sh: pkg-config: command not found

Hint: It's a good idea to run 'make test' ;)

    INSTALL redis-server
    INSTALL redis-benchmark
    INSTALL redis-cli
➜  bin# pwd
/usr/local/redis/bin
➜  bin# ll
total 5144
-rwxr-xr-x  1 root  wheel   417K Apr  2 22:08 redis-benchmark
lrwxr-xr-x  1 root  wheel    12B Apr  2 22:08 redis-check-aof -> redis-server
lrwxr-xr-x  1 root  wheel    12B Apr  2 22:08 redis-check-rdb -> redis-server
-rwxr-xr-x  1 root  wheel   354K Apr  2 22:08 redis-cli
lrwxr-xr-x  1 root  wheel    12B Apr  2 22:08 redis-sentinel -> redis-server
-rwxr-xr-x  1 root  wheel   1.8M Apr  2 22:08 redis-server

3. install_server.sh

# install_server.sh目录
cd /Users/list/soft/redis/redis-6.2.6/utils
  1. 无法找到默认配置文件redis.conf在哪儿(其实就在上一层目录里)
  2. install_server.sh替换下面内容
#Absolute path to this script
SCRIPT=$(readlink -f $0)
#Absolute path this script is in
SCRIPTPATH=$(dirname $SCRIPT)
#Absolute path this script is in
SCRIPTPATH=$(cd `dirname $0`; pwd)
  • unix不支持init.d
  • linux会将停启脚本写到init.d中,达到开机启动目的
  1. 因此mac没有redis_6379停启脚本
  2. 注释掉下面两部分
#copy to /etc/init.d
cp $TMP_FILE $INIT_SCRIPT_DEST && \
	chmod +x $INIT_SCRIPT_DEST || die "Could not copy redis init script to $INIT_SCRIPT_DEST"
echo "Copied $TMP_FILE => $INIT_SCRIPT_DEST"
/etc/init.d/redis_$REDIS_PORT start || die "Failed starting service..."
➜  utils# sudo ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server

readlink: illegal option -- f
usage: readlink [-n] [file ...]
Please select the redis port for this instance: [6379]                    # 指定port
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/redis/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/redis/bin/redis-server
Cli Executable : /usr/local/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Installing service...
No supported init tool found.                                             # init.d文件,linux支持。unix不支持
Installation successful!






 

 

 

 


 








 

4. redis_6379

mac上脚本路径~/shell

  1. 路径修改
  2. stop修改
    • linux命令-x /proc/${PID},unix不支持。linux一切皆文件,进程也是文件,可以cd /proc找到,unix找不到。替换为-f $PIDFILE
#!/bin/sh
#Configurations injected by install_server below....

EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli
PIDFILE=/var/run/redis_6379.pid
CONF=/etc/redis/6379.conf
REDISPORT="6379"

function Start(){
    if [ -f $PIDFILE ]; then
        echo "\033[33m$PIDFILE exists, process is already running or crashed\033[0m"
    else
        echo "\033[33mStarting Redis server...\033[0m"
        $EXEC $CONF
    fi
}

function Status(){
    if [ ! -f $PIDFILE ]; then
        echo '\033[33mRedis is not running\033[0m'
    else
        PID=$(cat $PIDFILE)
        echo "\033[33mRedis is running ($PID)\033[0m"
    fi
}

function Stop(){
    if [ ! -f $PIDFILE ]; then
        echo "\033[33m$PIDFILE does not exist, process is not running\033[0m"
    else
        PID=$(cat $PIDFILE)
        echo "\033[33mStopping ...\033[0m"
        $CLIEXEC -p $REDISPORT shutdown
        echo "\033[33mRedis stopped\033[0m"
    fi
}


while [ true ]; do
    echo "\033[31m------------------------------------------------------------- \033[0m"
    echo "\033[33m[start/stop/status/restart/exit] or [1/2/3/4/5]: \033[0m"
    read opt
    case "$opt" in
    start)
        Start
        ;;
    1)
        Start
        ;;
    stop)
        Stop
        ;;
    2)
        Stop
        ;;
    status)
        Status
        ;;
    3)
        Status
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    4)
        $0 stop
        $0 start
        ;;
    exit)
        echo "\033[33mbye bye~~\033[0m"
        break
        ;;
    5)
        echo "\033[33mbye bye~~\033[0m"
        break
        ;;
    *)
        echo "\033[33mPlease input right param. (^ _ ^)\033[0m"
        ;;
    esac
done

5. 6379.conf

  • pid和install_server.sh中的配置保持一致
# 1. 开启守护进程
daemonize yes

# 2. pid文件存放路径
pidfile /var/run/redis_6379.pid

6. 启动

# 1. 无指定conf启动。使用内嵌default_config。并没有使用任何路径下的conf
# 	 默认配置为:--port 6379 --daemonize no --dir ./ --dbfilename dump.rdb
➜  cluster# sudo redis-server
11018:C 08 Apr 2023 20:02:36.230 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
11018:C 08 Apr 2023 20:02:36.230 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=11018, just started
11018:C 08 Apr 2023 20:02:36.230 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
11018:M 08 Apr 2023 20:02:36.231 * Increased maximum number of open files to 10032 (it was originally set to 8192).
11018:M 08 Apr 2023 20:02:36.231 * monotonic clock: POSIX clock_gettime

# 2. 指定任一配置。即进行了Configuration loaded
➜  cluster# redis-server --port 6380
11361:C 08 Apr 2023 20:42:27.927 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
11361:C 08 Apr 2023 20:42:27.927 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=11361, just started
11361:C 08 Apr 2023 20:42:27.927 # Configuration loaded
11361:M 08 Apr 2023 20:42:27.928 * Increased maximum number of open files to 10032 (it was originally set to 8192).
11361:M 08 Apr 2023 20:42:27.928 * monotonic clock: POSIX clock_gettime

5. conf

# config引用其他config
# 一个主机 redis 有多个实例,有些配置是公共的,有些是私有的。私有配置引入公有配置即可
include /path/to/local.conf

# 添加module(模块),绝对路径
loadmodule /path/to/other_module.so

# 绑定ip,只有配置的ip,可以连接redis-server
bind 127.0.0.1

# 远程访问开关
protected-mode yes

# port
port 6379

# 后台运行
daemonize yes

# pid路径
pidfile /var/run/redis_6379.pid

# 日志级别
loglevel notice

# log路径
logfile /var/log/redis_6379.log

# db数量,0 ~ 15号
database 16

# 是否显示logo
always-show-logo yes

# security,是否需要密码
requirepass foobared

# 重命名,命令
rename-command CONFIG ""

# 最大允许连接数
maxclients 1000

# Memory management
# (1 ~ 10G)范围太大,做半数据化存储、数据迁移成本很高
maxmemory <bytes>

# 内存满了,处理策略
maxmemory-policy noeviction

6. Connection

1. 关闭6379防火墙

# 查看6379防火墙状态
firewall-cmd --query-port=6379/tcp

[root@hecs-168322 src]# firewall-cmd --query-port=6379/tcp
FirewallD is not running

# 关闭linux6379防火墙
firewall-cmd --add-port=6379/tcp

2. conf

6F44F9D6-A7F0-4847-A81F-6E8FA2FBD922
# 1. 关闭保护模式
protected-mode no

# 2. 注释下面代码,解除redis和本机的bind
bind 127.0.0.1 -::1