Redis Quick Start (1): Installing Redis in Linux Environment

1. Redis overview

1.1.Redis concept

Redis is an open source log-based, Key-Value database written in ANSI C language, supporting the network, memory-based or persistent, and providing APIs in multiple languages. From March 15, 2010, the development of Redis is hosted by VMware. Since May 2013, the development of Redis has been sponsored by Pivotal.

Insert picture description here

1.2. Application in the enterprise

Insert picture description here

The story of Sina Weibo relationship service and Redis: When
Weibo was just launched in 2009, the Weibo relationship service used the most traditional Memcache+Mysql solution;
Redis cache database was introduced in 2011;
Sina Weibo is the largest in history so far
The data of Redis Cluster Company as of 16 years is as follows :
500 billion Read/day
50 billion Write/day
18TB+ Memory

1.3. Job requirements

Insert picture description here

2.Redis quick start

2.1.Windows download and install

https://github.com/MicrosoftArchive/redis/releases

2.2.Linux installation
2.2.1. Preparation before installation

2.2.1.1. Ensure the normal operation of the Linux environment

Personal PC, VMWare or VirtualBox can be used to install virtual machine, operating system, it is recommended to install CentOS7.x version

2.2.1.2. To download Redis,
you can download related packages from the Redis official website https://redis.io/. It is recommended to download the 4.x commercial version and
Insert picture description here
2.2.1.3. Upload the Redis installation package to Linux

rz, sz command
ftp tool, for example: flashfxp

2.2.1.4. Enter the relevant directory and execute the command

	tar -zxvf redis-4.0.14.tar.gz

Insert picture description here
2.2.1.5. Install related gcc dependencies

	yum install gcc

When encountering a choice, just enter y

2.2.1.6. Enter the relevant directory

	cd redis-4.0.14

2.2.1.7. Execute the installation command

	# 编译
	make
	# 安装到/usr/local/redis目录下
	make PREFIX=/usr/local/redis install
	# 拷贝配置文件
	mkdir /usr/local/redis/etc/
	cp redis.conf /usr/local/redis/etc/
2.2.2. Start Redis

At this point Redis has been installed, but in order to make Redis more in line with our daily usage habits, some configurations need to be modified.

1. Let Redis be started in the background and modify the configuration file

	1) 进入安装目录:

yyyy

	2) 修改redis.conf配置文件:
		vim ./etc/redis.conf

Modify the following:

daemonize yes #后台启动

#bind 127.0.0.1 
#Redis的IP,可以在前面加#注释或者改为虚拟机相关IP,例如:bind 192.168.48.20 

protected-mode no #取消保护模式

Save and exit:

:wq

2. Start Redis

./bin/redis-server ./etc/redis.conf

Insert picture description here

2.2.3. Access Redis
#如果没有设置Redis的安全性,IP和端口,可以使用默认配置直接登陆
#如果设置,需要追加 -h IP -p 端口
./bin/redis-cli

Insert picture description here

2.2.4. Optimization operation

Because there is no configuration of related environment variables, it is necessary to enter the relevant directory or enter the full path of the redis-related startup command to start and access Redis, and the operating experience is poor.

#进入相关目录
cd /usr/local/redis
#启动Redis
./bin/redis-server ./etc/redis.conf

Insert picture description here

In order to make Redis run everywhere in the Linux environment like the ls command, you need to modify the environment variables:

  1. Enter user directory
cd ~
  1. View all files
ls -la
  1. Modify the configuration file
vim .bash_profile

Modify the content of PATH and add the following:

Insert picture description here

After modification, :wq, save and exit.

Let environment variables take effect

source .bash_profile

At this point, in Linux, we only need to enter Redis related commands to start Redis and log in to Redis.
Insert picture description here

2.2.5. Stop Redis

1. Use redis-cli to close redis

#如果没有设置Redis的安全性,IP和端口,可以使用默认配置直接退出
#如果设置,需要追加 -h IP -p 端口
#redis-cli -h 192.168.48.20 -p 6379 shutdown
redis-cli shutdown

2. If the command cannot be closed, use kill -9 to kill the redis process ID

#查看进程ID
ps -ef | grep redis
#执行kill -9 命令
#kill -9 1512

Insert picture description here

Note: Forcibly ending the program, using the pid of the kill -9 process, forcibly terminating the Redis process may result in the loss of redis persistence.

2.2.6. Firewall settings

During development, in order to prevent the network from being blocked and unable to access Redis, please turn off the CentOS firewall of the machine first.

  1. View firewall status
systemctl status firewalld.service

active (running) means the firewall is active
Insert picture description here

  1. Stop firewall
systemctl stop firewalld.service

At this time, check the firewall status, it is inactive (dead) that the firewall process is in an inactive state, and the service is stopped successfully.

Insert picture description here

Of course, for convenience, you don't need to disable the firewall every time you restart CentOS, you can disable the firewall from starting automatically.

systemctl disable firewalld.service

Insert picture description here

Note: The configuration of the firewall is recommended to be changed only in the environment of the local virtual machine.

2.3. Command line use

Insert picture description here

2.4. Java Client Jedis

* Jedis: 一款java操作redis数据库的工具.
	* 使用步骤:
		1. 下载jedis的jar包
		2. 使用
			//1. 获取连接
    		Jedis jedis = new Jedis("localhost",6379);
   			//2. 操作
   			jedis.set("username","zhangsan");
    		//3. 关闭连接
    		jedis.close();

Insert picture description here
Insert picture description here

Next chapter: Redis quick start (two): 5 commonly used data structures

Guess you like

Origin blog.csdn.net/weixin_46822085/article/details/109295784