Redis Database Installation Deployment

Source: Internet
Author: User
Tags memory usage redis server

1. Installation version

Redis-3.0.7.tar.gz

Website Link:http://redis.io/download

2. Download the installation package

wget http://download.redis.io/releases/redis-3.0.7.tar.gz

3. Unzip

TAR-ZXVF redis-3.0.7.tar.gz

Unzip into the redis-3.0.7 folder

4. Installation

The Redis folder comes with a makefile file, and you can enter the make command directly in the redis-3.0.7 folder .

5. Running Redis

CD src executes the ./redis-server command to run The default connection port on the Redis,redis server is 6379 .

6. Boot the Redis as a Linux service

/root/software/redis-3.0.7/src/redis-server

7. Client Connection Verification

Go to the/ROOT/SOFTWARE/REDIS-3.0.7/SRC folder and execute the./redis.cli command to enable Redis client Connections Redis server.

8. Working with the redis Database

9. Configure redis.conf

Daemonize Yes # specifies how Redis runs as a daemon.

Pidfile/var/run/redis.pid # when Redis is running as a daemon, write the PID to the specified file.

Port 6379 # Specifies the listening port, the default port is 6379.

Bind 192.168.0.35 # binds the host IP address.

Logfile/var/log/redis/redis.log # Specifies how logs are logged, default to standard output.

Databases # Sets the number of databases.

three conditions are available in the Save #Redis default configuration file:

Save 900 1

Save 300 10

save 60 10000 # seconds ( minutes ) inside 1 change, 300 seconds (5 minutes " within 10 60 in seconds there are 10000 When changing, synchronize data to a disk file.

Rdbcompression Yes # Specifies whether data is compressed when stored to a local database, by default Yes.

Dbfilename Dump.rdb # Specifies the local database file name.

Dir/home/banping/redis/data # Specifies the local database to hold the directory.

Requirepass Password # sets the Redis connection password, which is turned off by default.

MaxClients # Sets the maximum number of client connections, with no restrictions on default.

MaxMemory # Specifies The maximum memory that Redis can use.

( Note: Warning questions and workarounds

Warning: WARNING Overcommit_memory is set to 0! Background save may fail under low memory condition. ...

Workaround: Add vm.overcommit_memory = 1 in the/etc/sysctl.conf configuration file , and then sysctl- P Restart the service.

Warning: warning:the TCP Backlog setting of 511 cannot be enforced because/proc/sys/net/core/somaxconn are set to the lower VA Lue of.

Workaround: Add net.core.somaxconn = 1024x768 in the /etc/sysctl.conf configuration file and sysctl-p Restart the service.

Warning:WARNING you has Transparent Huge Pages (THP) support with enabled in your kernel. This would create latency and memory usage issues with Redis. ...

Workaround: perform the echo never >/sys/kernel/mm/transparent_hugepage/enabled command as root , This command is then added to the /etc/rc.local .

)

Correct log output:

Second, the python package to install Redis

1.redis python package download

My python is python2.6, execute download command for wget https://github.com/ Andymccurdy/redis-py/archive/master.zip can be downloaded.

2. Unzip the installation

Unzip Master.zip

CD Redis-py-master

sudo python2.6 setup.py Install

The Setuptools-20.2.2-py2.6.egg in the path /usr/lib/python2.6/sitepackage folder is what we want.

3. Test python to manipulate the database

Import Redis

R = Redis. Strictredis (host= ' localhost ', port=6379, db=0)

R.set (' Test ', ' test123 ')

R.get (' Test ')

R.delete (' Test ')

R.config_get ("MaxMemory")

As shown, you can use Python to manipulate the database.

Third, Python 's basic API encapsulation for redis Access

#!/usr/bin/python#coding=utf-8import redisclass credis:def __init__ (self): self.host = ' localhost ' self.port = 6379 self.db = 0 SELF.R = Redis. Redis (host = self.host, port = self.port, db = self.db) #1. Strings type and Operation #设置 key corresponds to a value of type string, set (self, key, value): Return Self.r.set (Key, value) def get ( Self, key): Return Self.r.get (key) #设置 key corresponds to a value of type string. If key already exists, return 0,nx is not exist meaning Def setnx (self, Key, value): Return Self.r.setnx (key, value) #设置 key corresponding The value is a string of type value and specifies the validity period for this key value, Def setex (self, key, time, value): Return Self.r.setex (Key, time, value) #设 The substring of the value of the specified key #例: SetRange name 8 gmail.com #其中的 8 refers to the character starting with the subscript 8 (including 8) to replace the Def setrange (self, key, Num, Val        UE): Return Self.r.setrange (Key, num, value) #获取指定 The substring of the value of key Def getrange (self, key, start, end): Return Self.r.getrange (key, start, end) #删除 def remove (self, key): Return Self.r.delete (Key) #自增 def incr (self, key, default = 1): if (1 = = default): Return SELF.R.INCR (  Key) Else:return SELF.R.INCR (key, default) #自减 def decr (self, key, default = 1): if (1 = = Default): Return SELF.R.DECR (Key) Else:return SELF.R.DECR (key, default) #清空当前db def c Lear (self): return self.r.flushdb () if __name__ = = ' __main__ ': R = Credis () r.set (name, Allesa) r.get (name) R.remov E (name)


Iv. Two methods of backup for Redis

1.RDB

By default, it refreshes to disk once [save 10000 when there are 1w bars When the keys data is changed ],theRedis Data set is saved in the name dump.rdb A binary file, this policy is called a snapshot.

You can call the Save or BGSAVE command manually:/root/software/redis-3.0.7/src/redis-cli-h 127.0.0.1-p 6379-a pwd Bgsave

2.AOF

Snapshots are easy to recover and files are small, but the snapshot data may be incomplete if you experience downtime. You may need to enable another persistence mode AOF, open [appendonly Yes]in the configuration file.

AOF rules for refreshing logs to disk :

Appendfsync always #always indicates that each write operation is synchronized, very slow and very secure.

The Appendfsync everysec #everysec indicates that the write operation is cumulative and synchronized once per second.

The official recommendation of the everysec, security, is that the speed is not fast enough, if the machine is in trouble, it may lose 1 seconds of data.

You can also manually perform bgrewriteaof for AOF backups:

/usr/local/bin/redis-cli-h 127.0.0.1-p 6379-a pwd bgrewriteaof

What we are doing now is a master (master) multi-slave (Slave), the main library does not open AOF persistence, just back up every day rdb[ The official advice is to back up the RDB file every hour, see your strategy , and turn on AOF Backup from the library. The corresponding backup file is pushed to the backup server using a script.

When the redis Server is hung up, the data is restored to memory at the following priority level when restarting:

If only AOF is configured, the AOF File recovery data is loaded when the reboot is restarted;

If RBD and AOF are configured at the same time, only the AOF File recovery data is loaded at startup ;

If you configure only RBD, the dump file recovery data will be loaded at startup .

The recovery should be noted that if the main library hangs can not directly restart the main library, otherwise it will directly overwrite the AOF file from the library , make sure that the files to be recovered correctly to start, otherwise it will flush out the original file.

Redis Database Installation Deployment

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.