Redis教程

Redis学习笔记--Redis安装

本文主要是介绍Redis学习笔记--Redis安装,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

下载安装

地址:
https://redis.io/download
在该页面也给出了安装方法:
Download, extract and compile Redis with:

$ wget https://download.redis.io/releases/redis-6.2.6.tar.gz
$ tar xzf redis-6.2.6.tar.gz
$ cd redis-6.2.6
$ make

The binaries that are now compiled are available in the src directory. Run Redis with:

$ src/redis-server

You can interact with Redis using the built-in client:

$ src/redis-cli
redis> set foo bar
OK
redis> get foo
“bar”

在安装的时候出现了错误信息:

[root@worker-02 ~]# make
……
server.c:5209:176: error: ‘struct redisServer’ has no member named ‘maxmemory’
serverLog(LL_WARNING,“WARNING: You specified a maxmemory value that is less than 1MB (current value is %llu bytes). Are you sure this is what you really want?”, server.maxmemory);
^
server.c:5212:31: error: ‘struct redisServer’ has no member named ‘server_cpulist’
redisSetCpuAffinity(server.server_cpulist);
^
server.c: In function ‘hasActiveChildProcess’:
server.c:1480:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
server.c: In function ‘allPersistenceDisabled’:
server.c:1486:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
server.c: In function ‘writeCommandsDeniedByDiskError’:
server.c:3826:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
server.c: In function ‘iAmMaster’:
server.c:5000:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
make[1]: *** [server.o] Error 1
make[1]: Leaving directory `/root/redis-6.0.6/src’
make: *** [all] Error 2

为gcc版本过低,查看gcc的版本。

#查看gcc的版本是否在 5.3以上,centos7默认是4.8.5.我这里的就是4.8.5
gcc -v
[root@worker-02 ~]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: …/configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)

解决方案:

#升级到 5.3及以上版本

yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash

注意:scl命令启用只是临时的,退出xshell或者重启就会恢复到原来的gcc版本。
如果要长期生效的话,执行如下:

echo “source /opt/rh/devtoolset-9/enable” >>/etc/profile
[root@worker-02 ~]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-9/root/usr/libexec/gcc/x86_64-redhat-linux/9/lto-wrapper
Target: x86_64-redhat-linux
Configured with: …/configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-9/root/usr --mandir=/opt/rh/devtoolset-9/root/usr/share/man --infodir=/opt/rh/devtoolset-9/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-9.3.1-20200408/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 9.3.1 20200408 (Red Hat 9.3.1-2) (GCC)

控制启动端口

[root@worker-02 src]# ./redis-server --port 6388

在这里插入图片描述

相应的,从cli命令接入Redis也需要加上端口

[root@worker-02 redis-6.0.6]# src/redis-cli -p 6388
127.0.0.1:6388>

利用conf文件启动

在安装目录中有一个redis.conf文件,修改该文件

  1. 去除解释和空白信息并写入文件
[root@worker-02 redis-6.0.6]# cat redis.conf |grep -v "#"|grep -v "^$" >redis-1110.conf 
[root@worker-02 redis-6.0.6]# cat redis-1110.conf 
bind 127.0.0.1
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
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
aof-use-rdb-preamble 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
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes

只需要保留几条需要的配置:

port 6379
#执行端口
daemonize *yes*
#是否作为守护进程后台运行
logfile "1110.log"
#log文件名字
dir /root/redis-6.0.6/data
#工作目录

后台启动:

[root@worker-02 redis-6.0.6]# src/redis-server redis-1110.conf
[root@worker-02 redis-6.0.6]# ps -ef |grep redis
root 8616 1 0 22:10 ? 00:00:00 src/redis-server *:6379
root 8640 6487 0 22:11 pts/0 00:00:00 grep --color=auto redis

查看日志文件:

[root@worker-02 data]# cat 1110.log
8615:C 09 Nov 2021 22:10:51.386 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8615:C 09 Nov 2021 22:10:51.386 # Redis version=6.0.6, bits=64, commit=00000000, modified=0, pid=8615, just started
8615:C 09 Nov 2021 22:10:51.386 # Configuration loaded
8616:M 09 Nov 2021 22:10:51.388 * Increased maximum number of open files to 10032 (it was originally set to 1024).
8616:M 09 Nov 2021 22:10:51.389 * Running mode=standalone, port=6379.
8616:M 09 Nov 2021 22:10:51.389 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
8616:M 09 Nov 2021 22:10:51.389 # Server initialized
8616:M 09 Nov 2021 22:10:51.389 # 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.
8616:M 09 Nov 2021 22:10:51.389 # 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.
8616:M 09 Nov 2021 22:10:51.389 * Ready to accept connections

启动多Redis进程

使用多个配置文件,指定不同端口和log文件

[root@worker-02 redis-6.0.6]# src/redis-server ./conf/redis-6379.conf
[root@worker-02 redis-6.0.6]# src/redis-server ./conf/redis-6380.conf
[root@worker-02 redis-6.0.6]# src/redis-server ./conf/redis-6381.conf
[root@worker-02 redis-6.0.6]# ps -ef |grep redis
root 9080 1 0 22:49 ? 00:00:00 src/redis-server *:6379
root 9109 1 0 22:50 ? 00:00:00 src/redis-server *:6380
root 9115 1 1 22:50 ? 00:00:00 src/redis-server *:6381
root 9121 6487 0 22:50 pts/0 00:00:00 grep --color=auto redis

从网络登录Redis

ItemIP Addr
Redis Server192.168.21.12
Redis Client192.168.21.11
  1. 在Client上登录

[root@worker-01 src]# ./redis-cli -h 192.168.21.12
Could not connect to Redis at 192.168.21.12:6379: No route to host
not connected> exit

  1. 测试IP层:

[root@worker-01 src]# ping 192.168.21.12
PING 192.168.21.12 (192.168.21.12) 56(84) bytes of data.
64 bytes from 192.168.21.12: icmp_seq=1 ttl=64 time=0.451 ms
64 bytes from 192.168.21.12: icmp_seq=2 ttl=64 time=1.58 ms

  1. 在Redis Server上关闭Iptables

[root@worker-02 data]# iptables -F

  1. Redis Client重新登录

[root@worker-01 src]# ./redis-cli -h 192.168.21.12
192.168.21.12:6379>
成功!

  1. 测试

192.168.21.12:6379> ping
(error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command ‘CONFIG SET protected-mode no’ from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to ‘no’, and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

  1. 按照提示在conf文件中加入protected-mode no并重启redis server

[root@worker-02 data]# ps -ef |grep redis
root 9080 1 0 22:49 ? 00:00:01 src/redis-server *:6379
root 9109 1 0 22:50 ? 00:00:01 src/redis-server *:6380
root 9115 1 0 22:50 ? 00:00:01 src/redis-server *:6381
root 9330 6487 0 23:09 pts/0 00:00:00 grep --color=auto redis
[root@worker-02 data]# kill -s 9 9080

[root@worker-02 data]# vim …/conf/redis-6379.conf

port 6379
daemonize yes
dir /root/redis-6.0.6/data
logfile “6379.log”
protected-mode no

[root@worker-02 data]# …/src/redis-server …/conf/redis-6379.conf

  1. Client重新测试

[root@worker-01 src]# ./redis-cli -h 192.168.21.12
192.168.21.12:6379> ping
PONG
192.168.21.12:6379> lpush list01 a1 b2 c3 e5
(integer) 4
192.168.21.12:6379> lrange list01 0 -1

  1. “e5”
  2. “c3”
  3. “b2”
  4. “a1”
    成功!
这篇关于Redis学习笔记--Redis安装的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!