preface

Redis is a commonly used memory based key-value database, which is more advanced than Memcache and supports a variety of data structures, efficient and fast. Redis can easily solve the problem of high concurrency data access; Also very good for real-time monitoring signal processing.

The environment

Ubuntu 18.04

Install the Redis server side

~ sudo apt-get install redis-serverCopy the code

After the installation, the Redis server will start automatically. We check the Redis server program

Check the Redis server system process

~ ps -agx|grep redisCopy the code

1 10382 10382? -1 Ssl 124 0:07 /usr/bin/redis-server 127.0.0.1:6379 2677 10618 10617 2677 PTS /0 10617 S+ 1000 0:00 grep –color=auto redis

Check the status of the Redis server by starting the command

~ netstat -nlt|grep 6379Copy the code

TCP 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN tcp6 0 0: :1:6379 ::* LISTEN

Check the status of the Redis server by starting the command

~$ sudo /etc/init.d/redis-server statusCopy the code

Low redis – server. The service – the Advanced key value store – the Loaded: the Loaded (/ lib/systemd/system/redis – server. The 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

8月 10 19:50:12 Zhangkun-Virtual-machine Systemd [1]: Starting Advanced Key v… . 8月 10 19:50:13 Zhangkun-virtual-machine SystemD [1]: Redis-server. service: C… Ry 8月 10 19:50:13 Zhangkun-Virtual-machine Systemd [1]: Started Advanced Key-VA… e. Hint: Some lines were ellipsized, use -l to show in full.

Access Redis from the command line client

Installing the Redis server automatically installs the Redis command line client program along with it.

The client program accesses the Redis server by entering the redis-cli command on the local computer.

~ redis-cliCopy the code

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>” for 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 operation

Add a record key1

Redis 127.0.0.1:6379> set key1 "hello" OK # redis 127.0.0.1:6379> get key1 "hello"Copy the code

Add a digital record

Redis 127.0.0.1:6379> INCR key2 (integer) 2 redis 127.0.0.1:6379> INCR key2 (integer) 3 # Print the record Redis 127.0.0.1:6379> get key2 "3"Copy the code

Add a list record key3

127.0.0.1:6379> LPUSH key3 a (integer) 1 # Insert list from left redis 127.0.0.1:6379> LPUSH key3 b (integer) 2 # Insert list from right redis 127.0.0.1:6379> LPUSH key3 127.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 3 1) "b" 2) "a" 3) "C"Copy the code

Add a hash entry key4

Redis 127.0.0.1:6379> HSET key4 name "John Smith" (integer) 1 # Insert into hash table Redis 127.0.0.1:6379> HSET key4 email "[email protected]" (integer) 1 # Print the hash table, Redis 127.0.0.1:6379> HGET key4 name" John Smith" # redis 127.0.0.1:6379> HGETALL key4 1) "name" 2) "John Smith" 3) "email" 4) "[email protected]"Copy the code

Add a hash table record key5

Redis 127.0.0.1:6379> HMSET key5 username antirez password P1pp0 age 3 OK Redis 127.0.0.1:6379> HMGET key5 username age 1) "antirez" 2) "3" # print complete hash table redis 127.0.0.1:6379> HGETALL key5 1) "username" 2) "antirez" 3) "password" 4) "P1pp0" 5) "age" 6) "3"Copy the code

Delete records

Redis 127.0.0.1:6379> keys * 1) "key2" 2) "key3" 3) "key4" 4) "key5" 5) "key1" # delete key1,key5 redis 127.0.0.1:6379> del key1 (integer) 1 redis 127.0.0.1:6379> del key5 (integer) 1 # Check all keys list redis 127.0.0.1:6379> keys * 1) "key2" 2) "key3" 3) "key4"Copy the code

Modify the Redis configuration

Use the Redis access account

By default, you do not need a password to access the Redis server. To increase security, you need to set a password to access the Redis server. Set the access password to redisredis.

Open the Redis server configuration file redis.conf using vi

Requirepass requirepass redisredis ~ sudo vi /etc/redis/redis.confCopy the code

Let Redis server be accessed remotely

By default, Redis server does not allow remote access, only native access, so we need to set up remote access enabled.

Open the Redis server configuration file redis.conf using vi

~ sudo vi /etc/redis/redis.conf # 解 决 bind #bind 127.0.0.1Copy the code

After modification, restart the Redis server.

~ sudo /etc/init.d/redis-server restart
Stopping redis-server: redis-server.
Starting redis-server: redis-server.Copy the code

Log in to Redis server without password

~ redis-cli

redis 127.0.0.1:6379> keys *
(error) ERR operation not permittedCopy the code

Found able to log in, but unable to execute command.

Log in to the Redis server and enter your password

~  redis-cli -a redisredis

redis 127.0.0.1:6379> keys *
1) "key2"
2) "key3"
3) "key4"Copy the code

After landing, everything was fine.

We check the network listening port of Redis

Check the port occupied by Redis server

~ netstat - NLT) | grep TCP 6379 0 0 0.0.0.0:6379 0.0.0.0: * LISTENCopy the code

We see that the network listener has changed from 127.0.0.1:3306 to 0 0.0.0.0:3306, indicating that Redis has allowed remote login access.

We access the Redis server remotely on another Linux

~ redis-cli -a redisredis -h 192.168.1.199

redis 192.168.1.199:6379> keys *
1) "key2"
2) "key3"
3) "key4"Copy the code

Remote access normal. Through the above operations, we have completed the installation of the Redis database server in Linux Ubuntu.