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