hithyc 2019-12-24
[ ~]# yum install gcc gcc-c++ make -y //安装环境组件 [ ~]# mount.cifs //192.168.100.3/LNMP-C7 /mnt/ //挂载软件包 Password for //192.168.100.8/LNMP-C7: [ ~]# cd /mnt/ [ mnt]# tar zxvf redis-5.0.7.tar.gz -C /opt/ //解压 [ mnt]# cd /opt/redis-5.0.7/ [ redis-5.0.7]# make //编译 [ redis-5.0.7]# make PREFIX=/usr/local/redis/ install //安装
[ redis-5.0.7]# cd utils/ [ utils]# ./install_server.sh //执行脚本进行配置 Welcome to the redis service installer This script will help you easily set up a running redis server Please select the redis port for this instance: [6379] //默认端口号 Selecting default: 6379 Please select the redis config file name [/etc/redis/6379.conf] //配置文件存放位置 Selected default - /etc/redis/6379.conf Please select the redis log file name [/var/log/redis_6379.log] //日志文件存放位置 Selected default - /var/log/redis_6379.log Please select the data directory for this instance [/var/lib/redis/6379] //数据文件存放位置 Selected default - /var/lib/redis/6379 Please select the redis executable path [] /usr/local/redis/bin/redis-server //可执行文件路径 [ utils]# ln -s /usr/local/redis/bin/* /usr/local/bin/ //制作链接文件便于系统识别 [ utils]# netstat -ntap | grep 6379 //查看端口是否开启 tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 44510/redis-server [ utils]# /etc/init.d/redis_6379 stop //关闭redis命令 Stopping ... Redis stopped [ utils]# /etc/init.d/redis_6379 start //开启redis命令 Starting Redis server... [ utils]# vim /etc/redis/6379.conf //修改配置文件 bind 127.0.0.1 192.168.144.128 //配置监听地址 [ utils]# /etc/init.d/redis_6379 restart //重启redis服务 Stopping ... Redis stopped Starting Redis server...
[ utils]# /usr/local/redis/bin/redis-cli 127.0.0.1:6379>
[ utils]# redis-cli -h 192.168.144.128 -p 6379 192.168.144.128:6379>
获取命令帮助
192.168.144.128:6379> help set ##help帮助 SET key value [expiration EX seconds|PX milliseconds] [NX|XX] summary: Set the string value of a key since: 1.0.0 group: string
192.168.144.128:6379> set teacher zhangsan OK 192.168.144.128:6379> set tea red OK
get:获取数据
192.168.144.128:6379> get tea ##查看键的值 "red"
127.0.0.1:6379> keys * //查看当前数据库中所有的键 127.0.0.1:6379> keys V* //查看当前数据库中以v开头的键 127.0.0.1:6379> keys v? //查看当前数据库中以v开头后面包含任意一个字符的键 127.0.0.1:6379> keys v?? //查看当前数据库中以v开头后面包含任意二个字符的键
192.168.144.128:6379> KEYS * ##查看所有的键 1) "teacher" 2) "tea" 192.168.144.128:6379> keys t?? ##查看键是t开头后面是两个字符的 1) "tea"
192.168.144.128:6379> EXISTS tea (integer) 1 //1是存在 192.168.144.128:6379> EXISTS teas (integer) 0 //0是不存在
192.168.144.128:6379> del teacher ##删除键 (integer) 1 192.168.144.128:6379> KEYS * 1) "tea"
192.168.144.128:6379> type tea ##查看键的类型 string
192.168.144.128:6379> rename tea t1 ##给键重命名 OK 192.168.144.128:6379> keys * 1) "t1" 192.168.144.128:6379> get t1 "red"
192.168.144.128:6379> dbsize (integer) 1
[ utils]# redis-benchmark -h 192.168.144.128 -p 6379 -c 100 -n 100000 //并发100,100000个请求 ====== SET ====== 100000 requests completed in 1.14 seconds //请求花费的时间 100 parallel clients 3 bytes payload keep alive: 1 84.66% <= 1 milliseconds 98.48% <= 2 milliseconds 99.69% <= 3 milliseconds 99.90% <= 18 milliseconds 100.00% <= 18 milliseconds 87642.41 requests per second ====== GET ====== 100000 requests completed in 1.144 seconds 100 parallel clients 3 bytes payload keep alive: 1 [ utils]# redis-benchmark -h 192.168.144.128 -p 6379 -q -d 100 //以字节形式指定set/get值的数据大小 SET: 90497.73 requests per second GET: 90991.81 requests per second
192.168.144.128:6379> select 10 //进入第11个库 OK 192.168.144.128:6379[10]> keys * (empty list or set) 192.168.144.128:6379[10]> select 0 //进入第1个库 OK
192.168.144.128:6379> move t1 10 //移动键值对到第11个库 (integer) 1 192.168.144.128:6379> select 10 //进入第11个库 OK 192.168.144.128:6379[10]> keys * //查看键 1) "t1" 192.168.144.128:6379[10]> get t1 "red"
192.168.144.128:6379[10]> flushdb //清除库中数据 OK 192.168.144.128:6379[10]> keys * //查看所有键 (empty list or set)
AOF方式:将执行的写命令写到文件的末尾,以日志的方式来记录数据的变化
配置文件选项
[ utils]# vim /etc/redis/6379.conf save 900 1 //900秒之内至少一次写操作 save 300 10 //300秒之内至少发生10次写操作 save 60 10000 //60秒之内发生至少10000次写操作;只要满足其一都会触发快照操作,注释所有的save项表示关闭RDB dbfilename dump.rdb //备份文件名称 dir /var/lib/redis/6379 //备份文件保存目录 rdbcompression yes //开启压缩
AOF持久化配置
[ utils]# vim /etc/redis/6379.conf appendonly yes //开启AOF持久化 appendfilename "appendonly.aof" //AOF文件名称 # appendfsync always //always:同步持久化,每次发生数据变化会立刻写入磁盘 appendfsync everysec //everysec:默认推荐,每秒异步记录次(默认值) # appendfsync no //no:不同步,交给操作系统决定如何同步 aof-load-truncated yes //忽略最后一条可能存在问题的指令
[ utils]# vim /etc/redis/6379.conf no-appendfsync-on-rewrite no //在日志进行BGREWRITEAOF时, 如果设置为yes表示新写操作不进行同步fsync,只暂存在缓冲区里,避免造成磁盘I0操作冲突,等重写完成后在写入。redis中默认为no auto-aof-rewrite-percentage 100 //当前AOF文件大小是上次日志重写时AOF文件大小两倍时,发生BGREWRITEAOF操作 auto-aof-rewrite-min-size 64mb //当前AOF文件执行BGREWRITEAOF命令的最小值,避免刚开始启动Reids时由于文件尺寸较小导致频繁的BGREWRITEAOF
[ utils]# /usr/local/redis/bin/redis-cli 127.0.0.1:6379> info memory
跟踪内存碎片率对理解redis实例的资源性能是非常重要的
避免内存交换