Foreword
Own encounter some problems when building redis services, a lot of people just tell you how to build a successful, but there is no finishing problems encountered in the process, all the landlord spent a little time to organize the next.
- linux environment, install redis
- Installation problems encountered and solutions
- How to use the installed code redis
- Username and password
- Problems encountered in the application program
Introduction
redis is a key-value storage system. And Memcached Similarly, it supports relatively more stored value type, comprising a string (string), List (list), SET (set), zset (sorted set - ordered set) and hash (hash type). These data types are supported push / pop, add / remove and on the intersection and union, and difference richer operation, and these operations are atomic. On this basis, redis support a variety of different ways of sorting. Like with memcached, in order to ensure efficiency, the data is cached in memory. Redis difference is periodically updated in the data written to disk or to modify the operation of writing additional log file, and on this basis realize the master-slave (master and slave) synchronization.
Redis is a key-value high-performance database. redis appears, to a large extent compensate for the lack of such memcached key / value store, it can play a very good complement to relational database in some situations. It provides Java, C / C ++, C #, PHP, JavaScript, Perl, Object-C, Python, Ruby, Erlang and other clients, very easy to use.
First, install redis
1, download the installation package
- cd /www/redis/
- wget http://download.redis.io/releases/redis-4.0.8.tar.gz
- takes -zxvf Redis-4.0.8.tar.gz
- mv redis-4.0.8 redis
2, compile redis
- cd /www/redis/redis/
- make MALLOC=libc
- make PREFIX=/usr/local/redis install
3, prepare profiles
- cd /usr/local/redis
- mkdir conf
- conf cd /
- force redis_6379.conf
Configuration file as follows:
- 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-only 128
- latency-monitor-threshold 0
- notify-keyspace-events ""
- hash-max-ziplist-entries 512
- hash-max-ziplist-value 64
4, start the service
- mkdir -p /www/redis/data/redis/6379/
- cd ../bin/
- ./redis-server ../conf/redis_6379.conf
5, using the client link
- ./redis-cli
Determine whether a successful start
6,
View data
- keys *
Settings
- set oneKey test
Get the value
- get oneKey
Second, the problems encountered and installation solutions
Question one:
- WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
Temporary solution:
- echo 511 > /proc/sys/net/core/somaxconn
Permanent solution
- we /etc/sysctl.conf
Add net.core.somaxconn = 1024 in which then execute sysctl -p to permanently eliminate this warning
Question two:
- 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
执行命令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) {
- //创建redis对象
- String ip = "";
- Jedis jedis=new Jedis(ip,6379);//链接redis
- //记录操作个数
- jedis.set("name", "小明");
- System.out.println("name已经赋值");
- String name = jedis.get("name");
- System.out.println("赋值后获取name的值为:"+name);
- jedis.del("name");
- System.out.println("name已经删除");
- String nameT = jedis.get("name");
- System.out.println("删除后获取name的值为:"+nameT);
- }
- //结果
- name已经赋值
- 赋值后获取name的值为:小明
- name已经删除
- 删除后获取name的值为:null
四、设置用户名和密码
1、在配置文件中redis_6379.conf直接添加requirepass 123456
2、通过命令添加
设置密码
- #设置密码
- config set requirepass 123456
查看密码
- config get requirepass
需要验证密码以后才可以查看
测试代码
- public static void main(String[] args) {
- //创建redis对象
- String ip = "";
- Jedis jedis=new Jedis(ip,6379);//链接redis
- jedis.auth("123456");
- //记录操作个数
- jedis.set("name", "小明");
- System.out.println("name已经赋值");
- String name = jedis.get("name");
- System.out.println("赋值后获取name的值为:"+name);
- jedis.del("name");
- System.out.println("name已经删除");
- String nameT = jedis.get("name");
- . System out.println ( "get name after deleting the value:" + nameT);
- }
- #result
- name has been assigned
- After obtaining the assignment name values are: Xiao Ming
- name has been deleted
- After obtaining delete name value: null
Fifth, the application encountered a problem
Link is denied or times out problem
ip redis beginning to configure the default ip and port 127.0.0.1:6379, the ip local services can only be linked. Solution:
In the configuration file this ip to comment
Configuring security group in the cloud Ali
After modifying the configuration files and security groups, you can access the external network ip and port redis by Ali cloud services.
But then, also reported a problem, denying access to protected mode
Follow the prompts to modify the configuration file redis_6379.conf the property protected-mode no, and restart the service
Test code again connected properly ~
【Editor's Choice】