Objective
Redis is a common memory-based Key-value database that is more advanced than memcache and supports a variety of data structures, efficiently and quickly. Redis makes it easy to solve high-concurrency data access problems, and it's good to handle real-time monitoring signals.
Environment
Ubuntu 18.04
Installing the Redis server side
~ sudo apt-get Install Redis-server
After the installation is complete, the Redis server starts automatically and we check the Redis server program
Check Redis Server System processes
~ Ps-agx|grep Redis
1 10382 10382 10382? -1 SSL 124 0:07/usr/bin/redis-server 127.0.0.1:6379
2677 10618 10617 2677 pts/0 10617 s+ $0:00 grep--color=auto Redis
Check Redis server Status by starting command
~ Netstat-nlt|grep 6379
TCP 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN
TCP6 0 0:: 1:6379:::* LISTEN
Check Redis server Status by starting command
~$ sudo/etc/init.d/redis-server Status
redis-server.service-advanced Key-value Store
Loaded:loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset:enabled)
Active:active (running) since Fri 2018-08-10 19:50:13 CST; 36min ago
Docs:http://redis.io/documentation,
Man:redis-server (1)
Main pid:10382 (Redis-server)
Tasks:4 (limit:2294)
CGroup:/system.slice/redis-server.service
└─10382/usr/bin/redis-server 127.0.0.1:6379
August 19:50:12 Zhangkun-virtual-machine systemd[1]: Starting Advanced key-v .....
August 19:50:13 zhangkun-virtual-machine systemd[1]: Redis-server.service:c...ry
August 19:50:13 zhangkun-virtual-machine systemd[1]: Started advanced KEY-VA...E.
Hint:some lines were ellipsized, use-l to show on full.
Accessing Redis from a command-line client
Installing the Redis server will automatically install the Redis command-line client program together.
The native Input redis-cli command can be started, and the client program accesses the Redis server.
~ REDIS-CLI
127.0.0.1:6379> Help
REDIS-CLI 4.0.9
To get help about Redis commands type:
"Help @<group>" to get a list of commands in <group>
"Help <command>" to help on <command>
"Help <tab>" to get a list of possible Help topics
"Quit" to exit
To set REDIS-CLI preferences:
": Set hints" Enable online hints
": Set nohints" Disable online hints
Set your preferences in ~/.REDISCLIRC
127.0.0.1:6379> set Key1 "Hello"
Ok
127.0.0.1:6379> Get Key1
"Hello"
Basic Redis Client Command operations
Add a record Key1
Redis 127.0.0.1:6379> set Key1 "Hello" ok# print records Redis 127.0.0.1:6379> get Key1 "Hello"
Add a digital record
Set Key2 1ok# let digital self-increment redis 127.0.0.1:6379> INCR key2 (integer) 2redis 127.0.0.1:6379> INCR key2 (integer) 3# print records Redis 1 27.0.0.1:6379> Get Key2 "3"
Add a list record Key3
Redis 127.0.0.1:6379> lpush Key3 A (integer) # from the left Insert list Redis 127.0.0.1:6379> lpush key3 B (integer) * Insert list from right to Redis 12 7.0.0.1:6379> rpush key3 C (integer) 3# print List records, in left-to-right order Redis 127.0.0.1:6379> lrange Key3 0) "B" 2) "a" 3) "C"
Add a hash record Key4
Redis 127.0.0.1:6379> hset key4 name "John Smith" (integer) # inserted in the hash table, the value of the key and value of the email redis 127.0.0.1:6379> hset k Ey4 email "[Email protected]" (integer) # Print hash table, name is the value of key Redis 127.0.0.1:6379> hget key4 name "John Smith" # Print entire hash table re Dis 127.0.0.1:6379> hgetall key41) "name" 2) "John Smith" 3) "email" 4) "[Email protected]"
Add a hash table record Key5
# Add a hash table record KEY5, insert multiple key and value values one at a time Redis 127.0.0.1:6379> hmset key5 username antirez password p1pp0 age 3ok# print hash tables, user Name and age are key values for Redis 127.0.0.1:6379> hmget key5 username age1) "Antirez" 2) "3" # Print full hash table records Redis 127.0.0.1:6379> Hgetall key51) "username" 2) "Antirez" 3) "password" 4) "P1pp0" 5) "age" 6) "3"
Deleting records
# View all key list Redis 127.0.0.1:6379> keys *) "Key2" 2) "Key3" 3) "Key4" 4) "Key5" 5) "Key1" # Delete Key1,key5redis 127.0.0.1:6379 > del key1 (Integer) 1redis 127.0.0.1:6379> del key5 (integer) # View all key list Redis 127.0.0.1:6379> keys *) "Key2" 2) "Key3" 3) "Key4"
Modifying Redis's configuration using Redis's access account
By default, access to the Redis server does not require a password, and for added security we need to set the access password for the Redis server. Set the access password to Redisredis.
Open the configuration file for Redis server with VI redis.conf
~ sudo vi/etc/redis/redis.conf# uncomment requirepassrequirepass Redisredis
Let Redis server be accessed remotely
By default, the Redis server does not allow remote access and allows only native access, so we need to set the ability to turn on remote access.
Open the configuration file for Redis server with VI redis.conf
~ sudo vi/etc/redis/redis.conf# comment bind#bind 127.0.0.1
After modification, restart the Redis server.
~ Sudo/etc/init.d/redis-server restartstopping Redis-server:redis-server. Starting Redis-server:redis-server.
Login to Redis server with no password
~ Redis-cliredis 127.0.0.1:6379> Keys * (Error) ERR operation not permitted
Found to be able to log on, but could not execute command.
Login to Redis server and enter your password
~ redis-cli-a Redisredisredis 127.0.0.1:6379> keys *) "Key2" 2) "Key3" 3) "Key4"
After landing, everything is fine.
We check Redis's network listening port
Check Redis server footprint ports
~ Netstat-nlt|grep 6379tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN
We saw that the network listener changed from the previous 127.0.0.1:3306 to 0 0.0.0.0:3306, which means that Redis has allowed remote login access.
We access the Redis server from another Linux remote
~ redis-cli-a redisredis-h 192.168.1.199redis 192.168.1.199:6379> keys *) "Key2" 2) "Key3" 3) "Key4"
Remote access is OK. With the above operation, we will install the Redis database server in Linux Ubuntu system.
Ubuntu18.04, installing Redis and simply using Redis