centos7安裝reids6

Hughes87發表於2024-04-02

以centos7.9.2009離線安裝reids6.2.9為例
redis的tar包下載平臺

http://download.redis.io/releases/
1.安裝準備

redis是c語⾔開發的,安裝redis需要c語⾔的編譯環境,需要安裝gcc(預設安裝)
redis的原始碼中,有一些測試和指令碼是使用tcl編寫的,需要安裝tcl(預設不安裝)

yum -y install gcc tcl
2.gcc版本切換

centos7預設安裝的gcc版本是4.8.5,而redis6.0只支援5.3以上版本,因此將gcc升級到9
安裝centos-release-scl軟體包,即在系統上啟用了scl(Software Collections)儲存庫,允許在系統上安裝不同版本的軟體,相互獨立,不會干擾。
安裝Devtoolset-9的GCC編譯器devtoolset-9-gcc
安裝Devtoolset-9的GCC編譯器devtoolset-9-gcc-c++
安裝Devtoolset-9的Binutils工具集devtoolset-9-binutils

yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils

透過scl,暫時性地啟用devtoolset-9 的軟體集合,而不會影響到系統中預設的工具鏈,退出當前終端,啟用將會被取消。

scl enable devtoolset-9 bash

設定環境變數和路徑,啟用 devtoolset-9 軟體集合,指令追加到 /etc/profile 檔案的末尾,被永久啟用。

echo "source /opt/rh/devtoolset-9/enable" >> /etc/profile

#回退方法:刪除/etc/profile中的內容
[root@localhost ~]# gcc -v
使用內建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
目標:x86_64-redhat-linux
配置為:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
執行緒模型:posix
gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)

#切換版本後

[root@localhost ~]# 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) 
3.安裝過程

以安裝在/data/redis/目錄為例
建立安裝目標路徑及子目錄

mkdir -p /data/redis/{conf,logs,run}

下載目標版本redis的tar包,解壓

wget https://download.redis.io/releases/redis-6.2.9.tar.gz
tar -zxvf redis-6.2.9.tar.gz 

編譯安裝

make
make install PREFIX=/data/redis/

配置檔案貼上

cp /data/redis-6.2.9/redis.conf /data/redis/conf
4.服務啟動

啟動命令

/data/redis/bin/redis-server  /data/redis/conf/redis.conf

建立啟動、停止指令碼

#服務啟動指令碼
touch /data/redis/start.sh
#!/bin/bash
/data/redis/bin/redis-server /data/redis/conf/redis.conf

#服務停止指令碼
touch /data/redis/stop.sh
#!/bin/bash
pid=\`ps -ef|grep "/data/redis/bin/redis-server"|grep -v "grep"| awk '{print \$2}'\`
if [ "\${pid}" ];then
  echo "kill \${pid}"
  kill \${pid}
fi

chmod +x /data/redis/*.sh
5.配置修改

複製示例配置並剔除註釋內容

cp /data/redis-6.2.9/redis.conf /data/redis/conf/redisbaseconf
cd /data/redis/conf/
cat redisbaseconf |grep -Ev "^#|^$" >> redis.conf

需要修改配置如下

1.Redis取消IP繫結,註釋bind 127.0.0.1這行,只能本地連線redis,不然無法使用遠端連線。
# bind 127.0.0.1

2.Redis關閉保護模式,將protected-mode的yes改為no,也是開啟遠端連線。
protected-mode no

3.修改redis的預設日誌存放位置
logfile /data/redis/logs/redis.log

4.修改redis的服務路徑
dir /data/redis/

5.新增redis密碼:requirepass + 密碼(不需要可省略)
requirepass 123456

可選修改配置(示例未新增)

1.pid儲存路徑
pidfile /data/redis/run/redis_6379.pid

2.監聽埠
port 6379

最終配置呈現

#bind 127.0.0.1 -::1
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /data/redis/logs/redis.log
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir /data/redis/
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes
6.服務管理

建立redis的systemctl管理檔案

vim /usr/lib/systemd/system/redis.service

檔案內容為:

[Unit]
Description=redis
After=network.target

[Service]
Type=forking
ExecStart=/data/redis/bin/redis-server /data/redis/conf/redis.conf
ExecReload=/data/redis/bin/redis-server -s reload
ExecStop=/data/redis/bin/redis-server -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

當Systemd系統服務的配置檔案修改後,需要重新載入Systemd守護程序管理器的配置檔案,用以生效配置。

systemctl daemon-reload
systemctl start redis

相關文章