从零开始安装Redis 集群(Linux CenOS7)

从零开始安装Redis 集群(Linux CenOS7)

使用ISO安装CentOS7虚拟机

安装jdk

  • 使用FileZilla上传jdk到Linux系统的/home/software
[root@localhost software]# mkdir /usr/java
...
[root@localhost software]# mkdir /home/software
...
[root@localhost software]# tar -zxvf jdk-8u231-linux-x64.tar.gz 
...
[root@localhost software]# mv jdk1.8.0_231 /usr/java/
...
  • 配置java环境变量
  [root@localhost java]# vim /etc/profile
  ...
	#最下方添加下面三条
  export JAVA_HOME=/usr/java/jdk1.8.0_231
  export CLASSPATH=.:%JAVA_HOME%/lib/dt.jar:%JAVA_HOME%/lib/tools.jar
  export PATH=$PATH:$JAVA_HOME/bin
  ...
  [root@localhost java]# source /etc/profile
  [root@localhost java]# java -version
  java version "1.8.0_231"
  Java(TM) SE Runtime Environment (build 1.8.0_231-b11)
  Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode)

安装Redis

  • https://redis.io/download 下载稳定版本

  • 使用FileZilla上传redis到Linux系统的/home/software

  • 解压压缩包

    [root@localhost software]# tar -zxvf redis-5.0.7.tar.gz 
    

    因为需要编译和生成redis,因此需要安装gcc

    [root@localhost software]# yum -y install gcc-c++
    ...
    已安装:
      gcc-c++.x86_64 0:4.8.5-39.el7                                                                                                                                                                                       
    
    作为依赖被安装:
      cpp.x86_64 0:4.8.5-39.el7              gcc.x86_64 0:4.8.5-39.el7  glibc-devel.x86_64 0:2.17-292.el7  glibc-headers.x86_64 0:2.17-292.el7  kernel-headers.x86_64 0:3.10.0-1062.9.1.el7  libmpc.x86_64 0:1.0.1-3.el7 
      libstdc++-devel.x86_64 0:4.8.5-39.el7  mpfr.x86_64 0:3.1.1-4.el7 
    
    完毕!
    

    进入redis-5.0.7解压目录,执行安装:

    [root@localhost redis-5.0.7]# make && make install
    
  • 配置Redis

    [root@localhost utils]# ll
    总用量 52
    -rw-rw-r--. 1 root root  593 11月 20 01:05 build-static-symbols.tcl
    -rw-rw-r--. 1 root root 1303 11月 20 01:05 cluster_fail_time.tcl
    -rw-rw-r--. 1 root root 1098 11月 20 01:05 corrupt_rdb.c
    drwxrwxr-x. 2 root root   60 11月 20 01:05 create-cluster
    -rwxrwxr-x. 1 root root 2149 11月 20 01:05 generate-command-help.rb
    drwxrwxr-x. 3 root root   31 11月 20 01:05 graphs
    drwxrwxr-x. 2 root root   39 11月 20 01:05 hashtable
    drwxrwxr-x. 2 root root   70 11月 20 01:05 hyperloglog
    -rwxrwxr-x. 1 root root 9567 11月 20 01:05 install_server.sh
    drwxrwxr-x. 2 root root   63 11月 20 01:05 lru
    -rw-rw-r--. 1 root root 1277 11月 20 01:05 redis-copy.rb
    -rwxrwxr-x. 1 root root 1352 11月 20 01:05 redis_init_script
    -rwxrwxr-x. 1 root root 1047 11月 20 01:05 redis_init_script.tpl
    -rw-rw-r--. 1 root root 1762 11月 20 01:05 redis-sha1.rb
    drwxrwxr-x. 2 root root  135 11月 20 01:05 releasetools
    -rwxrwxr-x. 1 root root 3787 11月 20 01:05 speed-regression.tcl
    -rwxrwxr-x. 1 root root  693 11月 20 01:05 whatisdoing.sh
    

    如上所示,在utils目录下,有一个redis_init_script文件,复制该文件到/etc/init.d/目录下,目的是为了配置redis为开机自启动。

    [root@localhost utils]# cp redis_init_script  /etc/init.d/
    [root@localhost utils]# mkdir /usr/local/redis -p
    [root@localhost redis-5.0.7]# cp redis.conf /usr/local/redis/
    
    

    创建/usr/local/redis目录,用于存放redis配置文件。

  • 修改redis配置文件

    ################################# GENERAL #####################################
    
    # 修改daemonize no为yes,目的是启动redis以后台进程运行
    daemonize yes
    # 修改redis工作路径(数据存储位置)
    dir /usr/local/redis/workingdb
    # 代表可以被远程访问,不受ip限制
    #bind 127.0.0.1
    bind 0.0.0.0
    # 修改密码
    requirepass 12345678
    
  • 修改redis_init_script文件中redis核心配置,修改文件权限

    
    REDISPORT=6379
    EXEC=/usr/local/bin/redis-server
    CLIEXEC=/usr/local/bin/redis-cli
    
    PIDFILE=/var/run/redis_${REDISPORT}.pid
    CONF="/usr/local/redis/redis.conf"
    [root@localhost init.d]# chmod 777 redis_init_script 
    #启动redis
    [root@localhost init.d]# ./redis_init_script start
    
    
  • 设置开机自启动

    1. /etc/init.d路径下的启动脚本文件中添加#chkconfig: 22345 10 90 &#description: Start and Stop redis
    [root@iZ2ze7s2v0b78922wia32rZ init.d]# vim redis_init_script 
    #!/bin/sh
    #
    # Simple Redis init.d script conceived to work on Linux systems
    # as it does use of the /proc filesystem.
    
    ### BEGIN INIT INFO
    # Provides:     redis_6379
    # Default-Start:        2 3 4 5
    # Default-Stop:         0 1 6
    # Short-Description:    Redis data structure server
    # Description:          Redis data structure server. See https://redis.io
    ### END INIT INFO
    
    #chkconfig: 22345 10 90
    #description: Start and Stop redis
    
    REDISPORT=6379
    EXEC=/usr/local/bin/redis-server
    CLIEXEC=/usr/local/bin/redis-cli
    
    PIDFILE=/var/run/redis_${REDISPORT}.pid
    CONF="/usr/local/redis/redis.conf"
    
    1. 执行chkconfig redis_init_script on,启动配置.

    2. 关闭redis

      [root@localhost redis]# /etc/init.d/redis_init_script stop
      Stopping ...
      (error) NOAUTH Authentication required.
      Waiting for Redis to shutdown ...
      Waiting for Redis to shutdown ...
      [root@localhost redis]# vim /etc/init.d/redis_init_script 
      # 在脚本中也需要添加密码验证,才能关闭redis
      stop)
              if [ ! -f $PIDFILE ]
              then
                      echo "$PIDFILE does not exist, process is not running"
              else
                      PID=$(cat $PIDFILE)
                      echo "Stopping ..."
                      $CLIEXEC -a "12345678" -p $REDISPORT shutdown
                      while [ -x /proc/${PID} ]
                      do
                          echo "Waiting for Redis to shutdown ..."
                          sleep 1
                      done
                      echo "Redis stopped"
              fi
              ;;
          *)
      
      
    3. 安装之后,远程连接失败,因为在CentOS7 默认开启防火墙

      # 停止防火墙
      [root@localhost ~]# systemctl stop firewalld.service
      # 禁止防火墙开机启动
      [root@localhost ~]# systemctl disable firewalld.service
      Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
      Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
      
      

      或者执行下述命令来开放6379端口

      firewall-cmd --zone=public --add-port=6379/tcp --permanent
      

完整克隆虚拟机

  • 直接在VM ware fusion中需要克隆的机器上右键,选择 创建完整克隆

    • CentOS 7.直接修改Ip

      vim /etc/sysconfig/network-scripts/ifcfg-ens33 # 修改IP配置
      service network restart 重置网络
      
    • CentOS 6或某些版本,需要更改MAC地址和IP

      vim /etc/udev/rule.d/70-persistent-ipoib.rules
      vim /etc/sysconfig/network-scripts/ifcfg-ens33 # 修改IP配置
      service network restart 重置网络
      

Mac 下配置环境变量失效问题

自己在 ~/.bash_profile 中配置环境变量, 可是每次重启终端后配置的不生效.需要重新执行 : $source ~/.bash_profile

发现zsh加载的是 ~/.zshrc文件,而 ‘.zshrc’ 文件中并没有定义任务环境变量。

解决办法

在~/.zshrc文件最后,增加一行:
source ~/.bash_profile

原文地址:https://www.cnblogs.com/zhangpan1244/p/12155141.html