Centos7安装Redis6,Linux redis 安装和开机启动

================================

©Copyright 蕃薯耀 2022-01-20

​​蕃薯耀的博客_CSDN博客

一、下载redis文件

https://redis.io/download

二、上传redis文件(redis-6.2.6.tar.gz)到服务器进行编译安装

1、解压文件:

tar -zxvf redis-6.2.6.tar.gz

2、修改文件夹名称,简短一点

mv redis-6.2.6 redis6

3、进入目录:

cd redis6/

4、编译文件:

make

如果make命令报错(没错可跳过):
make[3]: cc:命令未找到
make[3]: *** [alloc.o] 错误 127
make[3]: 离开目录“/java/redis6/deps/hiredis”
make[2]: *** [hiredis] 错误 2
make[2]: 离开目录“/java/redis6/deps”
make[1]: [persist-settings] 错误 2 (忽略)
CC adlist.o
/bin/sh: cc: 未找到命令
make[1]: *** [adlist.o] 错误 127
make[1]: 离开目录“/java/redis6/src”
make: *** [all] 错误 2

解决方案(cc:命令未找到):

yum -y install gcc automake autoconf libtool make

如果make命令还报错(没错可跳过):
zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录
#include <jemalloc/jemalloc.h>
^
编译中断。
make[1]: *** [adlist.o] 错误 1
make[1]: 离开目录“/java/redis6/src”
make: *** [all] 错误 2
解决方案:make增加参数(致命错误:jemalloc/jemalloc.h):

make MALLOC=libc

5、编译后安装Redis
PREFIX为指定安装的目录,自动创建bin目录

make install PREFIX=/java/redis6

查看(多出一个bin目录):

ll

三、Redis配置文件复制和数据存放目录

1、创建一个redis数据存放目录datas,以后其它所有应用程序都将数据存放这里,方便备份

sudo mkdir -p /var/datas/redis

2、修改目录拥有者

sudo chown -R java:java /var/datas

3、复制redis.conf配置文件到bin目录

cp /java/redis6/redis.conf /java/redis6/bin/

4、进入bin目录:

cd /java/redis6/bin/

四、redis.conf配置文件属性修改

修改redis.conf配置文件

vi /java/redis6/bin/redis.conf

1、Redis开启守护进程模式

找到daemonize配置项,修改为yes,开启守护进程模式

daemonize yes

2、Redis取消IP绑定

注释bind 127.0.0.1这行,只能本地连接redis,不然无法使用远程连接。

# bind 127.0.0.1

3、Redis关闭保护模式

将protected-mode的yes改为no,也是开启远程连接。

protected-mode no

4、修改Redis默认端口(可省略)

默认端口是6378,修改成6677

port 6677

位置和示例:
# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6677

5、Redis修改rdb文件名、存放目录、日志文件名

文件名及路径相关配置

pidfile /var/run/redis_101_6677.pid

logfile "redis-101-6677.log"

dbfilename dump-101-6677.rdb

dir /var/datas/redis/

dir 配置日志和rdb文件存放的目录,后面要带/

位置:
# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /var/datas/redis/

6、Redis修改密码:requirepass 密码(不需要可省略)

打开注释,修改密码为:123456

requirepass 123456

位置:
# IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatibility
# layer on top of the new ACL system. The option effect will be just setting
# the password for the default user. Clients will still authenticate using
# AUTH <password> as usually, or more explicitly with AUTH default <password>
# if they follow the new protocol: both will work.
#
# The requirepass is not compatable with aclfile option and the ACL LOAD
# command, these will cause requirepass to be ignored.
#
# requirepass foobared

三、Redis启动和测试

1、启动Redis

/java/redis6/bin/redis-server /java/redis6/bin/redis.conf

2、查看redis进程:

ps -ef | grep redis

3、启动Redis客户端

#默认启动命令(默认端口是6379)
/java/redis6/bin/redis-cli


#指定IP地址和指定端口启动
#-h host 指定IP地址
#-p port 指定端口号
/java/redis6/bin/redis-cli -h 127.0.0.1 -p 6677


#指定IP地址、端口号、密码启动
#-h host 指定IP地址
#-p port 指定端口号
#-a auth 指定密码,首先得在配置文件设置密码:requirepass 123456
/java/redis6/bin/redis-cli -h 127.0.0.1 -p 6677 -a redisPassword

四、Centos7设置Redis开机启动,Redis自启动

1、在系统服务目录里创建redis.service文件

sudo vi /etc/systemd/system/redis.service

2、redis.service文件粘贴内容(粘贴前先按字母 i , 进入编辑模式):

路径修改成自己的安装路径

[Unit]
#Description:描述服务
Description=Redis
#After:描述服务类别 
After=network.target

#服务运行参数的设置 
[Service]
#Type=forking是后台运行的形式 
Type=forking
#ExecStart为服务的具体运行命令,路径必须是绝对路径 
ExecStart=/java/redis6/bin/redis-server /java/redis6/bin/redis.conf
#ExecReload为重启命令 ,路径必须是绝对路径 
ExecReload=/java/redis6/bin/redis-server -s reload
#ExecStop为停止命令 ,路径必须是绝对路径 
ExecStop=/java/redis6/bin/redis-server -s stop
#PrivateTmp=True表示给服务分配独立的临时空间 
PrivateTmp=true

#运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
[Install]
WantedBy=multi-user.target

3、重载系统服务:

sudo systemctl daemon-reload

4、redis.service服务加入开机自启 (注意redis.service后面不能跟空格 )

sudo systemctl enable redis.service

5、重启服务器

reboot -f

6、系统重启后,查看服务运行状态:

sudo systemctl status redis.service

 7、其它命令

sudo systemctl start redis.service #启动redis服务 
sudo systemctl enable redis.service #设置开机自启动 
sudo systemctl disable redis.service #停止开机自启动 
sudo systemctl status redis.service #查看服务当前状态 
sudo systemctl restart redis.service  #重新启动服务 
sudo systemctl list-units --type=service |grep redis #查看所有已启动的服务

五、Redis连接的端口开放

1、开放端口:

sudo firewall-cmd --zone=public --add-port=6677/tcp --permanent

2、让端口生效:

sudo firewall-cmd --reload

3、查看防火墙所有开放的端口

sudo firewall-cmd --zone=public --list-ports

六、Linux Redis 重启数据丢失解决方案

在Linux系统设置一个参数(vm.overcommit_memory)即可解决。

步骤如下:

1、编辑 sysctl.conf 配置文件

sudo vi /etc/sysctl.conf 

2、在文件注释下面另起一行增加参数 vm.overcommit_memory 配置,如下

vm.overcommit_memory = 1

3、使配置文件生效

sudo sysctl -p

4、测试

#指定IP地址和指定端口启动
#-h host 指定IP地址
#-p port 指定端口号
/java/redis6/bin/redis-cli -h 127.0.0.1 -p 6677
set aa 11

get aa

5、重启服务器

重启服务器再重复第4步连接redis客户端查看数据

reboot -f

(时间宝贵,分享不易,捐赠回馈,^_^)

================================

©Copyright 蕃薯耀 2022-01-20

​​蕃薯耀的博客_CSDN博客

猜你喜欢

转载自blog.csdn.net/w995223851/article/details/122603347
今日推荐