linux centos7局域网配置redis,并实现局域网内远程连接


# 编译安装redis
$ cd /usr/local/src
$ wget http://download.redis.io/releases/redis-5.0.4.tar.gz
$ tar -zxvf redis-5.0.4 && cd redis-5.0.4

# 编译时添加参数防止Linux下make报错
$ make MALLOC=libc

# 安装TCL
$ cd src && make test
You need tcl 8.5 or newer in order to run the Redis test
$ yum install -y tcl

# 创建redis文件夹并将源文件中的服务器和客户端移入
$ mkdir -p /usr/local/redis
$ cp /usr/local/src/redis-5.0.4/src/redis-server /usr/local/redis/
$ cp /usr/local/src/redis-5.0.4/src/redis-cli /usr/local/redis/

# 设置Redis开机自启动
$ cd /usr/local/src/redis-5.0.4/utils
$ ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [] /usr/local/redis/redis-server
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/redis/redis-server
Cli Executable : /usr/local/redis/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!

# 查看redis进程
$ ps -ef | grep redis

# 修改redis配置
$ whereis redis
$ vim /etc/redis/6379.conf
#找到并注释掉 bind 127.0.0.1  
#bind 127.0.0.1 #限制IP
daemonize yes # 以后台守护进程方式运行
protected-mode no
requirepass 123456 # 设置密码

# 重启redis服务器
$ service redis_6379 restart

# 端口添加防火墙
$ firewall-cmd --zone=public --add-port=6379/tcp --permanent
# 重启防火墙
$ firewall--cmd --reload

# 建立redis客户端命令的软连接
$ ln -s /usr/local/redis/redis-cli /usr/bin/redis
$ redis
127.0.0.1:6379> AUTH 123456

# 开启关闭redis服务
$ service redis_6379 start
$ service redis_6379 restart
$ service redis_6379 stop

起初没自由注释掉bind 127.0.0.1 而是更改为别的主机ip了 死活连不上。后来发现https://www.jianshu.com/p/698b2fd3b5b1总说应该注释掉。结果ok了。

强调

#找到并注释掉 bind 127.0.0.1  
#bind 127.0.0.1 #限制IP

环境:

window10

redis 5.0.4

自带虚拟机:Hyper-v

系统:CentOS7

猜你喜欢

转载自blog.csdn.net/CaptainJava/article/details/89856989