cd /usr/local #官网获取redis源码包 wget http://download.redis.io/releases/redis-6.0.5.tar.gz
说明:若是还没有安装wget命令,请先安装wget命令linux
yum install -y wget
redis使用C语言写的,在编译源码的时候须要gcc,redis-6.x版本对gcc版本是有要求的,gcc版本不要低于5.3。c++
gcc -v
[root@localhost local]# gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-9/root/usr/libexec/gcc/x86_64-redhat-linux/9/lto-wrapper Target: x86_64-redhat-linux Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-9/root/usr --mandir=/opt/rh/devtoolset-9/root/usr/share/man --infodir=/opt/rh/devtoolset-9/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-9.3.1-20200408/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux Thread model: posix gcc version 9.3.1 20200408 (Red Hat 9.3.1-2) (GCC)
还没有安装gcc的状况,能够执行下面的步骤去安装git
yum -y install centos-release-scl yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils #scl命令启用只是临时的,退出shell或重启就会恢复原系统gcc版本 scl enable devtoolset-9 bash #写入系统执行脚本文件,永久生效 echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile
#进入源码压缩包所在目录 cd /usr/local/ #解压redis源码压缩包 tar -zxvf ./redis-6.0.5.tar.gz #进入redis源码包目录 cd /usr/local/redis-6.0.5 #编译redis源码并安装 make && make install
# 编译出错时,清出编译生成的文件 make distclean # 编译安装到指定目录下 make PREFIX=/usr/local/redis install # 卸载 make uninstall
vim /usr/local/redis-6.0.5/redis.conf
#默认绑定本地回环地址,可注释本配置,开放远程ip能够访问,或者指定对应的远程ip #bind 127.0.0.1 #设置密码 requirepass 123456 #表示之后台守护进程方式启动服务,默认值为no daemonize yes #表示以 CentOS systemd 系统服务方式启动,默认值为no supervised systemd #指定日志文件目录,注意此目录须要真实存在,不然redis启动报错 logfile "/var/log/redis/redis-server.log"
#进入systemd服务配置加载目录 cd /etc/systemd/system #建立redis服务配置文件 touch redis-server.service #改变文件权限,此处为最大权限,酌情而定 chmod 777 ./redis-server.service
# example systemd service unit file for redis-server # # In order to use this as a template for providing a redis service in your # environment, _at the very least_ make sure to adapt the redis configuration # file you intend to use as needed (make sure to set "supervised systemd"), and # to set sane TimeoutStartSec and TimeoutStopSec property values in the unit's # "[Service]" section to fit your needs. # # Some properties, such as User= and Group=, are highly desirable for virtually # all deployments of redis, but cannot be provided in a manner that fits all # expectable environments. Some of these properties have been commented out in # this example service unit file, but you are highly encouraged to set them to # fit your needs. # # Please refer to systemd.unit(5), systemd.service(5), and systemd.exec(5) for # more information. [Unit] Description=Redis data structure server Documentation=https://redis.io/documentation #Before=your_application.service another_example_application.service #AssertPathExists=/var/lib/redis [Service] #ExecStart=/usr/local/bin/redis-server --supervised systemd --daemonize yes ## Alternatively, have redis-server load a configuration file: #ExecStart=/usr/local/bin/redis-server /path/to/your/redis.conf ExecStart=/usr/local/bin/redis-server /usr/local/redis-6.0.5/redis.conf ExecStop=/usr/local/bin/redis-cli shutdown Restart=always LimitNOFILE=10032 NoNewPrivileges=yes #OOMScoreAdjust=-900 #PrivateTmp=yes #Type=notify # 注意 notify 会失败,换成 forking 方式启动,让主进程复制一个子进程的方式执行 Type=forking #TimeoutStartSec=100 #TimeoutStopSec=100 UMask=0077 #User=root #Group=root #WorkingDirectory=/var/lib/redis [Install] WantedBy=multi-user.target
说明:须要注意的是,ExecStart与ExecStop的配置须要必须符合你本地的实际状况github
systemctl daemon-reload
systemctl start redis-server.service
systemctl enable redis-server.service
systemctl status redis-server.service
netstat -tlnp | grep 6379
cat /var/log/redis/redis-server.log
说明:从redis启动日志中发现,有三条警告日志,下面将尝试解决这些警告。web
WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
#临时设置生效 sysctl -w net.core.somaxconn = 1024
vim /etc/sysctl.conf #在/etc/sysctl.conf文件中增长一行配置 #这是一个kernel参数,表示socket监听的backlog上限 net.core.somaxconn=1024
sysctl -p
说明:backlog是socket的监听队列,当一个请求(request)还没有被处理或创建时,他会进入backlog,而socket server会处理backlog队列中的请求,被处理掉的请求会被移出队列,当处理速度跟不上请求建立的速度时,队列满后,新来的请求会被拒绝掉。对于一个常常处理新链接的高负载 web服务环境来讲,默认的 128 过小了。大多数环境这个值建议增长到 1024 或者更多。redis
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.
#临时设置生效 sysctl -w vm.overcommit_memory = 1
vim /etc/sysctl.conf #在/etc/sysctl.conf文件中增长一行配置 #这是一个kernel参数,设置内存分配策略 #vm.overcommit_memory参数有三个选项,分别是0,1,2 #0:表示内核将检查是否有足够的可用内存供应用进程使用;若是有足够的可用内存,内存申请容许;不然,内存申请失败,并把错误返回给应用进程 #1:表示内核容许分配全部的物理内存,而无论当前的内存状态如何 #2:表示内核容许分配超过全部物理内存和交换空间总和的内存 vm.overcommit_memory = 1
sysctl -p
#临时设置生效 echo never > /sys/kernel/mm/transparent_hugepage/enabled
透明大页被临时禁用了shell
vim /etc/rc.local #在/etc/rc.local文件中增长以下脚本 if test -f /sys/kernel/mm/redhat_transparent_hugepage/enabled; then echo never > /sys/kernel/mm/redhat_transparent_hugepage/enabled fi
说明:linux内核中启用了透明大页的支持,默认是启用的,对于redis来讲,将形成redis的延迟和内存使用问题,redis须要禁用透明大页。bootstrap
systemctl restart redis-server.service
说明:重启后再次查看日志,发现以上三条警告日志消失了。vim
firewall-cmd --list-all
firewall-cmd --add-port=6379/tcp --permanent
firewall-cmd --reload
#进入redis-cli客户端 /usr/local/bin/redis-cli #因为redis开启了密码验证,此处须要验证密码 auth 123456 #执行ping命令 ping
推荐一个比较好用而且界面美观的图形化redis客户端(AnotherRedisDesktopManager),前往GitHub下载centos