Redis安装教程(各种坑)

网友投稿 723 2022-11-11

Redis安装教程(各种坑)

Redis安装教程(各种坑)

目录

​​Redis是什么?​​

​​安装Redis​​

Redis是什么?

相信大家对Redis一点都不陌生,在项目中,我们经常都会用到,多多少少对Redis都有一定的了解。我们先看看Redis官网(is an open source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker. Redis provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions, and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

大概意思是:

Redis是一个开源,运行于内存的数据存储技术。可用作于数据库、缓存、消息队列。Redis提供了字符串、哈希、集合、有序集合等数据类型。

Redis是Key-Value数据库,与其它数据库相比,Redis有一个特性是非常独特的:

Redis运行于内存,性能比较高,同时支持数据持久化,将内存中的数据保存到硬盘,需要的时候再重新加载到内存。

安装Redis

这次我们采用的是源码安装方式,先-Redis源码,这里我下的是6.0.9的版本,大家也可以按自己的需要-进行安装。

wget xzf redis-6.0.9.tar.gz

我们查看一下,是否成功解压。

[root@localhost ~]# lsanaconda-ks.cfg original-ks.cfg redis-6.0.9 redis-6.0.9.tar.gz

接着,我们进入解压后的目录,并编译。

cd redis-6.0.9make

可惜发生了报错:

[root@localhost ~]# cd redis-6.0.9[root@localhost redis-6.0.9]# makecd src && make allmake[1]: Entering directory `/root/redis-6.0.9/src' CC Makefile.depmake[1]: Leaving directory `/root/redis-6.0.9/src'make[1]: Entering directory `/root/redis-6.0.9/src'rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html Makefile.dep dict-benchmarkrm -f adlist.d quicklist.d ae.d anet.d dict.d server.d sds.d zmalloc.d lzf_c.d lzf_d.d pqsort.d zipmap.d sha1.d ziplist.d release.d networking.d util.d object.d db.d replication.d rdb.d t_string.d t_list.d t_set.d t_zset.d t_hash.d config.d aof.d pubsub.d multi.d debug.d sort.d intset.d syncio.d cluster.d crc16.d endianconv.d slowlog.d scripting.d bio.d rio.d rand.d memtest.d crcspeed.d crc64.d bitops.d sentinel.d notify.d setproctitle.d blocked.d hyperloglog.d latency.d sparkline.d redis-check-rdb.d redis-check-aof.d geo.d lazyfree.d module.d evict.d expire.d geohash.d geohash_helper.d childinfo.d defrag.d siphash.d rax.d t_stream.d listpack.d localtime.d lolwut.d lolwut5.d lolwut6.d acl.d gopher.d tracking.d connection.d tls.d sha256.d timeout.d setcpuaffinity.d anet.d adlist.d dict.d redis-cli.d zmalloc.d release.d ae.d crcspeed.d crc64.d siphash.d crc16.d ae.d anet.d redis-benchmark.d adlist.d dict.d zmalloc.d siphash.d(cd ../deps && make distclean)make[2]: Entering directory `/root/redis-6.0.9/deps'(cd hiredis && make clean) > /dev/null || true(cd linenoise && make clean) > /dev/null || true(cd lua && make clean) > /dev/null || true(cd jemalloc && [ -f Makefile ] && make distclean) > /dev/null || true(rm -f .make-*)make[2]: Leaving directory `/root/redis-6.0.9/deps'(rm -f .make-*)echo STD=-std=c11 -pedantic -DREDIS_STATIC='' >> .make-settingsecho WARN=-Wall -W -Wno-missing-field-initializers >> .make-settingsecho OPT=-O2 >> .make-settingsecho MALLOC=jemalloc >> .make-settingsecho BUILD_TLS= >> .make-settingsecho USE_SYSTEMD= >> .make-settingsecho CFLAGS= >> .make-settingsecho LDFLAGS= >> .make-settingsecho REDIS_CFLAGS= >> .make-settingsecho REDIS_LDFLAGS= >> .make-settingsecho PREV_FINAL_CFLAGS=-std=c11 -pedantic -DREDIS_STATIC='' -Wall -W -Wno-missing-field-initializers -O2 -g -ggdb -I../deps/hiredis -I../deps/linenoise -I../deps/lua/src -DUSE_JEMALLOC -I../deps/jemalloc/include >> .make-settingsecho PREV_FINAL_LDFLAGS= -g -ggdb -rdynamic >> .make-settings(cd ../deps && make hiredis linenoise lua jemalloc)make[2]: Entering directory `/root/redis-6.0.9/deps'(cd hiredis && make clean) > /dev/null || true(cd linenoise && make clean) > /dev/null || true(cd lua && make clean) > /dev/null || true(cd jemalloc && [ -f Makefile ] && make distclean) > /dev/null || true(rm -f .make-*)(echo "" > .make-cflags)(echo "" > .make-ldflags)MAKE hirediscd hiredis && make static make[3]: Entering directory `/root/redis-6.0.9/deps/hiredis'cc -std=c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -Wno-missing-field-initializers -g -ggdb net.cmake[3]: cc: Command not foundmake[3]: *** [net.o] Error 127make[3]: Leaving directory `/root/redis-6.0.9/deps/hiredis'make[2]: *** [hiredis] Error 2make[2]: Leaving directory `/root/redis-6.0.9/deps'make[1]: [persist-settings] Error 2 (ignored) CC adlist.o/bin/sh: cc: command not foundmake[1]: *** [adlist.o] Error 127make[1]: Leaving directory `/root/redis-6.0.9/src'make: *** [all] Error 2

截取其中一部分报错信息。

make[3]: cc: Command not found

初步判定,应该是“Linux系统没有安装gcc环境”。

那我们就先安装gcc。

yum install gcc

安装成功后,继续执行编译命令。

make

但还是报错了。

[root@localhost redis-6.0.9]# makecd src && make allmake[1]: Entering directory `/root/redis-6.0.9/src' CC Makefile.depmake[1]: Leaving directory `/root/redis-6.0.9/src'make[1]: Entering directory `/root/redis-6.0.9/src' CC adlist.oIn file included from adlist.c:34:0:zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory #include ^compilation terminated.make[1]: *** [adlist.o] Error 1make[1]: Leaving directory `/root/redis-6.0.9/src'make: *** [all] Error 2

只能继续找问题。

网上说是因为Redis默认使用jemalloc来做内存管理,因为jemalloc被证明解决fragmentation problems(内存碎片化问题)比libc更好。但是如果你又没有jemalloc而只有libc,当make出错时,你可以加这么一个参数即可。

make MALLOC=libc

很可惜,继续报错。

server.c:5297:29: error: ‘struct redisServer’ has no member named ‘pidfile’ if (background || server.pidfile) createPidFile(); ^server.c:5302:16: error: ‘struct redisServer’ has no member named ‘sentinel_mode’ if (!server.sentinel_mode) { ^server.c:5312:19: error: ‘struct redisServer’ has no member named ‘cluster_enabled’ if (server.cluster_enabled) { ^server.c:5320:19: error: ‘struct redisServer’ has no member named ‘ipfd_count’ if (server.ipfd_count > 0 || server.tlsfd_count > 0) ^server.c:5320:44: error: ‘struct redisServer’ has no member named ‘tlsfd_count’ if (server.ipfd_count > 0 || server.tlsfd_count > 0) ^server.c:5322:19: error: ‘struct redisServer’ has no member named ‘sofd’ if (server.sofd > 0) ^server.c:5323:94: error: ‘struct redisServer’ has no member named ‘unixsocket’ serverLog(LL_NOTICE,"The server is now ready to accept connections at %s", server.unixsocket); ^server.c:5324:19: error: ‘struct redisServer’ has no member named ‘supervised_mode’ if (server.supervised_mode == SUPERVISED_SYSTEMD) { ^server.c:5325:24: error: ‘struct redisServer’ has no member named ‘masterhost’ if (!server.masterhost) { ^server.c:5335:19: error: ‘struct redisServer’ has no member named ‘supervised_mode’ if (server.supervised_mode == SUPERVISED_SYSTEMD) { ^server.c:5342:15: error: ‘struct redisServer’ has no member named ‘maxmemory’ if (server.maxmemory > 0 && server.maxmemory < 1024*1024) { ^server.c:5342:39: error: ‘struct redisServer’ has no member named ‘maxmemory’ if (server.maxmemory > 0 && server.maxmemory < 1024*1024) { ^server.c:5343: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:5346:31: error: ‘struct redisServer’ has no member named ‘server_cpulist’ redisSetCpuAffinity(server.server_cpulist); ^server.c: In function ‘hasActiveChildProcess’:server.c:1478:1: warning: control reaches end of non-void function [-Wreturn-type] } ^server.c: In function ‘allPersistenceDisabled’:server.c:1484:1: warning: control reaches end of non-void function [-Wreturn-type] } ^server.c: In function ‘writeCommandsDeniedByDiskError’:server.c:3934:1: warning: control reaches end of non-void function [-Wreturn-type] } ^server.c: In function ‘iAmMaster’:server.c:5134:1: warning: control reaches end of non-void function [-Wreturn-type] } ^make[1]: *** [server.o] Error 1make[1]: Leaving directory `/root/redis-6.0.9/src'make: *** [all] Error 2

报错内容很长,只能截取其中一小部分。网上建议说安装jemalloc。但安装这个之前,需要先安装EPEL源。如果不先安装EPEL源,直接安装jemalloc,会报以下错误信息。

[root@localhost redis-6.0.9]# yum install jemallocLoaded plugins: fastestmirror, langpacksLoading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.163.com * updates: mirrors.aliyun.comNo package jemalloc available.Error: Nothing to do

因此,我们还是先安装好EPEL源。

yum -y install epel-release

安装日志:

[root@localhost redis-6.0.9]# yum -y install epel-releaseLoaded plugins: fastestmirror, langpacksLoading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.163.com * updates: mirrors.aliyun.comResolving Dependencies--> Running transaction check---> Package epel-release.noarch 0:7-11 will be installed--> Finished Dependency ResolutionDependencies Resolved============================================================================================================================================================================================================ Package Arch Version Repository Size============================================================================================================================================================================================================Installing: epel-release noarch 7-11 extras 15 kTransaction Summary============================================================================================================================================================================================================Install 1 PackageTotal download size: 15 kInstalled size: 24 kDownloading packages:epel-release-7-11.noarch.rpm | 15 kB 00:00:00 Running transaction checkRunning transaction testTransaction test succeededRunning transaction Installing : epel-release-7-11.noarch 1/1 Verifying : epel-release-7-11.noarch 1/1 Installed: epel-release.noarch 0:7-11 Complete!

安装EPEL源后,我们开始安装jemalloc。

yum install jemalloc

安装日志:

[root@localhost redis-6.0.9]# yum install jemallocLoaded plugins: fastestmirror, langpacksLoading mirror speeds from cached hostfileepel/x86_64/metalink | 5.2 kB 00:00:00 * base: mirrors.aliyun.com * epel: mirrors.bfsu.edu- * extras: mirrors.163.com * updates: mirrors.aliyun.comepel | 4.7 kB 00:00:00 (1/3): epel/x86_64/group_gz | 95 kB 00:00:00 (2/3): epel/x86_64/updateinfo | 1.0 MB 00:00:01 (3/3): epel/x86_64/primary_db | 6.9 MB 00:02:42 Resolving Dependencies--> Running transaction check---> Package jemalloc.x86_64 0:3.6.0-1.el7 will be installed--> Finished Dependency ResolutionDependencies Resolved============================================================================================================================================================================================================ Package Arch Version Repository Size============================================================================================================================================================================================================Installing: jemalloc x86_64 3.6.0-1.el7 epel 105 kTransaction Summary============================================================================================================================================================================================================Install 1 PackageTotal download size: 105 kInstalled size: 317 kIs this ok [y/d/N]: yDownloading packages:warning: /var/cache/yum/x86_64/7/epel/packages/jemalloc-3.6.0-1.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEYPublic key for jemalloc-3.6.0-1.el7.x86_64.rpm is not installedjemalloc-3.6.0-1.el7.x86_64.rpm | 105 kB 00:00:00 Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7Importing GPG key 0x352C64E5: Userid : "Fedora EPEL (7) " Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5 Package : epel-release-7-11.noarch (@extras) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7Is this ok [y/N]: yRunning transaction checkRunning transaction testTransaction test succeededRunning transaction Installing : jemalloc-3.6.0-1.el7.x86_64 1/1 Verifying : jemalloc-3.6.0-1.el7.x86_64 1/1 Installed: jemalloc.x86_64 0:3.6.0-1.el7 Complete!

然后,我们可以查看一下,我们安装了哪些文件。

rpm -ql jemalloc

结果如下:

[root@localhost redis-6.0.9]# rpm -ql jemalloc/usr/bin/jemalloc.sh/usr/lib64/libjemalloc.so.1/usr/share/doc/jemalloc-3.6.0/usr/share/doc/jemalloc-3.6.0/COPYING/usr/share/doc/jemalloc-3.6.0/README/usr/share/doc/jemalloc-3.6.0/VERSION/usr/share/doc/jemalloc-3.6.0/jemalloc.html

然后重新编译,带上指定MALLOC参数。

make MALLOC=/usr/local/jemalloc/lib

让人奔溃的来了,还是报错,而且报错的信息和之前应该是一样。

这就是源码安装最恶心的地方。

经过一番尝试,终于找到了解决方案。这个问题根源主要是gcc环境有问题,我们需要执行以下命令。

yum -y install centos-release-sclyum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutilsscl enable devtoolset-9 bash

成功后,再重新编译就行。

make

安装日志:

[root@localhost redis-6.0.9]# makecd src && make allmake[1]: Entering directory `/root/redis-6.0.9/src' CC server.o CC sds.o CC zmalloc.o CC lzf_c.o CC lzf_d.o CC pqsort.o CC zipmap.o CC sha1.o CC ziplist.o CC release.o CC networking.o CC util.o CC object.o CC db.o CC replication.o CC rdb.o CC t_string.o CC t_list.o CC t_set.o CC t_zset.o CC t_hash.o CC config.o CC aof.o CC pubsub.o CC multi.o CC debug.o CC sort.o CC intset.o CC syncio.o CC cluster.o CC crc16.o CC endianconv.o CC slowlog.o CC scripting.o CC bio.o CC rio.o CC rand.o CC memtest.o CC crcspeed.o CC crc64.o CC bitops.o CC sentinel.o CC notify.o CC setproctitle.o CC blocked.o CC hyperloglog.o CC latency.o CC sparkline.o CC redis-check-rdb.o CC redis-check-aof.o CC geo.o CC lazyfree.o CC module.o CC evict.o CC expire.o CC geohash.o CC geohash_helper.o CC childinfo.o CC defrag.o CC siphash.o CC rax.o CC t_stream.o CC listpack.o CC localtime.o CC lolwut.o CC lolwut5.o CC lolwut6.o CC acl.o CC gopher.o CC tracking.o CC connection.o CC tls.o CC sha256.o CC timeout.o CC setcpuaffinity.o LINK redis-server INSTALL redis-sentinel CC redis-cli.o LINK redis-cli CC redis-benchmark.o LINK redis-benchmark INSTALL redis-check-rdb INSTALL redis-check-aofHint: It's a good idea to run 'make test' ;)make[1]: Leaving directory `/root/redis-6.0.9/src'

看起来,有点成功的感觉。

那我们先启动Redis服务。

src/redis-server

日志:

[root@localhost redis-6.0.9]# src/redis-server28802:C 21 Dec 2020 22:54:23.427 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo28802:C 21 Dec 2020 22:54:23.427 # Redis version=6.0.9, bits=64, commit=00000000, modified=0, pid=28802, just started28802:C 21 Dec 2020 22:54:23.427 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf28802:M 21 Dec 2020 22:54:23.429 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 6.0.9 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 28802 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 28802:M 21 Dec 2020 22:54:23.431 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.28802:M 21 Dec 2020 22:54:23.431 # Server initialized28802:M 21 Dec 2020 22:54:23.432 # 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.28802:M 21 Dec 2020 22:54:23.432 # 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 madvise > /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 (set to 'madvise' or 'never').28802:M 21 Dec 2020 22:54:23.433 * Ready to accept connections

然后再用另外一个终端,打开Redis客户端,执行一些基本操作,验证以下是否已经安装成功。

src/redis-cli

日志:

[root@localhost redis-6.0.9]# src/redis-cli127.0.0.1:6379> set foo barOK127.0.0.1:6379> get foo"bar"127.0.0.1:6379>

执行了一些基本操作,证明我们已经成功安装了Redis。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot热部署
下一篇:详解SpringBoot封装使用JDBC
相关文章

 发表评论

暂时没有评论,来抢沙发吧~