手把手教你從零搭建一個Redis服務(wù)
前言
自己在搭建redis服務(wù)的時候碰到一些問題,好多人只告訴你怎么成功搭建,但是并沒有整理過程中遇到的問題,所有樓主就花了點時間來整理下。
- linux環(huán)境安裝redis
- 安裝中的碰到的問題和解決辦法
- 怎么在代碼中使用安裝的redis
- 設(shè)置用戶名和密碼
- 程序應(yīng)用中碰到的問題
介紹
redis是一個key-value存儲系統(tǒng)。和Memcached類似,它支持存儲的value類型相對更多,包括string(字符串)、list(鏈表)、set(集合)、zset(sorted set --有序集合)和hash(哈希類型)。這些數(shù)據(jù)類型都支持push/pop、add/remove及取交集并集和差集及更豐富的操作,而且這些操作都是原子性的。在此基礎(chǔ)上,redis支持各種不同方式的排序。與memcached一樣,為了保證效率,數(shù)據(jù)都是緩存在內(nèi)存中。區(qū)別的是redis會周期性的把更新的數(shù)據(jù)寫入磁盤或者把修改操作寫入追加的記錄文件,并且在此基礎(chǔ)上實現(xiàn)了master-slave(主從)同步。
Redis 是一個高性能的key-value數(shù)據(jù)庫。 redis的出現(xiàn),很大程度補償了memcached這類key/value存儲的不足,在部 分場合可以對關(guān)系數(shù)據(jù)庫起到很好的補充作用。它提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erlang等客戶端,使用很方便。
一、安裝redis
1、下載安裝包
- cd /www/redis/
- wget http://download.redis.io/releases/redis-4.0.8.tar.gz
- tar -zxvf redis-4.0.8.tar.gz
- mv redis-4.0.8 redis
2、編譯redis
- cd /www/redis/redis/
- make MALLOC=libc
- make PREFIX=/usr/local/redis install
3、準(zhǔn)備配置文件
- cd /usr/local/redis
- mkdir conf
- cd conf/
- vi redis_6379.conf
配置文件內(nèi)容如下:
- bind 127.0.0.1
- protected-mode no
- port 6379
- tcp-backlog 511
- timeout 0
- tcp-keepalive 300
- daemonize yes
- supervised no
- pidfile /www/redis/data/redis/6379/redis_6379.pid
- loglevel notice
- logfile "/www/redis/data/redis/6379/log.log"
- databases 16
- always-show-logo yes
- save 900 1
- save 300 10
- save 60 10000
- stop-writes-on-bgsave-error yes
- rdbcompression yes
- rdbchecksum yes
- dbfilename dump.rdb
- dir /www/redis/data/redis/6379/
- slave-serve-stale-data yes
- slave-read-only yes
- repl-diskless-sync no
- repl-diskless-sync-delay 5
- repl-disable-tcp-nodelay no
- slave-priority 100
- lazyfree-lazy-eviction no
- lazyfree-lazy-expire no
- lazyfree-lazy-server-del no
- slave-lazy-flush no
- appendonly yes
- appendfilename "appendonly.aof"
- appendfsync everysec
- no-appendfsync-on-rewrite no
- auto-aof-rewrite-percentage 100
- auto-aof-rewrite-min-size 64mb
- aof-load-truncated yes
- aof-use-rdb-preamble no
- lua-time-limit 5000
- slowlog-log-slower-than 10000
- slowlog-max-len 128
- latency-monitor-threshold 0
- notify-keyspace-events ""
- hash-max-ziplist-entries 512
- hash-max-ziplist-value 64
4、啟動服務(wù)
- mkdir -p /www/redis/data/redis/6379/
- cd ../bin/
- ./redis-server ../conf/redis_6379.conf
5、使用客戶端鏈接
- ./redis-cli
判斷是否啟動成功

6、使用
查看數(shù)據(jù)
- keys *
設(shè)置值
- set oneKey 測試
獲取值
- get oneKey

二、安裝中的碰到的問題和解決辦法
問題一:
- WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

臨時解決辦法:
- echo 511 > /proc/sys/net/core/somaxconn
永久解決辦法
- vi /etc/sysctl.conf

在里面添加net.core.somaxconn= 1024 然后執(zhí)行sysctl -p 就可以永久消除這個warning。

問題二:
- WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

可以參考問題一的解決
問題三:
- WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled

執(zhí)行命令echo never>/sys/kernel/mm/transparent hugepage/enabled
永久解決添加配置文件即可。
- vi /etc/rc.local

三、怎么在代碼中使用安裝的redis呢
需要引用的jar包有:
- commons-pool-1.6.jar
- jedis-2.9.0.jar
示例代碼:
- public static void main(String[] args) {
- //創(chuàng)建redis對象
- String ip = "";
- Jedis jedis=new Jedis(ip,6379);//鏈接redis
- //記錄操作個數(shù)
- jedis.set("name", "小明");
- System.out.println("name已經(jīng)賦值");
- String name = jedis.get("name");
- System.out.println("賦值后獲取name的值為:"+name);
- jedis.del("name");
- System.out.println("name已經(jīng)刪除");
- String nameT = jedis.get("name");
- System.out.println("刪除后獲取name的值為:"+nameT);
- }
- //結(jié)果
- name已經(jīng)賦值
- 賦值后獲取name的值為:小明
- name已經(jīng)刪除
- 刪除后獲取name的值為:null
四、設(shè)置用戶名和密碼
1、在配置文件中redis_6379.conf直接添加requirepass 123456
2、通過命令添加
設(shè)置密碼
- #設(shè)置密碼
- config set requirepass 123456

查看密碼
- config get requirepass
需要驗證密碼以后才可以查看

測試代碼
- public static void main(String[] args) {
- //創(chuàng)建redis對象
- String ip = "";
- Jedis jedis=new Jedis(ip,6379);//鏈接redis
- jedis.auth("123456");
- //記錄操作個數(shù)
- jedis.set("name", "小明");
- System.out.println("name已經(jīng)賦值");
- String name = jedis.get("name");
- System.out.println("賦值后獲取name的值為:"+name);
- jedis.del("name");
- System.out.println("name已經(jīng)刪除");
- String nameT = jedis.get("name");
- System.out.println("刪除后獲取name的值為:"+nameT);
- }
- #結(jié)果
- name已經(jīng)賦值
- 賦值后獲取name的值為:小明
- name已經(jīng)刪除
- 刪除后獲取name的值為:null
五、應(yīng)用中碰到的問題
鏈接被拒絕或者超時問題

redis剛開始配置的ip是默認(rèn)ip和端口127.0.0.1:6379,這個ip只能服務(wù)的本地進(jìn)行鏈接。解決辦法:
在配置文件中把這個ip給注釋

在阿里云中配置安全組

修改好配置文件和安全組以后,可以通過阿里云服務(wù)的外網(wǎng)ip和redis的端口訪問。
但是呢,又報一個問題,保護(hù)模式下拒絕訪問。

根據(jù)提示修改配置文件redis_6379.conf中屬性protected-mode no,并重啟服務(wù)。

再次測試代碼連接正常~