redis踩坑之旅

redis安装

安装步骤

1.将redis安装到ubuntu-1 192.168.234.131中

这里我使用的是ubantu图形化界面的linux服务器,ip为192.168.234.131

2.移动到文件夹目录/opt/module/redis-6.2.1

3.解压redis

tar -zxvf redis-6.2.1

4.进入redis目录,执行make

由于gcc问题报错,adlist.c:32:20: fatal error: stdlib.h: No such file or directory


image.png

这里经过查询与验证,是gcc源的问题,解决方式如下:

4.1 执行命令卸载之前下载的gcc

apt remove gcc --卸载gcc
cd /etc/apt --修改sources.list 换成外国的源deb
sudo vi sources.list

4.2 将etc/apt 下的source.list文件修改为如下源:

vi source.list

# deb cdrom:[Ubuntu 18.04.3 LTS _Bionic Beaver_ - Release amd64 (20190805)]/ bionic main restricted

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://cn.archive.ubuntu.com/ubuntu/ bionic main restricted
# deb-src http://cn.archive.ubuntu.com/ubuntu/ bionic main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://cn.archive.ubuntu.com/ubuntu/ bionic-updates main restricted
# deb-src http://cn.archive.ubuntu.com/ubuntu/ bionic-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://cn.archive.ubuntu.com/ubuntu/ bionic universe
# deb-src http://cn.archive.ubuntu.com/ubuntu/ bionic universe
deb http://cn.archive.ubuntu.com/ubuntu/ bionic-updates universe
# deb-src http://cn.archive.ubuntu.com/ubuntu/ bionic-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu 
## team, and may not be under a free licence. Please satisfy yourself as to 
## your rights to use the software. Also, please note that software in 
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://cn.archive.ubuntu.com/ubuntu/ bionic multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu/ bionic multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ bionic-updates multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu/ bionic-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://cn.archive.ubuntu.com/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu/ bionic-backports main restricted universe multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu bionic partner
# deb-src http://archive.canonical.com/ubuntu bionic partner

deb http://security.ubuntu.com/ubuntu bionic-security main restricted
# deb-src http://security.ubuntu.com/ubuntu bionic-security main restricted
deb http://security.ubuntu.com/ubuntu bionic-security universe
# deb-src http://security.ubuntu.com/ubuntu bionic-security universe
deb http://security.ubuntu.com/ubuntu bionic-security multiverse
# deb-src http://security.ubuntu.com/ubuntu bionic-security multiverse

4.3 更新源,重新安装gcc

sudo apt-get update
sudo apt-get upgrade
sudo apt install gcc

4.4 执行make命令
此时又出现错误了


image.png
fatal error: stdlib.h: No such file or directory
 #include 

cpp的东西还是有点搞不懂

5 改变安装方式

被搞得有点晕,换一种安装方式,按照redis的官方文档,发现Ubuntu安装方式早有说明

From the official Ubuntu PPA
You can install the latest stable version of Redis from the redislabs/redis package repository. Add the repository to the apt index, update it and install:

$ sudo add-apt-repository ppa:redislabs/redis
$ sudo apt-get update
$ sudo apt-get install redis

这样子就安装完成了
查看redis的占用状态

ps -ef |grep redis

linux 查询端口占用
netstat -anp |grep 端口号

6 测试连接

这时用代码测试一下是否可以连接成功,发现报如下错误:

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 lookback interface.

这是由于redis的配置问题,我们做如下修改

6.1 首先找到redis的位置

whereis redis
image.png

6.2 进入目录

cd /etc/redis
vi redis.conf

做如下操作

1.把下面对应的注释掉
# bind 127.0.0.1 
2.Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程,设置为no
daemonize no
3.保护模式关闭
protected-mode no 

6.3 启动redis

redis-server redis.conf

7 测试连接

  Jedis jedis = new Jedis("192.168.234.131");
        System.out.println("连接成功");
        //查看服务是否运行
        System.out.println("服务正在运行: "+jedis.ping());
        //存储数据到列表中
        jedis.lpush("site-list", "Runoob");
        jedis.lpush("site-list", "Google");
        jedis.lpush("site-list", "Taobao");
        // 获取存储的数据并输出
        List list = jedis.lrange("site-list", 0 ,0);
        for(int i=0; i keys = jedis.keys("*");
        Iterator it=keys.iterator() ;
        while(it.hasNext()){
            String key = it.next();
            System.out.println(key);
        }

结果:

连接成功
服务正在运行: PONG
列表项为: Taobao
site-list

下面就是集群的创建了,下个篇幅再继续踩坑

你可能感兴趣的:(redis踩坑之旅)