MysqL#Redis stand-alone deployment, master-slave synchronization

=============================================

One, compile and install stand-alone redis, add redis systemctl management

[root@redis-master ~]# mkdir -p /data/application     ---创建工作目录
[root@redis-master ~]# wget http://download.redis.io/releases/redis-4.0.9.tar.gz   ---下载redis
[root@redis-master ~]# tar xzf redis-4.0.9.tar.gz -C /data/application/    ---解压
[root@redis-master ~]# cd /data/application/
[root@redis-master application]# mv redis-4.0.9/ redis
[root@redis-master application]# cd redis/
[root@redis-master redis]# yum install -y gcc make  #安装编译工具
[root@redis-master redis]# make
直接make编译就行
编译完成,安装成功
注:如果报错请将刚才解压的安装包删除掉,再次重新解压并进行make安装即可。
[root@redis-master redis]# cp redis.conf redis.conf.bak
#redis通过多个配置文件创建多节点实例
[root@redis-master redis]# vim redis.conf     ---修改如下
bind 192.168.246.202  #只监听内网IP
daemonize yes     #开启后台模式将on改为yes,开启后可以在后台运行
timeout 300      #连接超时时间
port 6379                      #端口号
dir /data/application/redis/data  #本地数据库存放持久化数据的目录该目录-----需要存在
pidfile /var/run/redis_6379.pid  #定义pid文件
logfile /var/log/redis.log  #定义log文件
创建存放数据的目录
[root@redis-master redis]# mkdir /data/application/redis/data
配置redis为systemctl启动
[root@redis-master redis]# cd /lib/systemd/system
[root@redis-master system]# vim redis.service
[Unit]
Description=Redis
After=network.target

[Service]
ExecStart=/data/application/redis/src/redis-server /data/application/redis/redis.conf  --daemonize yes
ExecStop=/data/application/redis/src/redis-cli -h 127.0.0.1 -p 6379 shutdown

[Install]
WantedBy=multi-user.target
=====================
参数详解:
• [Unit] 表示这是基础信息 
• Description 是描述
• After 是在那个服务后面启动,一般是网络服务启动后启动

• [Service] 表示这里是服务信息 
• ExecStart 是启动服务的命令
• ExecStop 是停止服务的指令

• [Install] 表示这是是安装相关信息 
• WantedBy 是以哪种方式启动:multi-user.target表明当系统以多用户方式(默认的运行级别)启动时,这个服务需要被自动运行。
===================
启动服务:
[root@redis-master system]# systemctl daemon-reload
#加载一下刚才写的启动文件
[root@redis-master system]# systemctl start redis.service

Two, redis login test and simple use

登陆redis
./redis-cli -a password   如果指定了密码
内部:Auto  tab
[root@redis-master system]# cd /data/application/redis/src/
[root@redis-master src]# ./redis-cli -h 192.168.246.202 -p 6379
192.168.246.202:6379> ping     ---测试redis是否可以用
PONG
192.168.246.202:6379> set name newrain    #设置key--name,并设置值
OK
192.168.246.202:6379> get name    #获取到key
"newrain"
192.168.246.202:6379>
单机版redis已经部署完成。将ip和端口发给开发就可以了。

Three, redis related management tools

./redis-benchmark     #用于进行redis性能测试的工具 
./redis-check-dump    #用于修复出问题的dump.rdb文件
./redis-cli           #redis的客户端
./redis-server        #redis的服务端
./redis-check-aof     #用于修复出问题的AOF文件
./redis-sentinel      #用于集群管理

Fourth, deploy three machines redis-master-slave synchronization

redis-master----192.168.246.202
redis-slave-1-----192.168.246.203
redis-slave-2-----192.168.246.204
1.首先三台服务器将redis部署完成。
2.编辑master的redis配置文件:
[root@redis-master ~]# cd /data/application/redis/
[root@redis-master redis]# vim redis.conf
设置监听ip地址:bind 0.0.0.0
关闭加密保护:protected-mode no
2.修改slave1、slave2的配置文件:
[root@redis-slave-1 ~]# cd /data/application/redis/
[root@redis-slave-1 redis]# vim redis.conf      ---修改如下:
设置监听ip地址:bind 0.0.0.0
关闭加密保护:protected-mode no
搜索/slaveof
添加master的内网ip和端口
slaveof 10.11.67.31 6379
如果主服务器有密码(从服务器如果设置密码,设置成和住服务器一样)
masterauth 123
4.重启三台redis
[root@redis-master redis]# systemctl restart redis.service
5.测试主从
master 有读写的权利
slave 只有读的权利

1.在master上面执行
[root@redis-master redis]# cd src/
[root@redis-master src]# ./redis-cli 
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set name xuan
OK
127.0.0.1:6379> get name
"xuan"
127.0.0.1:6379>
2.分别在slave-1和slave-2上面执行:
[root@redis-slave-1 redis]# cd src/
[root@redis-slave-1 src]# ./redis-cli 
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> get name
"xuan"
127.0.0.1:6379>
[root@redis-slave-2 src]# ./redis-cli 
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> get name
"xuan"
127.0.0.1:6379>
查看复制状态
master执行:
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.246.203,port=6379,state=online,offset=490,lag=0
slave1:ip=192.168.246.204,port=6379,state=online,offset=490,lag=1
==============================================================================
slave上面执行:
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.246.202
master_port:6379
master_link_status:up

Five, detailed explanation of redis configuration file

1、include

Explanation: Introduce other configuration files. For example, when you have multiple servers and some configuration items are common to them, then you can write these common configuration items into a configuration file common.conf, and then these servers include this configuration file, and these servers themselves The configuration items are written in their own configuration files.

Example: include /path/to/common.conf

2、loadmodule

Explanation: This configuration item is not yet understood. The note is this: load the module at startup, if the server cannot load the module, this configuration will be ignored. You can use multiple loadmodules.
Example: loadmodule /path/to/my_module.so

3、bind

Explanation: By default, redis listens for client connections on all valid network interfaces on the server. If you only want it to listen on one or more network interfaces, then you bind one IP or multiple IPs. Multiple ip spaces can be separated.

Example: bind 192.168.1.100 10.0.0.1
bind 127.0.0.1 ::1

4、protected-mode

Explanation: When it is turned on, public network access to redis is prohibited. There are two conditions for it to be enabled. The first is that bind is not used, and the second is that no access password is set.

Example: protected-mode yes

5、port

Explanation: Specify the port number that the redis server monitors. The default is 6379, if 0 is specified, it will not listen.

Example: port 6379

6、tcp-backlog

Explanation: This parameter determines the length of the completed queue (after the three-way handshake) in the TCP connection. Of course, this value must not be greater than the /proc/sys/net/core/somaxconn value defined by the Linux system. The default is 511, while the Linux version The default parameter value is 128. When the system concurrency is large and the client speed is slow, you can set these two parameters together.
Example: tcp-backlog 511
7. Unixsocket
explanation: Specify the path of the unix socket.
Example: unixsocket /tmp/redis.sock
8. unixsocketperm
explanation: Specify the permissions of unix socket file.

Example: unixsocketperm 755
9, timeout

Explanation: How many seconds after the client is idle, the connection is closed. If it is set to 0, the function is closed.

Example: timeout 30
10, tcp-keepalive
Explanation: The unit is seconds, which means that SO_KEEPALIVE will be used periodically to check whether the client is still in a healthy state to avoid the server being blocked. The official recommended value is 300S.
Example: tcp-keepalive 300

11、daemonize

Explanation: Whether to start in daemon mode, the default is no, when configured as yes, start in daemon mode, then redis instance will write the process number pid to the default file /var/run/redis.pid.

Example: daemonize yes

12. Supervised
explanation: Redis daemon can be managed through upstart and systemd. This parameter is related to the specific operating system.

Example: supervised no

13. pidfile
explanation: configure the pid file path. When redis is started in daemon mode, if pidfile is not configured, the default value of pidfile is /var/run/redis.pid.
Example: pidfile /var/run/redis_6379.pid

14、loglevel

Explanation: Log level. The options are: debug (record a large amount of log information, suitable for development and testing phases); verbose (more log information); notice (appropriate log information, used in the production environment); warning (only some important and key information will be used) is recorded).

Example: loglevel notice

15.
Logfile explanation: The location of the log file, when specified as an empty string, is the standard output. If redis is running in daemon mode, the log will be output to /dev/null.

Example: logfile ""

16、syslog-enabled

Explanation: Whether to record the log in the system log. (What's the meaning?)
Example: syslog-enabled no
17, syslog-ident

Explanation: Set the id of the system log

Example: syslog-ident redis
18, syslog-facility
Explanation: Specify the syslog facility (facility), which must be user or local0 to local7.

Example: syslog-facility local0

19、databases

Explanation: Set the number of databases. The default database is DB 0. You can use the select <dbid> command to select a different database on each connection. dbid is a value between 0 and databases-1.
Example: databases 16

 

20、save

Explanation: Save data to disk. The format is: save <seconds> <changes>, which means that at least changes keys will be saved once after seconds.

Example:
save 900 1
save 300 10
save 60 10000

21、stop-writes-on-bgsave-error

Explanation: By default, if redis's last background save fails, redis will stop accepting write operations. This will let users know in a tough way that data cannot be persisted to disk correctly, otherwise no one will notice the disaster occur. If the background save process restarts, redis will also automatically allow write operations. However, if you install reliable monitoring, you may not want redis to do this, then you can change it to no.
Example: stop-writes-on-bgsave-error no

22、rdbcompression

Explanation: Whether to compress the string when dumping the .rdb database, the default setting is yes. If you want to save some cpu resources, you can set it to no, so the data set may be relatively large.

Example: rdbcompression yes
23, rdbchecksum
Explanation: Whether CRC64 checks the rdb file, there will be a certain performance loss (about 10%).

Example: rdbchecksum yes

24、dbfilename

Explanation: The name of the rdb file.

Example: dbfilename dump.rdb
25, dir
Explanation: The database storage directory. It must be a directory, and the aof file will also be saved in this directory.

Example: dir
./26, slaveof

Explanation: Set this machine as slave service. Format: slaveof <masterip> <masterport>. Set the IP address and port of the master service. When Redis starts, it will automatically synchronize data from the master.

Example: slaveof 192.168.0.1 6379

27. Masterauth
explanation: When the master service is set to password protection, the slav service connects to the master password.

示例:masterauth 123456
28、slave-serve-stale-data

Explanation: When a slave loses contact with the master, or when replication is in progress, the behavior of the slave in response to the request: 1) If yes (the default value), the slave will still answer the client request, but the returned data may be out of date. Or the data may be empty during the first synchronization; 2) If it is no, the slave will return a "SYNC with master in progress" error when you execute other commands except info and salveof.
Example: slave-serve-stale-data yes
29, slave-read-only

Explanation: Set whether the slave is read-only. From version 2.6, slave is read-only by default.

Example: slave-read-only yes

30. Repl-diskless-sync
explanation: Whether the master-slave data replication uses the diskless replication function.

Example: repl-diskless-sync no

31、repl-ping-slave-period

Explanation: Specify the period for the slave to ping the master regularly, the default is 10 seconds.

Example: repl-ping-slave-period 10

32、repl-timeout

Explanation: Set the main database batch data transmission time or ping reply time interval, the default value is 60 seconds.
Example: repl-timeout 60

33. Repl-disable-tcp-nodelay
explanation: Specify whether to disable the NO_DELAY option of the socket when synchronizing data to the slave. If configured as "yes", NO_DELAY is disabled, and the TCP protocol stack will merge small packets and send them uniformly. This can reduce the number of packets between the master and slave nodes and save bandwidth, but it will increase the time for data synchronization to the slave. If it is configured as "no", it indicates that NO_DELAY is enabled, and the TCP protocol stack will not delay the sending timing of small packets, so the data synchronization delay will be reduced, but a larger bandwidth is required. Normally, it should be configured to no to reduce synchronization delay, but when the network load between the master and slave nodes is already high, it can be configured to yes.

示例:repl-disable-tcp-nodelay no
34、repl-backlog-size

Explanation: Set the capacity of the master-slave replication backlog. This backlog is a buffer used to store slave data when slaves are disconnected, so when a slave wants to reconnect, it usually doesn’t want to resynchronize all of it, only partial synchronization is enough, just pass the slave when disconnected This part of the data is missing. The larger the value, the longer the time that the Salve can disconnect.

Example: repl-backlog-size 1mb

35. Repl-backlog-ttl
explanation: Configure how many seconds after the master and slave lose contact, clear the backlog to release space. When configured as 0, it means that it will never be cleared.

Example: repl-backlog-ttl 3600

36.
Slave -priority explanation: When the master fails to work normally, Redis Sentinel will select a new master from slaves. The smaller the value, the more preferred it will be selected, but if it is 0, it means this The slave cannot be selected. The default priority is 100.

Example: slave-priority 100
37, requirepass

Explanation: Set the redis connection password.

Example: requirepass foobared
38, rename-command
Explanation: Rename the command. For safety reasons, some important and dangerous commands can be renamed. When you rename a command to an empty string, you cancel the command.

示例:rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52

rename-command CONFIG “”
39、maxclients

Explanation: Set the maximum number of concurrent client connections. The default is unlimited. The number of client connections that Redis can open at the same time is the maximum number of file descriptors that the Redis process can open-32 (redis server itself will use some). If you set maxclients 0, Indicates no restriction. When the number of client connections reaches the limit, Redis will close the new connection and return a max number of clients reached error message to the client.

Example: maxclients 10000
40, maxmemory
Explanation: Specify the maximum memory limit of Redis. Redis will load data into memory when it starts. After the maximum memory is reached, Redis will first try to clear expired or about to expire keys. When this method After processing, the maximum memory setting is still reached, and no more write operations can be performed, but read operations can still be performed. Redis’s new vm mechanism will store Key in memory and Value in swap area, format: maxmemory <bytes>.

Example: maxmemory 1024000

41. Explanation of maxmemory-policy
: A clear strategy used by redis when memory usage reaches its maximum. There are the following options (Obviously there are 6 types, but the official configuration file says there are 5 options?):

1) Volatile-lru uses the LRU algorithm to remove the key with an expiration time (LRU: Least Recently Used)
2) allkeys-lru uses the LRU algorithm to remove any key

3) volatile-random removes random keys with expiration time set

4) allkeys-random removes random keys
5) volatile-ttl removes keys that are about to expire (minor TTL)

6) noeviction noeviction does not remove any key, but returns a write error. Default option
Example: maxmemory-policy noeviction

42. Explanation of maxmemory-samples
: Neither LRU nor minimal TTL algorithms are accurate algorithms, but relatively accurate algorithms (in order to save memory), you can choose the sample size for detection at will. Redis selects 3 samples for detection by default, and you can set the number of samples through maxmemory-samples.

Examples: maxmemory-samples 5
43, appendonly

Explanation: Whether to enable AOF persistence. That is, whether to log after each update operation. The default configuration is no, that is, the data is written to the disk in an asynchronous manner. If it is not turned on, some data may be lost when the power is off.

Example: appendonly no

44、appendfilename

Explanation: The name of the update log file, the default value is appendonly.aof.

示例:appendfilename “appendonly.aof”
45、appendfsync

Explanation: The frequency of refreshing the aof file. There are three types:

1) no Rely on OS to refresh, redis does not actively refresh AOF, which is the fastest, but the security is poor.
2) Always call fsync to refresh the AOF file every time a modification command is submitted, which is very, very slow, but also very safe.
3) Everysec calls fsync every second to refresh to the AOF file, which is very fast, but data within one second may be lost.
Example: appendfsync everysec
46, no-appendfsync-on-rewrite
Explanation: Specify whether to call fsync during the background AOF file rewrite, the default is no, which means that fsync is to be called (regardless of whether there is a child process in the background refreshing). During Redis writing RDB files or rewriting AOF files in the background, there will be a lot of disk IO. At this time, in some Linux systems, calling fsync may be blocked.

示例:no-appendfsync-on-rewrite no

47、auto-aof-rewrite-percentage

Explanation: When the AOF file grows to a certain size, Redis can call BGREWRITEAOF to rewrite the log file. When the growth rate of the AOF file size is greater than this configuration item, rewriting is automatically turned on.

Example: auto-aof-rewrite-percentage 100

48、auto-aof-rewrite-min-size

 

Explanation: When the AOF file grows to a certain size, Redis can call BGREWRITEAOF to rewrite the log file. When the AOF file size is larger than this configuration item, the rewriting is automatically enabled.

Example: auto-aof-rewrite-min-size 64mb

49. Aof-load-truncated
explanation: Redis can load the truncated AOF file when it starts, without the need to execute the redis-check-aof tool first.

Example: aof-load-truncated yes

50, taking time-limit

Explanation: The longest execution time of a Lua script, in milliseconds, if it is 0 or a negative number means unlimited execution time, the default is 5000.

Example: lua-time-limit 5000

Redis master/slave replication:

Redis's master/slave data replication method can be one-master-one-slave or one-master-multi-slave mode. Redis
is in the non-blocking mode of the master, which means that when the slave performs data synchronization, the master can accept client
requests. It does not affect the consistency of synchronized data. However, it is in blocking mode on the slave side. When the slave synchronizes master
data, it cannot respond to the client's query
. In the master/slave mode of Redis, the master provides data read and write services, while the slave only Provide reading services

Redis master/slave configuration method is to
add in the redis.conf configuration file under the Redis directory of the slave host :
slaveof master_ip master_port

For example:
we configure our slave as: redis-slave.conf
daemonize yes
pidfile redis-slave.pid
port 6380
timeout 300
loglevel verbose
logfile stdout
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump-slave.rd

dir /home/falcon/redis-2.0.0/
slaveof 127.0.0.1 6379
appendonly no
appendfsync everysec
vm-enabled no
vm-swap-file logs/redis-slave.swap
vm-max-memory 0
vm-page-size 32

m-pages 134217728
vm-max-threads 4
glueoutputbuf yes
hash-max-zipmap-entries 64
hash-max-zipmap-value 512
activerehashing yes

Guess you like

Origin blog.csdn.net/kakaops_qing/article/details/109254187