Linux下安装redis
Linux下安装redis
-
将redis压缩包上传至Linux,建议放在opt目录下
-
安装编译工具
yum install gcc-c++ # redis底层使用c++写的
- 解压redis文件
# 切换到redis压缩包所在路径执行解压命令
tar -zxvf redis-3.2.1.tar.gz
方式一
自定义redis安装目录
# 切换到redis解压目录下,在此目录下新建目录作为安装目录,也可以在其他地方创建
mkdir redis-bin
# 在解压的redis目录下执行以下命令设置安装路径及安装redis
make PREFIX=/home/hadoop/redis-bin install
安装完成,在新建目录下会生成bin目录,进入bin目录可以执行redis相关命令
方式二
默认安装目录:/usr/local/bin,切换到redis解压目录下,执行以下命令
# 编译
make
# 安装
make install
安装完成,在/usr/local/bin会生成redis相关文件,进入目录可以执行redis相关命令
启动
- 前台启动
bin目录下执行./redis-server
[root@zookeeper1 bin]# ./redis-server
10377:C 30 Apr 2022 12:17:39.830 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
10377:C 30 Apr 2022 12:17:39.830 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=10377, just started
10377:C 30 Apr 2022 12:17:39.830 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
10377:M 30 Apr 2022 12:17:39.831 * Increased maximum number of open files to 10032 (it was originally set to 1024).
10377:M 30 Apr 2022 12:17:39.831 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 10377
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
10377:M 30 Apr 2022 12:17:39.833 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
10377:M 30 Apr 2022 12:17:39.833 # Server initialized
10377:M 30 Apr 2022 12:17:39.833 # 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.
10377:M 30 Apr 2022 12:17:39.833 * Ready to accept connections
前台启动方式,命令行窗口不能关闭,否则服务停止
所以需要另开一个窗口同样在bin目录下执行./redis-cli命令启动redis客户端,通过ping可以知道是否连通
[user01@zookeeper1 bin]$ ./redis-cli
127.0.0.1:6379> ping
PONG
- 后台启动
redis默认没有开启支持后台启动,所以需要通过修改配置文件支持后台启动方式
因为涉及到配置文件的修改,所以为了安全,我们可以复制一份配置文件,修改复制的配置文件,redis启动时指定配置文件为复制的配置文件,这样可以既可以使配置生效又保留了原来的配置文件,如果以后配置文件修改乱了,还可以继续复制原来的配置文件进行修改
# 直接在bin目录下新建一个目录存放复制的配置文件
mkdir redis-config
# 复制配置文件到新建目录
cp /opt/redis-6.2.1/redis.conf redis-config/
修改配置文件
# 将 daemonize no 改成 daemonize yes
daemonize yes
指定配置文件启动redis
[root@zookeeper1 bin]# ./redis-server redis-config/redis.conf
查看redis进程
[root@zookeeper1 bin]# ps -ef|grep redis
root 11001 1 0 12:28 ? 00:00:00 ./redis-server 127.0.0.1:6379
root 11049 10179 0 12:29 pts/0 00:00:00 grep --color=auto redis
启动redis客户端
[root@zookeeper1 bin]# redis-cli
127.0.0.1:6379> ping
PONG
高版本的redis已经默认开启支持后台启动,但是执行 ./redis-server 还是以前台方式启动;如果需要以后台方式启动,在启动时指定配置文件即可:./redis-server redis.conf
退出
- redis客户端中关闭redis服务
127.0.0.1:6379> shutdown
not connected> exit
[root@zookeeper1 bin]#
[root@zookeeper1 bin]# ps -ef|grep redis
root 11618 10179 0 12:39 pts/0 00:00:00 grep --color=auto redis
- redis客户端外关闭redis服务
[root@zookeeper1 bin]# ps -ef|grep redis
root 11660 1 0 12:40 ? 00:00:00 ./redis-server 127.0.0.1:6379
root 11695 10179 0 12:40 pts/0 00:00:00 grep --color=auto redis
[root@zookeeper1 bin]# ./redis-cli shutdown
[root@zookeeper1 bin]# ps -ef|grep redis
root 11816 10179 0 12:43 pts/0 00:00:00 grep --color=auto redis
扩展:关闭指定端口redis服务
redis-cli -p 6379 shutdown
记得快乐
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!