CentOS 7 configure Redis

CentOS 7 configure Redis 4.0.9

foreword

Some data are frequently queried and are not changed frequently. If the database is queried every time it is needed, it will cause a lot of pressure on the database. You can choose to cache data to relieve the pressure on the database. Here we choose to learn Redis as the data cache.


Preliminary preparation

A CentOS 7 virtual machine with a separate IP configured and the firewall turned off (you can copy the previous virtual machine, and then reconfigure the IP address, refer to the link: WinSCP connects to the local virtual machine CentOS 7

C language compilation environment: GCC. (Because Redis is written in C language, the official website provides Redis source code for various versions of Linux distributions. If there is no C language compilation environment, you can use the command yum install gcc-c++to install online

Redis source code package (download the latest version from the official website, official website link: https://redis.io/ )


Install Redis

  • Upload the Redis source code package to CentOS 7, you can upload it with tools such as XShell, here we upload it to the root directory
  • Unzip the Redis source code package, through the command:tar zxvf redis-4.0.9.tar.gz
  • Enter the redis source code directory (for example: cd redis-4.0.9), compile the Redis source code (you need to install GCC first), run the command: make, to compile
  • Install, and specify the installation path, run the command:make install PREFIX=/usr/local/redis
  • Next, try to see if it works

Start and stop the Redis service

Start the Redis service

There are two ways to start the Redis service, one is front-end startup and the other is back-end startup.

In the case of no configuration, the default is the front-end startup. Startup method: first enter the bin folder in the redis installation directory (command: cd /usr/local/redis/bin), and then run the command: ./redis-serverstart the Redis service. If it is CentOS 7 in command line mode, the interface is occupied by Redis at this time, and you can Ctrl + Cexit . A database file is automatically generated - the dump.rdb snapshot file.

To start the backend, you need to configure redis.conf First, this file needs to be copied from the installation directory.
* Go to the installation directory and use the command: cp redis.conf /usr/local/redis/bincopy files.
* Edit the redis.conf file: vi redis.conf. Find the text daemonize no, change no to yes, save and exit. (Press I to edit, press ESC to exit after modification, enter :wqsave and exit)
* Start the backend, enter the command:./redis-server redis.conf

Shut down the Redis service

Close by closing the process
* Use the command to view the redis service process, as shown ps aux|grep redisin the figure (the default port of redis is 6379):

* Close by the kill command: kill 进程号, as shown in the figure above, the process is 1069 , then the command is:kill 1069

To close the service through the client provided by Redis, the command is: ./redis-cli shutdown(The default is to close the Redis service running on the local port 6379. If the port is modified, for example, I modified the port to 7001 here, the following code will prompt when running the command: Could not connect to Redis at 127.0.0.1:6379: Connection refused.It can also be seen from this line of error message that the Redis service running on the local port 6379 is closed by default)

Note: When using the Redis client to shut down the Redis service, you can shut down the Redis service by specifying the IP address and port number. For example, the port 7001 above can ./redis-cli -p 7001 shutdownbe

Redis client connection

In fact, in the above shutdown of Redis service, we have used the client provided by Redis official to us. Here we also briefly introduce some commands.

./redis-cliRunning this command will try to connect to the Redis service running on the local port 6379 by default

./redis-cli -h 192.168.229.162 -p 7001Then the connection host IP is 192.168.229.162, and the port number is the Redis service on 7001. If it is a local Redis service on port 7001, you can use the command: ./redis-cli -p 7001to connect.


afterword

At this point, you can install and simply use a stand-alone version of Redis. After learning through the materials, do it yourself, and then take notes to deepen your memory. At the same time, it is also easy to find when you need it in the future.

These are just the most basic installation, startup and connection operations. For more details, you can learn directly from Redis's official website.

grow in practice

HochenChong

2018-04-25

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324851113&siteId=291194637