# 安装 gcc
yum install -y gcc
# 升级 gcc 9
# CentOS7 默认安装的是 4.8.5,而 redis 6.0 只支持 5.3 以上版本
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
# 临时切换成 gcc 9
scl enable devtoolset-9 bash
# 永久切换
echo "source /opt/rh/devtoolset-9/enable" >> ~/.zshrc
# 查询 gcc 版本
gcc -v
wget http://download.redis.io/releases/redis-6.0.6.tar.gz
cd redis-6.0.6
make install
mkdir /etc/redis
cp ./redis.conf /etc/redis/redis.conf
# 修改 redis.conf
vi /etc/redis/redis.conf
# 修改 daemonize no 为 daemonize yes
# 注释掉 bind 127.0.0.1 或者改成 bind 0.0.0.0
# 设置 redis 连接密码: 在 requirepass foobard 改为requirepass xxx 这里 xxx 为你的密码
# 修改 port 监听端口,默认是6379
# 运行
./src/redis-server ./redis.conf
# 设置开机自启动
vi /etc/systemd/system/redis.service
redis.service
[Unit]
Description=redis-server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
会出错,提示Failed to get D-Bus connection: Operation not permitted
这是由于 WSL 不支持 systemd 导致的microsoft/WSL#994
解决方法如下:
mv /usr/bin/systemctl /usr/bin/systemctl.old
curl https://raw.githubusercontent.com/gdraheim/docker-systemctl-replacement/master/files/docker/systemctl.py > /usr/bin/systemctl
chmod +x /usr/bin/systemctl
systemctl start redis.service #启动redis服务
systemctl stop redis.service #停止redis服务
systemctl restart redis.service #重新启动服务
systemctl status redis.service #查看服务当前状态
systemctl enable redis.service #设置开机自启动
systemctl disable redis.service #停止开机自启动