Redis configures Internet access

Time:2021-11-30

Method 1: directly annotate the bind configuration options.

vim /etc/redis/6379.conf
# bind 127.0.0.1

#Restart service
service redis stop
service redis start

Method 2: bind the IP address of the server network card

Configure redis

#View the IP address of eth0 network card
ifconfig
  eth0: 172.18.xxx.xx

#Add the IP address of the network card to the configuration file
#Note that the IP address of the network card is used instead of the IP address of the external network
vim /etc/redis/6379.conf
bind 127.0.0.1 172.18.xxx.xx

Configure firewall

#Open the 6379 port of redis
firewall-cmd --zone=public --add-port=6379/tcp --permanent

#Restart the firewall for the configuration to take effect immediately
systemctl restart firewalld

#View all open ports of the system
firewall-cmd --zone=public --list-ports

#If necessary, you can open the port permission of ECs

Command line test

#Check whether reids is running
ps -ef|grep redis

#Restart service
service redis stop
service redis start
#Or
/usr/local/redis/src/redis-cli shutdown
/usr/local/redis/src/redis-server /etc/redis/6379.conf &

#Check whether the local connection configuration is effective
/usr/local/redis/src/redis-cli
config get daemonize
config get protected-mode 

#Remote connection
/Usr / local / redis / SRC / redis cli - H server extranet IP - P 6379

Local connection test script

<?php

try {
    $redis = new Redis(); 
    $redis - > connect ("extranet IP", 6379);
    $redis->set('tag', 'hello');
    echo 'name:' . $redis->get('tag');
    $redis->close();
} catch (\Exception $e) {
    var_dump($e->getMessage());
}