Deploy redis in Linux environment

Get into the habit of writing together! This is the 21st day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

deploy redis

1. Server resources

Service Name: Linux Server

IP: [please see resource allocation documentation]

Operating System: CentOS 6.9 x64

2. Download the redis compressed package

Download address: redis.io/,

The redis-5.0.5.tar.gz downloaded here is uploaded to the /opt/tools directory

3. Decompress the compressed package

# cd /opt/tools
复制代码
# tar -zxvf redis-5.0.5.tar.gz
复制代码

4. Compile after decompression

# cd /opt/tools/redis-5.0.5/
复制代码
# make
复制代码

Error compiling with make:

image.png   cc: command not found

Reason: The system lacks gcc, just install gcc.

Installation command: yum -y install gcc automake autoconf libtool make

After installing gcc and compiling redis, if the following error occurs:

zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory

zmalloc.h:55:2: error: #error "Newer version of jemalloc required"

make[1]: *** [adlist.o] Error 1

Reason: The allocator will build Redis according to the MALLOC variable, the default is not libc but jemalloc

Workaround: Use the following command to compile

# make MALLOC=libc
复制代码

5. Installation after compilation

# cd /opt/tools/redis-5.0.5/src
复制代码
# make install PREFIX=/opt/app/redis
复制代码

image.png

Six, set the redis directory structure

After the installation is successful, deploy Redis below

First of all, in order to facilitate management, move the conf configuration file and common commands in the Redis file to a unified file. The bin and etc directories are missing in the installation directory, and the files in the installation package need to be copied.

a) Create bin and etc folders

code show as below:

# mkdir -p /opt/app/redis/etc
复制代码

b) Execute the Linux file move command:

Copy the code The code is as follows:

# cd /opt/tools/redis-5.0.5

# cp redis.conf /opt/app/redis/etc/

# cd src/

# cp mkreleasehdr.sh /opt/app/redis/bin/
复制代码

Seven, configure redis

# cd /opt/app/redis/etc/

# vi redis.conf
复制代码

Edit the redis.conf file

a). 将daemonize属性改为yes(表明需要在后台运行)

image.png

b). 搜索requirepass关键字,定位到#requirepass foobared一行。设置密码的方法就是去掉注释的#,把foobared替换成自己的密码即可:redis12qw!@P@ssw0rd

image.png

c). 修改绑定的主机地址,把 bind 127.0.0.1的127.0.0.1修改为Linux服务器的IP地址

(注:有多行bind)

d). 配置数据库配置文件中protected-mode行的值为开启,protected-mode yes

e). 重命名或者禁用危险的命令。

rename-command FLUSHALL ""

rename-command FLUSHDB  ""

rename-command PEXPIRE  ""

rename-command SPOP     ""

rename-command SREM     ""

rename-command RENAME   ""

#rename-command CONFIG   ""

#rename-command DEL      ""
复制代码

  (FLUSHDB,FLUSHALL,PEXPIRE,DEL,SPOP,SREM,RENAME命令可能无法使用,则不能清空当前数据库,也不能清空所有数据库,也不能设置键的有效时间,也不能删除键,也不能从集合中随机删除元素,也不能删除集合中的元素,也不能重命名键。)

八、启动redis

新建redis专用用户并授权

# groupadd redis

# useradd redis -g redis -p /opt/app/redis

# useradd redis -g redis   (-g用户组,-p密码)
复制代码

把redis安装目录授权给redis用户

# cd /opt/app/

# chown -R redis:redis ./redis
复制代码

修改redis配置文件权限应小于600

chmod 600 /opt/app/redis/etc/redis.conf
复制代码

切换到redis用户

# su - redis
复制代码

使用/opt/app/redis/etc/redis.conf 配置文件来启动Redis 服务

# /opt/app/redis/bin/redis-server /opt/app/redis/etc/redis.conf
复制代码

image.png

服务端启动成功后,执行redis-cli启动Redis 客户端,查看端口号,默认是6379。

# /opt/app/redis/bin/redis-cli -h [host] -p [port]
复制代码

注意:host替换为Linux服务器IP

停止redis

# /opt/app/redis/bin/redis-cli shutdown
复制代码

输入密码并测试:

127.0.0.1:6379> auth ****

image.png

查看redis启动进程的用户名,输出redis

# ps -ef | grep -w redis-server | grep -v grep | awk '{print $1}'
复制代码

九、配置防火墙

启动6379端口

# vi /etc/sysconfig/iptables
复制代码

添加以下内容:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT

重启防火墙

# service iptables restart
复制代码

Well, the above is the whole process of deploying redis in linux, and that's it! ! ! ^_^

I'm here first today, skimming, skimming! ! ! ^_^

image.png

Guess you like

Origin juejin.im/post/7089001815252926471