在Ubuntu16.04版本系统下安装Redis,解决服务器外不能访问的问题

Redis是一个Key-Value内存数据库,程序员最常用的存储工具之一。介绍的文章一大堆,自行搜索查询,这里主要是记录在Ubuntu安装的记录,同样的是使用apt-get来安装。

1、更新系统环境

$ sudo apt-get update #更新软件列表
$ sudo apt-get upgrade #更新软件

2、安装Redis

$ sudo apt-get install redis-server
$ redis-server -v #查看redis版本

3、启动Redis

$ redis-server #启动服务
$ redis-cli #启动客户端

在Ubuntu16.04版本系统下安装Redis,解决服务器外不能访问的问题_第1张图片

127.0.0.1 是本机 IP ,6379 是 redis 服务端口。输入 PING 命令,返回PONG说明Redis已经安装成功了。

此时的Redis只能在本机进行连接,如果在其他地方可以连接使用需要修改配置文件。

4、修改Redis配置文件

   打开配置文件

sudo vim /etc/redis/redis.conf

#如果用vim不能打开用vi命令

sudo vi /etc/redis/redis.conf    #这里需要用户密码

主要配置信息

# 绑定ip, 如果需要远程访问, 则注释掉即可
 bind 127.0.0.1
 
# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
# 端口号, 默认是6379
port 6379
 
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
# 是否以守护进程运行, 默认是no, 推荐使用yes
daemonize yes
 
注: 守护进程(daemon)是一类在后台运行的特殊进程,用于执行特定的系统任务。
很多守护进程在系统引导的时候启动,并且一直运行直到系统关闭。另一些只在需要的时候才启动,
完成任务后就自动结束。
 
# The filename where to dump the DB
# 数据库在做持久化时存储的文件名
dbfilename dump.rdb
 
# The working directory.
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
# The Append Only File will also be created inside this directory.
# Note that you must specify a directory here, not a file name.
# 数据持久化文件存放的目录(如果目录不存在, 需要手动创建), 默认 ./ 表示当前目录
# dir ./
dir /var/lib/redis
 
# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
# 日志文件存放目录, 默认没有
# logfile ""
logfile "/var/log/redis/redis-server.log"
 
# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT  where
# dbid is a number between 0 and 'databases'-1
# 数据库数量, 默认16个, 编号0-15
databases 16
 
 
# Master-Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
# 1) Redis replication is asynchronous, but you can configure a master to
#    stop accepting writes if it appears to be not connected with at least
#    a given number of slaves.
# 2) Redis slaves are able to perform a partial resynchronization with the
#    master if the replication link is lost for a relatively small amount of
#    time. You may want to configure the replication backlog size (see the next
#    sections of this file) with a sensible value depending on your needs.
# 3) Replication is automatic and does not need user intervention. After a
#    network partition slaves automatically try to reconnect to masters
#    and resynchronize with them.
# 主从配置, 默认不需要配置
# slaveof  

保存之后重启Redis再测试一下就可以连接到Redis了。

你可能感兴趣的:(在Ubuntu16.04版本系统下安装Redis,解决服务器外不能访问的问题)