Redis

85 阅读3分钟

安装

基于CentOS 7.6

cd ~
mkdir soft
cd soft
wget https://download.redis.io/releases/redis-6.2.6.tar.gz
tar xf redis-6.2.6.tar.gz
mv redis-6.2.6 redis
cd redis
看README.md文件可以看到如何编译、安装
make
cd src #可以看到生成了可执行程序
cd ..
make install PREFIX=/usr/local/redis
# 进入/usr/local/redis/目录可以看到生成了bin目录 里面存放的是可执行的脚本
vi /etc/profile #添加环境变量
export REDIS_HOME=/usr/local/redis
export PATH=$PATH:$REDIS_HOME/bin
source /etc/profile

cd ~/soft/redis/utils
vi install_server.sh

#将下面的部分代码注释掉
#bail if this system is managed by systemd
#_pid_1_exe="$(readlink -f /proc/1/exe)"
#if [ "${_pid_1_exe##*/}" = systemd ]
#then
#       echo "This systems seems to use systemd."
#       echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
#       exit 1
#fi


./install_server.sh 
# 安装过程中会有如下内容显示 默认开始是6379端口。如果安装多个时,自己可以选择端口,输入自己定义的端口后,后面的文件会自动用该端口生成。最终会启动服务并设置为开机自启动

Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!

# 配置redis密码(可选)
编辑对应的xx.conf文件,搜索 requirepass 并删除该行的注释,配置访问密码

# 支持远程访问(非本机)
编辑对应的xx.conf文件,将 bind 127.0.0.1 -::1 注释掉

# 查看对应端口是否启动成功
netstat -tunpl|grep 6379

# 停止redis服务 
redis设置密码登录后,如果想关闭redis服务器需要使用redis-cli -p 6379 -a '密码' shutdown
-p 6379是redis实例的端口,如果不写 默认6379
-a 密码是对应实例中的conf文件中配置的密码,停服务时密码要用单引号包裹起来

# redis配置文件
/etc/redis/6379.conf
# redis日志文件
/var/log/redis_6379.log
# redis数据文件
/var/lib/redis/6379
# redis可执行的文件
/usr/local/bin/redis-server

注意:

  • ./install_server.sh可以执行一次或多次(如想再次创建redis实例直接运行该命令即可)
  • 一个物理机中可以有一个或多个redis实例(进程) ,通过port来区分
  • 可执行程序就存在一份,但是未来多个redis实例需要各自不同的配置文件,持久化目录资源
  • cd /etc/init.d/ 会发现多了一个redis_6379的文件
  • 可以通过systemctl status/start/stop redis_6379在任意目录来操作查看状态及启停

疑难杂症

本机可以访问,但远程服务器无法访问

  • 打开对应的配置文件redis.conf,注释掉这一行 bind 127.0.0.1 
  • 将保护模式参数设置为no protected-mode no
  • 重启redis即可