1.简述Lamp+Redis的架构环境
架构环境名称 | IP |
---|---|
Lamp | 172.25.0.100 |
Redis主库 | 172.25.0.200 |
Redis从库 | 172.25.0.210 |
PHP程序会读取MySQL数据库中的用户名和密码,然后将用户名和密码缓存到Redis中,下次用户通过浏览器再次使用,无需PHP程序通过读取MySQL数据库中的用户名和密码,从而减轻MySQL数据库中的压力
Redis运行在内存中但是可以持久化到磁盘,所以在对不同数据集进行高速读写时需要权衡内存,因为数据量不能大于硬件内存。在内存数据库方面的另一个优点是,相比在磁盘上相同的复杂的数据结构,在内存中操作起来非常简单,这样Redis可以做很多内部复杂性很强的事情。同时,在磁盘格式方面他们是紧凑的以追加的方式产生的,因为他们并不需要进行随机访问。
2.部署LAMP和Discuz
请点击参考文章:请点击这里
3.下载Redis
请参考redis官方地址:点击这里
#用wget下载
yum install wget -y
wget https://download.redis.io/releases/redis-6.2.3.tar.gz
4.源码编译Redis
#处理编译源文件包
yum install gcc gcc-c++ make cmake -y
#解压redis包
tar -zxvf /root/redis-6.2.3.tar.gz
cd /root/redis-6.2.3
#编译和安装redis
make PREFIX=/usr/local/redis install
#复制redis.conf的配置文件到编译后的redis目录
cp /root/redis-6.2.3/redis.conf /usr/local/redis/
5.设置redis环境变量
#编辑redis的环境变量
tee > /etc/profile.d/redis.sh << 'EOF'
export PATH=/usr/local/redis/bin:$PATH
EOF
#生成环境变量
source /etc/profile.d/redis.sh
6.设置redis的system(系统服务)开机自动启动
#编辑redis配置的开启后台运行redis服务
sed -i 's/daemonize no/daemonize yes/g' /usr/local/redis/redis.conf
#编辑redis的system
tee > /usr/lib/systemd/system/redis.service << 'EOF'
[Unit]
Description=Redis
Wants=network.target
[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
#加载redis的系统服务配置
systemctl daemon-reload
#启动redis
systemctl enable redis
systemctl start redis
#active (running)运行状态这样就运行成功啦
systemctl status redis
● redis.service - Redis
Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; vendor preset: disabled)
Active: active (running) since 六 2021-05-08 23:38:52 CST; 6s ago
Process: 5005 ExecStop=/bin/kill -s QUIT $MAINPID (code=exited, status=0/SUCCESS)
Process: 4684 ExecReload=/bin/kill -s HUP $MAINPID (code=exited, status=0/SUCCESS)
Process: 51473 ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf (code=exited, status=0/SUCCESS)
Main PID: 51474 (redis-server)
Tasks: 5
Memory: 8.3M
CGroup: /system.slice/redis.service
└─51474 /usr/local/redis/bin/redis-server 127.0.0.1:6379
5月 08 23:38:52 localhost.localdomain systemd[1]: Starting Redis...
5月 08 23:38:52 localhost.localdomain systemd[1]: Started Redis.
7.在Lamp安装php-redis
#安装php-redis
yum install php-pecl-redis -y
#重启httpd服务
systemctl restart httpd
tee > /var/www/html/index.php 'EOF'
<?
phpinfo();
?>
EOF
#用浏览器输入IP,看到以下图片就成功开启php-redis
8. Lamp+Redis缓存测试
#在Redis主库操作
#注释掉本地网卡接口
sed -i '75c #bind 127.0.0.1 -::1' /usr/local/redis/redis.conf
#关闭redis安全模式
sed -i '95c protected-mode no' /usr/local/redis/redis.conf
#如果不关闭redis安全模式,可以指定网卡接口,如下配置。否则不用以下配置
#在Redis主库操作
sed -i '75a bind 127.0.0.1 172.25.0.200' /usr/local/redis/redis.conf
#配置后要重启redis服务
systemctl restart redis
#在Lamp操作,编辑php连接redis的php脚本
tee > /var/www/html/index.php 'EOF'
<?php
//连接本地的 Redis 服务
$redis = new Redis();
$redis->connect('172.25.0.200', 6379);
echo "Connection to server successfully";
//输出换行
echo "
";
//查看服务是否运行
echo "Server is running: " . $redis->ping();
?>
EOF
用浏览器输入IP,查看php脚本是否连接redis成功
#填写redis主库的IP
sed -i "19c \$_config['memory']['redis']['server'] = '172.25.0.200';" /var/www/html/upload/config/config_global.php
#查看是否替换成功
grep -n 'redis' /var/www/html/upload/config/config_global.php
19:$_config['memory']['redis']['server'] = '172.25.0.200';
20:$_config['memory']['redis']['port'] = 6379;
21:$_config['memory']['redis']['pconnect'] = 1;
22:$_config['memory']['redis']['timeout'] = '0';
23:$_config['memory']['redis']['requirepass'] = '';
24:$_config['memory']['redis']['serializer'] = 1;
创建论坛测试用户
#打开论坛网站
http://172.25.0.100/upload/
#注册一个用户名为test和密码为123456
#查看注册用户是否写如redis缓存中
#在Redis主库操作,查看注册用户是否写如redis缓存中
redis-cli
127.0.0.1:6379> keys *
1) "bdP1xy_stamptypeid"
2) "bdP1xy_common_member_newprompt_4"
3) "bdP1xy_plugin"
4) "bdP1xy_ipbanned"
5) "bdP1xy_usergroup_10"
6) "bdP1xy_forumlinks"
7) "bdP1xy_cronpublish"
8) "bdP1xy_diytemplatename"
9) "bdP1xy_diytemplatenamehome"
10) "bdP1xy_userstats"
11) "bdP1xy_seccodedata"
12) "bdP1xy_ipctrl"
13) "bdP1xy_common_member_count_4"
14) "bdP1xy_fields_register"
15) "bdP1xy_fields_required"
16) "bdP1xy_announcements"
17) "bdP1xy_setting"
18) "bdP1xy_creditrule"
19) "bdP1xy_common_member_4"
20) "bdP1xy_style_default"
21) "bdP1xy_fields_optional"
22) "bdP1xy_pluginlanguage_system"
23) "bdP1xy_diytemplatenameforum"
24) "bdP1xy_usergroups"
25) "bdP1xy_common_member_field_home_4"
26) "bdP1xy_usergroup_7"
27) "bdP1xy_onlinerecord"
28) "bdP1xy_modreasons"
29) "bdP1xy_cronnextrun"
30) "bdP1xy_onlinelist"
31) "bdP1xy_heats"
32) "bdP1xy_forum_index_page_10"
33) "bdP1xy_historyposts"
34) "bdP1xy_magic"
127.0.0.1:6379> get bdP1xy_common_member_4
"a:23:{s:3:\"uid\";s:1:\"4\";s:5:\"email\";s:9:\"[email protected]\";s:8:\"username\";s:4:\"test\";s:8:\"password\";s:32:\"360de038550f1b4d688a962ba0c4d41b\";s:6:\"status\";s:1:\"0\";s:11:\"emailstatus\";s:1:\"0\";s:12:\"avatarstatus\";s:1:\"0\";s:16:\"videophotostatus\";s:1:\"0\";s:7:\"adminid\";s:1:\"0\";s:7:\"groupid\";s:2:\"10\";s:11:\"groupexpiry\";s:1:\"0\";s:11:\"extgroupids\";s:0:\"\";s:7:\"regdate\";s:10:\"1621007181\";s:7:\"credits\";d:4;s:11:\"notifysound\";s:1:\"0\";s:10:\"timeoffset\";s:4:\"9999\";s:5:\"newpm\";s:1:\"0\";s:9:\"newprompt\";i:0;s:11:\"accessmasks\";s:1:\"0\";s:12:\"allowadmincp\";s:1:\"0\";s:18:\"onlyacceptfriendpm\";s:1:\"0\";s:9:\"conisbind\";s:1:\"0\";s:6:\"freeze\";s:1:\"0\";}"
9.删除数据库的test用户
在Lamp操作
#查看数据库的用户名和密码
mysql -u root -p123456
MariaDB [(none)]> use discuz;
MariaDB [discuz]> select username,password from pre_ucenter_members ;
+----------+----------------------------------+
| username | password |
+----------+----------------------------------+
| admin | e8717a465762e8743e7c68ba0f5b5b67 |
| test | 907a5b99aee05e014169fc3fe06a32b7 |
+----------+----------------------------------+
2 rows in set (0.001 sec)
MariaDB [discuz]> delete from pre_ucenter_members where username='test';
Query OK, 1 row affected (0.001 sec)
用浏览器测试是否登录成功
10.Redis主从复制配置
#redis主配置
grep -vE '#|^$' /usr/local/redis/redis.conf
bind 127.0.0.1 172.25.0.200
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble 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
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes
#redis从配置
grep -vE '#|^$' /usr/local/redis/redis.conf
bind 127.0.0.1 172.25.0.210
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble 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
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes
#设置redis主从复制
slaveof 172.25.0.200 6379
#配置后要重启redis服务
systemctl restart redis
#测试redis主从复制是否成功
#在redis主库设置键对应的值
redis-cli
127.0.0.1:6379> set student1 "小明"
OK
127.0.0.1:6379> get student1
"\xe5\xb0\x8f\xe6\x98\x8e"
#在redis从库查看值
redis-cli --raw
127.0.0.1:6379> get student1
小明
11.半持久化RDB模式
RDB持久性按指定的时间间隔执行数据集的时间点快照。
#创建存放rdb的目录
mkdir -p /data/redis
#配置指定数据库镜像存放的目录
sed -i '456c dir /data/redis' /usr/local/redis/redis.conf
#一小时内至少1个键更改则进行快照
#5分钟内至少100个键更改则进行快照
#1分钟内至少10000个键更改则进行快照
sed -i -e "\$a\save 3600 1" -e "\$a\save 300 100" -e "\$a\save 60 10000" /usr/local/redis/redis.conf
#重启redis服务
systemctl restart redis
#查看是否有rdb数据文件的备份
ls /data/redis/
dump.rdb
12.持久化AOF模式
AOF 持久化记录服务器收到的每个写操作,在服务器启动时会再次播放,重建原始数据集。
#开启AOF模式
sed -i '1254c appendonly yes' /usr/local/redis/redis.conf
#注释everysec配置redis将fsync数据存储在磁盘上的次数
sed -i "s/appendfsync everysec/#appendfsync everysec/g" /usr/local/redis/redis.conf
#开启always配置redis将fsync数据存储在磁盘上的次数
sed -i "s/# appendfsync always/appendfsync always/g" /usr/local/redis/redis.conf
#配置后需要重启redis服务
systemctl restart redis
#查看是否有aof数据文件的备份
ls /data/redis/
appendonly.aof dump.rdb
appendfsync always:fsync每次将新命令附加到 AOF 时。非常非常缓慢,非常安全。请注意,在执行来自多个客户端或管道的一批命令之后,这些命令会附加到 AOF,因此这意味着单个写入和单个 fsync(在发送回复之前)。
appendfsync everysec:fsync每一秒。足够快(在 2.4 中可能和快照一样快),如果发生灾难,您可能会丢失 1 秒的数据。
appendfsync no:永远不要fsync,只需将您的数据交到操作系统的手中。更快、更不安全的方法。通常 Linux 会使用这种配置每 30 秒刷新一次数据,但这取决于内核的精确调整。
13.Redis哨兵模式
Redis Sentinel 为Redis 提供高可用。实际上,这意味着使用 Sentinel,您可以创建一个 Redis 部署,无需人工干预即可抵抗某些类型的故障。
#复制配置文件到安装到redis的目录下
cp /root/redis-6.2.3/sentinel.conf /usr/local/redis/
#为sentinel配置IP
sed -i '84c sentinel monitor mymaster 172.25.0.200 6379 2' /usr/local/redis/sentinel.conf
#启动哨兵
redis-sentinel /usr/local/redis/sentinel.conf
110394:X 06 Jun 2021 01:10:22.338 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
110394:X 06 Jun 2021 01:10:22.338 # Redis version=6.2.3, bits=64, commit=00000000, modified=0, pid=110394, just started
110394:X 06 Jun 2021 01:10:22.338 # Configuration loaded
110394:X 06 Jun 2021 01:10:22.338 * Increased maximum number of open files to 10032 (it was originally set to 1024).
110394:X 06 Jun 2021 01:10:22.338 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.3 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 110394
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
redis哨兵省略性讲解,以后会有详细解析篇的