Redis Installation and usage examples

Source: Internet
Author: User
Tags allkeys syslog redis server

Redis is a very special feature of many NoSQL products, supported by a rich set of data types and methods, as a durable software, actually make more of it as the cache. Think twice in the personal test environment installed for two or three days, although the document looked a lot, but in fact, but the overall view of the NoSQL product has not changed, I think all kinds of nosql products are just tools, and is a small tool, not the product. Small tools can play a big role this is not false (Redis is currently the most well-known case in the domestic Sina Weibo), but the premise is to find a suitable application scenario, in-depth understanding to use it. This article briefly describes the process of installing Redis in Linux environments, Redis-related parameters, and simple use. 1, install the download source package:# wget http://redis.googlecode.com/files/redis-2.4.10.tar.gzUnzip:# tar Xvfz redis-2.4.10.tar.gzEnter directory:# CD redis-2.4.10Compile:# makeNext do not need to make INSTALLSRC directory Redis-server and REDIS-CLI two name is the Redis server and client applications, these two commands can be directly called, it is recommended to copy directly to the user bin directory, to facilitate the call:# CP src/redis-*/usr/local/bin/Installation is complete. 2. Configurationdirectly execute Redis-server can start the Redis service, the default listening port is 6379, and then the client can connect to the server, to perform operations. It's too simple to have friends see the surprises that can be so irrepressible here. Yes, it can be so simple, good tools have such characteristics, easy to start, but want to use well, or need to study in a deep step. The same is true for Redis, which provides several parameters that can be used to customize the Redis service to achieve better performance and match the needs of the business side. There is a configuration file named Redis.conf in the source package that contains examples and functional descriptions of each of the Redis parameters. daemonize: Default value No, which is used to customize whether the Redis service is running in daemon mode. pidfile: Default value/var/run/redis.pid, specifies the Redis service's process number file path, which needs to be configured when running in daemon mode;Port : Default value of 6379, specifies the port of the Redis service;bind: Bind IP, default is native all network device;Timeout: The client disconnects after n seconds of inactivity;loglevel: Set the log level on the server side with the following options:Debug: Record detailed information for development or commissioning;verbose: Provide a lot of useful information, but not as detailed as debug, the default is this option;Notice: Moderate reminders, more for the product environment;WARNING: Only important warning messages are displayed;logfile: Specifies the output path of the log, the default value of stdout, indicating output to the screen, the Guardian mode is output to/dev/null;If you want to output the log to Syslog, you can start syslog-enabled Yes, which defaults to No. databases: Specifies the number of databases, which defaults to 16, and the database used by default is DB 0. The following are snapshot-related settingssave <seconds> <changes>: Specifies how long to refresh the snapshot to disk, this option has two attribute values and only fires when two attribute values are met; You can set multiple levels, such as the default parameter file:Save 900 1: Every 900 seconds (15 minutes) at least once the key value change is triggered;Save 300 10: Every 300 seconds (5 minutes) at least 10 times the key value change is triggered;Save 60 10000: Every 60 seconds at least 10,000 times the key value change is triggered;rdbcompression: The default is yes, when the dump database using LZF compressed string object, if the CPU resource is more tense, can be set to No, choose not to compress;Dbfilename: The default value dump.rdb,dump to the file name in the filesystem;dir: Default value./, that is, the current directory, dump out of the data file storage path;The following are replication-related settings, which are not enabled by default, so the following table parameters are commented on in the default parameter file# slaveof <masterip> <masterport>: Specify the primary IP and port for creating a mirror service;# Masterauth <master-password>: If Master is configured with a password, it should also be set up here;Slave-serve-stale-data: The default value is yes. When slave loses a connection to the master side, or the copy is still being processed, the slave will have the following two performances:When the value of this parameter is yes, slave continues to respond to client requests, although the data is not synchronized or even has no data (appearing in the case of initial synchronization);when the value of this parameter is no, slave returns the error message "SYNC with Master in Progreee";# Repl-ping-slave-period: Default value of 10, specifies the period of slave to ping master periodically;# Repl-timeout: Default value of 60, specifies the time-out. Note This parameter includes the time to bulk transfer data and ping the response. The following are security-related settings# requirepass: Specify a password, client connection also need to pass the password to successfully connect;# Rename-command: Redefine the command, such as renaming the Config command to a very complex name:Rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52;rename-command CONFIG "": Cancel this command;The following are settings for resource throttling# maxclients: Specifies the maximum number of concurrent connections for a client, by default there is no limit until Redis is unable to create a new process, and setting the parameter value to 0 also means no limit, and if the parameter specifies a value, Redis closes all new connections when the concurrent connection reaches the specified value and returns ' Max number of clients reached ' error message;# maxmemory: Set Redis maximum available memory. When the maximum memory is reached, Redis attempts to delete the key values according to the set recycle policy. If the key value cannot be deleted, or if the retention policy is set to not clear, Redis returns an error message to the memory-issuing request. This parameter is useful when Redis is cached as a first-level LRU. # Maxmemory-policy: Default value Volatile-lru, specifying the Purge policy, is available in the following ways:Volatile-lru, remove the key with a expire set using an LRU algorithmAllkeys-lru, remove any key accordingly to the LRU algorithmvolatile-random, remove a random key with an expire setallkeys->random, remove a random key, any keyVolatile-ttl, remove the key with the nearest Expire time (minor TTL)noeviction, don ' t expire at all, just return a error on write operations# Maxmemory-samples: The default value of 3,LRU and the minimum TTL policy is not a rigorous strategy, but an approximate estimate, so you can choose to sample the values for inspection. The following is the setting for Append only mode, where Redis asynchronously dumps data to disk by default, which in extreme cases may result in the loss of some data (such as a sudden server outage), if the data is important and does not want to be lost, you can enable direct write mode, In this mode, Redis synchronizes all the received writes to the Appendonly.aof file, which rebuilds all the data in memory when the Redis service starts. Note that this pattern has a very large impact on performance.appendonly: Default value No, specifies whether write-through mode is enabled;# appendfilename: Default file name appendonly.aof for write-through mode;Appendfsync: Call Fsync () mode to let the operating system write data to disk, data synchronization mode, there are the following modes:Always : Called every time, such as security, but the slowest speed;everysec: Synchronization per second, which is also the default mode;No : Do not call Fsync, the operating system determines when synchronization, such as fast mode;no-appendfsync-on-rewrite: Default Value No. When the AOF fsync policy is set to always or EVERYSEC, the background save process performs a large number of I/O operations. In some Linux configurations, Redis may block too many fsync () calls. auto-aof-rewrite-percentage: Default value ofauto-aof-rewrite-min-size: Default value 64MBThe following is a slow log-related setting that records queries that have an execution time exceeding the threshold. The execution time does not include the time taken by I/O operations or sending data to the client, but rather the time it takes to actually execute the command (that is, when the thread blocks cannot accept other requests):Slowlog-log-slower-than: Default value of 10000, in microseconds, defined as slow execution of the threshold;Slowlog-max-len: The default value is 1024, the maximum data for the slow log. Note that this consumes the content resource, and if you want to empty it you can execute the slowlog reset command;The following are virtual memory-related settings, virtual within the 2.4 version of the obsolete, it is not mentioned herevm-enabled NoVm-swap-file/tmp/redis.swapvm-max-memory 0vm-page-sizevm-pages 134217728Vm-max-threads 4The following are the advanced configuration-related settingshash-max-zipmap-entries: The default value of 512, when the number of elements of a map reaches the maximum, but the maximum element length does not reach the set threshold, its hash encoding takes a special way (more efficient use of memory). This parameter is used in combination with the following parameters to set these two thresholds. Set the number of elements;Hash-max-zipmap-value: The default value of 64, sets the maximum length of the value of the element in the map; these twolist-max-ziplist-entries: Default value 512, similar to hash, the list array that satisfies the condition also takes a special way to save space. list-max-ziplist-value: Default value ofset-max-intset-entries: The default value of 512, when the data in a set type is a numeric type, and the number of integral elements in set does not exceed the specified value, a special encoding is used. zset-max-ziplist-entries: Default value of 128, similar to hash and list. zset-max-ziplist-value: Default value ofactiverehashing: The default is yes to control whether the hash is automatically rebuilt. Active rehashing uses 1 microseconds of CPU time per 100 microseconds to reorganize the Redis hash table. Rebuilding is a lazy way, the more you write a hash table, the more steps you need to perform rehashing, and the rehashing operation will execute if the server is currently idle. If the requirement for real-time is high and it is difficult to accept the 2 microsecond latency that Redis occasionally has, you can set activerehashing to No, otherwise the recommended setting is yes to save memory space. The following is a set of included aspectsinclude: Used to specify a file containing other parameters;Create a Conf file (of course, you can also use the Redis redis.conf directly) and set the parameters according to the actual situation, and then when you start the Redis service, specify the configuration file, for example:# more Redis.confdaemonize YesPidfile/data/software/redis/redis.pidPort 6379Logfile/data/software/redis/redis.logdatabasesSave 1SaveSave 10000rdbcompression Yesdbfilename Dump.rdbdir/data/software/redis/# redis-server/data/software/redis/redis.conf3. ApplicationRedis itself also provides a command line client tool that can be used to learn the commands for data manipulation in Redis, where some simple additions and deletions are tested. Insert Data:Redis 127.0.0.1:6379> set name JSSOKQuery data:redis 127.0.0.1:6379> Get name"JSS"change the data, do not say, or setDelete data:redis 127.0.0.1:6379> del name(integer) 1Verify that the key value exists:Redis 127.0.0.1:6379> exists name(integer) 0manipulating structured data:redis 127.0.0.1:6379> hset tech:dep sa(integer) 1redis 127.0.0.1:6379> hset TECH:DEP dba 3(integer) 1Redis 127.0.0.1:6379> hset tech:dep Arch 6(integer) 1redis 127.0.0.1:6379> hgetall tech:dep1) "SA"2) "Ten"3) "DBA"4) "3"5) "Arch"6) "6"Redis commands are very numerous and can be consulted in detail in the official documentation: Http://redis.io/commandsEnglish Bad Friends can refer to this website, it provides the command of the Chinese version of the instructions: http://readthedocs.org/docs/redis/en/latest/Redis supports a wide range of clients, specifically for reference: Http://redis.io/clients. 4, how to help encounter errors! 1, Which:no tclsh8.5 .... Error[[email protected] redis-2.4.10]# make testCD src && make testmake[1]: Entering directory '/data/software/redis-2.4.10/src 'which:no tclsh8.5 in (/usr/local/mysql55/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin :/usr/sbin:/usr/bin:/root/bin)You need ' tclsh8.5 ' on order to run the Redis testmake[1]: * * * [Test] Error 1make[1]: Leaving directory '/DATA/SOFTWARE/REDIS-2.4.10/SRC 'Make : * * * [Test] Error 2Reason: The lack of TCL component solution, installation can, detailed reference http://www.linuxfromscratch.org/blfs/view/cvs/general/tcl.html installation steps:# wget http://downloads.sourceforge.net/tcl/tcl8.5.10-src.tar.gz# tar Xvfz tcl8.5.10-src.tar.gz# CD Tcl8.5.10/unix#./configure--prefix=/usr \--enable-threads \--mandir=/usr/share/man# make# sed-i \-E "[Email protected]^\ (tcl_src_dir= ' \). *@\1/usr/include ' @" \- E "/tcl_b/[email protected]=" \ (-l\) \?. *[email protected]= ' \1/usr/[email protected] "\tclconfig.sh# make install# make Install-private-headers# ln-v-SF tclsh8.5/usr/bin/tclsh# chmod-v 755/usr/lib/libtcl8.5.so

Redis Installation and usage examples

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.