Linux Redis

脚本内容

  1. #! /usr/bin/bash
  2. ##redis任何版本全程自动化源码编译安装
  3. ##用法:sh redis-install.sh 4.0.10 (后面跟的是安装的版本号,需要什么版本就写什么版本),这里安装的4.0.10
  4. version=$1
  5. usage(){
  6. echo "usage: $0 version"
  7. }
  8. if [ $# -ne 1 ]
  9. then
  10. usage
  11. exit -1
  12. fi
  13. #Redis安装包下载
  14. cd /usr/local/src
  15. if [ ! -f redis-${version}.tar.gz ]
  16. then
  17. curl -o /usr/local/src/redis-${version}.tar.gz http://download.redis.io/releases/redis-${version}.tar.gz
  18. fi
  19. #Redis依赖包安装
  20. yum clean all
  21. yum makecache fast
  22. yum -y install gcc gcc-c++ tcl
  23. #编译Redis所需要的gcc
  24. yum -y install centos-release-scl
  25. yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
  26. source /opt/rh/devtoolset-9/enable
  27. echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile
  28. gcc --version
  29. ##内系统参数核优化
  30. cat >> /etc/rc.d/rc.local << "EOF"
  31. ##关闭Linux的THP(内存管理系统)通过使用更大的内存页面,来减少具有大量内存的计算机上的TLB的开销
  32. if [ -f /sys/kernel/mm/transparent_hugepage/enabled ]
  33. then
  34. echo never > /sys/kernel/mm/transparent_hugepage/enabled
  35. fi
  36. if [ -f /sys/kernel/mm/transparent_hugepage/defrag ]
  37. then
  38. echo never > /sys/kernel/mm/transparent_hugepage/defrag
  39. fi
  40. EOF
  41. chmod u+x /etc/rc.d/rc.local
  42. if [ -f /sys/kernel/mm/transparent_hugepage/enabled ]
  43. then
  44. echo never > /sys/kernel/mm/transparent_hugepage/enabled
  45. fi
  46. if [ -f /sys/kernel/mm/transparent_hugepage/defrag ]
  47. then
  48. echo never > /sys/kernel/mm/transparent_hugepage/defrag
  49. fi
  50. cat >> /etc/sysctl.conf << "EOF"
  51. #Linux系统内核参数优化
  52. net.core.somaxconn = 2048
  53. net.ipv4.tcp_max_syn_backlog = 2048
  54. vm.overcommit_memory = 1
  55. EOF
  56. sysctl -p
  57. cat > /etc/security/limits.conf << "EOF"
  58. root soft nofile 65535
  59. root hard nofile 65535
  60. * soft nofile 65535
  61. * hard nofile 65535
  62. EOF
  63. #Redis编译安装
  64. cd /usr/local/src
  65. tar -zxvf redis-${version}.tar.gz
  66. cd /usr/local/src/redis-${version}
  67. make
  68. make PREFIX=/usr/local/redis install
  69. #Redis基础配置
  70. mkdir -p /usr/local/redis/{etc,logs,data}
  71. egrep -v "^$|^#" /usr/local/src/redis-${version}/redis.conf > /usr/local/redis/etc/redis.conf
  72. #sed -i "s/bind 127.0.0.1/bind 0.0.0.0/g" /usr/local/redis/etc/redis.conf
  73. sed -i "s/protected-mode yes/protected-mode no/g" /usr/local/redis/etc/redis.conf
  74. sed -i "s/daemonize no/daemonize yes/g" /usr/local/redis/etc/redis.conf
  75. sed -i "s/pidfile \/var\/run\/redis_6379.pid/pidfile \/usr\/local\/redis\/redis.pid/g" /usr/local/redis/etc/redis.conf
  76. sed -i "s/dir \.\//dir \/usr\/local\/redis\/data/g" /usr/local/redis/etc/redis.conf
  77. sed -i "s/logfile \"\"/logfile \"\/usr\/local\/redis\/logs\/redis.log\"/g" /usr/local/redis/etc/redis.conf
  78. sed -i "s/dbfilename dump.rdb/dbfilename dump.rdb/g" /usr/local/redis/etc/redis.conf
  79. sed -i "s/appendfilename \"appendonly.aof\"/appendfilename \"appendonly.aof\"/g" /usr/local/redis/etc/redis.conf
  80. #PATH配置
  81. echo "export PATH=${PATH}:/usr/local/redis/bin" >>/etc/profile
  82. source /etc/profile
  83. #启动redis服务
  84. /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
  85. #查看redis监听端口
  86. netstat -tanp|grep redis

使用方法

:::info 脚本后面的参数是需要安装的Redis的版本 :::

  1. chmod 755 redis-install.sh && sh redis-install.sh 4.0.10