redis安装部署(单机部署)
redis单机部署
//下载redis安装包 wget https://download.redis.io/releases/redis-6.2.2.tar.gz //解压到/opt/ [root@node1 opt]# tar zxvf redis-6.2.2.tar.gz -C /opt/ root@node1 opt]# ls anaconda-ks.cfg data redis-6.2.2 //下载编译工具 [root@node1 redis-6.2.2]# yum install -y gcc-c++ make //用make安装 [root@node1 redis-6.2.2]# make [root@node1 redis-6.2.2]# make ... ... ... Hint: It's a good idea to run 'make test' ;) make[1]: Leaving directory '/opt/redis-6.2.2/src' [root@node1 redis-6.2.2]# make cd src && make all make[1]: Entering directory '/opt/redis-6.2.2/src' CC Makefile.dep Hint: It's a good idea to run 'make test' ;) make[1]: Leaving directory '/opt/redis-6.2.2/src' [root@node1 redis-6.2.2]# make install cd src && make install make[1]: Entering directory '/opt/redis-6.2.2/src' Hint: It's a good idea to run 'make test' ;) INSTALL redis-server INSTALL redis-benchmark INSTALL redis-cli make[1]: Leaving directory '/opt/redis-6.2.2/src'
//redis的默认安装路径 [root@node1 bin]# ls redis-benchmark redis-check-rdb redis-sentinel zabbix_get zabbix_sender redis-check-aof redis-cli redis-server zabbix_js [root@node1 bin]# pwd /usr/local/bin //redis配置文件 [root@node1 bin]# mkdir redis_cp_conf [root@node1 bin]# ls redis-benchmark redis-check-rdb redis_cp_conf redis-server zabbix_js redis-check-aof redis-cli redis-sentinel zabbix_get zabbix_sender [root@node1 bin]# cp /opt/redis-6.2.2/redis.conf redis_cp_conf/ [root@node1 bin]# ls redis-benchmark redis-check-rdb redis_cp_conf redis-server zabbix_js redis-check-aof redis-cli redis-sentinel zabbix_get zabbix_sender [root@node1 bin]# cd redis_cp_conf/ [root@node1 redis_cp_conf]# ls redis.conf [root@node1 redis_cp_conf]# vim redis.conf ..... tcp-keepalive 300 ################################# TLS/SSL ##################################### daemonize yes //后台可以运行 ..... ..... //启动redis服务 [root@node1 bin]# redis-server redis_cp_conf/redis.conf [root@node1 bin]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 0.0.0.0:9000 0.0.0.0:* LISTEN 0 128 127.0.0.1:6379 0.0.0.0:*
//6379端口已经起来
LISTEN 0 128 [::]:22 [::]:* LISTEN 0 80 *:3306 *:* LISTEN 0 128 [::1]:6379 [::]:*
//使用redis客户端连接 [root@node1 bin]# redis-cli -p 6379 127.0.0.1:6379> ping PONG 127.0.0.1:6379> set name mei OK 127.0.0.1:6379> get name "mei" 127.0.0.1:6379> keys * //查看所有的key 1) "name" 127.0.0.1:6379> //关闭redis服务 127.0.0.1:6379> SHUTDOWN not connected> EXIT [root@node1 bin]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 0.0.0.0:9000 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 80 *:3306 *:*
测试性能
redis-benchmark 是一个压力测试工具
它是一个官方自带的性能测试工具
redis 性能测试工具可选参数如下所示:
序号 | 选项 | 描述 | 默认值 |
---|---|---|---|
1 | -h | 指定服务器主机名 | 127.0.0.1 |
2 | -p | 指定服务器端口 | 6379 |
3 | -s | 指定服务器 socket | |
4 | -c | 指定并发连接数 | 50 |
5 | -n | 指定请求数 | 10000 |
6 | -d | 以字节的形式指定 SET/GET 值的数据大小 | 2 |
7 | -k | 1=keep alive 0=reconnect | 1 |
8 | -r | SET/GET/INCR 使用随机 key, SADD 使用随机值 | |
9 | -P | 通过管道传输 <numreq> 请求 | 1 |
10 | -q | 强制退出 redis。仅显示 query/sec 值 | |
11 | --csv | 以 CSV 格式输出 | |
12 | -l | 生成循环,永久执行测试 | |
13 | -t | 仅运行以逗号分隔的测试命令列表。 | |
14 | -I | Idle 模式。仅打开 N 个 idle 连接并等待。 |
实例:
//测试:100个并发连接 100000个请求 [root@node1 bin]# redis-benchmark -h localhost -p 6379 -c 100 -n 100000 ====== SET ====== 100000 requests completed in 1.61 seconds //对10万个请求进行写入测试 100 parallel clients //100个并发客户端 3 bytes payload //每次写入三个字节 keep alive: 1 //只有一台服务器来处理这些请求,单机性能 host configuration "save": 3600 1 300 100 60 10000 host configuration "appendonly": no multi-thread: no
redis基础知识
redis默认有16个数据库
[root@node1 redis_cp_conf]# vim redis.conf ... ... # 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 //数据库有16个 # By default Redis shows an ASCII art logo only when started to ... ...
默认使用的是第0个
可以用select进行切换
[root@node1 bin]# redis-cli -p 6379 127.0.0.1:6379> select 3 //切换到第四个数据库 OK 127.0.0.1:6379[3]> dbsize //查看db大小 (integer) 0 127.0.0.1:6379[3]> set name mei OK 127.0.0.1:6379[3]> get name "mei" 127.0.0.1:6379[3]> dbsize (integer) 1 127.0.0.1:6379[3]> keys * //查看所有key 1) "name" 127.0.0.1:6379[3]> flushdb //清除当前数据库 OK 127.0.0.1:6379[3]> keys * (empty array) 127.0.0.1:6379> keys * 1) "mylist" 2) "counter:__rand_int__" 3) "name" 4) "key:__rand_int__" 5) "myhash" 127.0.0.1:6379> FLUSHDB OK 127.0.0.1:6379> keys * (empty array) 127.0.0.1:6379> set name mei OK 127.0.0.1:6379> get name "mei" 127.0.0.1:6379> keys * 1) "name" 127.0.0.1:6379> select 3 OK 127.0.0.1:6379[3]> keys * (empty array) 127.0.0.1:6379[3]> FLUSHALL //清空所有数据库 OK 127.0.0.1:6379[3]> SELECT 0 OK 127.0.0.1:6379> keys * (empty array) 127.0.0.1:6379>
redis是单线程的
redis是基于内存操作,cpu不是redis性能瓶颈,redis的瓶颈是根据机器的内存和网络带宽,既然可以使用单线程来实现,就使用单线程了;所以就有了单线程。
‘