Redis(1.1)redis安装,linux下安装redis

一、常见安装方式

【0】环境

  OS:CentOS7.5

  Redis:4.0.14

  yum源:本地源

【1】检查安装 gcc 依赖环境

gcc -v
#如果没安装会报错类似于 command not find
#安装gcc环境(注意,这个需要自己配置yun源,如果是网络源需要网络保持畅通--yum源配置
yum -y install gcc-c++
yum -y install tcl

 

【2】下载解压安装redis

【2.1】下载

  官网:https://redis.io/download (这里我用的是windows下载,然后上传,公司环境特殊 虚拟机没网络)

  linux下载:wget http://download.redis.io/releases/redis-4.0.14.tar.gz

【2.2】安装

下载之后

mkdir -p /data/redis/app
cd /data/redis/app
#然后拷贝redis文件过来
tar
-zxvf redis-4.0.14.tar.gz cd redis-4.0.14/ make
# 如果执行出错,执行 make distclean 清除出错信息,一般都是 gcc 没有安装导致 make 失败
cd src/ make test
#这里报错了,说我需要tcl8.5版本以上才能使用这个(记得安装好之后,再运行一次make test啊!)  


#如果有网络yum源,则直接

 yum -y install tcl

# 官网下载地址:https://sourceforge.net/projects/tcl/

# wget https://nchc.dl.sourceforge.net/project/tcl/Tcl/8.6.1/tcl8.6.1-src.tar.gz

#tcl安装tcl8.5.9-src.tar.gz
cd /tmp
tar -zxvf tcl8.6.1-src.tar.gz
cd tcl8.6.1/unix/
 ./configure --prefix=/usr/local/tcl/ --enable-shared
make
make install
echo "export PATH=${PATH}:/usr/local/tcl/bin" >> /etc/profile
source /etc/profile

#安装redis
cd
/data/redis/app/redis-4.0.14/
mkdir data
cd src
make PREFIX=/data/redis/app/redis-4.0.14/data install
  

 

   #把解压的redis路径下的redis.conf文件拷贝到安装路径下面

 到这里就算完成了,但我们看看这几个命令分别是干什么用的呢?
redis-benchmark:性能测试工具
redis-check-aof:aof 文件修复工具
redis-check-rdb:rdb 文件检查工具(快照持久化文件)
redis-cli:命令行客户端
redis.cnf:复制过来的配置文件,用于后端启动
redis-sentinel:哨兵接口工具
redis-server:服务启动工具

【2.3】启动redis

#【2.3.1】前端启动
#切入到redis安装目录 cd
/data/redis/app/redis-4.0.14/data/bin ./redis-server

OK

#【2.3.2】后端启动
#修改复制到redis安装目录的redis.conf配置文件
vim redis.conf
把如下图 daemonize 的 no 改为 yes
  

  

 

  #正式启动,利用server与.conf

  ./redis-server ./redis.conf

  

 

 

 #【2.3.3】核验redis是否启动

  ps -ef|grep redis

    

 【2.4】关闭redis

./redis-cli shutdown

ps -ef|grep redis

  

 

 【2.5】相关常用操作

//首先链接客户端
[root@CO7 redis]# ./bin/redis-cli
//检查网络是否可以
127.0.0.1:6379> ping
PONG
//设置一个键值对
127.0.0.1:6379> set name cheny
OK
//获取刚刚设置的键值对
127.0.0.1:6379> get name
"cheny"
//查看所有的键
127.0.0.1:6379> keys *
1) "name"
//删除name这个键
127.0.0.1:6379> del name
(integer) 1
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> 

 

【3】常见连接使用故障

1、connect timed out----关闭防火墙或者开启6379端口
2、connection refused : connect------将redis.conf中的127.0.0.1改成虚拟机地址--注意是虚拟机地址啊

            vim把redis-conf的第68行的bind 127.0.0.1改为bind 0.0.0.0,然后重启redis即可

 

参考:https://blog.51cto.com/13719714/2175486

 

二、生产上的安装

  生产上必须要注意的是安全问题,要用redis用户安装不要用root用户安装,不要用127.0.0.1作为绑定地址。

【1】下载 redis 软件

官网:https://redis.io/download (这里我用的是windows下载,然后上传,公司环境特殊 虚拟机没网络)
linux下载:wget http://download.redis.io/releases/redis-4.0.14.tar.gz

【2】使用root 创建 redis 用户和组

groupadd -g 601 redis
useradd -u 6001 -g 601 redis
id redis
passwd redis

【3】调整 redis 用户的 ulimit

vi /etc/security/limits.conf
#输入以下内容
redis  hard  nofile  10240
redis  soft  nofile  1024
redis  hard  nporc  8192
redis  soft  nproc  8192
#nproc 用户创建进程数 nofile 进程打开文件次数限制,更多参考《操作系统环境配置

【4】构建安装路径与数据路径

mkdir -p /data/redis
chown -R redis:redis /data/redis
mkdir -p /data/redis/log
mkdir -p /data/redis/conf
mkdir -p /data/redis/data
chown -R redis:redis /data/redis
chmod -R 777 /data/redis

【5】安装

#假设我们软件下载/上传在 /soft 目录下
chmod -R 777 /soft  
su -l redis
cd /soft
tar -zxvf redis-4.0.14.tar.gz
cd redis-4.0.14
make
cd ./src
make PREFIX=/data/redis/data install #注意,这里的 PREFIX 一定要大写
cd ../
cp redis.conf /data/redis/conf
#echo "export PATH=${PATH}:/data/redis/data/bin" >> /etc/profile
echo "export PATH=${PATH}:/data/redis/data/bin" >> ~/.bash_profile
source ~/.bash_profile #source /etc/profile
su -l root

【6】OS 调优

#【6.1】调整 vm.overcommit_memory
#vi /etc/sysctl.conf
echo "vm.overcommit_memory=1" >> /etc/sysctl.conf

#改参数关注的是内存分配策略
#0,标识内核将检查是否有足够的可用内存供应用进程使用;如果有则申请允许,否则内存申请失败,并返回错误给应用进程。
#1,标识内核允许分配所有的物理内存,而不管当前的内存状态如何。
#2,标识内核允许分配超过所有物理内存和交换空间(swap)的内存

#【6.2】调整 Transparent Huge Pages(THP)
#解决:redis 做 rdb 时会有部分请求超时的情况

echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo "never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.local

#【6.3】TCP backlog设置(详情见后续redis配置参数)
echo 2048 > /proc/sys/net/core/somaxconn
echo "net.core.somaxconn=2048" >> /etc/sysctl.conf
#动态加载
sysctl -p

#此参数确定了TCP连接中已完成队列(完成三次握手之后)的长度
#此值必须不大于linux系统定义的 /proc/sys/net/core/somaxconn的值,默认511
#而 linux 默认参数值是128,当系统并发量大并且客户端缓慢时,可以将这2个参数一起参考设置。

【7】调整redis配置文件  redis.conf(更多参考 redis 配置文件

vi /data/redis/conf/redis.conf
daemonize yes
logfile "/data/redis/log/redis.log"
#requirepass 123456
bind 0.0.0.0
pidfile /data/redis/conf/redis.pid
dbfilename redis.rdb
dir /data/redis/conf/

【8】启动与连接及关闭

8.1】启动
redis-server /data/redis/conf/redis.conf #默认端口是6379
【8.2】查看
ps -ef|grep redis
【8.3】连接登录
redis-cli -a 123456
【8.4】关闭redis
bash环境: redis-cli -a 123456 shutdown
redis环境: shutdown
Bash环境: pkill pid
【8.5】退出登录
exit/ctrl + c 都可以
【8.6】开机自启
vim /etc/rc.local
su -l redis -c "/data/redis/data/bin/redis-server /data/redis/conf/redis.conf"

【9】封装 redis 服务成系统服务(参考linux启动与关闭

#【9.1】构造文件
vi /usr/lib/systemd/system/redis.service

#【9.2】文件内容
[unit] Description
=Redis After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking User=redis Group=redis PIDFile=/data/redis/conf/redis.pid ExecStart=/data/redis/data/bin/redis-server /data/redis/conf/redis.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/data/redis/data/bin/redis-cli -a 123456 shutdown PrivateTmp=true [Install] WantedBy=multi-user.target

#【9.3】刷新守护进程,设置为开机自启
systemctl daemon-reload #重载系统服务
systemctl enable redis #设置开机自启

#【9.4】把该文件给 redis用户授权
chown -R redis:redis /usr/lib/systemd/system/redis.service
chmod -R 755 /usr/lib/systemd/system/redis.service

#【9.5】封装成系统服务后 常用启动关闭 redis命令
systemctl start redis  #启动redis
systemctl stop redis   #关闭redis
systemctl restart redis #重启redis
systemctl status redis #查看redis服务状态

 

 

三、4.0以后的快速安装redis成服务

前提是要把所有的文件准备好,比如配置文件,日志文件等等

#下载安装解压好之后
cd /soft/redis-4.0.14/utils
./install_server.sh
  

 

 很明显,上面的 selected config:就是要我们输入实例的端口,配置文件,日志文件,数据目录,可执行命令。这些都是需要提前准备好目录和文件的。

 这里就没写准备工作了,大家自行理解;

 后面的Copied /tmp/6479.cnf => /etc/init.d/redis_6479  这个redis_6479就是我们的自定义系统服务名称了

 倒数3-4行的Successfully..... 就是添加了开机自启了,不过这里用的是 centos6的自定义服务和自启方式。。不过没关系,这都是兼容的,就是自启可能会有点问题。



如何修改秘密?
  《1》修改配置文件,requirepass 后面的密码,然后重启
  《2》在线登录上客户端后修改:config set requirepass 123456

  

 

 

 

四、redis图形化界面软件

  

 Redis是一个超精简的基于内存的键值对NOSQL数据库(key-value),一般对并发有一定要求的应用都用其储存session,乃至整个数据库。不过它公自带一个最小化的命令行式的数据库管理工具redis-cli,但使用起来并不方便。所幸Github上面已经有了很多图形化的管理工具,如果你不想对着黑屏打码的话,这是一些选择。

Redis Desktop Manager

一款基于Qt5的跨平台Redis桌面管理软件

    支持: Windows 7+, Mac OS X 10.10+, Ubuntu 14+

    特点: C++ 编写,响应迅速,性能好。但不支持数据库备份与恢复。

    项目地址: https://github.com/uglide/RedisDesktopManager

 

 

 

Redis Studio

国人开发的一款C++编写的redis管理工具,仅支持windows,支持xp操作系统。

项目地址: https://github.com/cinience/RedisStudio

   

 

 

 

Redis Client

国人开发的一款,用Java编写,功能丰富

项目地址: https://github.com/caoxinyu/RedisClient

【5】生产最佳实践

(1)安装步骤

-------------------环境监察--------------------
gcc -v
yum -y install gcc-c++ 
yum -y install tcl
------------------ redis安装 ---------------------

mkdir -p /data/redis_6379
tar -zxf redis-5.0.4.tar.gz
cd redis-5.0.4
make
# make MALLOC=libc
cd ./src
make PREFIX=/usr/local/redis install
echo "export PATH=${PATH}:/usr/local/redis/bin" >> /etc/profile
source /etc/profile  


------------配置文件 /data/redis_6379/redis_6379.conf----------
cat <<eof>>/data/redis_6379/redis_6379.conf
#network
protected-mode no
port 6379

#general
daemonize yes
pidfile /data/redis_6379/redis_6379.pid
supervised no
loglevel notice
logfile "/data/redis_6379/redis_6379.log"
syslog-enabled no
databases 16

#SNAPSHOTTING
save 86400 1
stop-writes-on-bgsave-error no
rdbcompression yes
dbfilename dump.rdb
dir /data/redis_6379/

requirepass 44oVkZJw5nLVCBUb
masterauth 44oVkZJw5nLVCBUb
rename-command FLUSHALL 1123123_FLUSHALL
rename-command FLUSHDB  1124124_FLUSHDB
rename-command CONFIG   1123123_CONFIG

#replication
#slaveof 180.153.250.135 6379
#slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100

#limit
maxclients 10000
maxmemory 100gb
maxmemory-policy allkeys-lru

#append only mode
appendonly yes
appendfsync no
no-appendfsync-on-rewrite no
auto-aof-rewrite-min-size 100024000
auto
-aof-rewrite-percentage 100 eof --------------配置、启动、连接、核验------------- redis-server /data/redis_6379/redis_6379.conf ps -ef|grep redis redis-cli -a 44oVkZJw5nLVCBUb quit echo 'redis-server /data/redis_6379/redis_6379.conf'>>/etc/rc.local

(2)配置文件(redis5.0)

#network
protected-mode no
port 6380

#general
daemonize yes
pidfile /data/redis_6380/redis_6380.pid
supervised no
loglevel notice
logfile "/data/redis_6380/redis_6380.log"
syslog-enabled no
databases 16

#SNAPSHOTTING
save 86400 1
stop-writes-on-bgsave-error no
rdbcompression yes
dbfilename dump.rdb
dir /data/redis_6380

requirepass bfengzlgdredis2017


#replication
#slaveof 10.20.50.46 6380
masterauth bfengzlgdredis2017

#slow log
slowlog-log-slower-than 500000
slowlog-max-len 100000
#slave-read-only yes
repl-backlog-size 20048576
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100

#limit
maxclients 10000
maxmemory 10gb
maxmemory-policy allkeys-lru
client-output-buffer-limit normal 0 0 0
#client-output-buffer-limit slave 1000mb 600mb 60
client-output-buffer-limit slave 0 0 0
client-output-buffer-limit pubsub 8mb 2mb 60

#append only mode
appendonly yes
appendfsync no
no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 300

auto-aof-rewrite-min-size 100024000

(2)配置文件(redis6.0)

#network
protected-mode no
port 6382

#general
daemonize yes
pidfile /data/redis_6382/redis_6382.pid
supervised no
loglevel notice
logfile "/data/redis_6382/redis_6382.log"
syslog-enabled no
databases 16
io-threads-do-reads yes
io-threads 4


#SNAPSHOTTING
save 86400 1
stop-writes-on-bgsave-error no
rdbcompression yes
dbfilename dump.rdb
dir /data/redis_6382

requirepass bfengzlgdredis2017
masterauth bfengzlgdredis2017

#slow log
slowlog-log-slower-than 500000
slowlog-max-len 100000

#replication
#slaveof 10.20.50.14 6379
#masterauth 4v4vqgrQ4GLvQYeW
#slave-read-only yes
repl-backlog-size 20048576
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100

#limit
maxclients 10000
maxmemory 20gb
maxmemory-policy allkeys-lru
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 0 0 0
client-output-buffer-limit pubsub 32mb 8mb 60

#append only mode
appendonly yes
appendfsync no
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 300
auto-aof-rewrite-min-size 100024000

(3)自动化脚本(集群)

若是不要集群,则把配置文件中的集群选项注释掉即可

与redis 安装文件放在一起,执行案例

sh install_redis.sh -f redis-6.2.4.tar.gz -p 6380 -d /data/redis/
#!/bin/bash
source /etc/profile
usage(){
    echo $1
    echo '$#': $#
    echo "for example: sh install_redis.sh -f redis-6.2.4.tar.gz -p 6380 -d /data/redis/
-h usage
-f redis file tar.gz
-d datadir
-p port
"
}

while getopts :f:d:p:h opt
do
        case "$opt" in
                h) usage && exit ;;
                f) redis_file=$OPTARG ;;
                p) port=$OPTARG ;;
                d) datadir=$OPTARG ;;
                *) usage && exit;;
        esac
done

if [ ! $port ];then
   port=6379
fi

if [ `netstat -ano|grep ${port}|wc -l` -gt 0 ];then
echo "the port have already been used:${port}"
netstat -ano|grep ${port}
exit
fi


echo -n "check gcc......"
gcc -v >/dev/null 2>&1
if [ ! $? -eq 0  ];then
echo "None"
echo "yum -y install gcc 1>/dev/null 2>gcc_error.log......"
yum -y install gcc 1>/dev/null 2>gcc_error.log
#yum -y install tcl
fi
echo "ok"

if [ ! $datadir ];then
datadir=/data/redis/
fi

if [ ! $redis_file ] || [ ! -f $redis_file ];then
echo 'redis file option not input'
usage && exit
fi

#------------------ redis安装 ---------------------

mkdir -p ${datadir}${port}_redis
dir=`echo ${redis_file} |awk -F'.tar' '{print $1}'`

if [ ! -d $dir ];then
  tar -zxf $redis_file
  dir=`echo ${redis_file} |awk -F'.tar' '{print $1}'`
  cd $dir

  echo -n " making redis....... make 1>/dev/null 2>make_error.log"
  make 1>/dev/null 2>make_error.log
  if [ ! $? -eq 0  ];then
   echo "make  error"
   exit
  fi
  # make MALLOC=libc
  echo "ok"
  cd ./src
  make PREFIX=$datadir install 

elif [ ! -f "${dir}/src/redis-cli" ];then 
  cd $dir
    make
  if [ ! $? -eq 0  ];then
   echo "make  error"
   exit
  fi
  make PREFIX=$datadir install
  # make MALLOC=libc  
elif [ ! -f "${datadir}bin/redis-cli" ];then
 cd $dir
 make PREFIX=$datadir install
fi

if [ `echo ${PATH}|grep ${datadir}bin|wc -l` -eq 0 ];then
echo "export PATH=${PATH}:/data/redis/bin" >> /etc/profile
source /etc/profile
fi

if [ -f "${datadir}${port}_redis/${port}_redis.conf" ];then
echo "redis config file:${datadir}${port}_redis/${port}_redis.conf is already exists!Please check the file info."
exit
fi

#------------配置文件 /data/redis/redis_6379/redis_6379.conf----------
cat <<eof>>${datadir}${port}_redis/${port}_redis.conf
#network
protected-mode no
port ${port}

#general
daemonize yes
pidfile ${datadir}${port}_redis/${port}_redis.pid
supervised no
loglevel notice
logfile "${datadir}${port}_redis/${port}_redis.log"
syslog-enabled no
databases 16

#SNAPSHOTTING
save 86400 1
stop-writes-on-bgsave-error no
rdbcompression yes
dbfilename dump.rdb
dir ${datadir}${port}_redis/

requirepass 123456
masterauth 123456
#rename-command FLUSHALL 1123123_FLUSHALL
#rename-command FLUSHDB  1124124_FLUSHDB
#rename-command CONFIG   1123123_CONFIG

#replication
#slaveof 180.153.250.135 ${port}
#slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100

#io thread
io-threads 4
# 开启 4 个 IO 线程
io-threads-do-reads yes
#读请求解析也是用 IO 线程


#limit
maxclients 10000
maxmemory 100gb
maxmemory-policy allkeys-lru

#append only mode
appendonly yes
appendfsync no
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100

#cluster
cluster-enabled yes
    #-- 允许实例加入集群
cluster-config-file nodes-${port}.conf
    #-- 每个加入群集的实例都会有一个
cluster-node-timeout 15000
    #-- ms,节点响应超时
cluster-replica-validity-factor 10
    #-- 这个系数是 超时事件* 它 + 集群心跳检查时间;
cluster-require-full-coverage yes
    # --当为no时: 如果请求过来发现集群无法处理(比如某个主实例挂了又没有副本顶上,造成缺失部分hash槽),则可以重新分配hash槽 覆盖原有数据; yes 则必须所有hash槽 ok 状态集群才能访问                                         
cluster-migration-barrier 1 
    # -- 迁移壁垒,即当某个主实例下没有副本,同时又有某个主实例下有超过1个以上的副本,则至少保留一个副本,多的则可以迁移到没有副本的实例下做副本
cluster-replica-no-failover no
    # -- 副本是否故障转移,如果为 yes 则副本无法顶替主实例自动故障转移(可以手动)
cluster-allow-reads-when-down no 
    # --6.0参数,当集群不可用时,是否允许节点读写
eof


#--------------配置、启动、连接、核验-------------
redis-server ${datadir}${port}_redis/${port}_redis.conf
sleep 3
ps -ef|grep redis-server|grep -v grep

if [ `cat /etc/rc.local|grep "redis-server ${datadir}${port}_redis/${port}_redis.conf"|wc -l` -eq 0  ];then
echo "redis-server ${datadir}${port}_redis/${port}_redis.conf">>/etc/rc.local
fi

 

 

 

【6】报错处理

(1)make报错:zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录

什么情况出现这个? 先解压,后安装 gcc;

  

 

使用 make MALLOC=libc 即可

 

posted @ 2019-10-10 15:33  郭大侠1  阅读(524)  评论(0编辑  收藏  举报