第六章 Centos7.5安裝Redis 6.2.1


Tagging_Redis 服務器部署

一、需求

1.修改主機名,本地解析、yum源
2.redis的安裝

二、環境准備

主機 角色 IP
prd-redis001 Redis服務器 ec2-69-230-214-114.cn-northwest-1.compute.amazonaws.com.cn

三、相關軟件版本選擇

軟件名稱 版本號 下載地址
redis 6.2.1 https://redis.io/download

四、基本優化

#1.修改主機名
[root@ip-16-0-1-189 ~]# hostnamectl  set-hostname prd-redis001

#2.查看主機名
[root@ip-16-0-1-189 ~]# hostname
prd-redis001

#3.備份yum源
[root@prd-redis001 ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

#4.配置yum源
[root@prd-redis001 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2523  100  2523    0     0   9445      0 --:--:-- --:--:-- --:--:--  9449
[root@prd-redis001 ~]# curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   664  100   664    0     0  13414      0 --:--:-- --:--:-- --:--:-- 13551


#5.查看yum源
[root@prd-redis001 ~]# ll /etc/yum.repos.d/
total 44
-rw-r--r--  1 root root 2523 Mar 12 08:34 CentOS-Base.repo
-rw-r--r--. 1 root root 1664 May 17  2018 CentOS-Base.repo.backup
-rw-r--r--. 1 root root  664 Mar 12 08:35 epel.repo

#6.安裝常用軟件
[root@prd-redis001 ~]# yum -y install tree nmap sysstat lrzsz telnet bash-completion bash-completion-extras vim lsof net-tools rsync ntpdate nfs-utils wget

#7.同步系統時間
1.手動同步系統時間
[root@prd-redis001 ~]# ntpdate ntp.aliyun.com
23 Mar 03:30:13 ntpdate[14122]: adjust time server 203.107.6.88 offset 0.019851 sec
[root@prd-redis001 ~]# date
Tue Mar 23 03:30:15 UTC 2021

2.做定時同步系統時間
[root@prd-redis001 ~]# echo '#Timing synchronization time' >>/var/spool/cron/root
[root@prd-redis001 ~]# echo '*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com &>/dev/null' >>/var/spool/cron/root

3.查看定時任務
[root@prd-redis001 ~]# crontab -l
#Timing synchronization time
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com &>/dev/null

五、修改本地解析

[root@prd-redis001 ~]# vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
16.0.1.189 prd-inner-redis001
ec2-69-230-214-114.cn-northwest-1.compute.amazonaws.com.cn prd-outer-redis001

六、格式化文件系統

#1.查看所有磁盤分區情況
[root@prd-redis001 ~]# fdisk  -l

Disk /dev/nvme1n1: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/nvme0n1: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000acf0a

        Device Boot      Start         End      Blocks   Id  System
/dev/nvme0n1p1   *        2048   104857566    52427759+  83  Linux

#2.進行磁盤分區
[root@prd-redis001 ~]# fdisk  /dev/nvme1n1
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x5619bba8.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-209715199, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): 
Using default value 209715199
Partition 1 of type Linux and of size 100 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

#3.重讀分區表
[root@prd-redis001 ~]# partprobe

#4.磁盤格式化
[root@prd-redis001 ~]# mkfs.xfs  /dev/nvme1n1p1 -f
meta-data=/dev/nvme1n1p1         isize=512    agcount=4, agsize=6553536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=26214144, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=12799, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

#5.新建數據目錄data
[root@prd-redis001 ~]# mkdir /data

#6.掛載目錄
[root@prd-redis001 ~]# mount /dev/nvme1n1p1 /data/

#7.查看掛載點
[root@prd-redis001 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/nvme0n1p1   50G  1.3G   49G   3% /
devtmpfs        1.8G     0  1.8G   0% /dev
tmpfs           1.8G     0  1.8G   0% /dev/shm
tmpfs           1.8G   17M  1.8G   1% /run
tmpfs           1.8G     0  1.8G   0% /sys/fs/cgroup
tmpfs           356M     0  356M   0% /run/user/1000
/dev/nvme1n1p1  100G   33M  100G   1% /data

#8.實現永久掛載
1.查看UUID
[root@prd-redis001 ~]#  blkid
/dev/nvme0n1p1: UUID="8c1540fa-e2b4-407d-bcd1-59848a73e463" TYPE="xfs" 
/dev/nvme1n1: PTTYPE="dos" 
/dev/nvme1n1p1: UUID="da7acd39-597f-4a62-9cc9-a9979a53700c" TYPE="xfs" 
/dev/nvme0n1: PTTYPE="dos" 

2.編輯永久掛載點文件
[root@prd-redis001 ~]# vi /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Tue Jun  5 14:06:12 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=8c1540fa-e2b4-407d-bcd1-59848a73e463 /                       xfs     defaults        0 0
UUID=da7acd39-597f-4a62-9cc9-a9979a53700c /                       xfs     defaults        0 0

七、安裝Redis

1.下載安裝包

#1.創建軟件存放目錄
[root@prd-redis001 ~]# mkdir /data/software

#2.上傳安裝包
[root@prd-redis001 ~]# cd /data/software
[root@prd-redis001 software]# wget https://download.redis.io/releases/redis-6.2.1.tar.gz
或者
[root@prd-redis001 software]# rz redis-6.2.1.tar.gz

2.安裝依賴

[root@prd-redis001 software]# yum install -y cpp binutils glibc glibc-kernheaders glibc-common glibc-devel gcc make tcl

3.更新gcc版本

centos7 默認的 gcc 版本小於 5.3 無法編譯,需要先安裝gcc新版才能編譯
[root@prd-redis001 software]# yum -y install centos-release-scl
[root@prd-redis001 software]# yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils

#臨時生效,退出 shell 或重啟會恢復原 gcc 版本
[root@prd-redis001 software]# scl enable devtoolset-9 bash

#永久生效
[root@prd-redis001 software]# echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile

4 .編譯安裝

[root@prd-redis001 software]# tar zxf redis-6.2.1.tar.gz  -C /opt/
[root@prd-redis001 software]# cd /opt/redis-6.2.1/
[root@prd-redis001 /opt/redis-6.2.1]# make && make install

5.修改redis.conf

[root@prd-redis001 redis-6.2.1]# mkdir /etc/redis         
[root@prd-redis001 redis-6.2.1]# cp redis.conf  /etc/redis/
[root@prd-redis001 redis-6.2.1]# vim /etc/redis/redis.conf
#不限制遠程登錄IP
bind 0.0.0.0
#關閉保護模式
protected-mode no
#修改端口號
port 16379
#開啟守護進程模式
daemonize yes
#添加登錄密碼
requirepass xxxxxxxxxxxxxx

6. 配置system啟動

#1.創建配置目錄
[root@prd-redis001 redis-6.2.1]# mkdir /data/redis/

#2.配置system
[root@prd-redis001 /opt/redis-6.2.1/src]# vim /etc/systemd/system/redis.service
[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
ExecStart=/opt/redis-6.2.1/src/redis-server /etc/redis/redis.conf
#ExecReload=/opt/redis-6.2.1/src/redis-server -s reload
#ExecStop=/opt/redis-6.2.1/src/redis-server -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

#3.使服務自動運行
[root@prd-redis001 redis-6.2.1]# systemctl daemon-reload
[root@prd-redis001 redis-6.2.1]# systemctl  enable  redis.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /etc/systemd/system/redis.service.

#4.啟動服務
[root@prd-redis001 redis-6.2.1]# systemctl restart redis
[root@prd-redis001 redis-6.2.1]# systemctl status redis

#4.驗證服務
[root@prd-redis001 redis-6.2.1]# netstat  -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name   
tcp        0      0 0.0.0.0:16379           0.0.0.0:*               LISTEN      25568/redis-server 
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      523/rpcbind         
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1273/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1058/master         
tcp6       0      0 :::16379                :::*                    LISTEN      25568/redis-server 
tcp6       0      0 :::111                  :::*                    LISTEN      523/rpcbind         
tcp6       0      0 :::22                   :::*                    LISTEN      1273/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1058/master         

7.配置環境變量

#1.配置環境變量
[root@prd-redis001 /opt/redis-6.2.1/src]# vim /etc/profile.d/redis.sh
export PATH=/opt/redis-6.2.1/src:$PATH

#2.使其配置生效
[root@prd-redis001 /opt/redis-6.2.1/src]# source  /etc/profile

8.測試redis連接

#1.驗證密碼登錄成功
[root@prd-redis001 /opt/redis-6.2.1/src]# redis-cli  -p 16379
127.0.0.1:16379> auth 密碼
OK
127.0.0.1:16379> 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM