Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Redis installation configuration and Optimization of NoSQL (Theory + practice)

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Relational database and non-relational database A structured database, created on the basis of relational model, is generally oriented to records, including Oracle, MySQL, SQL Server, Microsoft Access, DB2 and other non-relational databases other than mainstream relational databases. Those who are considered to be non-relational include Redis, MongBD, Hbase, Background of non-relational databases such as CouhDB High performance-- requirements for high concurrent reading and writing of databases Huge Storage-- requirements for efficient storage and access of massive data High Scalability & High Availability-- requirements for high scalability and high availability of databases brief introduction to Redis Redis runs based on memory and supports persistence the storage form of key-value (key-value pairs) has the advantages of extremely high data read and write speed and supports rich numbers. Data backup Redis configuration file configuration parameter (/ etc/redis/6379.conf) bind: listener host address port: Port daemonize yes: enable daemon pidfile: specify PID file loglevel notice: log level logfile: specify log file build practice install service environment component And mount the compressed package. Compile and install localhost [root @ root ~] # yum install gcc gcc-c++ make-y / / install environment components [root@localhost ~] # mount.cifs / / 192.168.100.3/LNMP-C7 / mnt/ mount the package Password for root@//192.168.100.8/LNMP-C7: [root ~] # cd / mnt/ [root@localhost mnt] # tar zxvf redis-5.0.7.tar.gz- C / opt/ extract [root@localhost mnt] # cd / opt/redis-5.0.7/ [root@localhost redis-5.0.7] # make / / compile [root@localhost redis-5.0.7] # make PREFIX=/usr/local/redis/ install / / install and execute the configuration Redis configuration file script And configure [root@localhost redis-5.0.7] # cd utils/ [root@localhost utils] #. / install_server.sh / / execute the script to configure Welcome to the redis service installerThis script will help you easily set up a running redis serverPlease select the redis port for this instance: [6379] / / default port number Selecting default: 6379Please select the redis config file name [/ etc/redis/6379.conf] / / configuration file storage Location Selected default-/ etc/redis/6379.confPlease select the redis log file name [/ var/log/redis_6379.log] / / Log file location Selected default-/ var/log/redis_6379.logPlease select the data directory for this instance [/ var/lib/redis/6379] / / data file location Selected default-/ var/lib/redis/6379Please select the redis executable path [] / usr/local/redis/bin/redis -server / / executable file path [root@localhost utils] # ln-s / usr/local/redis/bin/* / usr/local/bin/ create a link file to make it easy for the system to recognize [root@localhost utils] # netstat-ntap | grep 6379 / / check whether the port tcp 0 0127.0.0.1 ln 6379 0.0.0.0 is open : * LISTEN 44510/redis-server [root@localhost utils] # / etc/init.d/redis_6379 stop / / close the redis command Stopping. Redis stopped [root@localhost utils] # / etc/init.d/redis_6379 start / / Open the redis command Starting Redis server... [root@localhost utils] # vim / etc/redis/6379.conf / / modify Configuration file bind 127.0.0.1 192.168.144.128 / configure the listening address [root@localhost utils] # / etc/init.d/redis_6379 restart / / restart the redis service Stopping. Redis stoppedStarting Redis server...Redis Database Common commands redis-cli Command Line tool connects to the local database [root@localhost utils] # / usr/local/redis/bin/redis -cli127.0.0.1:6379 > Connect to the remote database [root@localhost utils] # redis-cli-h 192.168.144.128-p 6379192.168.144.128 redis-cli 6379 >

Get command help

192.168.144.128SET key value 6379 > help set # # help help SET key value [expiration EX seconds | PX milliseconds] [NX | XX] summary: Set the string value of a keysince: 1.0.0group: stringset: store data 192.168.144.128SET key value 6379 > set teacher zhangsanOK192.168.144.128:6379 > set tea redOK

Get: getting data

192.168.144.128keys 6379 > get tea # # View key values "red" key related commands keys: get a list of key values that conform to the rules 127.0.0.1key 6379 > keys * / / View all keys in the current database 127.0.0.1keys V* / / View the keys in the current database that begin with v 127.0.0.1 key 6379 > keys v? / View when In the previous database, the key 127.0.0.1 containing any character followed by v > keys vault? / / View the key in the current database that contains any two characters beginning with v. 192.168.144.128teacher 6379 > KEYS * # View all keys 1) "teacher" 2) "tea" 192.168.144.1286379 > keys tweak? # after checking that the key starts with t Face is two characters of 1) "tea" exists: determine whether the key value exists 192.168.144.128 integer 6379 > EXISTS tea (integer) 1 / / 1 is 192.168.144.128 integer 6379 > EXISTS teas (integer) 0 / / 0 does not exist del: delete the current database of the specified key192.168.144.128:6379 > del teacher # # delete key (integer) 1192.168. 144.128type tea 6379 > KEYS * 1) "tea" type: get the value value type 192.168.144.128key 6379 > type tea # # View key type stringrename / renamenx (not overwritten): rename the existing key 192.168.144.128type tea 6379 > rename tea T1 # rename the key OK192.168.144.128:6379 > keys * 1) "T1" 192.168.144.1286379 > get T1 "red" "dbsize: view the number of key in the current database 192.168.144.128 key 6379 > dbsize (integer) 1redis-benchmark Test tool-h: specify server hostname-p: specify server port-c: specify number of concurrent connections-n: specify number of requests-d: specify the data size of the SET/ get value in bytes-Q: force the introduction of redis. Only show query/ secvalue [root@localhost utils] # redis-benchmark-h 192.168.144.128-p 6379-c 100-n 100000 / / concurrent 100000 100000 requests = SET = 100000 requests completed in 1.14 seconds / / time spent 100 parallel clients3 bytes payloadkeep alive: 184.66% select 10 / / enter the 11th library OK192.168.144.128:6379 [10] > keys * / / View key 1) "T1" 192.168.144.1286379 [10] > get T1 "red" clears the data in the database 192.168.144.128 parallel clients3 bytes payloadkeep alive 6379 [10] ] > flushdb / / clear data OK192.168.144.128:6379 in the library [10] > keys * / / View all keys (empty list or set) Redis persistence Overview Redis is running in memory Data in memory lost due to power outage in order to reuse Redis data or prevent system failure, we need to write the data in Redis to disk space, that is, persistent classification RDB mode: create snapshots to obtain copies of all data in Redis at a certain time advantages: low resource consumption disadvantages: can not guarantee the real-time preservation of data

AOF mode: write the executed write command to the end of the file and log the changes in the data.

Advantages: disadvantages of real-time data recording: large consumption of resources, long synchronization time when data is lost, default persistence mode of RDB persistence Redis default file name trigger condition within a specified time interval, perform a specified number of write operations (configuration file control) execute save or bgsave (asynchronous) commands execute flushall commands, empty all data in the database (not recommended) execute shutdown commands Ensure that the server shuts down normally and does not lose any data advantages and disadvantages are suitable for large-scale data recovery. If the business does not require high data integrity and consistency, RDB is a good choice. When backup takes up memory to recover data through RDB files, copy dump.rdb files to the bin directory of redis's installation directory, and restart the redis service.

Profile option

[root@localhost utils] # vim / etc/redis/6379.conf save 9001 / / at least one write within 900s save 300 10 / 300 seconds at least 10 write operations save 60 10000 / / at least 10000 write operations within 60 seconds Snapshot operations will be triggered as long as one of them is met. Note that all save entries close RDBdbfilename dump.rdb / / backup file name dir / var/lib/redis/6379 / / backup file save directory rdbcompression yes / / enable compressed AOF persistence Redis default is not enabled to make up for the deficiency of RDB (data inconsistency) use log form to record each write operation And append to the file Redis restart will execute the write instruction from front to back according to the contents of the log file to complete the data recovery work. According to the AOF file recovery data, copy the appendonly.aof file to the bin directory of the redis installation directory, and restart the redis service.

AOF persistent configuration

[root@localhost utils] # vim / etc/redis/6379.conf appendonly yes / / enable AOF persistence appendfilename "appendonly.aof" / / AOF file name # appendfsync always / / always: synchronous persistence, each data change will be immediately written to disk appendfsync everysec / / everysec: recommended by default Asynchronous records per second (default) # appendfsync no / / no: out of sync, it is left to the operating system to decide how to synchronize aof-load-truncated yes / / ignore the last potentially problematic instruction AOF rewriting mechanism AOF works by appending writes to the file, the redundant content of the file will become more and more when the size of the AOF file exceeds the set threshold Redis will compress the contents of the AOF file the principle of AOF rewriting Redis will fork a new process, read the data in memory (not read the old file), and rewrite it to a temporary file, and finally replace the old aof file AOF rewrite configuration [root@localhost utils] # vim / etc/redis/6379.conf no-appendfsync-on-rewrite no / / when the log is BGREWRITEAOF If it is set to yes, it means that the new write operation does not synchronize fsync and is only temporarily stored in the buffer to avoid disk I0 operation conflicts and write after the rewrite is completed. In redis, the default is noauto-aof-rewrite-percentage 100 / / when the current AOF file size is twice the size of the AOF file in the last log rewrite, the BGREWRITEAOF operation occurs auto-aof-rewrite-min-size 64mb / / the minimum value for the current AOF file to execute the BGREWRITEAOF command Avoid frequent BGREWRITEAOFRedis performance management due to small file size at the beginning of starting Reids View redis memory usage [root@localhost utils] # / usr/local/redis/bin/redis-cli127.0.0.1:6379 > info memory memory fragmentation rate is calculated by dividing the memory value allocated by the system used_memory_rss by the memory value used by redis, it is calculated that the memory fragmentation is inefficiently allocated / recycled by the operating system. Discontiguous physical memory allocation caused by memory

Tracking memory fragmentation rates is very important to understand the resource performance of redis instances

It is reasonable that the memory fragmentation rate is slightly greater than 1. This value indicates that the memory fragmentation rate is relatively low and exceeds 1.5, indicating that redis consumes 150% of the actual physical memory, of which 50% is the memory fragmentation rate less than 1, indicating that the Redis memory allocation exceeds the physical memory. The memory utilization rate of the redis instance is more than the maximum available memory. The operating system will start swapping memory and swap space

Avoid memory swapping

For cache data size, choose to use Hash data structure to set key expiration time recovery key to ensure reasonable allocation of redis limited memory resources when memory usage reaches the set maximum threshold. You need to select a recycling policy for key. By default, the recycling policy forbids deleting the maxmemory-policy attribute value from the redis.conf configuration file. Volatile-lru: use the LRU algorithm to phase out data from datasets that have set expiration times. Volatile-ttl: select expired data from datasets with expiration times set (recommended) volatile-random: from datasets with expiration times set Random data selection in Hezhong to eliminate allkeys-lru: use the LRU algorithm to phase out data from all data sets allkeys-random: randomly select data from data sets to phase out no-enviction: prohibit data elimination

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report