linux redis安装使用,linux安装redis
Linux(CentOS)中Redis介紹、安裝、使用【一篇就夠】
2018-05-13 13:36:16?sjmz30071360?閱讀數 1590更多
分類專欄:?redis
版權聲明:本文為博主原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接和本聲明。
一、介紹
Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries.
二、下載 redis版本包到 CentOS上
curl -O http://download.redis.io/releases/redis-4.0.9.tar.gz
三、安裝
mkdir redis?創建redis存放目錄
mv redis-4.0.9.tar.gz reids?將安裝包移動到redis存放目錄下
cd redis?進入目錄
tar -xvf redis-4.0.9.tar.gz?解壓安裝包
ls -lrt
ls -lrt redis-4.0.9
cd redis-4.0.9?進入redis-4.0.9
make 運行make命令
如果運行出現make:gcc:Commond not found錯誤,一般出現這種錯誤是因為安裝系統的時候使用的是最小化mini安裝,系統沒有安裝make、vim等常用命令,直接yum安裝下即可。
運行yum -y install gcc automake autoconf libtool make即可
make命令正常運行完畢后,在安裝目錄的src目錄下可以看到服務程序redis-server和用于測試的客戶端程序redis-cli等
四、使用
cd src?進入src目錄
./redis-server?啟動redis服務(這種方式使用的是默認配置,也可以通過啟動參數告訴redis使用指定的配置文件)
如: ./redis-server redis.conf
啟動后的界面如下:
通過另一臺機器,登錄客戶端,使用客戶端和redis服務交互
cd src
./redis-cli
setkey value
getkey
參考:http://www.runoob.com/redis/redis-install.html
五、windows端通過java程序訪問虛擬機linux上的redis
1)修改redis.conf配置文件
vi redis.conf
注釋掉 bind 127.0.0.1
bind的意思是綁定哪個ip地址能夠訪問服務 ,簡單說bind指定的ip才可以訪問redis server。
ps:
bind 127.0.0.1 //指定只有本機才能訪問redis服務器
bind 0.0.0.0? ? // 所有的機子都可以訪問到redis server
bind? 192.168.1.253? //只有這個ip的機子才可以訪問redis server
2)關閉linux防火墻
systemctl stop firewalld.service
3)java程序(虛擬機linux ip地址為:192.168.0.104)
import redis.clients.jedis.Jedis; public class RedisJava { public static void main(String[] args) { Jedis jedis = new Jedis("192.168.0.104"); System.out.println("連接成功"); //查看服務是否運行 System.out.println("服務正在運行:" + jedis.ping()); } }
執行時報下面錯誤:
Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions:?1)?Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent.?2)Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server.?3)?If you started the server manually just for testing, restart it with the '--protected-mode no' option.?4)Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
修改redis.conf,去掉requirepass foobared前面的#,啟用密碼驗證(密碼為 foobared)
相應的在java程序中增加密碼設置
import redis.clients.jedis.Jedis; public class RedisJava { public static void main(String[] args) { Jedis jedis = new Jedis("192.168.0.104"); jedis.auth("foobared"); //輸入redis密碼 System.out.println("連接成功"); //查看服務是否運行 System.out.println("服務正在運行:" + jedis.ping()); } }
重新啟動redis,再次執行java程序,執行成功!
連接成功
服務正在運行:PONG
Redis Java String(字符串)實例
import redis.clients.jedis.Jedis; public class RedisJava { public static void main(String[] args) { Jedis jedis = new Jedis("192.168.0.104"); jedis.auth("foobared"); //輸入redis密碼 System.out.println("連接成功"); //查看服務是否運行 System.out.println("服務正在運行:" + jedis.ping()); //設置redis字符串數據 jedis.set("testK", "first redis value"); //獲取存儲的數據并輸出 System.out.println("redis存儲的字符串為:" + jedis.get("testK")); } }
執行結果:
連接成功
服務正在運行:PONG
redis存儲的字符串為:first redis value
總結
以上是生活随笔為你收集整理的linux redis安装使用,linux安装redis的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux live cd ubuntu
- 下一篇: 银川看子宫内膜异位症最好的医院推荐