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

What is the method of building redis and setting up data?

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

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what is the method of building redis and setting up data". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. compilation and installation of redis

1. Dependent system packages

Yum install-y wget gcc make tcl

2. Download the package address

1. Download address http://download.redis.io/releases/2 of each version of redis, and install the latest version of 4.0.9wget http://download.redis.io/releases/redis-4.0.9.tar.gz in this article.

3. Compile and install

1. Extract: tar xf redis-4.0.9.tar.gz & & cd redis-4.0.92, compile: make3, compile Test: make test4, install: make PREFIX=/usr/local/redis install5, copy the configuration file to the configuration folder: mkdir / usr/local/redis/etc & & cp redis-4.0.9/redis.conf / usr/local/redis/etc/ II, configuration parameters of redis

Necessary parameter settings

Setting the "daemonize" property to "yes" means that we will start the Redis service as a background process; set the "port" property to the specified port, where the default is "6379"; set the "logfile" property to the specified log path; set "dir" to the specified working directory and set "requirepass" to the local connection password. The rest of the properties can be kept by default.

Other parameter settings

Bind: specify that redis will only receive requests from this IP. If not, all requests will be processed. It is best to set this protected-mode in the production process: protection mode, default is on, only local client connections are allowed, password can be set or bind can be added to connect to port: specify the port on which redis is running, default is the maximum number of 6379tcp-backlog:TCP listeners, in a high concurrency environment You need to increase this value to avoid the problem of slow client connections. The Linux kernel will reduce this value to the corresponding value of / proc/sys/net/core/somaxconn. To increase the concurrency, you need to modify these two values to achieve the goal. Default is 511timeout: specify how many seconds after a client is idle to close the connection (0 means never close) tcp-keepalive: in seconds, SO_KEEPALIVE will be used periodically to check whether the client is still in a healthy state to avoid server blocking. The official recommended value is 300s. If set to 0, daemonize will not be detected periodically: redis does not run as a daemon by default If you want it to run in the background, you can change it to yes. When redis runs as a daemon, it writes a pid to the / var/run/redis.pid file supervised: the Redis daemon can be managed through upstart and systemd. Default is no. Do not start interactive supervised no-do not supervise interactive supervised upstart-start signal supervised systemd-signal systemd write READY = 1 to $NOTIFY_SOCKET supervised auto by putting Redis in SIGSTOP mode-detect upstart or systemd methods based on UPSTART_JOB or NOTIFY_SOCKET environment variable pidfile: configure the PID file path when redis runs as a daemon It will write pid to the / var/redis/run/redis_6379.pid file by default loglevel: define the log level, which can be the following values: debug (record a lot of log information, suitable for development and testing) verbose (more log information) notice (appropriate amount of log information) Warning (only some important and critical information will be recorded) logfile: location of the log file syslog-enabled: if you want to record the log to the system log, change it to yes, or optionally update other syslog parameters to meet your requirements syslog-ident: set the IDsyslog-facility of the system log: specify the system log settings Must be a value between USER or LOCAL0-LOCAL7 databases: sets the number of databases. The default database is DB 0, and you can use the select command to select a different database on each connection. Dbid is a value between 0 and databases-1. Whether always-show-logo:redis displays loginsave at startup: save data to disk save 900 1dbid within 900 seconds according to a given time interval and number of writes. If the value of at least 10 key changes within 300 seconds, save save 60 10000 key within 60 seconds if the value of at least 10000 key changes, save stop-writes-on-bgsave-error: if the user turns on the RDB snapshot function, then if the redis persists data to disk if it fails, by default, redis will stop accepting all write requests rdbcompression: for snapshots stored on disk You can set whether to compress storage, if so, redis will use LZF algorithm to compress rdbchecksum: after storing snapshots, we can also let redis use CRC64 algorithm for data verification, but doing so will increase the performance consumption by about 10%. If you want to get the maximum performance improvement, you can turn off this feature dbfilename: set snapshot file name dir: set snapshot file storage path This configuration item must be a directory, not the file name requirepass: local connection password slaveof: master-slave copy, use slaveof to make one redis instance a copy of another reids instance, turn off masterauth by default: if master requires password authentication, set it here. Default does not set slave-serve-stale-data: when a slave loses contact with master, or when replication is in progress Slave may have two manifestations: 1) if yes, slave will still answer client requests, but the data returned may be out of date, or the data may be empty at the time of the first synchronization 2) if no, when you execute a command other than info he salveof Slave will return a "SYNC with master in progress" error slave-read-only: you can configure whether a slave entity accepts a write operation appendonly: the default redis uses rdb persistence, which is sufficient in many applications. However, if the redis goes down midway, it may result in a few minutes of data loss. Persistence is based on save strategy. Append Only File is another persistence method, which can provide better persistence characteristics. Redis will write the data written to the appendonly.aof file after it is received. When starting, Redis will first read the data of this file into memory, ignoring the configuration appendfsync always of the RDB file appendfilename:aof filename appendfsync:aof persistence policy first, indicating that fsync is executed every write to ensure that the data is synchronized to disk appendfsync everysec, indicating that fsync is executed every second, which may result in the loss of the 1s data appendfsync no. Does not execute fsync, and the operating system ensures that the data is synchronized to disk. The fastest no-appendfsync-on-rewrite: specifies whether to call fsync during the background aof file rewrite. The default is no, indicating that fsync is to be called (regardless of whether there is a child process in the background or not) Redis has a large number of disk IO when writing RDB files or rewriting afo files in the background. In some linux systems, calling fsync may block auto-aof-rewrite-percentage: specify the condition for redis to rewrite aof files, which defaults to 100. compared with the aof file size of the last rewrite, background rewrite will be triggered when the growth of the current aof file exceeds 100% of the last afo file size. If configured to 0, automatic rewriteauto-aof-rewrite-min-size is disabled: specify the size of the aof file that triggers the rewrite. If the aof file is less than this value, automatic rewrite will not be triggered even if the increment ratio of the current file reaches the configuration value of auto-aof-rewrite-percentage. That is, when these two configuration items are satisfied at the same time, it will trigger whether the incremental file synchronization policy will be adopted in the rewriteaof-rewrite-incremental-fsync:aof rewrite process. The default is "yes". During rewrite, file synchronization is performed for every 32m data, which can reduce the number of operations on disk written to large aof files. Slowlog-log-slower-than:slog log is used to record the slow execution of commands while redis is running. 3. Start a single instance of redis

Start

1 、 cd / usr/local/redis2 、. / bin/redis-server. / etc/redis.conf

Client command line entry

1. Cd / usr/local/redis2,. / bin/redis-cli-h 127.0.0.1-p 6379-a 123456 127.0.0.1 bin/redis-cli 6379 > set name 'beijing' OK 127.0.0.1 bin/redis-cli 6379 > get name "beijing"

Close

1. Cd / usr/local/redis2,. / bin/redis-cli-h 127.0.0.1-p 6379-a 123456 shutdown IV. Master-slave configuration startup of redis

1. Master, configuration file modification

Port 6379logfile "/ data/redis/data6379/redis.log" pidfile / var/run/redis_6379.piddir / data/redis/data6379requirepass 123456 main redis is generally configured to persist to hard disk # save 900 1#save 300 10#save 60 10000appendonly no without using RDB and AOF

2. Slave, configuration file modification

Port 6380logfile "/ data/redis/data6380/redis.log" pidfile / var/run/redis_6380.piddir / data/redis/data6380requirepass 123456slaveof 127.0.0.1 6379masterauth 123456slave-read-only yes from redis can be configured for RDB and AOF persistence to hard disk save 900 1save 300 10save 60 10000appendonly yes

3. Start master and slave

1 、 cd / usr/local/redis2 、. / bin/redis-server. / etc/redis6379.conf3 、. / bin/redis-server. / etc/redis6380.conf

4. Verify master-slave write data synchronization

Master [root@dbtest1 redis] #. / bin/redis-cli-h 127.0.0.1-p 6379-a 123456 127.0.0.1 bin/redis-cli 6379 > get name (nil) 127.0.0.1 bin/redis-cli 6379 > set name "beijing" OK 127.0.1 slave [root@dbtest1 redis] #. / bin/redis-cli-h 127.0.0.1-p 6380-a 123456 127.0.1 6380 > get name "beijing" 5. The difference between RDB and AOF in data storage

The write-back mode of RDB

Synchronous writeback is the SAVE command, and the main process writes back data directly to the disk. In the case of large data, the system will be suspended for a long time, so it is not the recommended asynchronous writeback or BGSAVE command. After fork, the main process copies itself and writes back to the disk through this new process. After the writeback ends, the new process shuts down itself. Since this does not require the main process to block, the system will not fake death.

Characteristics of RDB

* RDB is Snapshot snapshot storage and is the default persistence mode * it can be understood as semi-persistence mode, that is, data is saved to disk periodically according to a certain policy, save: data is saved to disk according to a given time interval and number of writes * the corresponding data file is dump.rdb, and the snapshot period is defined through the save parameter in the configuration file * the RDB file of Redis will not break. Because the write operation is carried out in a new process * when a new RDB file is generated, the child process generated by Redis will first write the data to a temporary file, and then rename the temporary file to RDB file through atomic rename system calls, so that Redis's RDB file is always available at any time of failure.

Characteristics of AOF

* AOF (Append-Only File) has better persistence than RDB method * because when using AOF persistence mode, Redis appends every write command received to the file through the Write function. Binlog* similar to MySQL rebuilds the contents of the entire database in memory by re-executing the write command saved in the file when Redis restarts. * the corresponding setting parameter is: * appendonly yes Enable AOF persistence method * the name of the appendfilename appendonly.aof,AOF file. The default is appendonly.aof * appendfsync always. Every time you receive a write command, it is forced to write to disk immediately. It is the most guaranteed complete persistence, but the speed is also the slowest. Generally, it is not recommended to use * appendfsync everysec. It is forced to write to disk once per second. It makes a good compromise between performance and persistence. It is the recommended way * appendfsync no. Completely dependent on OS writes, usually about once in 30 seconds. Performance is the best but persistence is the least guaranteed. * AOF's full persistence method is not recommended. At the same time, the persistence file will become larger and larger. In order to compress the AOF persistence file, Redis provides the bgrewriteaof command. After receiving this command, Redis will save the data in memory to a temporary file in a way similar to a snapshot, and finally replace the original file, so as to control the growth of the AOF file.

Restore data according to the following priorities

1. If only AOF is configured, AOF files will be loaded when restarting to restore data 2. If both RDB and AOF are configured, only AOF files will be loaded to restore data at startup. 3. If only RDB is configured, RDB files will be loaded to restore data (it should be noted that when using AOF backup data to restore data, the main library needs to open appendonly yes before the data is restored. When using RDB backup data to recover data, it is not necessary for the main database to open the corresponding parameters) VI. Processing after the main library downtime

1. Downtime occurs in the main database

Simulation of main library downtime 1, cd / usr/local/redis2,. / bin/redis-cli-h 127.0.0.1-p 6379-a 123456 shutdown

2. Cancel the synchronization status on the slave to prevent the master database from restarting before the data recovery is completed, thus directly overwriting the data on the slave database, resulting in the loss of all data.

127.0.0.1 6380 > slaveof no one

3. Copy the slave data to the host

1. Copy AOF data to restore cp data6380/appendonly.aof data6379/appendonly.aof master library. / etc/redis6379.conf enable appendonly yes2, copy RDB data to restore 127.0.0.1 etc/redis6379.conf 6380 > BGSAVEcp data6380/dump.rdb data6379/dump.rdb

4. Restart the main library

1 、 cd / usr/local/redis2 、. / bin/redis-server. / etc/redis6379.conf

5. Re-configure master-slave synchronization. At this time, there is no need to specify a password. There is an original record.

127.0.0.1 slaveof 6380 > 127.0.0.1 6379 "what is the method of redis building and data storage" is introduced here, thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Database

Wechat

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

12
Report