Redis explain in detail the deployment of two installation explain in detail the two installation deployment of Redis

Excerpt: https://www.cnblogs.com/jamaler/p/11882806.html

Explain in detail the two installation deployment of Redis

 

Redis is a more popular NoSQL database, we usually do Redis cache, this is an article about Redis installed, it will not involve Redis advanced features and usage scenarios, Redis is compatible with most of the POSIX systems , such as Linux, OS X, etc., but unfortunately does not support installation on Windows, of course, if you need to install redis in the windows, it is also possible, Microsoft's open Source technology group maintenance branch a Redis on GitHub, GitHub address is: https://github.com/microsoftarchive/redisI looked at the top of this older version, so I personally do not recommend using this to install Redis, Windows users can use to install Docker containers, it is also very convenient, simple, and then we take a look at Redis installation of it.

Install redis under 1, Linux system

installation

There installation tutorials, links redis official website: https://redis.io/downloadinstallation steps I copied the following steps:

$ wget http://download.redis.io/releases/redis-5.0.6.tar.gz
$ tar xzf redis-5.0.6.tar.gz
$ cd redis-5.0.6 $ make

These commands me under / usr / local directory operations, that Redis installation directory is / usr / local, after the execution Redis installed on your machine these commands, the installation process, if on your machine not installed gcc, gcc installed after you re-make may report the following error

jemalloc/jemalloc.h: No such file or directory

There was no interception detailed error information, only the main section of interception, this error occurs because after our last make an error, has compiled files, so we need a clear time remaining files and then recompile, will make replaced make distclean && make it.

redis.conf file

redis.conf is Redis configuration file, all equipped with redis in this file inside this big file nearly 1400 lines, operations related to redis, the instructions are on the inside, you can read a detailed reading this profile, large In some cases we use the default configuration on the line, you only need a small amount of configuration settings on the line. Storage location redis.conf at Redis installation directory, I have here is /usr/local/redis-5.0.5 directory, a look we might modify several configurations:

  • 127.0.0.1 the bind : allow access to the machine's IP, the machine can access only the default, you can change the ip to run other machines can access, but if you want all machines can access it, directly to  bind 0.0.0.0  on the line a.
  • 6379 Port : Redis instance starts ports, the default is 6379
  • NO daemonize : whether by way daemon is running, the default is no, which means you start the window is closed, redis example will close, usually this option we set to  yes , daemon to run, say vulgar It is running in the background.
  • /var/run/redis_6379.pid the PidFile : If we use daemon mode, it will generate a file name suffix .pid. This is also the default line
  • ./ dir : persistent file location, configuration we set about this as well, I am here to set the  dir / usr / local / redis_data
  • NO appendOnly : whether to open the AOF persistent way, redis default only opens the RDB mode, where we set to  yes , two ways are open, double insurance, about the difference between these two approaches, we are behind in learning

Probably seems like these settings, additional configuration on redis.conf, you can read redis.conf profile or access to relevant manuals.

redis start

Redis start very simple, Redis after the installation is complete, interactive command shell will /usr/local/redis-5.0.5/src store Redis, which has a redis-server, this is the start of Redis commands, execute:

./redis-server /usr/local/redis-5.0.5/redis.conf

Is followed by an redis.conf file path, no accident, then we will start successfully, you will see the following interface:

redis start

We use here is the way to start the daemon, so no boot screen with redis logo, we can use shell commands to the Redis Log in or under the src directory, execute the following command:

./redis-cli

This is you enter a command shell interface, / redis-cli command with parameters, e.g. -h IP Redis instance can enter the specified machine, then you can enter some operated, as shown below:

redis operation

redis Close

There are two Redis off, one is closed shell interface, the other is the process number kill + closed Redis instance of the way

closed shell interface

shutdown [nosave|save]

In an input shutdown command shell interface Redis instances may be closed, followed by an optional parameter, the nosave the data in memory is not persistent, Save the data in memory is persistent. shutdown is a more elegant way to shut off, it is recommended to use this way closed

Kill + process number closed Redis instance

Use  ps -ef|grep redis View Redis process ID, as shown below:

View redis process ID

Here we find the need to close redis instance process ID, such as a process where our number is 27133, then we directly use the  kill 27133 closed Redis instance of the service, in this way we need to pay attention to a place that we need to delete the pid file , pid file storage location we configured in redis.conf where  pidfile /var/run/redis_6379.pidwe need to go to  /var/run the directory to redis_6379.pid deleted, so the next time to restart normal Redis service.

The above two methods can close Redis service, pick any kind will, but remember not to use Kill 9 closed Redis way process, so that will not be Redis persistence operations, in addition, will not cause a buffer and other resources elegant closed, in extreme cases can cause loss of data replication and circumstances of AOF

redis boot from the start

On the server we may need to Redis is set to boot from the start, in fact, this is very simple, we only need to do the following four steps can be.

1, write a configuration script vim /etc/init.d/redis
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
#chkconfig: 2345 80 90 #description:auto_run # 端口号 REDISPORT=6379 # 启动命令 EXEC=/usr/local/redis-5.0.5/src/redis-server # shell 交付命令 CLIEXEC=/usr/local/redis-5.0.5/src/redis-cli # pid 存放位置 PIDFILE=/var/run/redis_${REDISPORT}.pid # redis 配置文件 CONF="/usr/local/redis-5.0.5/redis.conf" case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed" else echo "Starting Redis server..." $EXEC $CONF fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $CLIEXEC -p $REDISPORT shutdown while [ -x /proc/${PID} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; *) echo "Please use start or stop as first argument" ;; esac
2, modify redis.conf, set to run as a daemon process redis
################################# GENERAL #####################################

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
3, modify the file execute permissions
chmod +x /etc/init.d/redis
4, set the boot
# 启动 redis
service redis start
# 停止 redis
service redis stop
# 开启服务自启动 chkconfig redis on

2, Docker mounting Redis

Docker installation as a whole Redis is more convenient, I mean non-production environment, is draped over their own testing or learning environment, the following steps you have already fully established on the basis of Docker installed on your computer, following on to open installation trip.

1, the mirror pulling redis

docker pull redis

2, quick start

docker run -p 6379:6379 --name myredis -d redis redis-server --appendonly yes

In this way the redis.conf start using the default configuration, we take a look at the meaning of these parameters

  • -p 6379: 6379: port mapping foregoing redis external port 6379, 6379 is behind the inside of the container port redis
  • --name myredis: a name corresponding to the container
  • redis redis-server: redis redis represents the mirror redis-server represents the command to be executed, but also on redis start command, linux with us as following ./redis-server
  • --appendonly yes: open AOF persistence
3, using redis

Through the above steps, we have started in Docker in the Redis service, here we come to visit it by redis-cli, use the following command can start redis-cli

docker exec -it dockerRedis redis-cli

Where you start dockerRedis Redis container name, not surprisingly, you can start a redis-cli client, as shown below:

docker redis-cli client

Docker above is to use a simple start Redis, the whole installation than the above linux start to facilitate a lot, but you also can run on windows system, although in the end it is still running in linux above, but this process we are not aware of . You might ask: I want to know redis.conf not feasible at startup? The answer is feasible, but if you do not understand Docker, it may encounter some pit, I met, because I do not quite understand Docker, usually using a docker only need to pass parameters are just fine, did not pass over files. About startup configuration file is specified in the instructions redis mirror there, but linux is the following, Docker configuration is not under the windows system, so I Baidu to following this command

docker run -v /d:/dockerdata/redis/config/redis.conf:/usr/local/etc/redis/redis.conf --name myredis redis redis-server /usr/local/etc/redis/redis.conf

This order is a pit, are they not, start this command, you will get the following feedback:

Obviously, this command is of no use, of course, this is only my personal opinion, maybe I am a mistake, maybe I knowledge is not enough, if my friends find errors also requested the exhibitions, I tried to get here when it is wrong, correct the practice is located on the host Docker's redis.conf file, apparently Docker windows system is not the host, but the virtual machine on the windows system, so we need to go to a virtual machine inside, Docker Quickstart Terminal start the default interface and no Log on to the virtual machine being really, so we need to change the login, use the  docker-machine ssh command, as shown below:

Docker Quickstart Terminal startup mode

So we enter into a true virtual machine inside, we operated on a virtual machine, like us on linux installed, we first create two directories used to store Redis configuration:

/ usr / local / redis: storage redis.conf
/ usr / local / Redis / the Data: persistent storage file

After you create two directories, we redis.conf in / usr / local under / redis directory, use the following command to start the Redis this Docker image:

docker run -p 6379:6379 -v /usr/local/redis/redis.conf:/usr/local/etc/redis/redis.conf -v /usr/local/redis/data:/data --name dockerRedis -d redis redis-server /usr/local/etc/redis/redis.conf

这条 docker 启动命令跟上面的有一点区别,有两个参数我在这里说明一下:

到此,Docker 安装 Redis 的复杂操作也做完了,如果没什么特别要求的话,使用简单的 docker 启动就好了,简单方便,完全够用了。

最后

目前互联网上很多大佬都有 Redis 系列教程,如有雷同,请多多包涵了。原创不易,码字不易,还希望大家多多支持。若文中有所错误之处,还望提出,谢谢。

欢迎扫码关注微信公众号:「平头哥的技术博文」,和平头哥一起学习,一起进步。

Flathead brother of technical Bowen

 
分类:  Java

Redis 是一款比较常用的 NoSQL 数据库,我们通常使用 Redis 来做缓存,这是一篇关于 Redis 安装的文章,所以不会涉及到 Redis 的高级特性和使用场景,Redis 能够兼容绝大部分的 POSIX 系统,例如 Linux、OS X 等,但是很遗憾不支持在 Windows 上安装,当然如果你需要在 windows 下安装 redis 的话,也是可以的,微软公司的开源技术组在 GitHub 上 维护一个 Redis 的分支,GitHub 地址为:https://github.com/microsoftarchive/redis,我看了一下这上面的版本比较旧,所以我个人不推荐使用这个来安装 Redis ,Windows 用户可以使用 Docker 容器来安装,也是非常方便、简单的,接下来我们就一起来看看 Redis 的安装方式吧。

1、Linux 系统下安装 redis

安装

在 redis 官网中有安装教程,链接:https://redis.io/download,安装步骤我拷贝过来了,步骤如下:

$ wget http://download.redis.io/releases/redis-5.0.6.tar.gz
$ tar xzf redis-5.0.6.tar.gz
$ cd redis-5.0.6 $ make

我在 /usr/local 目录下面操作的这些命令,也就是说 Redis 的安装目录为 /usr/local ,这些命令执行完之后你的机器上安装好 Redis ,在安装的过程中,如果你的机器上没有安装 gcc ,你安装好 gcc 之后再 make 可能会报以下错误

jemalloc/jemalloc.h: No such file or directory

当时没有截取详细的错误信息,只把主要的一段截取出来了,这个错误的原因是我们上一次 make 报错之后,有编译后的文件,所以我们需要清除上一次的残留文件再重新编译,将 make 换成 make distclean && make 就可以了。

redis.conf 文件

redis.conf 是 Redis 的配置文件,redis 的所有配置有在这个文件里面,这个文件挺大的有接近 1400 行,有关 redis 的操作、使用说明都在里面,可以详细的阅读阅读这个配置文件,大部分情况下我们使用默认配置就行,只需要设置少量配置就行。redis.conf 的存放位置在 Redis 的安装目录下,我这里是 /usr/local/redis-5.0.5 目录下,一起来看看我们可能会修改的几个配置:

  • bind 127.0.0.1:允许访问机器的IP,默认只有本机才能访问,你可以修改 ip 来运行其他机器也能访问,但是如果你想让所有机器都可以访问的话,直接设置为 bind 0.0.0.0 就行了。
  • port 6379:redis 实例启动的端口,默认为 6379
  • daemonize no:是否以守护进程的方式运行,默认是 no,也就是说你把启动窗口关闭了,redis 实例也就关闭了,一般这个选项我们设置为 yes,以守护进程的方式运行,说俗一点就是后台运行。
  • pidfile /var/run/redis_6379.pid:如果我们使用守护进程方式运行的话 ,就会产生一个后缀名为 .pid 的文件,这个使用默认的也行
  • dir ./:持久化文件存放位置,这个配置我们还是设置一下为好,我这里设置为 dir /usr/local/redis_data
  • appendonly no:是否开启 AOF 持久化方式,redis 默认只开启了 RDB 模式,这里我们设置为 yes,两种方式都开启,双重保险,关于这两种方式的区别,我们后面在学习

好像大概设置这几个就好了,更多关于 redis.conf 的配置,你可以详细阅读 redis.conf 配置文件或者查阅相关手册。

redis 的启动

Redis 的启动非常简单,Redis 安装完成之后,会在 /usr/local/redis-5.0.5/src 存放 Redis 的 shell 交互命令,其中有一个 redis-server ,这个就是 Redis 的启动命令,执行:

./redis-server /usr/local/redis-5.0.5/redis.conf

后面跟的是 redis.conf 的文件路径,不出意外的话我们将启动成功,你会看到如下界面:

redis start

这里我们使用的是守护进程的方式启动,所以不会出现带有 redis logo 的启动界面,我们可以使用 shell 命令登录到 Redis 中,还是在 src 目录下面,执行下面这条命令:

./redis-cli

这命令你就进入了 shell 交互界面,./redis-cli 命令可以带一些参数,例如 -h IP 这个就可以进入指定机器的 Redis 实例,进入之后你就可以进行一些操作了,如下图所示:

redis operation

redis 关闭

Redis 的关闭方式有两种,一种是在 shell 交互界面关闭,另一种是 kill + 进程号关闭 Redis 实例的方式

shell 交互界面关闭

shutdown [nosave|save]

在 shell 交互界面输入 shutdown 命令就可以关闭 Redis 实例,后面有一个可选参数,nosave 就是不将内存中的数据持久化,save 就是将内存中的数据持久化。shutdown 关闭方式是比较优雅的关闭方式,建议使用这种关闭方式

Kill + 进程号关闭 Redis 实例

使用 ps -ef|grep redis 查看 Redis 进程号,如下图所示:

View redis process ID

在这里找到我们需要关闭 redis 实例的进程号,比如这里我们的进程号为 27133,那么我们就直接使用 kill 27133 关闭 Redis 实例服务,这种方式我们需要注意一个地方,那就是需要我们去把 pid 文件删掉,pid 文件存放的位置我们在 redis.conf 里配置的 pidfile /var/run/redis_6379.pid,我们需要到 /var/run 目录下把 redis_6379.pid 删掉,这样下一次才能正常重启 Redis 服务。

上面两种方式都可以关闭 Redis 服务,随便选一种都行,但是切记不要使用 Kill 9 方式关闭 Redis 进程,这样 Redis 不会进行持久化操作,除此之外,还会造成缓冲区等资源不能优雅关闭,极端情况下会造成 AOF 和复制丢失数据的情况

redis 开机自启动

在服务器上我们可能需要将 Redis 设置为开机自启动,其实这个也非常简单,我们只需要做以下四步操作即可。

1、 编写配置脚本 vim /etc/init.d/redis
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
#chkconfig: 2345 80 90 #description:auto_run # 端口号 REDISPORT=6379 # 启动命令 EXEC=/usr/local/redis-5.0.5/src/redis-server # shell 交付命令 CLIEXEC=/usr/local/redis-5.0.5/src/redis-cli # pid 存放位置 PIDFILE=/var/run/redis_${REDISPORT}.pid # redis 配置文件 CONF="/usr/local/redis-5.0.5/redis.conf" case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed" else echo "Starting Redis server..." $EXEC $CONF fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $CLIEXEC -p $REDISPORT shutdown while [ -x /proc/${PID} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; *) echo "Please use start or stop as first argument" ;; esac
2、修改 redis.conf,设置 redis 为守护进程方式运行
################################# GENERAL #####################################

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
3、修改文件执行权限
chmod +x /etc/init.d/redis
4、设置开机启动
# 启动 redis
service redis start
# 停止 redis
service redis stop
# 开启服务自启动 chkconfig redis on

2、Docker 安装 Redis

Docker 安装 Redis 整体来说比较方便,我说的是非生产环境,就是自己搭着测试或者学习的环境,下面的步骤全部建立在你已经在你的电脑上安装了 Docker 的基础上,下面就来开启安装之旅。

1、拉取 redis 镜像

docker pull redis

2、快速启动

docker run -p 6379:6379 --name myredis -d redis redis-server --appendonly yes

这种方式启动使用的默认的 redis.conf 配置,我们先来看看这几个参数的意思

  • -p 6379:6379:映射端口,前面的 6379 是外部 redis 端口,后面的 6379 是容器内部的 redis 端口
  • --name myredis :容器对应的名称
  • redis redis-server:redis 代表着 redis 镜像 redis-server 表示的是执行的命令,也是就 redis 的启动命令,跟我们 linux 下面的 ./redis-server 一样
  • --appendonly yes:开启 AOF 持久化
3、使用 redis

通过上面的步骤,我们已经在 Docker 中启动了 Redis 服务,下面我们就来通过 redis-cli 访问一下,使用下面这条命令就可以启动 redis-cli

docker exec -it dockerRedis redis-cli

其中 dockerRedis 是你启动 Redis 容器名称,不出意外的话,你可以启动一个 redis-cli 客户端,如下图所示:

docker redis-cli client

上面就是使用 Docker 简单的启动 Redis ,整体来说比 linux 上面安装启动要方便不少,主要是你可以在 windows 系统上运行,虽然最终它还是运行在 linux 上面的,但是这个过程我们是无感知的。你可以能会问:我想在启动的时候知道 redis.conf 可行不?答案是可行的,但是如果你对 Docker 不了解的话,可能会遇到一些坑,我就遇到了,因为我对 Docker 不是太了解,平时使用 docker 都是只需要传入参数就好了,没有传过文件。关于启动时指定配置文件,在 redis 镜像那里有说明,但是是 linux 下面的,并不是 windows 系统下的 Docker 配置方式,所以我就百度到了下面这段命令

docker run -v /d:/dockerdata/redis/config/redis.conf:/usr/local/etc/redis/redis.conf --name myredis redis redis-server /usr/local/etc/redis/redis.conf

这段命令就是一个坑,压根就没有,启动这条命令,你将得到如下反馈:

显然这条命令是没有用的,当然这只是我个人认为,也许是我操作失误,也许是我知识面不够,如果朋友们发现错误还请多多指教,这里我就先当它是错误的,正确的做法是在 Docker 的宿主机上存放 redis.conf 文件,显然 Docker 的宿主机并不是 windows 系统,而是启动在 windows 系统上的虚拟机,所以我们需要进入到虚拟机里面,Docker Quickstart Terminal 启动默认界面并没有正真的登录到虚拟机,所以我们需要更改登录方式,使用 docker-machine ssh 命令,如下图所示:

Docker Quickstart Terminal startup mode

这样我们就进入到了真正的虚拟机里面,我们就在一台虚拟机上操作了,跟我们在 linux 上的安装一样,我们先建立两个目录用来存放 Redis 配置:

/usr/local/redis:存放redis.conf
/usr/local/redis/data :存放持久化文件

建立好两个目录后, 我们把 redis.conf 放在 /usr/local/redis 目录下,使用下面这条 Docker 命令启动 Redis 镜像:

docker run -p 6379:6379 -v /usr/local/redis/redis.conf:/usr/local/etc/redis/redis.conf -v /usr/local/redis/data:/data --name dockerRedis -d redis redis-server /usr/local/etc/redis/redis.conf

This docker start command with the above there is little difference between the two parameters I am here to explain:

This complex operation Docker install Redis has done, if no special requirements, then using a simple docker started just fine, simple, completely sufficient.

At last

Currently on the Internet has a lot of heavyweights Redis tutorial series, any similarity, please forgive me up. The original is not easy, the code word is not easy, but also hope that we can support. If something incorrect in the text, but also look made, thank you.

Welcome scan code concern micro-channel public number: learn together "flat head brother's technology blog," the first Colombian peace, progress together.

Flathead brother of technical Bowen

Guess you like

Origin www.cnblogs.com/xichji/p/11883817.html