系统优化
# 系统优化 echo 'net.core.somaxconn= 2048' >> /etc/sysctl.conf # 尽可能不使用swap echo 'vm.swappiness = 0' >> /etc/sysctl.conf # 设置kill内存指标 echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf # 关闭大叶内存 echo never > /sys/kernel/mm/transparent_hugepage/enabled # 生效 sysctl -p
安装redis
# 安装依赖及命令 yum -y install gcc automake autoconf libtool make jemalloc wget # 下载安装包 wget http://download.redis.io/releases/redis-5.0.9.tar.gz # 解压软件包 tar -xf redis-5.0.9.tar.gz # 移到工作目录 mv redis-5.0.9 /usr/local/redis50 # 进入工作目录 cd /usr/local/redis50 # 初始化安装 make make install # 配置环境变量 cat >/etc/profile.d/redis50.sh<<'EOF' export PATH=/usr/local/redis50/src:$PATH EOF # 授权 chmod 700 /etc/profile.d/redis50.sh # 生效 source /etc/profile.d/redis50.sh
配置redis
# 创建数据目录 mkdir -p /data/redis/redis6379/ # 配置配置文件 cat >> /data/redis/redis6379/redis.conf <<EOF port 6379 bind 0.0.0.0 daemonize yes databases 16 protected-mode yes tcp-backlog 511 timeout 3600 tcp-keepalive 60 pidfile /data/redis/redis6379/redis.pid loglevel notice logfile "/data/redis/redis6379/redis.log" dbfilename dump.rdb save 900 1 save 300 10 save 60 10000 # redis最大占用内存 根据个人需求设置 maxmemory 4gb # redis最大连接数 根据个人需求设置 maxclients 20000 maxmemory-policy noeviction dir /data/redis/redis6379/ requirepass password ############################## aof日志配置 ############################### appendonly yes appendfsync everysec no-appendfsync-on-rewrite yes auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-rewrite-incremental-fsync yes ################################# 复制配置 ################################# #slaveof <master_ip> <master_port> masterauth password slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 repl-ping-slave-period 10 repl-timeout 600 repl-backlog-size 100mb repl-backlog-ttl 3600 ################################## 慢日志 ################################### # 10000微妙10毫秒 slowlog-log-slower-than 10000 slowlog-max-len 512 ############################### 高级配置 ############################### client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 ################################## 安全配置重命名危险命令 ################################### rename-command SHUTDOWN "SHUTDOWN_W9C6Q5xTchUqNL" rename-command SAVE "SAVE_ohFejQ8aYuEU2x" rename-command FLUSHALL "FLUSHALL_GaFBQfATtJM7Py" rename-command FLUSHDB "FLUSHDB_j6ruW0iQ97TenF" rename-command KEYS "KEYS_ZX778890" EOF
手工启停redis
# 指定配置文件启动redis redis-server /data/redis/6379/redis.conf # 指定实例及密码关闭redis redis-cli -p 6379 -a password SHUTDOWN_W9C6Q5xTchUqNL
使用systemd 管理redis
# 编辑配置文件 cat >/usr/lib/systemd/system/redis6379.service<<'EOF' [Unit] Description=Redis 6379 server After=syslog.target network.target remote-fs.target nss-lookup.target [Install] WantedBy=multi-user.target [Service] Type=forking # 启动redis ExecStart=/usr/local/redis50/src/redis-server /data/redis/redis6379/redis.conf # 关闭redis ExecStop=/usr/local/redis50/src/redis-cli -p 6379 -h 127.0.0.1 -a password SHUTDOWN_W9C6Q5xTchUqNL Restart=always PrivateTmp=true EOF # 重载systemd配置 systemctl daemon-reload
启停redis
# 启动redis systemctl start redis6379 # 查看状态 systemctl status redis6379
连接redis
redis-cli -h 127.0.0.1 -p 6379 -a password
测试redis
127.0.0.1:6379> set a 111 OK 127.0.0.1:6379> set b 2222 OK 127.0.0.1:6379> set c 3333 OK 127.0.0.1:6379> set d 4444 OK 127.0.0.1:6379> DBSIZE (integer) 4
远程连接
redis-cli -h 10.10.8.203 -p 6379 -a password 10.10.8.203:6379> DBSIZE (integer) 4 10.10.8.203:6379> exit # 退出命令行 # 查看内存 info memory
redis性能监控
# Server # Clients # Memory # Persistence # Stats # Replication # CPU # Cluster # Keyspace
开启主从复制
# 登录从库指定主库地址 127.0.0.1:6379> slaveof 10.10.8.203 6379 OK # 查看状态 127.0.0.1:6379> info replication
或者
# 免登录指定 redis-cli -p 6381 -h 10.10.8.204 -a Pass0rd SLAVEOF 10.10.8.203 6379 redis-cli -p 6382 -h 10.10.8.206 -a Pass0rd SLAVEOF 10.10.8.203 6379 # 免登录查看状态 redis-cli -p 6379 -h 10.10.8.203 -a Pass0rd info replication redis-cli -p 6379 -h 10.10.8.204 -a Pass0rd info replication redis-cli -p 6379 -h 10.10.8.206 -a Pass0rd info replication
本文章仅供参考
欢迎点赞、转发、评论。