1, foreword
Redis Is a commonly used memory-based Key-Value data base It is more advanced than Memcache and supports many kinds data structure It is efficient and fast. Redis can easily solve the problem of high concurrent data access; it is also very good as a time-to-time monitoring signal processing.
2, installation
//Installing Redis Server in Terminal
sudo apt-get install redis-server
- 1
- 2
- 1
- 2
After installation, the Redis server will start automatically. Let's check the Redis server program.
//Check Redis Server System Processes in Terminal
ps -aux|grep redis
- 1
- 2
- 1
- 2
As you can see:
//Check Redis Server Status in Terminal by Start Command
netstat -nlt|grep 6379
- 1
- 2
- 1
- 2
Display: TCP 0 0 127.0.0.1:6379 0.0.0.0.0:* LISTEN
//Check Redis Server Status by Start Command
sudo /etc/init.d/redis-server status
- 1
- 2
- 1
- 2
Display: redis-server is running
3. Accessing Redis through the command-line client
Installing the Redis server automatically installs the Redis command line client program together.
You can start by typing redis-cli command locally, and the client program accesses the Redis server.
~ redis-cli
redis 127.0.0.1:6379>
# Command Line Help
redis 127.0.0.1:6379> help
redis-cli 2.2.12
Type: "help @" to get a list of commands in
"help " for help on
"help " to get a list of possible help topics
"quit" to exit
# View all key lists
redis 127.0.0.1:6379> keys *
(empty list or set)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
Basic Redis client command operation
- Add a string record key1
# Add a record key1
redis 127.0.0.1:6379> set key1 "hello"
OK
# Print record
redis 127.0.0.1:6379> get key1
"hello"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
_2. Add a digital record key2
# Add a numeric record key2
set key2 1
OK
# Let the numbers grow by themselves
redis 127.0.0.1:6379> INCR key2
(integer) 2
redis 127.0.0.1:6379> INCR key2
(integer) 3
# Print record
redis 127.0.0.1:6379> get key2
"3"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
_3. Add a list to record key3
# Add a list record key3
redis 127.0.0.1:6379> LPUSH key3 a
(integer) 1
# Insert the list from the left
redis 127.0.0.1:6379> LPUSH key3 b
(integer) 2
# Insert the list from the right
redis 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"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
_4. Add a hash table record key4
# Add a hash entry key4
redis 127.0.0.1:6379> HSET key4 name "John Smith"
(integer) 1
# Insert the values of Key and Value of email into the hash table
redis 127.0.0.1:6379> HSET key4 email "abc@gmail.com"
(integer) 1
# Print the value of name as key in the hash table
redis 127.0.0.1:6379> HGET key4 name
"John Smith"
# Print the entire hash table
redis 127.0.0.1:6379> HGETALL key4
1) "name"
2) "John Smith"
3) "email"
4) "abc@gmail.com"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
_5. Add a hash table record key5
# Add a hash table record key5 and insert multiple keys and value s at a time
redis 127.0.0.1:6379> HMSET key5 username antirez password P1pp0 age 3
OK
# Print the value of username and age as key s in the hash table
redis 127.0.0.1:6379> HMGET key5 username age
1) "antirez"
2) "3"
# Print a complete hash table record
redis 127.0.0.1:6379> HGETALL key5
1) "username"
2) "antirez"
3) "password"
4) "P1pp0"
5) "age"
6) "3"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
_6. Delete records
# View all key lists
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
# View all key lists
redis 127.0.0.1:6379> keys *
1) "key2"
2) "key3"
3) "key4"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
4. Modify Redis configuration
1. Access account using Redis
By default, no password is required to access the Redis server. To increase security, we need to set the access password of the Redis server. Set the access password to redis.
Open the Redis server configuration file redis.conf with vi
~ sudo vi /etc/redis/redis.conf
#Unannotate requirepass
requirepass redis
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
2. Let Redis server be remotely accessed
By default, Redis servers do not allow remote access, only local access, so we need to set up the function to open remote access.
Open the Redis server configuration file redis.conf with vi
~ sudo vi /etc/redis/redis.conf
#Note bind
#bind 127.0.0.1
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
After modification, restart the Redis server.
~ sudo /etc/init.d/redis-server restart
Stopping redis-server: redis-server.
Starting redis-server: redis-server.
- 1
- 2
- 3
- 1
- 2
- 3
Log on to Redis server without password
~ redis-cli
redis 127.0.0.1:6379> keys *
(error) ERR operation not permitted
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
It was found that it was possible to log in, but the command could not be executed.
Log on to Redis server and enter your password
~ redis-cli -a redisredis
redis 127.0.0.1:6379> keys *
1) "key2"
2) "key3"
3) "key4"
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
After landing, everything was normal.
Let's check Redis's network listening port
//Check the Redis server occupied port
~ netstat -nlt|grep 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN
- 1
- 2
- 3
- 1
- 2
- 3
We see that network sniffing between them has changed from 127.0.0.1:6379 to 0.0.0.0:6379, indicating that Redis has allowed remote login access.
We're at another remote station. Linux Accessing Redis Server
~ redis-cli -a redisredis -h 192.168.1.199
redis 192.168.1.199:6379> keys *
1) "key2"
2) "key3"
3) "key4"
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
Remote access is normal. Through the above operation, we will complete the installation of the Redis database server and the system in Linux Ubuntu.