首页 > 数据库 >Ubuntu18.04环境下安装redis 6.2.0

Ubuntu18.04环境下安装redis 6.2.0

时间:2023-01-08 23:00:25浏览次数:64  
标签:sudo joey redis usr JoeyRedfield615 Ubuntu18.04 local 6.2

环境是win11的Linux子系统Ubuntu-18.04,安装方式是源码安装,也可以用apt安装(见本文最后参考资料),用的用户是默认用户(所以一些关键命令要注意用sudo,不用会报错)

安装:

joey@JoeyRedfield615:~$ sudo apt-get update
joey@JoeyRedfield615:~$ sudo apt-get -y install build-essential
#build-essential把所有要安装的全部都安装好。build-essential是c/c++语言的开发包,包含了gcc、g++、make、gdb和libc等函数库,很多工具。相当于已经安装了gcc,g++,make
joey@JoeyRedfield615:~$ sudo apt install wget
joey@JoeyRedfield615:~$ wget https://download.redis.io/releases/redis-6.2.0.tar.gz joey@JoeyRedfield615:~$ tar xzf redis-6.2.0.tar.gz 
# 移动到你要安装的目录,我这里安装到了/usr/local下

  joey@JoeyRedfield615:~$ tar xzf redis-6.2.0.tar.gz
  joey@JoeyRedfield615:~$ ls
  redis-6.2.0 redis-6.2.0.tar.gz
  joey@JoeyRedfield615:~$ sudo mv ./redis-6.2.0 /usr/local/redis
  joey@JoeyRedfield615:~$ cd /usr/local/redis

 # 执行make命令(编译)

  joey@JoeyRedfield615:/usr/local/redis$ sudo make

如果make出现错误,可能是前面安装命令没执行成功的问题,

joey@JoeyRedfield615:~/redis-6.2.0/src$ make
/bin/sh: 1: pkg-config: not found
    CC adlist.o
/bin/sh: 1: cc: not found
Makefile:359: recipe for target 'adlist.o' failed
make: *** [adlist.o] Error 127

解决方法:

joey@JoeyRedfield615:~$ sudo apt-get update
joey@JoeyRedfield615:~$ sudo apt-get -y install build-essential

 

 

注意如果make命令已经报过错了,就要执行 make distclean  && make 命令,清除上一次残留文件

执行完make后,要测试编译是否成功

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

  make[1]: Leaving directory '/usr/local/redis/src'

  # 上面Hint和make[1]是make成功的提示,接下来要测试编译是否成功
  joey@JoeyRedfield615:/usr/local/redis$ sudo make test

 

如果make test出错如下

joey@JoeyRedfield615:/usr/local/redis$ sudo make test
cd src && make test
make[1]: Entering directory '/usr/local/redis/src'
/bin/sh: 1: pkg-config: not found
    CC Makefile.dep
/bin/sh: 1: pkg-config: not found
You need tcl 8.5 or newer in order to run the Redis test
Makefile:374: recipe for target 'test' failed
make[1]: *** [test] Error 1
make[1]: Leaving directory '/usr/local/redis/src'
Makefile:6: recipe for target 'test' failed
make: *** [test] Error 2

解决方案:

joey@JoeyRedfield615:~$ wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz
# 这个要下载挺久的,我在后面放网盘链接了

joey@JoeyRedfield615:~$ sudo tar xzvf tcl8.6.1-src.tar.gz  -C /usr/local/
joey@JoeyRedfield615:~$ cd  /usr/local/tcl8.6.1/unix/
joey@JoeyRedfield615:/usr/local/tcl8.6.1/unix$ sudo ./configure
joey@JoeyRedfield615:/usr/local/tcl8.6.1/unix$ sudo make
joey@JoeyRedfield615:/usr/local/tcl8.6.1/unix$ sudo make install

tcl8.6.1网盘链接:https://pan.baidu.com/s/1BmLkY0oV636nka_SGzB9pQ?pwd=fkwx

提取码:fkwx

 

安装好tcl8.6.1之后,回去执行sudo make test

joey@JoeyRedfield615:/usr/local/tcl8.6.1/unix$ cd /usr/local/redis/
joey@JoeyRedfield615:/usr/local/redis$ sudo make test

  joey@JoeyRedfield615:/usr/local/redis$ cd src
  joey@JoeyRedfield615:/usr/local/redis/src$ sudo make install

如果sudo make install报错(pkg.config not found),就执行安装命令:sudo apt-get install pkg-config

 

再去安装目录(一路执行下来的话,默认是/usr/local/bin)下执行./redis.server

 进入这里就相当于安装成功了。然后ctrl+c退出。

如果用的不是root用户,由于没有相应权限去创建/写入dump.rdb,所以ctrl+c没法退出。这时候如果强行关闭窗口的话,redis就会一直占用6379端口。

要处理一下也很简单,查出进程号关闭就好了。

joey@JoeyRedfield615:~$ ps aux|grep redis
root     11124  0.5  0.1  62980  8824 ?        Sl   16:05   0:00 ./redis-server *:6379
joey     11163  0.0  0.0  14864  1084 pts/0    S+   16:05   0:00 grep --color=auto redis

joey@JoeyRedfield615:~$ kill -9 11124
-bash: kill: (11124) - Operation not permitted
joey@JoeyRedfield615:~$ sudo kill -9 11124
[sudo] password for joey:
joey@JoeyRedfield615:~$ ps aux|grep redis
joey     11168  0.0  0.0  14864  1156 pts/0    S+   16:06   0:00 grep --color=auto redis
joey@JoeyRedfield615:~$

所以以一般用户开启redis,也要 sudo ./redis-server

 

可以用kill (正常) 或者kill -9(强制) 去结束进程,也可以通过redis-cli去结束。

joey@JoeyRedfield615:/usr/local/redis/bin$ ./redis-cli
127.0.0.1:6379> shutdown
not connected>

#然后ctrl+c就结束进程了
joey@JoeyRedfield615:/usr/local/redis/bin$ ps aux|grep redis
joey     11284  0.0  0.0  14864  1024 pts/0    S+   16:22   0:00 grep --color=auto redis

 

另外linux子系统一个问题就是如果用的是默认的用户,它是没有root级别权限的,一些安装、访问类型的命令会有权限限制,每次都要用sudo。

joey@JoeyRedfield615:/usr/local/redis/bin$ sudo passwd root
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

joey@JoeyRedfield615:/usr/local/redis/bin$ su root Password:
root@JoeyRedfield615:/usr/local/redis/bin# ls redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server

切换一下就好了。

 

现在要改配置文件以接近生产环境,还有设置成开机自启。

sudo vi /usr/local/redis/etc/redis.conf
# 注释掉bind 127.0.0.1,bind用于限制访问Redis的机器ip,直接关掉,方便后续做集群或其他客户端进行连接
# bind 127.0.0.1
# 修改daemonize no为yes,让Redis可以后台启动
daemonize yes
 
protected-mode no

# 这个是为了方便看日志,如果不设置的话,logs will be sent to /dev/null
logfile "/var/log/redis/redis_server.log"

# 记得去创建对应目录和文件,joey@JoeyRedfield615:/var/log/redis$ sudo touch redis_server.log # 为Redis添加服务密码# requirepass foobared,这里我们将密码设置为123456 requirepass 123456 # 修改rdb文件存储路径,rdb文件是Redis默认的数据持久化到磁盘的文件,默认配置为dir ./,则存储在相对目录 # 即每次运行redis-server的目录,这里我们修改为绝对路径,以便于后续的管理 dir /var/lib/redis/rdb/
#:wq 保存并退出

 

 按n可以向前搜索。

设置自启动配置文件

joey@JoeyRedfield615:/var/lib/redis$ cd /usr/local/redis/
joey@JoeyRedfield615:/usr/local/redis$ ls
00-RELEASENOTES  CONTRIBUTING  MANIFESTO  TLS.md      runtest            runtest-sentinel  tests
BUGS             COPYING       Makefile   deps        runtest-cluster    sentinel.conf     utils
CONDUCT          INSTALL       README.md  redis.conf  runtest-moduleapi  src
joey@JoeyRedfield615:/usr/local/redis$ vim utils/redis_init_script

joey@JoeyRedfield615:/usr/local/redis$ cd utils/

#接下来是重命名、复制文件、添加权限
joey@JoeyRedfield615:/usr/local/redis/utils$ mv redis_init_script redisd
joey@JoeyRedfield615:/usr/local/redis/utils$ cp redisd /etc/init.d/

  joey@JoeyRedfield615:/usr/local/redis/utils$ sudo chmod +x /etc/init.d/redisd
  joey@JoeyRedfield615:/usr/local/redis/utils$ sudo update-rc.d redisd defaults

 update-rc.d就相当于设置redis为自启动了

 

编辑redis_init_script时,头部加上这两句
#!/bin/sh
# chkconfig: 2345 90 10

正文部分:
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
 
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"

其中,6379是redis端口号;CONF是redis配置文件,将在下一步设置;EXEC、CLIEXEC是服务所在路径,在上步make install时,已经放入默认路径/usr/local/bin中

joey@JoeyRedfield615:/usr/local/bin$ cd /etc/redis/
joey@JoeyRedfield615:/etc/redis$ ls
joey@JoeyRedfield615:/etc/redis$ sudo cp /usr/local/redis/redis.conf ./

  joey@JoeyRedfield615:/etc/redis$ sudo mv redis.conf 6379.conf
  joey@JoeyRedfield615:/etc/redis$ ls
  6379.conf

也可以直接去把CONF的地址改成原来redis文件夹下redis.conf的地址。重要的是要EXEC、CLIEXEC、CONF三者地址要匹配。

到这里算是配置成功了。

要验证的话,可以重启一下Ubuntu再看看。这里要用管理员权限打开cmd窗口。

 如果redis没有自动启动的话,参考这个博客:ubuntu 如何安装 sysv-rc-conf 配置chkconfig_forrestGTJU的博客-CSDN博客,然后将redisd的2,3,4,5级别全部选中,q键退出

然后修改redis-server.log的权限;chmod 777 /var/log/redis/redis_server.log

再去修改redisd文件

 

再执行 sudo update-rc.d redisd defaults

 

 

参考资料

redis 安装fatal error: jemalloc/jemalloc.h: No such file or directory 错误_baidu_38558076的博客-CSDN博客

ubuntu命令安装redis并设置自启动_擦肩而过的博客-CSDN博客_ubuntu启动redis

Redis开机自启失败(systemd服务)_學如逆水行舟的博客-CSDN博客_redis systemd

 You need tcl 8.5 or newer in order to run the Redis test - 我为什么要写这个 - 博客园 (cnblogs.com)

 

标签:sudo,joey,redis,usr,JoeyRedfield615,Ubuntu18.04,local,6.2
From: https://www.cnblogs.com/joey-redfield/p/17034752.html

相关文章

  • 6.2window对象及常用方法
    ​  window对象及常用方法什么是window对象Window对象描述Window对象简单理解就是把浏览器抽象成一个对象,它表示一个浏览器窗口或一个框架。在客户端JavaScript......
  • 6.2window对象及常用方法
    ​  window对象及常用方法什么是window对象Window对象描述Window对象简单理解就是把浏览器抽象成一个对象,它表示一个浏览器窗口或一个框架。在客户端JavaScript......
  • SpringBoot——Redis 的集成和简单使用
    前言在实际工作中,在存储一些非结构化或者缓存一些临时数据及热点数据时,一般上都会用上mongodb和redis进行这方面的需求。所以本文准备讲下缓存数据库Redis的集成,同时会介......
  • 【SpringBoot实战专题】「开发实战系列」从零开始教你舒服的使用RedisTemplate操作Red
    SpringBoot快速操作Redis数据在SpringBoot框架中提供了spring-boot-starter-data-redis的依赖组件进行操作Redis服务,当引入了该组件之后,只需要配置Redis的配置即可进行链......
  • Redis未授权访问漏洞复现
    redis数据库默认无密码认证,当数据库未设置认证时,即存在未授权漏洞,可以导致服务器被远控等危害。复现使用客户端连接redis远程服务器./redis-cli-h127.0.0.1-p6379......
  • 布隆过滤器与Redis的BitMap之区别
     布隆过滤器特点:占用空间更小;可以用来判断一个数据是否存在;布隆过滤器说有,那么该数据不一定,但是布隆说没有一定没有;优点:节省空间;缺点:1.会出现误判;......
  • 工作场景中的Redis主从不一致
    背景某种类型页面的数据缓存时间是6分钟,上沙盒之后,发现在该类型页面下不同的实际页面的数据有些展现,有些不展现,此外,刚刚展现的同一个页面刷新后数据就不展现了,再刷新又展......
  • Redis查询之RediSearch和RedisJSON讲解
    目录1Redis查询1.1RedisMod介绍1.2安装Redis1.3RediSearch+RedisJSON安装1.3.1下载安装1.3.2修改配置1.4RedisJSON操作1.4.1基本操作1.4.1.1保存操作JSON.SET1.4.......
  • Redis
    Redis安装1、Redis官网下载2、tarzxvfredis目录-C/usr/local3、安装gccyuminstallgcc-c++4、编译(在/usr/local/redis目录)make5、执行(进入src目录)makei......
  • Linux:使用snap安装Redis
    文档官网​​https://snapcraft.io/​​红帽Linux平台下安装教程​​https://snapcraft.io/docs/installing-snap-on-red-hat​​搜索应用​​https://snapcraft.io/se......