yum安装redisrpm
一、安装
#下载fedora的epel仓库#yum -y install epel-release1.安装redis数据库yum -y install redis
二、启动
2.设置启动与开机启动sudo systemctl start redis && sudo systemctl enable redis#或 sudo service redis start && chkconfig redis on#启动sudo systemctl start redis#或 service redis start#重启sudo systemctl restart redis#或 service redis restart#开机启动sudo systemctl enable redis#或 sudo service redis star#关闭开机启动sudo systemctl disable redis#或 sudo chkconfig redis off
三、修改redis的配置 redis.conf
- 输入命令 vim /etc/redis.conf 进入编辑模式 ggdG 全删 ggyG 全部复制 ggvG ggVG 高亮显示
#绑定IPbind 0.0.0.0#绑定端口, 默认是6379, 需要「安全组配置」开放端口port 6379#后台启动daemonize yes#保护模式protected-mode no#指定持久化方式appendonly yes#设置密码requirepass 123456#指定数据存放路径rdb存放的路径dir /var/lib/redis# ./redis-server /path/to/redis.conf################################## INCLUDES #################################### include /path/to/local.conf# include /path/to/other.conf################################## MODULES ###################################### loadmodule /path/to/my_module.so# loadmodule /path/to/other_module.so################################## NETWORK ###################################### bind 192.168.1.100 10.0.0.1# bind 127.0.0.1 ::1tcp-backlog 511# unixsocket /tmp/redis.sock# unixsocketperm 700timeout 0tcp-keepalive 300################################# GENERAL #####################################supervised nopidfile /var/run/redis_6379.pidloglevel noticelogfile /var/log/redis/redis.logdatabases 16always-show-logo yes################################ SNAPSHOTTING ################################# save <seconds> <changes>save 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdb################################# REPLICATION ################################## replicaof <masterip> <masterport># masterauth <master-password>replica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5# repl-ping-replica-period 10# repl-timeout 60repl-disable-tcp-nodelay no# repl-backlog-size 1mb# repl-backlog-ttl 3600replica-priority 100# min-replicas-to-write 3# min-replicas-max-lag 10# replica-announce-ip 5.5.5.5# replica-announce-port 1234################################## SECURITY #################################### rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52################################### CLIENTS ##################################### maxclients 10000############################## MEMORY MANAGEMENT ################################# maxmemory <bytes># maxmemory-policy noeviction# maxmemory-samples 5# replica-ignore-maxmemory yes############################# LAZY FREEING ####################################lazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush no############################## APPEND ONLY MODE ###############################appendfilename "appendonly.aof"appendfsync everysec# appendfsync nono-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yes################################ LUA SCRIPTING ###############################lua-time-limit 5000################################ REDIS CLUSTER ################################ cluster-enabled yes# cluster-config-file nodes-6379.conf# cluster-node-timeout 15000# cluster-replica-validity-factor 10# cluster-migration-barrier 1# cluster-require-full-coverage yes# cluster-replica-no-failover no########################## CLUSTER DOCKER/NAT support ######################### cluster-announce-ip 10.1.1.5# cluster-announce-port 6379# cluster-announce-bus-port 6380################################## SLOW LOG ###################################slowlog-log-slower-than 10000slowlog-max-len 128################################ LATENCY MONITOR ##############################latency-monitor-threshold 0############################# EVENT NOTIFICATION ##############################notify-keyspace-events ""############################### ADVANCED CONFIG ###############################hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60# client-query-buffer-limit 1gb# proto-max-bulk-len 512mbhz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yes# lfu-log-factor 10# lfu-decay-time 1########################### ACTIVE DEFRAGMENTATION ######################## activedefrag yes# active-defrag-ignore-bytes 100mb# active-defrag-threshold-lower 10# active-defrag-threshold-upper 100# active-defrag-cycle-min 5# active-defrag-cycle-max 75# active-defrag-max-scan-fields 1000
非必须步骤:选择使用配置文件启动 redis-server /etc/redis.conf
四、防火墙开放相应端口
4. 防火墙开放相应端口#第一种(推荐选择)#netstat -lnp|grep 6379 #执行开放端口号时候可以看看那些端口在运行#netstat -anp #哪些端口被打开#firewall-cmd --state #查看防火墙状态systemctl restart firewalldfirewall-cmd --zone=public --add-port=80/tcp --permanentfirewall-cmd --zone=public --add-port=443/tcp --permanentfirewall-cmd --zone=public --add-port=6379/tcp --permanentsystemctl restart firewalld #注意:重启防火墙,80端口是必须要开放的sudo systemctl restart redis #重启redis服务#第二种/sbin/iptables -I OUTPUT -p tcp --dport 6380 -j ACCEPT #关闭启6379/sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT #开启6379/etc/rc.d/init.d/iptables save #保存service iptables save #centos7下执行sudo systemctl restart redis #重启redis服务
注意:如果使用阿里云服务器,则需要在安全组规则配置对应的端口
快速部署redis
yum -y install redissudo systemctl start redis && sudo systemctl enable redissystemctl restart firewalldfirewall-cmd --zone=public --add-port=80/tcp --permanentfirewall-cmd --zone=public --add-port=443/tcp --permanentfirewall-cmd --zone=public --add-port=6379/tcp --permanentsystemctl restart firewalld #注意:重启防火墙,80端口是必须要开放的sudo systemctl restart redis #重启redis服务#------------------------------------------------------------------------------#编辑redis.conf文件 vim /etc/redis.confsudo systemctl restart redis #重启redis服务
查看
查看redis文件所在地whereis redis && which redis && find / -name redis查看redis状态systemctl status redis查看redis进程ps -ef |grep redis 或 ps aux | grep redis查看端口netstat -lnp|grep 6379#查看启动项的服务列表systemctl list-unit-files --type=service | grep enabled
关闭与卸载
第一步:查看 reids 是否在运行,如果在运行则先关闭方式1: kill -9 <pid进程>方式2: sudo pkill redis-server方式3: systemctl stop redis方式4: redis-cli shutdown第二步:移除redis#移除fedora的epel仓库#yum -y remove epel-release卸载redis数据库yum -y remove redis
安装包模式
一、安装之前
vim /etc/resolv.confnameserver 8.8.8.8 #google域名服务器nameserver 8.8.4.4 #google域名服务器因为redis是c语言编写的,所以要安装gcc,yum -y install gcc-c++
二、下载解压编译安装
选择在Linux下安装redis,现在采用虚拟机安装的centos7 进行安装的
#2.1 下载redis安装包wget http://download.redis.io/releases/redis-5.0.4.tar.gz#2.2 解压redis安装包并且进入redis目录tar -zxvf redis-5.0.4.tar.gz && cd redis-5.0.4#2.3 编码安装#make && make PREFIX=/usr/local/redis installmake PREFIX=/usr/local/redis install#拷贝redis.conf到安装目录#cp redis.conf /usr/local/redis
下载Redis时,如果出现报如下错误:wget: unable to resolve host address “download.redis.io” vim /etc/resolv.conf nameserver 8.8.8.8 #google域名服务器 nameserver 8.8.4.4 #google域名服务器
三、修改redis的配置 redis.conf
- 修改redis.conf配置文件ggdG 全删 ggyG 全部复制 ggvG ggVG 高亮显示 sudo vim /usr/local/redis/redis.conf
#绑定IPbind 0.0.0.0#绑定端口, 默认是6379, 需要「安全组配置」开放端口port 6379#后台启动daemonize yes#保护模式protected-mode no#指定持久化方式appendonly yes#设置密码requirepass 123456#指定数据存放路径rdb存放的路径dir /usr/local/redis# ./redis-server /path/to/redis.conf################################## INCLUDES #################################### include /path/to/local.conf# include /path/to/other.conf################################## MODULES ###################################### loadmodule /path/to/my_module.so# loadmodule /path/to/other_module.so################################## NETWORK ###################################### bind 192.168.1.100 10.0.0.1# bind 127.0.0.1 ::1tcp-backlog 511# unixsocket /tmp/redis.sock# unixsocketperm 700timeout 0tcp-keepalive 300################################# GENERAL #####################################supervised nopidfile /var/run/redis_6379.pidloglevel noticelogfile /usr/local/redis/redis.logdatabases 16always-show-logo yes################################ SNAPSHOTTING ################################# save <seconds> <changes>save 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdb################################# REPLICATION ################################## replicaof <masterip> <masterport># masterauth <master-password>replica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5# repl-ping-replica-period 10# repl-timeout 60repl-disable-tcp-nodelay no# repl-backlog-size 1mb# repl-backlog-ttl 3600replica-priority 100# min-replicas-to-write 3# min-replicas-max-lag 10# replica-announce-ip 5.5.5.5# replica-announce-port 1234################################## SECURITY #################################### rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52################################### CLIENTS ##################################### maxclients 10000############################## MEMORY MANAGEMENT ################################# maxmemory <bytes># maxmemory-policy noeviction# maxmemory-samples 5# replica-ignore-maxmemory yes############################# LAZY FREEING ####################################lazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush no############################## APPEND ONLY MODE ###############################appendfilename "appendonly.aof"appendfsync everysec# appendfsync nono-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yes################################ LUA SCRIPTING ###############################lua-time-limit 5000################################ REDIS CLUSTER ################################ cluster-enabled yes# cluster-config-file nodes-6379.conf# cluster-node-timeout 15000# cluster-replica-validity-factor 10# cluster-migration-barrier 1# cluster-require-full-coverage yes# cluster-replica-no-failover no########################## CLUSTER DOCKER/NAT support ######################### cluster-announce-ip 10.1.1.5# cluster-announce-port 6379# cluster-announce-bus-port 6380################################## SLOW LOG ###################################slowlog-log-slower-than 10000slowlog-max-len 128################################ LATENCY MONITOR ##############################latency-monitor-threshold 0############################# EVENT NOTIFICATION ##############################notify-keyspace-events ""############################### ADVANCED CONFIG ###############################hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60# client-query-buffer-limit 1gb# proto-max-bulk-len 512mbhz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yes# lfu-log-factor 10# lfu-decay-time 1########################### ACTIVE DEFRAGMENTATION ######################## activedefrag yes# active-defrag-ignore-bytes 100mb# active-defrag-threshold-lower 10# active-defrag-threshold-upper 100# active-defrag-cycle-min 5# active-defrag-cycle-max 75# active-defrag-max-scan-fields 1000
配置文件redis.conf 参数改动点
- 方法一 编辑 vim /usr/local/redis/redis.conf
- bind 0.0.0.0 绑定IP
- port 6379 绑定端口, 默认是6379, 需要「安全组配置」开放端口
- daemonize yes 后台启动
- appendonly yes 指定持久化方式
- protected-mode no 关闭保护模式
- requirepass 123456 设置密码
- dir /usr/local/redis/log 指定数据存放路径rdb存放的路径
- 方法二 修改redis服务器的参数配置
- 127.0.0.1:6379 > config set daemonize “no”
- 127.0.0.1:6379 > config set protected-mode “no”
- 127.0.0.1:6379 > config set requirepass 123456
四、设置全局变量
- 为redis设置全局变量vim ~/.bash_profile 或 vim /etc/profile
#设置暂时性全局变量export PATH=$PATH:/usr/local/redis/bin3.设置永久性全局变量方式1:使用>>输入进文件echo 'export PATH="$PATH:/usr/local/redis/bin"' >> ~/.bash_profilesource ~/.bash_profile方式2: vim ~/.bash_profile 或者 vim /etc/profile,添加下面代码PATH=$PATH:/usr/local/redis/bin
使用永久性全局变量别忘了使文件生效 source ~/.bash_profile 或 source /etc/profile
五、启动
5.启动redis服务/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf &
六、防火墙开放相应端口
6. 防火墙开放相应端口#第一种(推荐选择)netstat -lnp|grep 6379 #执行开放端口号时候可以看看那些端口在运行netstat -anp #哪些端口被打开systemctl restart firewalldfirewall-cmd --zone=public --add-port=80/tcp --permanentfirewall-cmd --zone=public --add-port=443/tcp --permanentfirewall-cmd --zone=public --add-port=6379/tcp --permanentsystemctl restart firewalld #注意:重启防火墙,80端口是必须要开放的#第二种/sbin/iptables -I OUTPUT -p tcp --dport 6380 -j ACCEPT #关闭启6379/sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT #开启6379/etc/rc.d/init.d/iptables save #保存service iptables save #centos7下执行sudo systemctl restart redis #重启redis服务
注意:如果使用阿里云服务器,则需要在安全组规则配置对应的端口
进入客户端
第一种方式: /usr/local/redis/bin/redis-cli --raw 处理中文乱码问题第二种方式: redis-cli -h 127.0.0.1 -p 6379 设置了全局变量后
查看
查看是否启动成功:ps aux | grep redis ps -ef | grep redis查看redis文件所在地whereis redis && which redis && find / -name redis查看虚拟机的ipifconfig
关闭与卸载
第一步:查看reids 是否在运行,如果在运行则先关闭方式1: kill -9 <pid进程>方式2: sudo pkill redis-server方式3: /usr/local/redis/bin/redis-cli shutdown第二步:删除相对于的目录rm -rf /usr/local/redis
apt-get安装redisdeb
安装与启动
sudo apt-get install redis-server #安装sudo redis-server /etc/redis/redis.conf & #启动
修改redic.conf配置文件
sudo vim /etc/redis/redic.conf ,之后参考上面
关闭与卸载
第一步:查看reids 是否在运行,如果在运行则先关闭方式1: redis-cli shutdown方式2: kill -9 <pid进程>方式3: sudo pkill redis-server第二步:移除redisyum remove redis
登录
redis-cli -h 119.23.72.172 [ -p 6379] [-a 密码]redis-cli -h 119.23.72.172 -p 6379 -a 123456
- -h ip地址
- -p 端口
- -a连接redis的密码
- 选择这种登录会出现:warning: Using a password with ‘-a’ or ‘-u’ option on the command line interface may not be safe.
- 如果使用了会提示,也可以使用命令 auth <密码>来进行验证
