Redis安装与配置 LInux Centos

1.介绍Redis

Redis 是完全开源免费的,遵守BSD协议,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。

特点: 支持数据的持久化,可以将内存中的数据保存到硬盘,在重启后再次加载使用。 支持的数据结构丰富,String,list,set, zset, hash等等。 支持数据备份,master-slave模式进行数据备份。

优势: 性能高,Redis能读的速度是110000次/s,写的速度是81000次/s。 数据类型丰富 原子性,redis中所有操作都是原子的,并且多个操作也支持原子性 丰富的特性,如通知,key过期等。

2.安装与配置

1.下载压缩包

wget http://download.redis.io/releases/redis-5.0.3.tar.gz

1)在/usr/local目录下创建一个redis目录又来放置解压后的redis。

mkdir /usr/local/redis

2)在本例中redis下载在/home/lisen目录,来到该目录执行解压

tar -zxvf redis-5.0.3.tar.gz  -C /usr/local/redis

3)编译-安装(在/usr/local/redis/redis-5.0.3目录下执行) 没有c++ 环境的先运行 yum install gcc-c++

#编译
make
​

#安装
make install

编译成功的输出:

安装完成时的输出:

可以查看编译完成的redis命令文件:

ls /usr/local/bin/redis*
文件 作用
/usr/local/bin/redis-benchmark 性能测试工具
/usr/local/bin/redis-check-aof 更新日志检查
/usr/local/bin/redis-check-dump 本地数据文件检查
/usr/local/bin/redis-cli 命令行操作工具
/usr/local/bin/redis-server 服务器程序

在执行编译后,安装之前,可以选择性的执行make test,我们下载的一般都是release版本,该步骤是可选的(该步骤运行所有的单元测试代码,需要较长的时间),如果执行时报:You need tcl 8.5 or newer in order to run the Redis test,则需要先安装tcl,可以使用: wget https://nchc.dl.sourceforge.net/project/tcl/Tcl/8.6.8/tcl8.6.8-src.tar.gz执行安装。

3.修改相关配置文件

 vim /usr/local/redis/redis-5.0.3/redis.conf

配置文件需要做如下修改:

下方参数文件可能过多 可以使用 :/bind 查找

  • #bind 127.0.0.1 改行限制redis只能本机访问,需要注释掉 | 如果正式使用配置项目设配端口号后面加即可 |也可以改为 0.0.0.0

  • port 6379 设置redis的访问端口,一般保存为默认值6379即可

  • protected-mode no 关闭保护模式,如果开启则需要将可以访问redis的机器IP地址配置到bind属性中,同时为redis设置访问密码

  • daemonize yes 开启守护进程模式。在该模式下,redis会在后台运行,并将pid写入到redis.conf选项pidfile设置的文件中,此时redis将一直运行,除非手动kill该进程。

  • requirepass 123456 设置访问密码,如果protected-mode设置为yes,则必须设置密码

  • pidfile /var/run/redis_6379.pid,如果使用默认端口则保持默认值即可。

  • logfile /usr/local/redis/redis-5.0.3/redis_log.log 设置redis日志

  • dir redis位置,默认为./ 当前目录,保持默认值。

4. redis服务与关闭

1)启动 安装成功后可以使用redis-server命令进行启动,改命令已经放入/usr/local/bin目录下,且该目录已经放入path环境变量,所以不必进入redis的安装目录也可以执行redis-server命令,在执行时为了使在/usr/local/redis/redis-5.0.3/redis.conf配置文件起效,需要作为启动参数提供。

redis-server  /usr/local/redis/redis-5.0.3/redis.conf

注意:为了能正常读取redis.conf配置文件,需要切换到root用户,或通过sudo命令启动。

启动成功后可以通过如下命令查看:

ps -aux|grep redis

2)关闭 可以使用如下命令进行关闭

redis-cli shutdown      #未设置密码,直接关闭
redis-cli -a 密码  shutdown   #设置密码,在关闭时需要提供密码

5. redis服务的开机启动

1) 在 usr/local/redis/redis-5.0.3 目录下,可以看到有utils目录

[root@localhost redis-5.0.3]# ls
00-RELEASENOTES  COPYING  Makefile   redis.conf       runtest-sentinel  tests
BUGS             deps     MANIFESTO  runtest          sentinel.conf     utils
CONTRIBUTING     INSTALL  README.md  runtest-cluster  src
[root@localhost redis-5.0.3]#

2) 进入utils目录,并指向install_server.sh脚本

./install_server.sh 这个运行文件 高版本可以能会出现问题 我们需要修改此文件

进行到./install_server.sh时报错,

This systems seems to use systemd.
Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!

解决方案:

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即可。

[root@localhost utils]# ./install_server.sh
​
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] /usr/local/redis/redis-5.0.3/redis.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    : /usr/local/redis/redis-5.0.3/redis.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!
/var/run/redis_6379.pid exists, process is already running or crashed
Installation successful!

该命令是交互式的,需要交互式的输入port, redis.conf,log文件等。

3)在/etc/init.d/目录下可以看到redis_6379这个自启动脚本

[root@localhost utils]# cd /etc/init.d
[root@localhost init.d]# ls
functions  netconsole  network  README  redis_6379  tomcat
[root@localhost init.d]#
./usr/local/redis/redis-5

4) chkconfig --list命令查看

[root@localhost init.d]# chkconfig --list
​
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
​
      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。
​
netconsole      0:关    1:关    2:关    3:关    4:关    5:关    6:关
network         0:关    1:关    2:开    3:开    4:开    5:开    6:关
redis_6379      0:关    1:关    2:开    3:开    4:开    5:开    6:关
tomcat          0:关    1:关    2:开    3:开    4:开    5:开    6:开
[root@localhost init.d]#

可以看到redis程序在2,3,4,5这四个等级下是开机自启动的

猜你喜欢

转载自blog.csdn.net/qq_62898618/article/details/127822963
今日推荐