Redis

1, the introduction

1.1. What is Redis

Full name: REmote DIctionary Server. Is a completely open source free, written in C language, comply with the BCD protocol of a non-relational database. Is a high performance key-value distributed memory database, based on memory running and support persistence NoSQL database, is currently one of the most popular NoSQL database, known as data structure server.

Features:

  • Redis supports persistent data, which can keep data in memory on disk and be reloaded for use upon restart.
  • Redis not only supports simple key-value data, but also provides storage of list, set, zset, hash and other data structures, all of which are stored in key-value mode.
  • Redis supports data backup in master-slave mode. When the host is down, the sentry will automatically promote the slave machine.

1.2 advantages of Redis

High performance: Redis can read 110,000 times /s and write 81,000 times /s.

Rich data types: Redis supports multiple data type operations.

Atomic: All operations in Redis are atomic, and Redis also supports atomic execution (transaction queues) after several operations have been combined.

Rich features: Redis also supports subscriptions (one call for more than one listen), key expiration, and more.

Lock free contention: Single threading eliminates unnecessary context switches and race conditions.

NIO: Uses the multiplex I/O multiplexing model, non-blocking IO.

1.3 Application scenarios of Redis

Redis is mainly used in cache scenarios with high read efficiency. It can also be used in task queues, data invalidation, distributed locks, etc. The specific use method still depends on the business scenario, not all need to cache to solve, other methods are not bad.

2, installation,

2.1. Environment Preparation

Redis is written in C language, so you need to have a GCC compilation environment, directly use GCC — version instruction, if the failure of the environment. You need to manually install the environment, execute the command, directly enter Y when you meet the choice, and wait for the installation to be completed.

 yum install gcc-c++
Copy the code

2.2. Upload files

Upload the downloaded Redis package to your environment. Redis can be downloaded from the following address. There is no requirement for the upload directory. You can save it in your own installation directory.

Redis. IO/English address

www.redis.cn/ Chinese address

2.3 compilation and installation

Decompress the package to the current directory.

The tar - ZXVF redis - 5.0.5. Tar. GzCopy the code

Then enter the Redis file directory to compile, if the compilation is not successful, delete it and follow the steps again.

CD redis - 5.0.5 makeCopy the code

Finally, the installation, or in the current Redis directory for operation, is to compile just completed executable file installation in the system environment, installation directory can be arbitrarily defined, in the normal installation directory can be.

make PREFIX=/home/admin/app/redis install
Copy the code

2.4. Installation and configuration

Go to the installation directory, and then go to the bin directory below, which stores the Redis server, client and other tools.

Executable file function
redis-benchmark Performance testing tool
redis-check-aof AOF File Repair Tool (Persistent files)
redis-check-dump RDB file checking tool (Persistent files)
redis-cli Client startup tool
redis-server Server startup tool

Copy the redis-conf file from the decompressed directory to the installation directory for configuration loading on startup (note the directory change), and then delete the compressed package and decompressed file, leaving it in the way and taking up space.

cp redis.conf /home/admin/app/redis
Copy the code

3. Start access

3.1 Start of front-end mode

Executing the redis-server file directly will start using the front end model, but after starting it will not be able to do the rest of the operation on the current interface, must end (CTRL + C) to switch to other command operations, this method of starting does not work.

./redis-server
Copy the code

3.2. Start in back-end mode

First, you need to modify the configuration file information, open the configuration file you just copied, go to daemonize, change no to yes, and start it by daemon mode, in other words, background process mode.

vim redis-conf
Copy the code

Then start the service and add the address of the configuration file.

./bin/redis-server ./redis-conf
Copy the code

If you look at the process, you can see that Redis is already running in the background. The default access port is 6379.

ps -ef | grep redis
Copy the code

3.3 client connection

Run the client connection tool directly to display an interface similar to the mysql command line, in the form of host: port >.

You can also specify a host port for connection.

./redis-cli -h host -p port
Copy the code

For most people still prefer to use interface operation tools to operate and view data, tools can be used a lot, RedisDesktopManager software can be a good view of the data information in the database. You only need to enter the corresponding host address and port to access the operation. If the password is configured, you also need to verify the password input. Software can connect multiple database information, also can display all the current database under all library information, the default is 16 space.

3.4. The server is closed

  • It is possible to kill the process directly, but this may result in the loss of persistence files.
kill -9 pid
Copy the code
  • Sent when the client connectsshutdownNormal command shutdown
127.0.0.1:6379 > shutdownCopy the code
  • However, there may be exceptions that do not allow you to close the process. The error says that the process is not configured for logging. If this occurs, you need to kill the process and modify the configuration file. Find the logfile location and add the address of the required logfile. The file must exist first.
logfile  "/home/admin/app/redis/redis.log"
Copy the code
  • After the configuration is complete, restart the server tool and end the process normally after connecting to the client.

3.5. Server Installation

If you use the server to install, directly through the pagoda installation line, configuration file these have been configured, but need to pay attention to the port number as far as possible change, and to add access key, otherwise it will soon be implanted mining, and then the server downtime restart.

requirepass 123456
Copy the code

After the setting is completed, the input key will be raised when the connection is operated. After the input, the operation can be carried out normally.

auth 123456
Copy the code