Install Redis easily: don't worry about configuration issues

1. Install Redis on Centos

1. Install the EPEL source

Redis is not in the official CentOS repository, you need to install the EPEL source to access the Redis package. Run the following command to install the EPEL source:

sudo yum install epel-release

2. Install Redis

Install Redis with the following command:

sudo yum install redis

3. Start Redis

After the Redis installation is complete, the Redis service is started by default. You can use the following command to check the status of the Redis service:

sudo systemctl status redis

If the Redis service is running, you will see the following output:

● redis.service - Redis persistent key-value database
   Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2022-07-20 10:20:22 CST; 4s ago
 Main PID: 32361 (redis-server)
   CGroup: /system.slice/redis.service
           └─32361 /usr/bin/redis-server 127.0.0.1:6379

4. Configure Redis

The Redis configuration file is located at /etc/redis.conf, which can be edited with a text editor (such as vi) and modified as needed. You can configure the listening address, port number, password, data persistence, master-slave replication, etc. of Redis.

5. Use Redis

After installing and configuring Redis, you can start using Redis. It can be operated through the Redis client command line tool redis-cli, for example:

redis-cli

Start the Redis client, and then you can execute Redis commands, for example:

set mykey hello

Store the string "hello" in Redis with the key "mykey".

2. Install Redis on Ubuntu

1. Update the system

Before installing Redis, first make sure that the system has been updated to the latest version. The system can be updated with the following command:

sudo apt update
sudo apt upgrade

2. Install Redis

Install Redis with the following command:

sudo apt install redis-server

3. Start Redis

After the Redis installation is complete, the Redis service is started by default. You can use the following command to check the status of the Redis service:

sudo systemctl status redis-server

If the Redis service is running, you will see the following output:

● redis-server.service - Advanced key-value store
   Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2023-05-16 12:15:49 UTC; 2min 37s ago
     Docs: http://redis.io/documentation,
           man:redis-server(1)
 Main PID: 9414 (redis-server)
    Tasks: 4 (limit: 2319)
   Memory: 1.2M
      CPU: 60ms
   CGroup: /system.slice/redis-server.service
           └─9414 /usr/bin/redis-server 127.0.0.1:6379

4. Configure Redis

The Redis configuration file is located at /etc/redis/redis.conf, which can be edited with a text editor (such as vi) and modified as needed. You can configure the listening address, port number, password, data persistence, master-slave replication, etc. of Redis.

5. Use Redis

After installing and configuring Redis, you can start using Redis. It can be operated through the Redis client command line tool redis-cli, for example:

redis-cli

Start the Redis client, and then you can execute Redis commands, for example:

set mykey hello

Store the string "hello" in Redis with the key "mykey".

3. Docker installs Redis

1. Install Docker

Before installing Redis, you need to install Docker first. You can download the Docker installer for your own system from the Docker official website, and then follow the prompts to install it. After the installation is complete, you can use the following command to check whether Docker has been successfully installed:

docker version

2. Pull the Redis image

A powerful feature of Docker is to use images (Images) to create containers (Containers), so the Redis image needs to be pulled first. The latest version of Redis can be pulled using the following command:

docker pull redis

3. Create a Redis container

Create a Redis container named "my-redis" with the following command:

docker run -d --name my-redis -p 6379:6379 redis

Among them, -d means to run the container as a daemon process, --name specifies the name of the container as "my-redis", -p maps port 6379 of the container to port 6379 of the host, and redis represents the name of the Redis image used.

4. Use Redis

After the Redis container starts, you can use the Redis client to connect to the Redis service and execute Redis commands. The Redis client can be started with the following command:

docker run -it --rm --link my-redis:redis redis redis-cli -h redis -p 6379

Among them, -it means to use the interactive shell mode to start the container, --rm means to delete the container immediately after the container stops, --link my-redis:redis means to connect to the Redis container named "my-redis", and redis means to use Redis client image name, redis-cli means to start the Redis client command line tool, -h redis means the host name to connect to the Redis service is "redis" (container name), -p 6379 means the port number to connect to the Redis service is 6379.

The above are the detailed steps to install Redis in Docker. Multiple Redis containers can be created in a similar way to implement functions such as Redis cluster and master-slave replication.

Fourth, install Redis manually

1. Download Redis

You can download the latest version of Redis from the Redis official website at https://redis.io/download. Select the appropriate version, unzip it to the appropriate directory after downloading.

2. Compile Redis

In the decompressed Redis directory, execute the following command to compile:

make

If make is not installed, you can install it with the following command:

sudo apt-get install make

After compiling, you can test it with the following command:

make test

3. Install Redis

After the compilation is complete, you can use the following command to install Redis into the /usr/local/bin directory:

sudo make install

4. Configure Redis

After the installation is complete, you need to configure Redis. You can copy the redis.conf file in the Redis directory to the /etc/redis directory and configure accordingly. For example, the following configuration items can be modified:

  • daemonize yes: enable daemon mode;
  • bind 127.0.0.1: Bind IP address to prevent unauthorized access;
  • port 6379: Specify the port number that the Redis service listens on;
  • requirepass yourpassword: Set the access password for the Redis service.

5. Start Redis

After the configuration is complete, the Redis service can be started with the following command:

redis-server /etc/redis/redis.conf

Background mode can also be enabled with the following command:

redis-server /etc/redis/redis.conf --daemonize yes

6. Test Redis

After Redis has started, the Redis client can be started with the following command:

redis-cli

After the connection is successful, you can use the Redis command to test, for example:

set mykey hello
get mykey

The above are the detailed steps to manually install Redis. In actual use, it is also necessary to perform corresponding configuration and tuning according to specific requirements.

Guess you like

Origin blog.csdn.net/whc888666/article/details/130882840