Ubuntu16.04安装Redis

Ubuntu16.04安装Redis


前言

Redis是常用基于内存的Key-Value数据库,比Memcache更先进,支持多种数据结构,高效,快速。用Redis可以很轻松解决高并发的数据访问问题;作为实时监控信号处理也非常不错。

环境

Ubuntu 16.04

安装Redis服务器端

~ sudo apt-get install redis-server

安装完成后,Redis服务器会自动启动,我们检查Redis服务器程序

检查Redis服务器系统进程

~ ps -aux|grep redis

redis     4162  0.1  0.0  10676  1420 ?        Ss   23:24   0:00 /usr/bin/redis-server /etc/redis/redis.conf

conan     4172  0.0  0.0  11064   924 pts/0    S+   23:26   0:00 grep --color=auto redis

通过启动命令检查Redis服务器状态

~ netstat -nlt|grep 6379

tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN

通过启动命令检查Redis服务器状态

~$ 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 四 2017-11-09 12:22:09 CST; 59s ago

     Docs: http://redis.io/documentation,

           man:redis-server(1)

 Main PID: 5394 (redis-server)

   CGroup: /system.slice/redis-server.service

           └─5394 /usr/bin/redis-server 127.0.0.1:6379

 

11月 09 12:22:09 zzf systemd[1]: Starting Advanced key-value store...

11月 09 12:22:09 zzf run-parts[5388]: run-parts: executing /etc/redis/redi...le

11月 09 12:22:09 zzf run-parts[5395]: run-parts: executing /etc/redis/redi...le

11月 09 12:22:09 zzf systemd[1]: Started Advanced key-value store.

Hint: Some lines were ellipsized, use -l to show in full.

通过命令行客户端访问Redis

安装Redis服务器,会自动地一起安装Redis命令行客户端程序。

在本机输入redis-cli命令就可以启动,客户端程序访问Redis服务器。

~ redis-cli

redis 127.0.0.1:6379>

 

# 命令行的帮助

 

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

 

 

# 查看所有的key列表

 

redis 127.0.0.1:6379> keys *

(empty list or set)

基本的Redis客户端命令操作

增加一条记录key1

redis 127.0.0.1:6379> set key1 "hello"

OK

 

# 打印记录

redis 127.0.0.1:6379> get key1

"hello"

增加一条数字记录

set key2 1

OK

 

# 让数字自增

redis 127.0.0.1:6379> INCR key2

(integer) 2

redis 127.0.0.1:6379> INCR key2

(integer) 3

 

# 打印记录

redis 127.0.0.1:6379> get key2

"3"

增加一个列表记录key3

redis 127.0.0.1:6379> LPUSH key3 a

(integer) 1

 

# 从左边插入列表

redis 127.0.0.1:6379> LPUSH key3 b

(integer) 2

 

# 从右边插入列表

redis 127.0.0.1:6379> RPUSH key3 c

(integer) 3

 

# 打印列表记录,按从左到右的顺序

redis 127.0.0.1:6379> LRANGE key3 0 3

1) "b"

2) "a"

3) "c"

增加一个哈希记表录key4

redis 127.0.0.1:6379> HSET key4 name "John Smith"

(integer) 1

 

# 在哈希表中插入,email的Key和Value的值

redis 127.0.0.1:6379> HSET key4 email "abc@gmail.com"

(integer) 1

 

# 打印哈希表中,name为key的值

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) "abc@gmail.com"

增加一条哈希表记录key5

# 增加一条哈希表记录key5,一次插入多个Key和value的值

redis 127.0.0.1:6379> HMSET key5 username antirez password P1pp0 age 3

OK

 

# 打印哈希表中,username和age为key的值

redis 127.0.0.1:6379> HMGET key5 username age

1) "antirez"

2) "3"

 

# 打印完整的哈希表记录

redis 127.0.0.1:6379> HGETALL key5

1) "username"

2) "antirez"

3) "password"

4) "P1pp0"

5) "age"

6) "3"

删除记录

# 查看所有的key列表

redis 127.0.0.1:6379> keys *

1) "key2"

2) "key3"

3) "key4"

4) "key5"

5) "key1"

 

# 删除key1,key5

redis 127.0.0.1:6379> del key1

(integer) 1

redis 127.0.0.1:6379> del key5

(integer) 1

 

# 查看所有的key列表

redis 127.0.0.1:6379> keys *

1) "key2"

2) "key3"

3) "key4"

修改Redis的配置

使用Redis的访问账号

默认情况下,访问Redis服务器是不需要密码的,为了增加安全性我们需要设置Redis服务器的访问密码。设置访问密码为redisredis。

用vi打开Redis服务器的配置文件redis.conf

~ sudo vi /etc/redis/redis.conf

 

#取消注释requirepass

requirepass redisredis

Redis服务器被远程访问

默认情况下,Redis服务器不允许远程访问,只允许本机访问,所以我们需要设置打开远程访问的功能。

用vi打开Redis服务器的配置文件redis.conf

~ sudo vi /etc/redis/redis.conf

 

#注释bind

#bind 127.0.0.1

修改后,重启Redis服务器。

~ sudo /etc/init.d/redis-server restart

Stopping redis-server: redis-server.

Starting redis-server: redis-server.

未使用密码登陆Redis服务器

~ redis-cli

 

redis 127.0.0.1:6379> keys *

(error) ERR operation not permitted

发现可以登陆,但无法执行命令了。

登陆Redis服务器,输入密码

~  redis-cli -a redisredis

 

redis 127.0.0.1:6379> keys *

1) "key2"

2) "key3"

3) "key4"

登陆后,一切正常。

我们检查Redis的网络监听端口

检查Redis服务器占用端口

~ netstat -nlt|grep 6379

tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN

我们看到网络监听从之前的 127.0.0.1:3306 变成 0 0.0.0.0:3306,表示Redis已经允许远程登陆访问。

我们在远程的另一台Linux访问Redis服务器

~ redis-cli -a redisredis -h 192.168.1.199

 

redis 192.168.1.199:6379> keys *

1) "key2"

2) "key3"

3) "key4"

远程访问正常。通过上面的操作,我们就把Redis数据库服务器,在Linux Ubuntu中的系统安装完成。

 

 

博文最后更新时间:


评论

  • teamvie

    <a href="http://www.mavruka.net/mens-nfl-sweatshirts-for-cheap-dressl">mens nfl sweatshirts for cheap</a> <a href="http://www.sterlingep.net/aj-super-fly-cheap-all-grey-shoes-trainersuk">aj super fly cheap all grey shoes</a> <a href="http://www.brnioc.net/nike-air-huarache-huarache-ultra-shoesdk">nike air huarache huarache ultra</a> teamvie http://www.teamvie.net/

  • einspurauto

    <a href="http://www.scrgruppen.com/atlanta-braves-hat-mesh-hats-handk">atlanta braves hat mesh hats</a> <a href="http://www.sppmis.com/adidas-j-wall-2-shoes-all-white-blue-skode">adidas j wall 2 shoes all white blue</a> <a href="http://www.bocksberg.com/mens-nike-dallas-cowboys-morris-claiborne-24-limited-navy-blue-throwback-jersey-sale-nflf">mens nike dallas cowboys morris claiborne 24 limited navy blue throwback jersey sale</a> <a href="http://www.sowegaland.com/adidas-x-soccer-cleats-billig-runso">adidas x soccer cleats billig</a> <a href="http://www.cwmipl.com/james-harden-vol-1-below-zero-schoenenuk">james harden vol 1 below zero</a> <a href="http://www.staygmi.com/nike-air-trainer-46-billig-schuhees">nike air trainer 46 billig</a> [url=http://www.einspurauto.com/]einspurauto[/url]

  • duxoutdoors

    <a href="https://www.fracingsand.com/shockproof-a-waterproof-phone-pouzdro-pouzdroe">shockproof a waterproof phone pouzdro</a> <a href="https://www.alicecassidy.com/retro-%C5%A1aty-up-satyc">retro 拧aty up</a> <a href="https://www.cprwestwood.com/nike-donna-aerobill-legacy-91-perforated-berretto-cappelloa">nike donna aerobill legacy 91 perforated berretto</a> <a href="https://www.seepadre.com/mens-wide-high-top-sneakers-bootsa">mens wide high top sneakers</a> <a href="https://www.duxoutdoors.com/moccasins-schwarz-friday-stiefeld">moccasins schwarz friday</a> <a href="https://www.westwoodcpr.com/blu-life-one-x2-mini-hoesje-hoesjea">blu life one x2 mini hoesje</a> <a href="https://www.duxoutdoors.com/" >duxoutdoors</a> [url=https://www.duxoutdoors.com/]duxoutdoors[/url]

  • abtheater

    <a href="https://www.fracingsand.com/giants-22-andrew-mccutchen-grey-flexbase-authentic-collection-road-stitched-mlb-jersey-jerseyc">giants 22 andrew mccutchen grey flexbase authentic collection road stitched mlb jersey</a> <a href="https://www.alicecassidy.com/river-island-torebki-cena-torebkid">river island torebki cena</a> <a href="https://www.cprwestwood.com/s9-waterfall-case-casec">s9 waterfall case</a> <a href="https://www.seepadre.com/nordstrom-phone-portemonnee-tassenb">nordstrom phone portemonnee</a> <a href="https://www.duxoutdoors.com/lg-stylo-4-slim-pouzdro-pouzdroe">lg stylo 4 slim pouzdro</a> <a href="https://www.westwoodcpr.com/n%C3%A1mo%C5%99nictvo-modr%C3%BD-%C5%A1aty-coat-satyc">n谩mo艡nictvo modr媒 拧aty coat</a> <a href="https://www.abtheater.com/" >abtheater</a> [url=https://www.abtheater.com/]abtheater[/url]

  • actiocloud

    <a href="https://www.gotosubic.com/69-nike-limited-mike-mcglinchey-youth-jersey-nfl-san-francisco-49ers-rush-realtree-camo-jerseyc">69 nike limited mike mcglinchey youth jersey nfl san francisco 49ers rush realtree camo</a> <a href="https://www.antakyahotel.com/cash-system-portfel-torebkid">cash system portfel</a> <a href="https://www.appsnuage.com/otterbox-symmetry-clear-case-iphone-xr-casec">otterbox symmetry clear case iphone xr</a> <a href="https://www.anamyth.com/loungefly-darth-vader-handtas-tassenb">loungefly darth vader handtas</a> <a href="https://www.gorgianhomes.com/sony-xperia-z3-external-battery-pouzdro-pouzdroe">sony xperia z3 external battery pouzdro</a> <a href="https://www.mozsteel.com/printed-pleated-%C5%A1aty-satyc">printed pleated 拧aty</a> <a href="https://www.actiocloud.com/" >actiocloud</a> [url=https://www.actiocloud.com/]actiocloud[/url]

  • osbornenash

    <a href="http://www.holzsiegel.com/canada-white-sale-nike-premier-ii-ic-billig-runningau">canada white sale nike premier ii ic billig</a> <a href="http://www.sscion.com/orlando-pride-womens-soccer-jersey-dressk">orlando pride womens soccer jersey</a> <a href="http://www.acoulen.com/adidas-predator-white-orange-for-cheap-trainersca">adidas predator white orange for cheap</a> <a href="http://www.locallers.com/all-grey-replica-yeezy-billig-schoenenuk">all grey replica yeezy billig</a> <a href="http://www.laramari.com/jordan-5-purple-grape-schoenende">jordan 5 purple grape</a> <a href="http://www.primetatler.com/uk-brown-shoes-nike-hypervenom-phinish-billig-obuvuk">uk brown shoes nike hypervenom phinish billig</a> osbornenash http://www.osbornenash.com/

发表评论

博客统计

访问量:5263906

博文总数:750 评论总数:910100

原创126 翻译20 转载604