NoSQL数据库之Redis安装
Redis下载安装
官方下载地址:http://download.redis.io/releases/
yum安装redis
在centos系统上需要安装epel源
CentOS 8 由系统源提供
#查看yum仓库redis版本
[root@centos8 ~]# dnf info redis
Name : redis
Version : 5.0.3
CentOS 7 由epel源提供
[root@centos7 ~]# yum info redis
Loaded plugins: fastestmirror
Name : redis
Arch : x86_64
Version : 3.2.12
yum安装 redis
[root@centos8 ~]# dnf -y install redis
编译安装 redis
下载当前最新release版本redis 源码包:http://download.redis.io/releases/
编译安装
官方的安装方法:https://redis.io/download
范例: 编译安装过程
#安装依赖包
[root@centos7 ~]# yum -y install gcc jemalloc-devel
#如果支持systemd需要安装下面包
[root@centos7 ~]# yum -y install gcc jemalloc-devel systemd-devel
[root@ubuntu1804 ~]# apt -y install make gcc libjemalloc-dev libsystemd-dev
#下载源码
[root@centos7 ~]# wget http://download.redis.io/releases/redis-5.0.7.tar.gz
[root@centos7 ~]# tar xvf redis-5.0.7.tar.gz
#编译安装
[root@centos7 ~]# cd redis-5.0.7/
[root@centos7 redis-5.0.7]# make PREFIX=/apps/redis install
#指定redis安装目录
#如果支持systemd,需要执行下面
[root@centos7 redis-6.2.4]# make USE_SYSTEMD=yes PREFIX=/apps/redis install
#配置变量
[root@centos7 ~]# echo 'PATH=/apps/redis/bin:$PATH' > /etc/profile.d/redis.sh
[root@centos7 ~]#. /etc/profile.d/redis.sh
#准备相关目录和配置文件
[root@centos7 ~]# mkdir /apps/redis/{etc,log,data,run}
#创建配置文件、日志、数据等目录
[root@centos7 redis-5.0.7]# cp redis.conf /apps/redis/etc/
范例:前台启动 redis
[root@centos7 ~]# redis-server /apps/redis/etc/redis.conf
范例: 开启 redis 多实例
[root@centos7 ~]# redis-server --port 6380
解决启动时的三个警告提示
tcp-backlog
WARNING: The TCP backlog setting of 511 cannot be enforced because
/proc/sys/net/core/somaxconn is set to the lower value of 128.
backlog参数控制的是三次握手的时候server端收到client ack确认号之后的队列值,即全连接队列
[root@centos7 redis-5.0.7]# vim /etc/sysctl.conf
net.core.somaxconn = 1024
[root@centos7 redis-5.0.7]# sysctl -p
vm.overcommit_memory
WARNING overcommit_memory is set to 0! Background save 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.
内核参数说明:
0、表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1、表示内核允许分配所有的物理内存,而不管当前的内存状态如何
2、表示内核允许分配超过所有物理内存和交换空间总和的内存
范例:
[root@centos7 ~]# vim /etc/sysctl.conf
vm.overcommit_memory = 1
[root@centos7 ~]# sysctl -p
transparent hugepage
WARNING you have Transparent Huge Pages (THP) support enabled in your kernel.
This will create latency and memory usage issues with Redis. To fix this issue
run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as
root, and add it to your /etc/rc.local in order to retain the setting after a
reboot. Redis must be restarted after THP is disabled.
警告:您在内核中启用了透明大页面(THP,不同于一般内存页的4k为2M)支持。 这将在Redis中造成延迟和
内存使用问题。 要解决此问题,请以root 用户身份运行命令“echo never>
/sys/kernel/mm/transparent_hugepage/enabled”,并将其添加到您的/etc/rc.local中,以便在
重启后保留设置。禁用THP后,必须重新启动Redis。
范例:
[root@centos7 ~]# echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.d/rc.local
[root@centos7 ~]# cat /etc/rc.d/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
echo never > /sys/kernel/mm/transparent_hugepage/enabled
[root@centos7 ~]# chmod +x /etc/rc.d/rc.local
再次启动 redis
将以上配置同步到其他redis 服务器
[root@centos7 ~]# redis-server /apps/redis/etc/redis.conf
创建 redis 用户和数据目录
[root@centos7 ~]# useradd -r -s /sbin/nologin redis
#设置目录权限
[root@centos7 ~]# chown -R redis.redis /apps/redis/
编辑 redis 服务启动文件
#复制CentOS8安装生成的redis.service文件,进行修改
[root@centos7 ~]# scp 172.31.0.8:/lib/systemd/system/redis.service
/lib/systemd/system/
[root@centos7 ~]# vim /usr/lib/systemd/system/redis.service
[Unit]
Description=Redis persistent key-value database
After=network.target
[Service]
ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT $MAINPID
#Type=notify 如果支持systemd可以启用此行
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
验证 redis 启动
[root@centos7 ~]# systemctl daemon-reload
[root@centos7 ~]# systemctl start redis
[root@centos7 ~]# systemctl status redis
使用客户端连接 redis
[root@centos7 ~]# /apps/redis/bin/redis-cli -h IP/HOSTNAME -p PORT -a PASSWORD
范例:
[root@centos7 ~]# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> info
...
127.0.0.1:6379> exit
[root@centos7 ~]#
创建命令软连接
[root@centos7 ~]# ln -sv /apps/redis/bin/redis-* /usr/bin/
编译安装后的命令
[root@centos7 ~]# ll /apps/redis/bin/
total 32772
-rwxr-xr-x 1 root root 4366792 Feb 16 21:12 redis-benchmark #redis性能测试工具
-rwxr-xr-x 1 root root 8125184 Feb 16 21:12 redis-check-aof #AOF文件检查工具
-rwxr-xr-x 1 root root 8125184 Feb 16 21:12 redis-check-rdb #RDB文件检查工具
-rwxr-xr-x 1 root root 4807856 Feb 16 21:12 redis-cli #客户端工具
lrwxrwxrwx 1 root root 12 Feb 16 21:12 redis-sentinel -> redis-server #哨兵,软连接到server
-rwxr-xr-x 1 root root 8125184 Feb 16 21:12 redis-server #redis 服务启动命令
一键脚本编译安装Redis
[root@centos8 ~]# cat install_redis.sh
#!/bin/bash
#Author: xuanlv
#Date: 2021-07-01
#FileName:install_redis.sh
#
VERSION=redis-6.2.4
TAR=.tar.gz
REDIS_URL=http://download.redis.io/releases/
PASSWORD=123456
SRC_DIR=/usr/local/src/
INSTALL_DIR=/apps/redis
CPUS=`lscpu | awk '/^CPU\(s\)/{print $2}'`
GREEND="echo -e \E[1;32m"
RED="echo -e \E[1;31m"
END="\E[0m"
os_type(){
awk -F'[ "]' '/^NAME/{print $2}' /etc/os-release
}
os_version(){
awk -F'"' '/^VERSION_ID/{print $2}' /etc/os-release
}
check(){
[ -e ${INSTALL_DIR} ] && { ${GREEND} "Redis 已安装,请卸载后再安装"${END}; exit; }
cd ${SRC_DIR}
if [ -e ${VERSION}${TAR} ];then
${GREEND}"相关文件已准备好"${END}
else
${GREEND}"开始下载Redis源码包"${END}
if rpm -q wget &> /dev/null || yum install -y wget &> /dev/null
then
wget ${REDIS_URL}${VERSION}${TAR} &> /dev/null
[ -f ${SRC_DIR}${VERSION}${TAR} ] && ${GREEND}"Redis源码包下载成功!"${END} || { ${RED}"下载 ${VERSION}${TAR}文件失败"${END}; exit; }
else
dpkg -s wget &> /dev/null || apt install -y wget
wget ${REDIS_URL}${VERSION}${TAR} &> /dev/null
[ -f ${SRC_DIR}${VERSION}${TAR} ] && ${GREEND}"Redis源码包下载成功!"${END} || { ${RED}"下载 ${VERSION}${TAR}文件失败"${END}; exit; }
fi
[ $? -ne 0 ] && { ${RED}"下载 ${VERSION}${TAR}文件失败"${END}; exit; }
fi
}
install_redis(){
${GREEND}"开始安装Redis"${END}
if id redis &> /dev/null;then
${GREEND}"Redis用户已存在"${END}
else
useradd -r -s /sbin/nologin redis
${GREEND}"创建Redis用户"${END}
fi
${GREEND}"开始安装Redis依赖包"${END}
if [ `os_type` == "CentOS" -a `os_version` == '8' ];then
yum -q install -y gcc make systemd-devel tcl &> /dev/null
elif [ `os_type` == "CentOS" -a `os_version` == '7' ];then
yum -q install -y gcc make systemd-devel &> /dev/null
else
apt update &> /dev/null
apt -y install make gcc libjemalloc-dev libsystemd-dev &> /dev/null
fi
cd $SRC_DIR
tar xf ${VERSION}${TAR}
cd ${VERSION}
make -j ${CPUS} PREFIX=${INSTALL_DIR} install
make USE_SYSTEMD=yes PREFIX=/apps/redis install
[ $? -eq 0 ] && ${GREEND}"Redis编译安装成功"${END} || { ${RED}"Redis编译安装失败,退出"${END}; exit; }
ln -s ${INSTALL_DIR}/bin/redis-* /usr/bin/
mkdir -p ${INSTALL_DIR}/{etc,log,data,run}
cp redis.conf ${INSTALL_DIR}/etc/
sed -i -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e "/# requirepass/a requirepass $PASSWORD" -e "/^dir .*/c dir ${INSTALL_DIR}/data/" -e "/logfile .*/c logfile ${INSTALL_DIR}/log/redis-6379.log" -e "/^pidfile .*/c pidfile ${INSTALL_DIR}/run/redis_6379.pid" ${INSTALL_DIR}/etc/redis.conf
chown -R redis.redis ${INSTALL_DIR}
cat >> /etc/sysctl.conf <<-EOF
net.core.somaxconn = 1024
vm.overcommit_memory = 1
EOF
sysctl -p
echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local
chmod +x /etc/rc.local
/etc/rc.local
cat > /usr/lib/systemd/system/redis.service <<-EOF
[Unit]
Description=Redis persistent key-value database
After=network.target
[Service]
ExecStart=${INSTALL_DIR}/bin/redis-server ${INSTALL_DIR}/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT \$MAINPID
#Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now redis.service &> /dev/null
systemctl is-active redis &> /dev/null || { ${RED}"Redis启动失败,退出"${END}; exit; }
${GREEND}"Redis安装完成!!!"${END}
}
main(){
check
install_redis
}
main