• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Y-wee
博客园    首页    新随笔    联系   管理     

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
记得快乐
posted @ 2020-12-07 20:16  Y-wee  阅读(162)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3