CenOS7搭建Redis-3.2.9及整合Jedis

1.上传安装包到CentOS

上传的方法有很多种,我这里采用的是ftp上传。如果想搭建ftp的可以参考我的另一篇博文:
CentOS7 搭建vsftpd详细教程
通过ftp我可以很方便的上传文件:
这里写图片描述

2.解压redis-3.2.9.tar.gz

tar -zxvf redis-3.2.9.tar.gz

[root@localhost redis]# ls
redis-3.2.9  redis-3.2.9.tar.gz
[root@localhost redis]# cd redis-3.2.9

[root@localhost redis-3.2.9]# ls
00-RELEASENOTES  COPYING  Makefile   redis.conf       runtest-sentinel  tests
BUGS             deps     MANIFESTO  runtest          sentinel.conf     utils
CONTRIBUTING     INSTALL  README.md  runtest-cluster  src

3.安装

编译的时候需要gcc
没果没有的话安装:

yum -y install gcc-c++

进入刚刚解压的目录:

cd redis-3.2.9
make
make PREFIX=/redis/6379 install #这里是指定redis的安装目录

把redis.conf文件复制到安装目录

 cp /usr/yong.cao/dev/redis/redis-3.2.9/redis.conf /redis/6379/ #这是我的目录,请自定义修改

4.配置Redis

4.1 修改成后台进程启动

修改刚刚复制过去的配置文件:

[root@localhost redis-3.2.9]# cd /redis/6379/
[root@localhost 6379]# vim redis.conf
daemonize yes

4.2 注释bind 127.0.0.1

band localhost 只能本机访问,局域网内计算机不能访问,这个配置要注意

# bind 127.0.0.1

4.3 取消保存模式

protected-mode no

这个地方是个大坑,如果不改很可能导致客户端连接不上:

redis.clients.jedis.exceptions.JedisDataException: 
DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, 
no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. 
If you want to connect from external computers to Redis you may adopt one of the following solutions: 

1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by 
connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly 
accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 

2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the 
protected mode option to 'no', and then restarting the server. 

3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 

4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order 
for the server to start accepting connections from the outside.

4.5 设置防火墙

当然没有开启防火墙的可以忽略这一步。

[root@localhost 6379]# firewall-cmd --zone=public --list-ports #查看开放的端口
20880/tcp 80/tcp 2181/tcp 22122/tcp 23000/tcp 9999/tcp

firewall-cmd --zone=public --add-port=6379/tcp --permanent #添加redis的端口

firewall-cmd --complete-reload #便其生效

5.设置Redis开机启动

系统开机启动时会去加载/etc/init.d/下面的脚本,每个脚本文件会自定义实现程序的启动。新的程序开机启动,只需在该目录下添加一个自定义启动程序的脚本,然后设置相应规则即可。
如在这里我们在/etc/init.d/下新建一个 redis 的脚本,开机启动时会去加载执行该脚本。
新建redis自启动文件:

vim /etc/init.d/redis

复制如下内容:

#!/bin/sh
# chkconfig: 2345 10 90
# description: Start and Stop redis
#redis服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10

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

#端口号,这是默认的,不是默认端口号,需要修改
REDISPORT=6379
#redis-server启动位置
EXEC=/redis/6379/bin/redis-server
#redis-cli客户端启动位置
REDIS_CLI=/redis/6379/bin/redis-cli

#在redis.conf中可以找到
PIDFILE=/var/run/redis_6379.pid
#redis.conf配置文件的位置
CONF="/redis/6379/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..."
                       $REDIS_CLI -p $REDISPORT  SHUTDOWN
                        sleep 2
                       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

设置执行权限:

chmod 755 redis

设置开机启动:

chkconfig redis on

测试启动:

[root@localhost 6379]# service redis start
Starting Redis server...
Redis is running...

如果到这一步了,开机仍然不能启动,可以试试下面这种方法:

vim /etc/rc.d/rc.local

在rc.local中加入service redis start

不能开机启动的原因,一般是redis自启动文件有误,仔细检查。在文件开始一定要加上#!/bin/sh,实测CentOS 7.3.1611不加不能启动。

6.启动Redis

手动启动方法:

./bin/redis-server redis.conf    #到/redis/6379目录下启动redis
./bin/redis-cli    #启动redis客户端

查看进程:

[root@localhost 6379]# ps -aux | grep redis
root       2384  0.1  0.4 136920  2368 ?        Ssl  Jun30   0:03 ./bin/redis-server *:6379
root       2650  0.0  0.1 112648   964 pts/0    R+   00:33   0:00 grep --color=auto redis

测试redis:

#redis设置值
127.0.0.1:6379> set test 1000

#redis获取值
127.0.0.1:6379> get test 
"1000"

#redis增加值
127.0.0.1:6379> incr test 
(integer) 1001

#redis获取值
127.0.0.1:6379> get test 
"1001"

#redis减值
127.0.0.1:6379> decr test 
(integer) 1000

#redis指定增加值
127.0.0.1:6379> incrby test 5
(integer) 1005

7.Jedis连接测试

项目中引入Jedis的包:

这里写图片描述

测试代码:

public class TestRedis {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.128.128",6379);
        Long pno = jedis.incr("test");
        System.out.println(String.valueOf(pno));
        jedis.close();
    }
}

成功:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/m0_37797991/article/details/74020517
今日推荐