Centos7安装Redis并设置远程访问

下载安装文件

wget http://download.redis.io/releases/redis-4.0.2.tar.gz

安装编译器

yum install gcc-c++
Redis安装文件复制到/usr/local/src目录下,进入/usr/local/src目录下,解压redis安装文件
cd /usr/local/src
tar -xzvf redis-4.0.2.tar.gz
进入解压后的文件目录,之后直接编译即可
cd /usr/local/src/redis-4.0.2

make
创建存储redis文件目录,复制redis-server redis-cli到新建立的文件夹
mkdir -p /usr/local/redis
cp /usr/local/src/redis-4.0.2/src/redis-server /usr/local/redis/
cp /usr/local/src/redis-4.0.2/src/redis-cli /usr/local/redis/

设置Redis开机自动启动

进入/usr/local/src/redis-4.0.2的util目录, 执行./install_server.sh

[[email protected] 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
# 这个写你新建的那个目录的redis-server
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开启远程访问

# 查找Redis配置(注意不是安装目录下的redis.conf)
# 打开第五步设计的Redis配置,默认为:/etc/redis/6379.conf
# 修改配置文件如下几项,其它保持不变
daemonize yes
#bind 127.0.0.1 (注释,不限制IP)
protected-mode no
将 requirepass foobared前的“#”去掉,密码改为你想要设置的密码(我设置为123456)

# 重启服务
[[email protected] redis-3.2.11]# service redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...

# 开放6379端口
firewall-cmd --zone=public --add-port=6379/tcp --permanent
# 重启防火墙,否则开放端口不起作用
firewall-cmd --reload

创建redis命令软连接
ln -s /usr/local/redis/redis-cli /usr/bin/redis
# 这样就可以输入进入redis进行控制台了
[[email protected] ~]# redis
127.0.0.1:6379> auth "123456"
OK
127.0.0.1:6379>

 

关闭redis服务

service redis_6379 stop
Stopping ...
OK
(error) NOAUTH Authentication required.
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...

## 报错

解决方法:修改redis服务脚本,加入如下所示的信息即可:
vim /etc/init.d/redis_6379
# 修改 添加 -a "password"
$CLIEXEC -a "123456" -p $REDISPORT shutdown
# 关闭redis服务
[[email protected] init.d]# service redis_6379 stop
Stopping ...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Redis stopped

申明:   本站提供的一切软件、教程和内容信息仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。如果您喜欢该程序,请支持正版,购买注册,得到更好的正版服务。如有侵权请邮件[email protected]与我们联系处理。敬请谅解!