1redis安装配置启动关闭及应用场景

redis的应用场景:

1缓存session会话

缓存用户信息,找不到再去mysql查,查到后回写到readi

2排行榜-列表&有序集合

热度排名排行榜

发布时间排行榜

3计数器应用-天然支持计数器

帖子浏览数

视频播放浏览数

商品浏览数

4社交网络-集合

踩/赞,粉丝,共同好友/喜好,推送,打标签

消息队列系统-发布订阅

配合elk实现日志收集

redis的安装部署:

1添加域名解析(虚拟机需要此步)

[root@db01 ~]# vim /etc/hosts
[root@db01 ~]# tail -3 /etc/hosts
10.0.0.201 db01
10.0.0.202 db02
10.0.0.203 db03

2为了让三个linux虚拟机之间使用ssh不需要用户名和密码登录,生成公匙

[root@db01 redis]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:mN523hN2s9ytF+N45HMTP6AFnh/BN3TalP7yCBs5ZRk root@db01
The key's randomart image is:
+---[RSA 2048]----+
| .|
| Eoo|
| . o*.|
| o . o=+.|
| o S . o+o o|
| . . oB++=.|
| . o ..+OBB*|
| . o ooo+BB|
| . ..oo=|
+----[SHA256]-----+

[root@db01 ~]# cd ~/.ssh #查看ssh文件
[root@db01 .ssh]# ls
authorized_keys id_rsa id_rsa.pub known_hosts
[root@db01 .ssh]#

3把本地的ssh公钥文件安装到远程主机对应的账户下

[root@db01 redis]# ssh-copy-id db02
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'db02 (10.0.0.202)' can't be established.
ECDSA key fingerprint is SHA256:LmXjBa71iSVZZLGnF+YyIR7Wj92nqPwktskTKwBJvqw.
ECDSA key fingerprint is MD5:54:85:d5:c4:fb:4a:79:94:d4:2d:86:da:9a:1f:db:d1.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@db02's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'db02'"
and check to make sure that only the key(s) you wanted were added.

[root@db01 redis]# ssh-copy-id db03
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'db03 (10.0.0.203)' can't be established.
ECDSA key fingerprint is SHA256:LmXjBa71iSVZZLGnF+YyIR7Wj92nqPwktskTKwBJvqw.
ECDSA key fingerprint is MD5:54:85:d5:c4:fb:4a:79:94:d4:2d:86:da:9a:1f:db:d1.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@db03's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'db03'"
and check to make sure that only the key(s) you wanted were added.

[root@db01 redis]#

 4创建目录下载软件并解压创建软连

[root@db01 ~]# mkdir -p /data/soft
[root@db01 ~]# mkdir -p /data/redis_cluster/redis_6379
[root@db01 ~]# mkdir -p /opt/redis_cluster/redis_6379/{conf,pid,logs}
[root@db01 ~]# cd /data/soft
[root@db01 soft]# wget http://download.redis.io/releases/redis-3.2.9.tar.gz
[root@db01 soft]# ls
redis-3.2.9.tar.gz
[root@db01 soft]# tar -zxf redis-3.2.9.tar.gz -C /opt/redis_cluster/ #解压到指定目录
[root@db01 soft]# ln -s /opt/redis_cluster/redis-3.2.9/ /opt/redis_cluster/redis #创建软连接
[root@db01 soft]# cd /opt/redis_cluster/redis #进入redis目录 

5接下来就是编译了,通过make命令,如果编译的时候报gcc命令找不到的话,可以通过下面的命令安装gcc命令,gcc是c的编译命令

[root@db01 src]# yum install gcc-c++
[root@db01 src]# cd /opt/redis_cluster/redis
[root@db01 redis]# make #编译安装,可以看到ll -tr src目录中生成了几个redis文件
cd src && make all
make[1]: 进入目录“/opt/redis_cluster/redis-3.2.9/src”

Hint: It's a good idea to run 'make test' ;)

make[1]: 离开目录“/opt/redis_cluster/redis-3.2.9/src”
[root@db01 redis]# make install #这一步相当于在这个目录中/usr/local/bin创建了ll -tr src目中中生成几个redis文件的软连接
cd src && make install
make[1]: 进入目录“/opt/redis_cluster/redis-3.2.9/src”

Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: 离开目录“/opt/redis_cluster/redis-3.2.9/src”

[root@db01 redis]# cd ~
[root@db01 ~]# redis-server #启动redis

6新打开一个交互窗口,查看redis是否启动,并关闭redis:

[root@db01 ~]# ps -ef|grep redis
root       2063    946  0 00:20 pts/0    00:00:00 redis-server *:6379
root       2088   2070  0 00:22 pts/1    00:00:00 grep --color=auto redis
[root@db01 ~]# redis-cli shutdown #关闭redis
[root@db01 ~]# ps -ef|grep redis
root       2091   2070  0 00:22 pts/1    00:00:00 grep --color=auto redis

7编写配置文件:

[root@db01 src]# vim /opt/redis_cluster/redis_6379/conf/redis_6379.conf

### 以守护进程模式启动
daemonize yes
### 绑定的主机地址
bind 10.0.0.51 127.0.0.1
### 监听端口
port 6379
### pid 文件和 log 文件的保存地址
pidfile /opt/redis_cluster/redis_6379/pid/redis_6379.pid
logfile /opt/redis_cluster/redis_6379/logs/redis_6379.log
### 设置数据库的数量,默认数据库为 0
databases 16
### 指定本地持久化文件的文件名,默认是 dump.rdb
dbfilename redis_6379.rdb
### 本地数据库的目录
dir /data/redis_cluster/redis_6379

8使用自定义的配置文件启动redis:

[root@db01 utils]# redis-server /opt/redis_cluster/redis_6379/conf/redis_6379.conf
[root@db01 utils]# ps -ef |grep redis
root       2225      1  4 01:36 ?        00:00:00 redis-server 10.0.0.201:6379
root       2229   2070  0 01:36 pts/1    00:00:00 grep --color=auto redis
[root@db01 utils]# 

 备注:

redis自带了一个交互的官方脚本工具,可以设置默认的配置文件(可供借鉴学习):

[root@db01 ~]# cd /opt/redis_cluster/redis/utils
[root@db01 utils]# ls
build-static-symbols.tcl  generate-command-help.rb  lru                    redis-sha1.rb
cluster_fail_time.tcl     hashtable                 redis-copy.rb          releasetools
corrupt_rdb.c             hyperloglog               redis_init_script      speed-regression.tcl
create-cluster            install_server.sh         redis_init_script.tpl  whatisdoing.sh
[root@db01 utils]#  bash 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/bin/redis-server] 
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
[root@db01 utils]# 

 还可以通过下列方法查看默认的配置文件内容:

[root@db01 utils]# grep '^[a-Z]' /etc/redis/6379.conf
bind 127.0.0.1
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis_6379.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis/6379
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
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
[root@db01 utils]# 

 

posted @ 2021-03-31 17:45  linuxTang  阅读(80)  评论(0)    收藏  举报