0
点赞
收藏
分享

微信扫一扫

服务器安装redis

自由情感小屋 2022-04-07 阅读 56
java

环境准备

[root@VM_0_13_centos ~]#  yum -y install gcc-c++

上传并安装redis

#创建上传文件夹
[root@VM_0_13_centos /]#  mkidr data
[root@VM_0_13_centos /]#  cd data
#创建安装文件夹
[root@VM_0_13_centos data]#  mkdir redis
[root@VM_0_13_centos data]# cd redis
#上传安装包
[root@VM_0_13_centos redis]# redis-4.0.10.tar.gz
#解压安装包
[root@VM_0_13_centos redis]#  tar -zxvf redis-4.0.10.tar.gz
[root@VM_0_13_centos redis]# redis-4.0.10.tar.gz redis-4.0.10 
#进入解压目录进行编译
[root@VM_0_13_centos redis]#  cd redis-4.0.10 
[root@VM_0_13_centos redis-4.0.10]# make PREFIX=/data/redis/redis-4.0.10 install
#指定安装地址,执行该命令后,/data/redis/redis-4.0.10路径下有一个bin文件夹
#将redis解压目录中的"redis.conf"文件拷贝到"/data/redis/bin"中,方便修改配置文件后重新加载
[root@VM_0_13_centos bin]# cd /data/redis/redis-4.0.10
[root@VM_0_13_centos redis-4.0.10]# cp redis.conf  /data/redis/redis-4.0.10/bin

绑定服务器内网ip

[root@VM_0_13_centos redis-4.0.10]# cd bin
[root@VM_0_13_centos bin]# vi redis.conf
[root@VM_0_13_centos bin]# ./redis-server  redis.conf

在这里插入图片描述

前端启动(会占用当前窗口)

[root@VM_0_13_centos bin]# ./redis-server  

后端启动(不会占用当前窗口)

[root@VM_0_13_centos bin]#vi redis.conf
[root@VM_0_13_centos bin]# ./redis-server  redis.conf

在这里插入图片描述

配置redis密码

[root@VM_0_13_centos bin]# vi redis.conf
[root@VM_0_13_centos bin]# ./redis-server  redis.conf

在这里插入图片描述

使用工具连接

[root@VM_0_13_centos bin]# vi redis.conf
[root@VM_0_13_centos bin]# ./redis-server  redis.conf

在这里插入图片描述

查看redis进程

[root@VM_0_13_centos bin]# ps aux|grep redis

关闭redis

#6379是redis的默认端口
[root@VM_0_13_centos bin]# ./redis-cli -p 6379 shutdown

进入redis的命令行

[root@VM_0_13_centos bin]# ./redis-cli

在这里插入图片描述

退出redis的命令行

127.0.0.1:6379> exit

在这里插入图片描述

配置redis操作密码

192.168.64.131:6379>config set requirepass redis

Java代码连接redis

[root@localhost redis]#  systemctl stop firewalld
[root@localhost redis]#  systemctl disable firewalld.server
import org.junit.Test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class TestRedis {
	
	
	@Test
	public void testJedisSingle(){
		//创建一个Jedis对象		
		Jedis jedis = new Jedis("192.168.163.128",6379);
		//调用Jedis对象的方法,方法名称和redis的命令一致		
		jedis.set("nama", "jedis test");		
		String string = jedis.get("name");			
		System.out.println(string);		
		//关闭Jedis		
		jedis.close();	
		}
	
	/*
	 * 使用Jedis连接池
	 */
	@Test
	public void testJedisPool(){
		//创建jedis连接池
		JedisPool pool = new JedisPool("192.168.163.128",6379);
		//从连接池中获取Jedis对象
		Jedis jedis1 = pool.getResource();
		Jedis jedis2 = pool.getResource();
		jedis1.set("age", "20");
		String age = jedis2.get("age");
		System.out.println(age);
		//关闭jedis对象
		jedis1.close();
		jedis2.close();
		//关闭连接池
		pool.close();
	}
}

redis常用命令

举报

相关推荐

0 条评论