Redis 的安装: https://fqk.io/redis-installation/

根据安装 Redis 的流程,将默认的端口号 6379 改为替换为新的端口号即可。

以 Ubuntu 为例,在执行上述标准配置之后,一键新建端口脚本(保存,用 sudo 执行)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/bin/bash

if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi

targetPort=$1
defaultPort=6379

if [ -z ${targetPort} ]; then
echo "Parameters error"
exit
fi

redisRoot="/usr/local/etc/redis"

if [ ! -d "${redisRoot}/${targetPort}" ]; then
mkdir -p "${redisRoot}/${targetPort}"
fi

sed -e 's/'${defaultPort}'/'${targetPort}'/g' "${redisRoot}/${defaultPort}/redis.conf" > "${redisRoot}/${targetPort}/redis.conf"
sed -e 's/'${defaultPort}'/'${targetPort}'/g' /etc/init.d/redis_${defaultPort} > /etc/init.d/redis_${targetPort}
chmod +x "/etc/init.d/redis_${targetPort}"

## Ubuntu 开机自动启动
update-rc.d redis_${targetPort} defaults
## CentOS 开机自动启动
# echo "/etc/init.d/redis_${targetPort} start" >> /etc/rc.d/rc.local

## 启动 Redis
/etc/init.d/redis_${targetPort} start

macOS 脚本示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/bin/bash

targetPort=$1
defaultPort=6379

if [ -z ${targetPort} ]; then
echo "Parameters error"
exit
fi

redisRoot="/usr/local/etc/redis"

if [ ! -d "${redisRoot}/${targetPort}" ]; then
mkdir -p "${redisRoot}/${targetPort}"
fi

sed -e 's/'${defaultPort}'/'${targetPort}'/g' "${redisRoot}/${defaultPort}/redis.conf" > "${redisRoot}/${targetPort}/redis.conf"

sed -e 's/'${defaultPort}'/'${targetPort}'/g' "${redisRoot}/redis_${defaultPort}" > "${redisRoot}/redis_${targetPort}"
chmod +x "${redisRoot}/redis_${targetPort}"

## 启动 Redis
${redisRoot}/redis_${targetPort} start

## macOS 开机自动启动
sed -e 's/'${defaultPort}'/'${targetPort}'/g' ~/Library/LaunchAgents/io.redis.${defaultPort}.plist > ~/Library/LaunchAgents/io.redis.${targetPort}.plist
launchctl load -w ~/Library/LaunchAgents/io.redis.${targetPort}.plist