CentOS 6.6下Redis安装
安装redis的过程非常的简单,具体参考教程官网:http://redis.io/download
1、下载并安装
下载的redis-3.2.5.tar.gz安装包默认在/usr/local/src/目录下
$> wget http://download.redis.io/releases/redis-3.2.5.tar.gz #下载redis安装包
$> tar xzf redis-3.2..tar.gz #解压到当前目录
$> mv redis-3.2.5 /usr/local/redis #移动解压文件夹到上层的redis目录
$> cd /usr/local/redis
$> make #编译
2、默认启动
$> cd /usr/local/redis
$> src/redis-server
redis默认的是6379端口
$> netstat -ntpl #查看redis是否启动
上面这种启动没有指定配置文件,此时redis使用默认的内置配置。
不过在正式环境中,常常通过指定配置文件(通常叫redis.conf)来配置redis.
3、使用配置启动
在/usr/local/redis/目录下我们可以看见一个redis.conf文件,新建一个config目录并复制一份配置文件到里面。

打开/usr/local/redis/config/redis.conf文件,修改端口6379为6380。
# Accept connections on the specified port, default is (IANA #).
# If port is specified Redis will not listen on a TCP socket.
port
然后用下面的方法启动。
$> cd /usr/local/redis
$> src/redis-server ./config/redis.conf
#查看redis启动情况
$> netstat -ntpl

可以发现现在启动的redis有两个,一个端口是6379是先前用默认启动的,一个端口是6380是指定配置文件启动的 .
4、命令操作
先看一些命令
redis> keys * //查看所有的key
redis> dbsize //当前数据库中key的数量
redis> info //服务器基本信息
redis> config get parameter //获取服务器的参数配置 parameter替换具体参数
redis> flushdb //清空当前数据库
redis> flushall //清除所有数据库
redis> exists larry //当前的key是否存在
(integer)
redis> del lv //删除当前key
下面我们使用redis自带的redis-cli登录进行具体操作:
$> cd /usr/local/redis #使用redis-cli登录默认端口的redis
$> src/redis-cli
127.0.0.1:> config get port #查看配置的端口
)"port"
)""
127.0.0.1:> set foo bar #设置一个key-value
OK
127.0.0.1:> get foo "bar" #查看key对应的value
127.0.0.1:> exit #退出 #使用redis-cli登录指定端口的redis
$> src/redis-cli -h 127.0.0.1 -p
127.0.0.1:>config get requirepass #查看配置的密码,默认密码是空的
)"requirepass"
)""
127.0.0.1:>keys *
(empty list or set) #还没键值对
127.0.0.1:> exit #退出
5、停止redis
$> cd /usr/local/redis
$> src/redis-cli shutdown #不指定端口,默认停止6379端口的redis #指定端口停止
$> src/redis-cli -p shutdown
6、配置认证密码
找到redis的配置文件/usr/local/redis/config/redis.conf,配置信息都在里面。
#打开配置文件找到
#requirepass foobared
去掉行前的注释,并修改密码为所需的密码,保存文件
requirepass test123
按照上面的指定配置文件的方法启动redis。然后使用redis-cli尝试登陆,发现可以登上,但是执行具体命令是提示操作不允许。
$> src/redis-cli -h 127.0.0.1 -p
127.0.0.1:> keys *
(error) NOAUTH Authentication required. #提示无权操作
127.0.0.1:> auth test123 #验证密码
OK
127.0.0.1:> keys *
(empty list or set)
127.0.0.1:> set name jerry
OK
127.0.0.1:> get name
"jerry"
127.0.0.1:> exit ##########################################
##本地的话 -h 127.0.0.1可以省略
##可以加上-a 密码 直接进去,
$> src/redis-cli -p 6380 -a test123
127.0.0.1:6380>
如果redis一开始就是启动状态,如何不重启服务设置密码呢?
可以使用命令的方式设置密码,接着上面用6380端口启动的redis修改密码做例子
# src/redis-cli -p
127.0.0.1:> auth test123 #验证身份
OK
127.0.0.1:> config set requirepass abc456 #重新设置密码
OK
127.0.0.1:> config get requirepass #查看现在的密码,发现已经修改了
) "requirepass"
) "abc456"
127.0.0.1:> exit
我们到/usr/local/redis/config/redis.conf配置文件中查看requirepass配置发现还是先前设置的test123 ,
也就是说命令设置密码没在磁盘上持久化,我们停掉6380端口的redis再重启一下验证看看
$> cd /usr/local/redis
$> src/redis-cli -p
127.0.0.1:> auth abc456
OK
127.0.0.1:> shutdown
not connected> exit
#这里可以输入netstat -nptl 命令确认一下6380端口的redis是否停止了 $> src/redis-server ./config/redis.conf #启动redis #重新开一个命令窗口
$> cd /usr/local/redis
$> src/redis-cli - 6380
127.0.0.1:6380> keys *
(error) NOAUTH Authentication required.
127.0.0.1:6380> auth abc456 #验证命令修改的密码
(error) ERR invalid password #密码无效
127.0.0.1:6380> auth test123 #验证开始设置的配置文件中的密码
OK #验证通过
127.0.0.1:6380> keys *
1) "name"
127.0.0.1:6380> exit
所以,想要持久化密码还是直接修改redis.conf文件。如果想不重启有想持久化,那么就两个地方都修改,再下次redis启动的时候生效的密码也是正确的。
7、配置外网可访问
redis采用的安全策略,默认会只准许本地访问。
查看 /usr/local/redis/config/redis.conf 配置文件。
################################## NETWORK ##################################### # By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 lookback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#bind 127.0.0.1 #这里注释掉这一行配置
注释掉所有的bind配置。
然后修改 Linux 的防火墙(iptables),开启你的redis服务端口,我们这里用6380
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport -j ACCEPT
重启redis。
这样我们就可以外网访问redis了。
8、配置服务(service)
其实redis提供了配置服务的脚本,在redis源码目录下,utils/redis_init_script文件就是。
#拷贝服务脚本文件到目录/etc/init.d目录下,并重名为redis
$>cp /usr/local/redis/utils/redis_init_script /etc/init.d/redis
由于原脚本加入服务运行会有些问题,所有我们修改 /etc/init.d/redis 脚本,下面标红的为添加或改动的内容
#!/bin/sh
#
# chkconfig: 2345 90 10
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
#服务端所处位置,在make install后默认存放‘/usr/local/bin/redis-server’,我们没有make install所以要修改该路径
REDISPORT=
EXEC=/usr/local/redis/src/redis-server
CLIEXEC=/usr/local/redis/src/redis-cli PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"
PWD="test123" #配置密码(如果redis设置了密码的话) case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT -a $PWD shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep
done
echo "Redis stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac
说明:
(1) #chkconfig: 2345 90 10
2345表示系统运行级别是2,3,4或者5时都启动此服务,90表示启动的优先级,10表示关闭的优先级。
(2) $CLIEXEC -p $REDISPORT -a $PWD shutdown
停止redis服务的命令,如果没有密码就去掉 -a #PWD
(3) CONF="/etc/redis/${REDISPORT}.conf"
这个是redis的配置文件位置,现在的配置文件在/usr/local/redis/redis.conf,所以我们修改一下
#在/etc目录下新建redis目录
$>mkdir /etc/redis
#拷贝原配置文件到/etc/redis目录下并重命名6379.conf
$>cp /usr/local/redis/redis.conf /etc/redis/6379.conf
6379.conf就我们要使用的redis配置文件,相关配置我们可以修改它。
redis配置文件参数说明,参考:http://www.cnblogs.com/qq78292959/p/3331032.html
好了,下面把redis加入服务,执行下面的命令:
#注册服务
$> chkconfig --add redis
#设置为开机自启动服务
$> chkconfig redis on #打开redis服务
$> service redis start #关闭redis服务
$> service redis stop
CentOS 6.6下Redis安装的更多相关文章
- CentOS 6.6下Redis安装配置记录
转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/120.html?1455855209 在先前的文章中介绍过redis,以下 ...
- CentOS 6.5下Redis安装记录
Redis简介: Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发工 ...
- CentOS 6.5下Redis安装详细步骤
Redis简介: Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发工 ...
- (转)CentOS 6.5下Redis安装详细步骤
Redis简介:Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发工作 ...
- CentOS 6.5下Redis安装测试
NoSQL之Redis - CentOS 6.5安装测试 1.下载redis 可以在线安装或者下载 redis ①在线安装前需要检测是否存在rpm包不存在的话查看yum在线是否存在rpm包不存在的话就 ...
- CentOS 下 redis 安装与配置
CentOS 下 redis 安装与配置 1.到官网上找到合适版本下载解压安装 [root@java src]# wget -c http://redis.googlecode.com/files ...
- 不要着急改代码,先想想--centos 6.8下编译安装tmux
诸位读者新年好,2017开年第一篇博客,请允许我先问候一下看到这篇博客的诸位.写博客是我2017年定下的目标之一,希望我会坚持下去. 最近打算尝试一下tmux这个神器,于是有了这一篇关于思维方式的Bl ...
- Redis、Redis+sentinel安装(Ubuntu 14.04下Redis安装及简单测试)
Ubuntu下Redis安装两种安装方式: 1.apt-get方式 步骤: 以root权限登录,切换到/usr目录下. 接下来输入命令,apt-get install redis-server,如图: ...
- ZeroMQ 在 centos 6.5_x86_64 下的安装
ZeroMQ 在 centos 6.5_x86_64 下的安装 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs 一.ZeroMQ介绍 ZeroMQ是一个开 ...
随机推荐
- JSF 2 checkboxes example
In JSF, <h:selectBooleanCheckbox /> tag is used to render a single HTML input element of " ...
- AutoCAD.NET二次开发:扩展数据之XData
结果缓存——ResultBuffer 结果缓存即 Autodesk.AutoCAD.DatabaseServices.ResultBuffer 类型,使用 ResultBuffer 对象时需要提供一个 ...
- CSS 酷站
http://mikkelbang.com/#!videos
- TextKit学习(三)NSTextStorage,NSLayoutManager,NSTextContainer和UITextView
先上一张图: 这是使用UITextView时用到的iOS7新增加的类:NSTextContainer.NSLayoutManager.NSTextStorage及其相互关系: 这三个新出的类还没有在官 ...
- Js面向对象和数据类型内存分配(转)
一 Js基本数据类型以及内存情况 1 Undefined Undefined类型只有一个值undefined,在使用了声明但未初始化的变量的时候,这个变量值就是undefined 1 var hi; ...
- pygame简单动态图 & 动态图片的移动
之前在学pygame 时看了一些博客(来自http://eyehere.net/2011/python-pygame-novice-professional-plant-zombie-1/),觉得写得 ...
- Android内存优化(使用SparseArray和ArrayMap代替HashMap)
在Android开发时,我们使用的大部分都是Java的api,比如HashMap这个api,使用率非常高,但是对于Android这种对内存非常敏感的移动平台,很多时候使用一些java的api并不能达到 ...
- [置顶] Quartz的DateBuilder详解
DateBuilder类有两个方法: nextGivenMinuteDate和nextGivenSecondDate: Method: (a) public static Date nextG ...
- 教你50招提升ASP.NET性能(二十三):StringBuilder不适用于所有字符串连接的场景;String.Join可能是
(41)StringBuilder is NOT the answer for all string concatenation scenarios; String.Join could be 招数4 ...
- Invoke-Express 执行多个批处理命令的函数
function Mult_ping ($ips) { # $cmdline = "PIng" foreach ($ip in $ips) { $cmdline = "p ...
