Linux服务器安装Redis数据库

Linux服务器安装Redis数据库

1.下载redis并安装

  在Linux服务器上,命令行执行以下命令(cd ./usr local/src 一般源码放在这里(推荐源码安装))

wget http://download.redis.io/releases/redis-3.0.7.tar.gz
tar xzf redis-3.0.7.tar.gz
cd redis-3.0.7
make
make all
make install
  说明:

  wget:下载远程文件到本地当前文件夹中。

  tar xzf :解压文件到当前文件夹目录

  cd:进入文件夹

  make make all make install:安装redis

  执行完,进入目录/usr/local/bin  多了几个文件:

redis-server:Redis服务器的daemon启动程序
redis-cli:Redis命令行操作工具。当然,你也可以用telnet根据其纯文本协议来操作
redis-benchmark:Redis性能测试工具,测试Redis在你的系统及你的配置下的读写性能
redis-stat:Redis状态检测工具,可以检测Redis当前状态参数及延迟状况

具体过程:

# pwd
/usr/local/perry
[root@cloud perry]# wget http://download.redis.io/releases/redis-3.0.7.tar.gz
-bash: wget: command not found -->wget 命令不识别 安装即可
[root@cloud perry]# yum -y install wget
...
Installed:
  wget.x86_64 0:1.12-10.el6                                                                         

Complete!
# wget http://download.redis.io/releases/redis-3.0.7.tar.gz
--2017-09-09 17:24:34--  http://download.redis.io/releases/redis-3.0.7.tar.gz
Resolving download.redis.io... 109.74.203.151
Connecting to download.redis.io|109.74.203.151|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1375200 (1.3M) [application/x-gzip]
Saving to: “redis-3.0.7.tar.gz”

100%[==========================================================>] 1,375,200    561K/s   in 2.4s    

2017-09-09 17:24:38 (561 KB/s) - “redis-3.0.7.tar.gz” saved [1375200/1375200]

[root@cloud perry]# ls
apache-tomcat-8.5.20.tar.gz  redis-3.0.7.tar.gz  tomcat

至此已经下载完成了,直接安装

# ls
apache-tomcat-8.5.20.tar.gz  redis-3.0.7  redis-3.0.7.tar.gz  tomcat
[root@cloud perry]# cd redis-3.0.7
[root@cloud redis-3.0.7]# ls
00-RELEASENOTES  COPYING  Makefile   redis.conf       runtest-sentinel  tests
BUGS             deps     MANIFESTO  runtest          sentinel.conf     utils
CONTRIBUTING     INSTALL  README     runtest-cluster  src
[root@cloud redis-3.0.7]# make
....

Hint: It's a good idea to run 'make test' ;)

make[1]: Leaving directory `/usr/local/perry/redis-3.0.7/src'
# make all
cd src && make all
make[1]: Entering directory `/usr/local/perry/redis-3.0.7/src'

Hint: It's a good idea to run 'make test' ;)

make[1]: Leaving directory `/usr/local/perry/redis-3.0.7/src'

redis-3.0.7]# make install
cd src && make install
make[1]: Entering directory `/usr/local/perry/redis-3.0.7/src'

Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: Leaving directory `/usr/local/perry/redis-3.0.7/src'
[root@cloud redis-3.0.7]# cd /usr/local/bin 
[root@cloud bin]# ls
crc32      parl     par.pl  redis-benchmark  redis-check-dump  redis-sentinel  scandeps.pl
instmodsh  parldyn  pp      redis-check-aof  redis-cli         redis-server    tkpp

redis源码位置

# cd /usr/local/perry/redis-3.0.7
[root@cloud redis-3.0.7]# pwd
/usr/local/perry/redis-3.0.7
[root@cloud redis-3.0.7]# ls
00-RELEASENOTES  COPYING  Makefile   redis.conf       runtest-sentinel  tests
BUGS             deps     MANIFESTO  runtest          sentinel.conf     utils
CONTRIBUTING     INSTALL  README     runtest-cluster  src

2.配置redis

# pwd
/usr/local/perry/redis-3.0.7
# vi redis.conf
# bind 127.0.0.1  # 注释掉绑定IP,可以允许远程访问或自行绑定固定IP
daemonize yes  #后台启动
requirepass 密码 #密码,尽可能设置复杂,容易被程序多次试错破解

2.1 在Linux中开放防火墙的6379端口

​ 打开配置文件

# vi /etc/sysconfig/iptables

​ 添加下面一行代码到文件中去 (:wq保存退出)

-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT  -->已经存在Port22
-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT --->下面加一个86379

​ 重启防火墙

service iptables restart 

3.启动停止Redis

通过配置文件启动->查看redis进程是否在运行—>看端口6379监听情况—>启动redis命令行客户端

# redis-server /usr/local/perry/redis-3.0.7/redis.conf
[root@cloud ~]# ps -ef |grep redis
root     28139     1  0 17:55 ?        00:00:00 redis-server *:6379                                 
root     28150 24866  0 17:55 pts/0    00:00:00 grep redis
[root@cloud ~]# 
[root@cloud ~]# netstat -lntp | grep 6379
tcp        0      0 0.0.0.0:6379                0.0.0.0:*                   LISTEN      28139/redis-server  
tcp        0      0 :::6379                     :::*                        LISTEN      28139/redis-server  
[root@cloud ~]# ./redis-cli -a 密码
-bash: ./redis-cli: No such file or directory
[root@cloud ~]# redis-cli
127.0.0.1:6379> set key 999999

关闭redis(无密码的redis可以直接关闭,那种一般都是绑定本地的,安全性高)

redis-cli -a 密码 -h 127.0.0.1 -p 6379 shutdown

补充:

如果是用apt-get或者yum install安装的redis,可以直接通过下面的命令停止/启动/重启redis

/etc/init.d/redis-server stop
/etc/init.d/redis-server start
/etc/init.d/redis-server restart
如果是通过源码安装的redis,则可以通过redis的客户端程序redis-cli的shutdown命令来重启redis

redis-cli -h 127.0.0.1 -p 6379 shutdown
如果上述方式都没有成功停止redis,则可以使用终极武器 kill -9

4.开机启动Redis

​ 4.1修改redis.conf,打开后台运行选项

daemonize yes

​ 4.2 编写脚本,vim /etc/init.d/redis:

# vi /etc/init.d/redis

脚本内容:注意

下面面的注释的意思是,redis服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10。不能更改

# chkconfig:   2345 90 10
# description:  Redis is a persistent key-value database

PATH=/usr/local/bin:/sbin:/usr/bin:/bin

REDISPORT=6379 #实际环境而定
PWD=密码 #密码,有就设置,没有不需要
EXEC=/usr/local/perry/redis-3.0.7/src/redis-server #实际环境而定,安装包的位置找
REDIS_CLI=/usr/local/perry/redis-3.0.7/src/redis-cli #实际环境而定

PIDFILE=/var/run/redis.pid
CONF="/usr/local/perry/redis-3.0.7/redis.conf" #实际环境而定

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
                if [ "$?"="0" ]
                then
                        echo "Redis is running..."
                fi
                ;;
        stop)
                if [ ! -f $PIDFILE ]
                then
                        echo "$PIDFILE exists, process is not running."
                else
                        PID=$(cat $PIDFILE)
                        echo "Stopping...use password...."
                        # 这里没有密码的可以去掉-a $PWD,设置了密码的要加上,不然无法stop
                        $REDIS_CLI -a $PWD -p $REDISPORT SHUTDOWN 
                        while [ -x $PIDFILE ]
                        do
                                echo "Waiting for Redis to shutdown..."
                                sleep 1
                        done
                        echo "Redis stopped"
                fi
                ;;
        restart|force-reload)
                ${0} stop
                ${0} start
                ;;
        *)
                echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
                exit 1
esac

​ 4.3 执行权限

# chmod +x /etc/init.d/redis

​ 4.5开机启动

# chkconfig redis on

4.6服务启动redis

# service redis start
Starting Redis server...
Redis is running...
# service redis stop
Stopping...
Redis stopped

参考文档

Linux服务器上Redis安装和配置

开机启动设置报错

Centos开机自启动redis

你可能感兴趣的:(Linux服务器安装Redis数据库)