1.redis安装
[root@ser1 ~]# wget http://download.redis.io/releases/redis-5.0.4.tar.gz
[root@ser02 redis-5.0.4]# make
[root@localhost conf]# mkdir -p /data/redis
[root@ser02 redis-5.0.4]# make PREFIX=/data/redis install
[root@ser02 redis]# mkdir /data/redis/conf [root@ser02 redis]# cd /opt/redis-5.0.4/ [root@ser02 redis-5.0.4]# cp -p redis.conf /data/redis/conf/
2.生成的指令
[root@ser02 redis-5.0.4]# cd /data/redis/bin/
[root@ser02 bin]# ls
redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server
3.启动
[root@ser02 bin]# /data/redis/bin/redis-server -h
[root@ser02 bin]# /data/redis/bin/redis-server /data/redis/conf/redis.conf
[root@ser02 ~]# ps -ef |grep redis
root 6236 1687 0 22:25 pts/1 00:00:00 /data/redis/bin/redis-server
127.0.0.1:6379
root 6263 6244 0 22:28 pts/2 00:00:00 grep --color=auto redis
[root@ser02 ~]# netstat -alpn |grep 6236
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN
6236/redis-server 1
[root@ser02 ~]# /data/redis/bin/redis-server /data/redis/conf/redis.conf &
[root@ser02 ~]# /data/redis/bin/redis-cli shutdown
4.调优
6236:M 30 Apr 2019 22:25:23.046 # WARNING: The TCP backlog setting of 511 cannot
be enforced because /proc/sys/net/core/somaxconn is set to the lower value of
128.
6236:M 30 Apr 2019 22:25:23.046 # Server initialized
6236:M 30 Apr 2019 22:25:23.046 # 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.
6236:M 30 Apr 2019 22:25:23.046 # 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.
6236:M 30 Apr 2019 22:25:23.046 * Ready to accept connections
三个内核参数的调整:
backlog值得调整
服务端在服务启动后产生一个进程,
[root@ser02 ~]# cat /proc/sys/net/core/somaxconn
128
[root@ser02 ~]# echo 1024 > /proc/sys/net/core/somaxconn
[root@ser02 ~]# echo 1 > /proc/sys/vm/overcommit_memory
#overcommit_memory 1 容许过量使用内存 《= 物理内存 + swap 8G+2G ==10G
#12G内存 linux内存 oom内存泄漏 oom_kill
[root@ser02 ~]# cat /sys/kernel/mm/transparent_hugepage/enabled
[always] madvise never
[root@ser02 ~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
[root@ser02 ~]# cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]
5.客户端链接
.1 本地连接
[root@ser02 bin]# /data/redis/bin/redis-cli
127.0.0.1:6379>
127.0.0.1:6379> set name anliu
OK
127.0.0.1:6379> get name
"anliu"
127.0.0.1:6379> set IP 192.168.12.12
OK
127.0.0.1:6379> get IP
"192.168.12.12"
启动客户端、验证
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set var "hello world"
OK127.0.0.1:6379> get var"hello world"
6.远程连接
需要已经安装redis,可以使用redis-cli命令
redis-cli -h host -p port -a password
7.安全性设置
1 设置密码
打开redis.conf找到requirepass,去掉默认,修改
requirepass yourpassword
验证密码的正确性
[root@ser02 bin]# vim /data/redis/conf/redis.conf
requirepass redis
localhost:6379> auth jihite
OK
8.命令别名
rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52
设置启动日志
[root@ser02 bin]# vim /data/redis/conf/redis.conf
logfile "/data/redis/log/logs"
9.redis自启动脚本
[root@ser02 redis]# vim redserver.sh
#!/bin/bash
stop(){
/data/redis/bin/redis-cli -a redis shutdown
}
start(){
/data/redis/bin/redis-server /data/redis/conf/redis.conf &
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage:$0 (start|stop|restart)"
esac
[root@ser02 redis]# chmod +x redserver.sh
[root@ser02 redis]# vim /etc/profile
export PATH=/data/redis/:$PATH
[root@ser02 redis]# source /etc/profile
10.退出
关闭redis-server
redis-cli shutdown
例子
$ps -ef | grep redis
root 23422 19813 0 10:59 pts/5 00:00:08 redis-server *:6379
$sudo redis-cli shutdown
$ps -ef | grep redis
jihite 30563 19813 0 12:11 pts/5 00:00:00 grep redis
注:如果设置上密码后,单纯的redis-cli是关不掉的,必须加上ip、port、passwd
sudo redis-cli -h host -p port -a passwd shutdown
退出客户端
localhost:6379> QUIT
11.查看redis-server统计信息
INFO
[root@mini redis-4.0.11]# vim /usr/local/redis-4.0.11/redis.conf
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
# 默认情况下 redis 不是作为守护进程运行的,如果你想让它在后台运行,你就把它改成 yes。
# nothing bad happens, the server will start and run normally.
pidfile /var/run/redis_6379.pid
# 当redis作为守护进程运行的时候,它会把 pid 默认写到 /var/run/redis.pid 文件里面,你也可以
指定写入的位置
# in order to get the desired effect.
tcp-backlog 511
# TCP 监听的最大容纳数量
# 在高并发的环境下,你需要把这个值调高以避免客户端连接缓慢的问题。
# Examples:
bind 127.0.0.1 ::1 192.168.1.100
# 默认情况下,redis 在 server 上所有有效的网络接口上监听客户端连接。
# 你如果只想让它在一个网络接口上监听,那你就绑定一个IP或者多个IP。
# Close the connection after a client is idle for N seconds (0 to disable)
timeout 0
# 指定在一个 client 空闲多少秒之后关闭连接(0 就是不管它)
# Redis default starting with Redis 3.2.1.
tcp-keepalive 0
# tcp 心跳包
# 如果设置为非零,则在与客户端缺乏通讯的时候使用 SO_KEEPALIVE 发送 tcp acks 给客户端。
# debug (a lot of information, useful for development/testing)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
# warning (only very important / critical messages are logged)
loglevel notice
# 定义日志级别(debug,verbose,notice,warning)
# output for logging but daemonize, logs will be sent to /dev/null
logfile ""
# 指定日志文件的位置
# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT <dbid> where
# dbid is a number between 0 and 'databases'-1
databases 16
# 设置数据库的数目。
# 默认数据库是 DB 0,你可以在每个连接上使用 select 命令选择一个不同的数据库,
# 但是 dbid 必须是一个介于 0 到 databasees - 1 之间的值
# However if you have setup your proper monitoring of the Redis server
# and persistence, you may want to disable this feature so that Redis will
# continue to work as usual even if there are problems with disk,
# permissions, and so forth.
stop-writes-on-bgsave-error yes
# 默认情况下,如果 redis 最后一次的后台保存失败,redis 将停止接受写操作,
# 这样以一种强硬的方式让用户知道数据不能正确的持久化到磁盘,
# 否则就会没人注意到灾难的发生。
# 如果后台保存进程重新启动工作了,redis 也将自动的允许写操作。
# the dataset will likely be bigger if you have compressible values or keys.
rdbcompression yes
# 是否在 dump .rdb 数据库的时候使用 LZF 压缩字符串
# 默认都设为 yes
# 如果你希望保存子进程节省点 cpu ,你就设置它为 no ,
# 不过这个数据集可能就会比较大
# tell the loading code to skip the check.
rdbchecksum yes
# 是否校验rdb文件
# The filename where to dump the DB
dbfilename dump.rdb
# 设置 dump 的文件位置
# Note that you must specify a directory here, not a file name.
dir ./
#指定本地数据库存放目录
slaveof <masterip> <masterport>
#设置当本机为slav服务时,设置master服务的IP地址及端口,在Redis启动时,它会自动从master进行数据
同步
masterauth <master-password>
#当master服务设置了密码保护时,slave服务连接master的密码
equirepass foobared
#设置Redis连接密码,如果配置了连接密码,客户端在连接Redis时需要通过AUTH <password>命令提供密
码,默认关闭
maxclients 10000
#设置同一时间最大客户端连接数,默认无限制,Redis可以同时打开的客户端连接数为Redis进程可以打开的
最大文件描述符数,如果设置 maxclients 0,表示不作限制。当客户端连接数到达限制时,Redis会关闭新
的连接并向客户端返回max number of clients reached错误信息
maxmemory <bytes>
#指定Redis最大内存限制,Redis在启动时会把数据加载到内存中,达到最大内存后,Redis会先尝试清除已到
期或即将到期的Key,当此方法处理 后,仍然到达最大内存设置,将无法再进行写入操作,但仍然可以进行读取
操作。Redis新的vm机制,会把Key存放内存,Value会存放在swap区
appendonly no
指定是否在每次更新操作后进行日志记录,Redis在默认情况下是异步的把数据写入磁盘,如果不开启,可能会
在断电时导致一段时间内的数据丢失。因为 redis本身同步数据文件是按上面save条件来同步的,所以有的
数据会在一段时间内只存在于内存中。默认为no
appendfilename "appendonly.aof"
#指定更新日志文件名,默认为appendonly.aof
appendfsync everysec
#指定更新日志条件,共有3个可选值:
no:表示等操作系统进行数据缓存同步到磁盘(快)
always:表示每次更新操作后手动调用fsync()将数据写到磁盘(慢,安全)
everysec:表示每秒同步一次(折衷,默认值)
activerehashing yes
指定是否激活重置哈希,默认为开启
include /path/to/local.conf
#指定包含其它的配置文件,可以在同一主机上多个Redis实例之间使用同一份配置文件,而同时各个实例又拥
有自己的特定配置文件。