1. 程式人生 > 其它 >Redis 單節點安裝

Redis 單節點安裝

1.Redis官⽹

https://redis.io/download  選擇5.0.10版本

2.規劃⽬錄

mkdir -p /data/soft                      #下載⽬錄
mkdir -p /opt/redis_6379/{conf,logs,pid} #安裝⽬錄,⽇志⽬錄,pid⽬錄,配置⽬錄
mkdir -p /data/redis_6379/               #資料⽬錄

3.安裝命令 (編譯安裝)

編譯安裝解釋
./config             指定編譯引數
make                按照要求編譯生成可執行程式
make install      把生成的可執行檔案複製到/usr/local/bin/
1.直接下載 cd /data/soft/ wget http://download.redis.io/releases/redis-5.0.7.tar.gz 2.上傳rz cd /data/soft/ redis-5.0.7.tar.gz 3.解壓到/opt/ 目錄下 tar zxf redis-5.0.7.tar.gz -C /opt/ 4.做個軟連結 ln -s /opt/redis-5.0.7 /opt/redis 5.安裝依賴 yum install gcc make -y 6.進入redis 目錄,編譯 cd /opt/redis make && make install 如果安裝make 時報錯,執行 make
MALLOC=libc

4.編寫配置文件

1.建立資料和日誌目錄
mkdir -p /opt/redis_6379/{conf,logs,pid}
mkdir -p /data/redis_6379

2.編寫配置文件
cat >/opt/redis_6379/conf/redis_6379.conf<<EOF
daemonize yes
bind 127.0.0.1 10.0.0.51
port 6379
pidfile /opt/redis_6379/pid/redis_6379.pid
logfile /opt/redis_6379/logs/redis_6379.log
EOF

5.啟動redis

redis-server /opt/redis_6379/conf/redis_6379.conf

為什麼不用將redis寫入環境變數中,因為PATH 本身就是
/usr/local/bin 下,因為make install 將生成的可執行檔案複製到/usr/local/bin/下 [root@db01 /opt/redis_6379/logs23:15:19]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/mysql/bin:/root/bin

6.檢查是否啟動

ps -ef|grep redis
netstat -lntup|grep 6379

7.連線redis

[root@db01 ~]# redis-cli      
127.0.0.1:6379> set k1 v1       #設定K1 values 為v1
OK
127.0.0.1:6379> get k1          #查k1 的values 
"v1"
可以顯示以上說明redis 連線上,而且正常

8.關閉命令

方法一: 在redis 中輸入shutdown 再輸入exit 或者ctrl +c
       [root@db01 ~]# redis-cli
       127.0.0.1:6379> SHUTDOWN
       
方法二: 在虛擬機器機輸入 redis-cli shutdown
       [root@db01 ~]# redis-cli shutdown
       
方法三: kill redis
       pkill redis

9 .system啟動配置

1.先關閉redis
  redis-cli shutdown
  
2.建立redis虛擬使用者和授權
  groupadd redis -g 2000
  useradd redis -u 2000 -g 2000 -M -s /bin/nologin
  chown -R redis.redis /opt/redis*
  chown -R redis.redis /data/redis*
  
 3. 編寫配置檔
 cat >/usr/lib/systemd/system/redis.service<<EOF
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/local/bin/redis-server /opt/redis_6379/conf/redis_6379.conf --supervised systemd
ExecStop=/usr/local/bin/redis-cli shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target
EOF

4.執行啟動命令
systemctl daemon-reload   每次redis 斷開後需要重行執行命令,systemctl start redis 才可以執行,要不然報錯
systemctl start redis

10.優化警告

用systemctl start redis 後redis 日誌會錯以下四個警告錯誤

1. maximum open files過低
17068:M 23 Jun 2020 10:23:55.707 # You requested maxclients of 10000
requiring at least 10032 max file descriptors.
17068:M 23 Jun 2020 10:23:55.707 # Server can't set maximum open files
to 10032 because of OS error: Operation not permitted.
17068:M 23 Jun 2020 10:23:55.707 # Current maximum open files is 4096.
maxclients has been reduced to 4064 to compensate for low ulimit. If you
need higher maxclients increase 'ulimit -n'

解決方法: systemd啟動⽂件新增引數
vim /usr/lib/systemd/system/redis.service
[Service]
..............
LimitNOFILE=65536

[root@db01 /data/soft22:55:03]# cat /usr/lib/systemd/system/redis.service
[Service]
ExecStart=/usr/local/bin/redis-server /opt/redis_6379/conf/redis_6379.conf --supervised systemd
ExecStop=/usr/local/bin/redis-cli shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=65536                              *****

2.WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

解決方法: 執行以下命令
echo "511" > /proc/sys/net/core/somaxconn 

3.overcommit_memory設定 虛擬記憶體相關
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 vm.overcommit_memory=1
 
 4.關閉THP⼤記憶體⻚
 WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
 
 解決方法:
 臨時解決: 虛擬機器重起後就沒有了
 echo never > /sys/kernel/mm/transparent_hugepage/enabled
 
 永久解決: 寫入開機啟動檔案中
 vim /etc/rc.d/rc.local
 echo never > /sys/kernel/mm/transparent_hugepage/enabled
 
 賦予開機啟動檔案執行許可權
 chmod +x /etc/rc.d/rc.local

11.配置⽂件解釋

1.daemonize yes
# daemonize no 預設情況下, redis 不是在後臺運⾏的,如果需要在後臺運⾏,把該項的值更改為 yes

2.bind 127.0.0.1 10.0.0.51
# 指定 redis 只接收來⾃於該 IP 地址的請求,如果不進⾏設定,那麼將處理所有請求

3.port 6379
# 指定redis運⾏的端⼝,預設是 6379

4.pidfile /opt/redis_6379/pid/redis_6379.pid
# 當redis在後臺運⾏的時候, Redis預設會把pid⽂件放在 /var/run/redis.pid ,你可以配置到其他地址。
# 當運⾏多個redis服務時,需要指定不同的 pid ⽂件和端⼝

5.logfile /opt/redis_6379/logs/redis_6379.log
# 配置 log ⽂件地址

12.redis yum 直接安裝相關配置和啟動

#1.安裝
   yum install -y redis   
#2.檢視預設配置檔和相關檔案
  rpm -ql redis 
  
 [root@shell /var/log/redis22:23:13]# rpm -ql redis
/etc/logrotate.d/redis
/etc/redis-sentinel.conf
/etc/redis.conf
/etc/systemd/system/redis-sentinel.service.d
/etc/systemd/system/redis-sentinel.service.d/limit.conf
/etc/systemd/system/redis.service.d
/etc/systemd/system/redis.service.d/limit.conf
/usr/bin/redis-benchmark
/usr/bin/redis-check-aof
/usr/bin/redis-check-rdb
/usr/bin/redis-cli
/usr/bin/redis-sentinel
/usr/bin/redis-server
/usr/lib/systemd/system/redis-sentinel.service
/usr/lib/systemd/system/redis.service
/usr/libexec/redis-shutdown
/usr/share/doc/redis-3.2.12
/usr/share/doc/redis-3.2.12/00-RELEASENOTES
/usr/share/doc/redis-3.2.12/BUGS
/usr/share/doc/redis-3.2.12/CONTRIBUTING
/usr/share/doc/redis-3.2.12/MANIFESTO
/usr/share/doc/redis-3.2.12/README.md
/usr/share/licenses/redis-3.2.12
/usr/share/licenses/redis-3.2.12/COPYING
/usr/share/man/man1/redis-benchmark.1.gz
/usr/share/man/man1/redis-check-aof.1.gz
/usr/share/man/man1/redis-check-rdb.1.gz
/usr/share/man/man1/redis-cli.1.gz
/usr/share/man/man1/redis-sentinel.1.gz
/usr/share/man/man1/redis-server.1.gz
/usr/share/man/man5/redis-sentinel.conf.5.gz
/usr/share/man/man5/redis.conf.5.gz
/var/lib/redis
/var/log/redis
/var/run/redis
#3.啟動redis
 /usr/bin/redis-server  /etc/redis.conf     #這樣啟動會在前臺夯住,可以放後臺啟動
 nohup /usr/bin/redis-server  /etc/redis.conf &
#4.檢視是否啟動OK
[root@shell /var/log/redis22:23:26]# ps -ef |grep redis
root       7718   7624  0 22:16 pts/1    00:00:01 /usr/bin/redis-server 127.0.0.1:6379
root       7741   7443  0 22:25 pts/0    00:00:00 grep --color=auto redis
#5.連線redis
 redis-cli 或redis-cli -h ip #如果是多埠需要寫明ip 埠,如果是單節點一個埠就是用這個命令redis-cli
#6.檢視redis 版本資訊
 連線上redis後輸入info 即可看到版本資訊
 [root@shell /var/log/redis22:19:02]# redis-cli
127.0.0.1:6379> info
# Server
redis_version:3.2.12
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:7897e7d0e13773f
redis_mode:standalone
os:Linux 3.10.0-957.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.8.5
process_id:7718
run_id:c4c51ede67e3e8baf06fc60bbbabe34a9f5d9611
tcp_port:6379
uptime_in_seconds:183
uptime_in_days:0
hz:10
lru_clock:6571998
executable:/usr/bin/redis-server
config_file:/etc/redis.conf

Do everything well