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 redisView 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 27133closed 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/runthe 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 Terminalstart the default interface and no Log on to the virtual machine being really, so we need to change the login, use the docker-machine sshcommand, 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

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/jamaler/p/11882806.html