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

Installation of redis and master-slave configuration and master-slave switching

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

Share

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

Install redis

Download the redis source code and perform related operations, as follows:

Wget http://download.redis.io/releases/redis-3.2.3.tar.gz

Extract and install redis

[root@redis ~] # tar zxf redis-3.2.3.tar.gz

After the decompression is complete, start the installation now, as follows:

[root@redis ~] # cd redis-3.2.3/

[root@redis redis-3.2.3] # make&& make install

From the figure above, we can easily see that redis is installed to the

/ usr/local,/usr/local/bin,/usr/local/share,/usr/local/include,/usr/local/lib,/usr/l

Under ocal/share/man directory.

Then change to the utils directory and execute the redis initialization script install_server.sh, as follows:

[root@redis redis-3.2.3] # cd utils/

[root@redisutils] #. / install_server.sh

Welcome to the redis service installer

This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379]

Selecting default: 6379

Please select the redisconfig file name [/ etc/redis/6379.conf]

Selected default-/ etc/redis/6379.conf

Please select the redis log file name [/ var/log/redis_6379.log]

Selected default-/ var/log/redis_6379.log

Please select the data directory for this instance [/ var/lib/redis/6379]

Selected default-/ var/lib/redis/6379

Please select the redis executable path [/ usr/local/bin/redis-server]

Selected config:

Port: 6379

Config file: / etc/redis/6379.conf

Log file: / var/log/redis_6379.log

Data dir: / var/lib/redis/6379

Executable: / usr/local/bin/redis-server

CliExecutable: / usr/local/bin/redis-cli

Is this ok? Then press ENTER to go on or Ctrl-C to abort.

Copied / tmp/6379.conf = > / etc/init.d/redis_6379

Installing service...

Successfully added to chkconfig!

Successfully added to runlevels 345!

Starting Redis server...

Installation successful!

Through the above installation process, we can see that the redis configuration file after redis initialization is

/ etc/redis/6379.conf, log file is / var/log/redis_6379.log, data file dump.rdb is saved

Put it in the / var/lib/redis/6379 directory, and the startup script is / etc/init.d/redis_6379.

Now we are going to use systemd, so create a unit file name under / etc/systems/system

For redis_6379.service.

[root@redisutils] # vi / etc/systemd/system/redis_6379.service

The contents are as follows:

[Unit]

Description=Redis on port 6379

[Service]

Type=forking

ExecStart=/etc/init.d/redis_6379 start

ExecStop=/etc/init.d/redis_6379 stop

[Install]

WantedBy=multi-user.target

Note: here Type=forking is the form of background operation.

Start redis

[root@redisutils] # systemctl daemon-reload

[root@redisutils] # systemctl enable redis_6379.service

[root@redisutils] # systemctl start redis_6379.service

[root@redisutils] # systemctl status redis_6379.service

● redis_6379.service-Redis on port 6379

Loaded: loaded (/ etc/systemd/system/redis_6379.service; enabled; vendor preset: disabled)

Active: active (running) since Wed 2016-11-16 21:07:26 CST; 4min 25s ago

Process: 7732 ExecStart=/etc/init.d/redis_6379 start (code=exited, status=0/SUCCESS)

Main PID: 7734 (redis-server)

CGroup: / system.slice/redis_6379.service

└─ 7734 / usr/local/bin/redis-server 127.0.0.1:6379

Nov 16 21:07:26 redissystemd [1]: Starting Redis on port 6379...

Nov 16 21:07:26 redis redis_6379 [7732]: Starting Redis server...

Nov 16 21:07:26 redissystemd [1]: Started Redis on port 6379.

[root@redisutils] # netstat-anpt | grep 6379

Tcp 0 0 127.0.0.1:6379 0.0.0.0: LISTEN 7734/redis-server 1

From the display result, we can see that redis listens to port 6379 of 127.0.0.1 by default.

Firewall rule settin

[root@redisutils] # firewall-cmd-permanent-add-port=6379/tcp

Success

[root@redisutils] # firewall-cmd-- reload

Success

Now let's look at the redis version using the redis-cli-version command, as follows

[root@redisutils] # redis-cli-- version

Redis-cli 3.2.3

By displaying the results, we can see that the redis version is 3.2.3.

To this source mode to install redis will be introduced.

After redis is installed, let's configure redis

Set the address for redis listening, and add the ip for listening to redis hosts

For security, we need to enable the password authentication function of redis, the requirepass parameter

The final redis configuration file is as follows:

[root@redis ~] # grep-Ev'^ # | ^ $'/ etc/redis/6379.conf (the red part is modified)

Bind 127.0.0.1 192.168.31.106

Protected-mode yes

Port 6379

Tcp-backlog 511

Timeout 0

Tcp-keepalive 300

Daemonize yes

Supervised no

Pidfile / var/run/redis_6379.pid

Loglevel notice

Logfile / var/log/redis_6379.log

Databases 16

Save 900 1

Save 300 10

Save 60 10000

Stop-writes-on-bgsave-error yes

Rdbcompression yes

Rdbchecksum yes

Dbfilenamedump.rdb

Dir / var/lib/redis/6379

Slave-serve-stale-data yes

Slave-read-only yes

Repl-diskless-sync no

Repl-diskless-sync-delay 5

Repl-disable-tcp-nodelay no

Slave-priority 100

Requirepass pwd@123

Appendonly no

Appendfilename "appendonly.aof"

Appendfsynceverysec

No-appendfsync-on-rewrite no

Auto-aof-rewrite-percentage 100

Auto-aof-rewrite-min-size 64mb

Aof-load-truncated yes

Lua-time-limit 5000

Slowlog-log-slower-than 10000

Slowlog-max-len 128

Latency-monitor-threshold 0

Notify-keyspace-events ""

Hash-max-ziplist-entries 512

Hash-max-ziplist-value 64

List-max-ziplist-size-2

List-compress-depth 0

Set-max-intset-entries 512

Zset-max-ziplist-entries 128

Zset-max-ziplist-value 64

Hll-sparse-max-bytes 3000

Activerehashing yes

Client-output-buffer-limit normal 0 0 0

Client-output-buffer-limit slave 256mb 64mb 60

Client-output-buffer-limitpubsub 32mb 8mb 60

Hz 10

Aof-rewrite-incremental-fsync yes

Restart the redis service

[root@redis ~] # systemctl restart redis_6379.service

[root@redis ~] # netstat-anpt | grep redis

Tcp 0 0 192.168.31.106:6379 0.0.0.0: LISTEN 8418/redis-server 1

Redis command

The Redis command is used to perform operations on the redis service.

A redis client is required to execute commands on the redis service. The Redis client downloaded it before us.

In the installation package of redis.

Grammar

The basic syntax for the Redis client is:

$redis-cli

Example

The following example shows how to start the redis client:

Start the redis client, open the terminal and enter the command redis-cli. This command connects to the local redis service.

$redis-cli

Redis 127.0.0.1 purl 6379 >

Redis 127.0.0.1 6379 > PING

PONG

In the above example, we connect to the local redis service and execute the PING command, which is used to detect the redis service

Whether or not to start.

Execute commands on remote services

If you need to execute a command on a remote redis service, we also use the redis-cli command.

Grammar

$redis-cli-h host-p port-a password

Example

The following example shows how to connect to a redis with host 127.0.0.1, port 6379 and password mypass

In service.

$redis-cli-h 127.0.0.1-p 6379-a "mypass"

Redis 127.0.0.1 purl 6379 >

Redis 127.0.0.1 6379 > PING

PONG

Master-slave replication

Environment description:

Master redis:192.168.10.1 6379

From redis:192.168.10.2 6380

1. Master-slave configuration

1. Change the daemonize no in the master-slave redis configuration file redis.conf to yes

2. Change port 6379 in redis configuration file redis.conf to 6380, and add slaveof

192.168.10.1 6379

Three methods: a, add slaveof [masterHost] [masterPort] to the configuration file

B. Add-slaveof [masterHost] [masterPort] when redis-server starts

C. Directly use the command slaveof [masterHost] [masterPort]

3. Start the master-slave service

Master redis:

[root@localhost redis-2.8.3] # src/redis-server / soft/redis-2.8.3-master/redis2.8.3/redis.conf

From redis:

[root@localhost redis-2.8.3] # src/redis-server / soft/redis-2.8.3-slave/redis2.8.3/redis.conf

4. Test data synchronization

Master redis:

[root@localhost redis-2.8.3] # src/redis-cli-p 6379

127.0.0.1 purl 6379 > set name abc

OK

127.0.0.1 purl 6379 > get name

"abc"

127.0.0.1purl 6379 >

From redis:

[root@localhost redis-2.8.3] # src/redis-cli-p 6380

127.0.0.1 6380 > get name

"abc"

127.0.0.1purl 6380 >

5. Read-write separation is the default.

From redis:

[root@localhost redis-2.8.3] # src/redis-cli-p 6380

127.0.0.1 set name 6380 >

(error) READONLY You can't write against a read only slave.

Second, master-slave switching

1. Stop the main redis

[root@localhost redis-2.8.3] # src/redis-cli-n 6379 shutdown

[root@localhost redis-2.8.3] # src/redis-cli-p 6379

Could not connect to Redis at 127.0.0.1:6379: Connection refused

Not connected >

2. Set slave redis to master redis

[root@localhost redis-2.8.3] # src/redis-cli-p 6380 slaveof NO ONE

OK

3. Test whether the slave redis is switched to the slave master redis

[root@localhost redis-2.8.3] # src/redis-cli-p 6380

127.0.0.1 set name 6380 >

OK

127.0.0.1 6380 > get name

"123"

127.0.0.1purl 6380 >

4. The original main redis has returned to normal and needs to be switched back.

1) Save the data of the current master redis

[root@localhost redis-2.8.3] # src/redis-cli-p 6380

127.0.0.1 6380 > get name

"abc"

127.0.0.1 set name 6380 >

OK

127.0.0.1 6380 > get name

"123"

127.0.0.1 6380 > save

OK

127.0.0.1 6380 > get name

"123"

127.0.0.1purl 6380 >

2) overwrite the copy of the dump.rdb file under the current main redis root directory to the root directory of the original main redis

3) start the original master redis

[root@localhost redis-2.8.3] # src/redis-server / soft/redis-2.8.3-master/redis2.8.3/redis.conf

4) switch in the current main redis

[root@localhost redis-2.8.3] # src/redis-cli-p 6380 slaveof 192.168.10.1 637

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