环境准备:CentOS 7.5
一、调整系统配置:
1、调整 vm.overcommit_memory
echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf
它是内存分配策略
可选值:0、1、2。
0, 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1, 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2, 表示内核允许分配超过所有物理内存和交换空间总和的内存。
2、调整 Transparent Huge Pages (THP)
解决:redis 做 rdb 时会有部分请求超时的 case
echo never > /sys/kernel/mm/transparent_hugepage/enabled
并在/etc/rc.local 追加
echo "never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.local
3、TCP backlog 设置
echo 2048 > /proc/sys/net/core/somaxconn
在/etc/sysctl.conf 添加 net.core.somaxconn = 2048
echo "net.core.somaxconn = 2048" >> /etc/sysctl.conf
sysctl -p
4、配置资源限制文件
1 2 3 4 | echo "redis hard nofile 10240" >> /etc/security/limits .conf echo "redis soft nofile 10240" >> /etc/security/limits .conf echo "redis hard nproc 8192" >> /etc/security/limits .conf echo "redis soft nproc 8192" >> /etc/security/limits .conf |
二、部署redis
1、创建用户
groupadd -g 601 redis
useradd -u 6001 -g 601 redis
id redis
passwd redis
2、创建目录
mkdir -p /redis/log
mkdir -p /redis/conf
mkdir -p /redis/data
3、编译安装
su - redis
tar zxvf redis-5.0.5.tar.gz
cd redis-5.0.5
make
make PREFIX=/redis install
4、配置redis用户环境变量
vi ~/.bash_profile
PATH=$PATH:$HOME/.local/bin:$HOME/bin:/redis/bin
source ~/.bash_profile
5、调整 redis.conf
daemonize yes
logfile "/redis/log/redis.log"
requirepass beijing
bind 172.16.254.106
6、设置开机启动
vi /etc/rc.local
su - redis -c "/redis/bin/redis-server /redis/conf/redis.conf"
三、日常运维
1、启动redis
redis-server /redis/conf/redis.conf
2、关闭redis
redis-cli -p 6379 -h 172.16.254.106 -a beijing
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.16.254.106:6379> shutdown
not connected>
或者
pkill redis-server
3、redis密码管理
1)初始化密码
修改/redis/conf/redis.conf
重启redis
2)不重启redis修改密码
vi redis.conf
redis-cli -p 6379 -h 172.16.254.106 -a beijing
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.16.254.106:6379> config get requirepass
1) "requirepass"
2) "beijing"
172.16.254.106:6379> config set requirepass beijing123
OK
172.16.254.106:6379> config get requirepass
1) "requirepass"
2) "beijing123"