Mysql集群配置
一、主从备份
需要从服务器的数据库状态随时与主服务器保持一致
特点:①从服务器主动从主服务器上下载数据,同步数据(备份)
②从服务器分担主服务器的查询压力(负载均衡)
实验过程
1)下载mariadb mariadb-server 并设置开机自启和启动
[root@localhost ~]$ yum -y install mariadb mariadb-server
#centos7没有mysql服务了,安装mariadb的客户端和服务端
[root@localhost ~]$ systemctl start mariadb && systemctl enable mariadb
#设置开启自启,服务开启
[root@localhost ~]$ mysqladmin -u root password root
#设置root用户的密码,可以不设置
2)配置mariadb的配置文件,开启binlog日志功能。
现在开始设置mariadb的配置文件,先将其关闭
[root@localhost ~]$ service mysqld stop
[root@localhost ~]$ vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
user=mysql
log-bin=mysql-bin #启动二进制日志
server-id=151 #设置服务器id
# # Settings user and group are ignored when systemd is used.
# # If you need to run mysqld under a different user or group,
# # customize your systemd unit file for mariadb according to the
# # instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# #
# # include all files from the config directory
# #
!includedir /etc/my.cnf.d
3)现在开始在主服务器的mariadb上进行授权从服务器的信息
[root@localhost ~]$ mysql -uroot -p123
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> grant replication slave on *.* to slave@'192.168.28.30' identified by '123';
#设置从服务器的ip,密码为123
Query OK, 0 rows affected (0.00 sec)
MariaDB [web]> show master status; #查看二进制文件名和大小
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 820 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
4)接着在从服务器上保存授权信息
[root@localhost ~]$ mysql -uroot -proot
mysql> change master to
master_user='slave', --授权用户
master_password='123', --密码
master_host='192.168.28.151', --主服务器地址
master_log_file='mysql-bin.000003',--主服务器的二进制日志
master_log_pos=820;--当前日志文件大小
5)接着会在从服务器上产生授权文件
[root@localhost ~]$ cd /var/lib/mysql
[root@localhost mysql]# ls
aaa aria_log_control ibdata1 ib_logfile1 mariadb-relay-bin.000002 master.info mysql-bin.000001 mysql.sock relay-log.info
aria_log.00000001 bbb ib_logfile0 mariadb-relay-bin.000001 mariadb-relay-bin.index mysql mysql-bin.index performance_schema test
[root@localhost mysql]$ cat master.info
18
mysql-bin.000003
1297
192.168.28.151
slave
123
3306
60
0
0
1800.000
0
master.info mysql.sock
6)开启从服务器 start slave,并查看
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.28.151
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 1297
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB:
7)最后测试,在主服务器上mariadb数据库上创建数据,从服务器上也会同步创建。
二、主主备份
在主从备份的基础上,在从服务器上授权主服务器为从,主服务器上授权从服务器为主。
replicate-do-db=test #提高执行效率将不必要的资源不写入二级制文件
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
auto-increment-increment=2 #防止主键冲突
auto-increment-offset=1
三、多主一从
1)在主服务器上设置数据库配置文件,开启bin-log日志,设置server-id
2)在从服务器上这样设置
[root@localhost ~]$ vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
#M-M-S
[mysqld_multi]
mysqld=/usr/bin/mysqld_safe
mysqladmin=/usr/bin/mysqladmin
log=/tmp/multi.log
#两个主服务器的标签
[mysqld10]
prot=3306 #端口
datadir=/var/lib/mysqla/
pid-file=/var/lib/mysqla/mysqld.pid
socket=/var/lib/mysqla/mysql.sock
user=mysql
server-id=30
[mysqld20]
prot=3307
datadir=/var/lib/mysqlb/
pid-file=/var/lib/mysqlb/mysqld.pid
socket=/var/lib/mysqlb/mysql.sock
user=mysql
server-id=30
2.1)在从服务器上初始化数据库,生成目录 mysqla,mysqlb
[root@localhost ~]$ mysql_install_db --datadir=/var/lib/mysqla --user=mysql
[root@localhost ~]$ mysql_install_db --datadir=/var/lib/mysqlb --user=mysql
2.2)设置 mysqla,mysqlb 目录及以下文件的属主为 mysql(防止出现权限问题)
[root@localhost ~]$ chown -R mysql /var/lib/mysqla/
[root@localhost ~]$ chown -R mysql /var/lib/mysqlb/
2.3)启动从服务器线程
[root@localhost ~]$ mysqld_multi --defaults-file=/etc/my.cnf start 10
#start是前面配置文件写的主服务器标签
[root@localhost ~]$ mysqld_multi --defaults-file=/etc/my.cnf start 20
3)登录主服务并保存授权信息
主服务器1
mysql> change master to
master_user='slave',
master_password='root',
master_host='192.168.88.10',
master_log_file='mysql-bin.000001',
master_log_pos=106;
mysql> start slave
主服务器2
[root@localhost ~]$ mysql -uroot -P 3307 -S /var/lib/mysqlb/mysql.sock
mysql> change master to
master_user='slave',
master_password='root',
master_host='192.168.88.20',
master_log_file='mysql-bin.000001',
master_log_pos=258;
四、一主多从
1)主服务器照常配置
[root@localhost ~]$ service mysqld stop
#systemctl stop mariadb
[root@localhost ~]$ vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin=mysql-bin #启用二进制
server-id=10 #服务器的id
[root@localhost ~]$ service mysqld start
2)从服务器上也是照常配置(两个server-id不同即可)
[root@localhost ~]$ mysql -uroot -proot
mysql> change master to
master_user='slave', --授权用户
master_password='root', --密码
master_host='192.168.88.10', --主服务器地址
master_log_file='mysql-bin.000001',--主服务器的二进制日志
master_log_pos=187;--当前日志文件大小
#复制粘贴需要删除注释
Query OK, 0 rows affected (0.01 sec)
#可以直接粘贴这个
change master to master_user='root', master_password='root', master_host='192.168.88.10', master_log_file='mysql-bin.000001',master_log_pos=1563;
mysql> start slave;
五、Mysql中间件——Amoeba(读写分离)
amoeba这段可参考这里
中间件: 一种提供在不同技术、不同的软件之间共享资源的程序,更大化了利用了数据库的性能,可以无限扩展(注:真实环境中并非如此)
数据库的中间件:
mysql proxy (官方版本) 性能低,需要 lua 脚本
atlas 性能低,响应时间长
amoeba 陈思儒研发的
1))先搭建一个主从关系的服务器
2) 从服务器上保存授权信息,并开启从服务线程
2.1)关闭从服务器线程
为了做读写分离时,测试有明显的实验效果(实际生产环境中不能停掉)
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into demo.a1 values (2,'bbb',19);
Query OK, 1 row affected (0.00 sec)
3)配置读写分离
3.1)安装 gcc 环境(amoeba 需要源码安装)
redis安装
链接:https://pan.baidu.com/s/1zmkFgVCQIHjlk6aAnSSNWw
提取码:2ktw
[root@localhost ~]$ yum -y install gcc* unzip
[root@localhost ~]$ unzip amoeba-n.zip
3.2)拷贝第三方软件,创建单独的目录
[root@localhost ~]$ mkdir /amoeba
3.2.1)安装 amoeba
[root@localhost ~]$ cd /root/amoeba-n
[root@localhost ~]$ unzip amoeba-mysql-1.3.1-BETA.zip -d /usr/local/amoeba
3.2.2)赋予权限
[root@localhost ~]$ chmod -R +x /usr/local/amoeba/bin/
3.2.3)配置amoeba配置文件
[root@localhost ~]$ vim /usr/local/amoeba/conf/amoeba.xml
<server>
...
<!--监听自己本机 -->
<property name="ipAddress">192.168.88.100</property>
<!--用户名 -->
<property name="user">amoeba</property>
<!--密码 -->
<property name="password">amoeba</property>
</server>
<dbServerList>
#主服务器,复制一下,19yy
<dbServer name="server1">
<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
<property name="manager">defaultManager</property>
<property name="port">3306</property>
<property name="ipAddress">192.168.88.101</property>
<property name="schema">test</property>
<property name="user">abc</property>
<property name="password">abc</property>
</factoryConfig>
<poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
<property name="maxActive">200</property>
<property name="maxIdle">200</property>
<property name="minIdle">10</property>
<property name="minEvictableIdleTimeMillis">600000</property>
<property name="timeBetweenEvictionRunsMillis">600000</property>
<property name="testOnBorrow">true</property>
<property name="testWhileIdle">true</property>
</poolConfig>
</dbServer>
#从服务器
<dbServer name="server2">
<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
<property name="manager">defaultManager</property>
<property name="port">3306</property>
<property name="ipAddress">192.168.88.102</property>
<property name="schema">test</property>
<property name="user">abc</property>
<property name="password">abc</property>
</factoryConfig>
<poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
<property name="maxActive">200</property>
<property name="maxIdle">200</property>
<property name="minIdle">10</property>
<property name="minEvictableIdleTimeMillis">600000</property>
<property name="timeBetweenEvictionRunsMillis">600000</property>
<property name="testOnBorrow">true</property>
<property name="testWhileIdle">true</property>
</poolConfig>
</dbServer>
</dbServerList>
3.3)声明用 java 写出来的程序如何调用(/etc/profile)
[root@localhost ~]$ vim /etc/profile
#最后一行添加如下内容
# java
JAVA_HOME=/amoeba/jdk
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CLASSPATH=.:$JAVA_HOME/bin/tools.jar:$JAVA_HOME/lib/dt.jar:$CLASSPATH
export CLASSPATH
[root@localhost ~]$ source /etc/profile #立即生效
六、安装redis
[root@localhost ~]$ cd redis-7.0.5/
make
make PREFIX=/usr/local/redis install
[root@localhost ~]$ cd redis-7.0.5/
[root@localhost redis-7.0.5]# ls
00-RELEASENOTES COPYING MANIFESTO runtest-cluster sentinel.conf utils
BUGS deps README.md runtest-moduleapi src
CODE_OF_CONDUCT.md INSTALL redis.conf runtest-sentinel tests
CONTRIBUTING.md Makefile runtest SECURITY.md TLS.md
[root@localhost etc]$ mkdir /usr/local/redis/etc
[root@localhost redis-7.0.5]$ cp -a redis.conf /usr/local/redis/etc/
#复制redis.conf /usr/local/redis/etc下,方便管理
[root@localhost etc]$ vim /usr/local/redis/etc/redis.conf
原 daemonize no 修改为yes,后台启动
[root@localhost etc]$ ln -s /usr/local/redis/bin/* /usr/local/bin/
#做个链接,命令更方便使用
[root@localhost etc]$ redis-server /usr/local/redis/etc/redis.conf
#启动服务(必须配置文件位置,否则启动错误)
5)客户端连接
[root@localhost redis-7.0.5]$ redis-cli
127.0.0.1:6379>
[root@localhost redis-7.0.5]$ /usr/local/redis/bin/redis-cli shutdown
#关闭服务
[root@localhost redis-7.0.5]$ pkill -9 redis
redis常用命令
①string类型及其操作(一个key对应一个value)
1、set:设置变量与值的关闭;get:获取变量对应的值
[root@localhost ~]# redis-cli
127.0.0.1:6379>
127.0.0.1:6379> set name zlf
OK
127.0.0.1:6379> get name
"zlf"
2、setnx:nx为no exist 判断变量是否存在,如果已经创建,则返回0
127.0.0.1:6379> setnx name libai
(integer) 0
127.0.0.1:6379> setnx name zlf
(integer) 0
3、mset:同时设置多个值;mget:同时获取多个值
127.0.0.1:6379> mset age 11 sex man wel cn
OK
127.0.0.1:6379> mget age sex wel
1) "11"
2) "man"
3) "cn"
4、incrby:对值进行加减操作
127.0.0.1:6379> INCRBY age 10
(integer) 21
127.0.0.1:6379> INCRBY age -10
(integer) 11
5、del:对变量进行删除
127.0.0.1:6379> del age
(integer) 1
127.0.0.1:6379> mget age sex
1) (nil)
2) "man"
127.0.0.1:6379> get sex
"man"
②hash类型及操作(创建表)
一个键对应一个值
1、hset:设置hash filed为指定值,如果key不存在,则先创建。
例如:为num1表创建一个叫变量为name,值为zhangsan
127.0.0.1:6379> mset name zlf age 22 sex man
OK
127.0.0.1:6379> hset num1 name zlf age 22 sex man
(integer) 3
2、hget、hmget、hmset类似
127.0.0.1:6379> hget num1 name
"zlf"
127.0.0.1:6379> hget num1 age
"22"
127.0.0.1:6379> hmget num1 name age sex
1) "zlf"
2) "22"
3) "man"
3、hdel:删除表中某一键值对
127.0.0.1:6379> hdel num1 age
(integer) 1
127.0.0.1:6379> hmget num1 name age sex
1) "zlf"
2) (nil)
3) "man"
4、hgetall:获取表中所有键值对
127.0.0.1:6379> hgetall num1
1) "name"
2) "zlf"
3) "sex"
4) "man"
③list类型及其操作(链表内不含键名,只存在值)
一个表对应多个值
1、lpush:在key对应list头部添加字符串元素(无法对已存在的键进行链表)
127.0.0.1:6379> LPUSH name zlf
#由于上面name被设置为string类型了,所以无法链表
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> LPUSH name1 zlf
(integer) 1
127.0.0.1:6379> LPUSH zhangsan zhangsan
(integer) 1
127.0.0.1:6379> LPUSH zhangsan 18
(integer) 2
127.0.0.1:6379> LPUSH zhangsan man
(integer) 3
2、lrange:从指定链表中获取指定范围的元素("一般用0+空格+-1" 来取链表内所有值)
127.0.0.1:6379> LRANGE zhangsan 0 -1
1) "man"
2) "18"
3) "zhangsan"
3、lpush、lpop、rpush、rpop、lrange,如图:
④set类型及其操作(无序集合)
1、sadd:添加一个或多个元素到集合里
127.0.0.1:6379> sadd jihe 1 2 3 4
(integer) 4
2、smember:获取集合中的所有元素
127.0.0.1:6379> SMEMBERS jihe
1) "1"
2) "2"
3) "3"
4) "4"
3、srem:从集合中删除指定的一个或多个元素
127.0.0.1:6379> srem jihe 2 3
(integer) 2
127.0.0.1:6379> SMEMBERS jihe
1) "1"
2) "4"
4、spop:随机从集合中删除一个元素,并返回
127.0.0.1:6379> spop jihe
"3"
127.0.0.1:6379> SMEMBERS jihe
1) "1"
2) "2"
3) "4"
127.0.0.1:6379> spop jihe
"2"
127.0.0.1:6379> SMEMBERS jihe
1) "1"
2) "4"
5、scard:获取集合中的元素个数
127.0.0.1:6379> scard jihe
(integer) 2
127.0.0.1:6379> sadd jihe 3 4
(integer) 1
127.0.0.1:6379> sadd jihe 2 3
(integer) 1
127.0.0.1:6379> scard jihe
(integer) 4
6、sdiff:返回集合1与集合2的差值,集合1为主
127.0.0.1:6379> sadd jihe2 3 4 5 6
(integer) 4
127.0.0.1:6379> sdiff jihe jihe2
1) "1"
2) "2"
127.0.0.1:6379> sdiff jihe2 jihe
1) "5"
2) "6"
7、sinter:获取两个集合的交集
127.0.0.1:6379> sinter jihe jihe2
1) "3"
2) "4"
8、sunion:获取两个集合的并集
127.0.0.1:6379> sunion jihe jihe2
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
⑤zset类型及其操作(有序集合)
set的升级版,增加了顺序
1、zadd:向一个指定的有序集合添加元素,每一个元素会有一个对应的分数。(如果重复增加,则会覆盖)
127.0.0.1:6379> ZADD zset 2 zhangsan 1 lisi 1 wangwu 0 liming
(integer) 4
2、zrange:按score(分数)由小到大显示出来。相同score值的安装字典顺序排序,withscore显示score "0+空格+-1"表示全部显示
127.0.0.1:6379> ZRANGE zset 0 -1
1) "liming"
2) "lisi"
3) "wangwu"
4) "zhangsan"
127.0.0.1:6379> ZRANGE zset 0 -1 withscores
1) "liming"
2) "0"
3) "lisi"
4) "1"
5) "wangwu"
6) "1"
7) "zhangsan"
8) "2"
3、zrem:删除有序集合的指定值
127.0.0.1:6379> zrem zset liming
(integer) 1
127.0.0.1:6379> ZRANGE zset 0 -1 withscores
1) "lisi"
2) "1"
3) "wangwu"
4) "1"
5) "zhangsan"
6) "2"
4、zcard:返回有序集合元素个数
127.0.0.1:6379> ZRANGE zset 0 -1 withscores
1) "liming"
2) "0"
3) "zhangsan"
4) "0"
5) "lisi"
6) "1"
7) "wangwu"
8) "1"
127.0.0.1:6379> zcard zset
(integer) 4
其他相关命令
1、keys:查找指定的键。支持通配符(?*等)
127.0.0.1:6379> keys *set
1) "mset"
2) "zset"
127.0.0.1:6379> keys n*me
1) "name"
2、exists:确认一个键是否存在(1表示存在)
127.0.0.1:6379> exists name
(integer) 1
127.0.0.1:6379> exists zset
(integer) 1
3、del:删除一个键(通用)
127.0.0.1:6379> set home zx
OK
127.0.0.1:6379> del home
(integer) 1
127.0.0.1:6379> get home
(nil)
4、ttl:以秒为单位,返回指定键的剩余时间
返回 -2 代表不存在
返回 -1 代表永不过期
其他的就是键的生存时间
5、expire:给键设置过期时间,如果到达时间,则自动删除
127.0.0.1:6379> expire name 60
(integer) 1
127.0.0.1:6379> ttl name
(integer) 55
127.0.0.1:6379> ttl name
(integer) 54
127.0.0.1:6379> ttl name
(integer) 50
6、select:选择一个数据库,默认连接的是数据库0,可以支持共16个数据库,在配置文件中,通过database 16关键字定义
127.0.0.1:6379> SELECT 1
OK
127.0.0.1:6379[1]>
7、move:将当前数据库的键移动到指定数据库
127.0.0.1:6379> move age 1 #将数据库0的键age移动到数据库1
(integer) 1
127.0.0.1:6379> select 1 #进入数据库1
OK
127.0.0.1:6379[1]> get age #查看age
"22"
127.0.0.1:6379[1]>
8、type:返回键的类型
127.0.0.1:6379[1]> type age
string
9、dbsize:返回当前库中键的数量(所有类型)
127.0.0.1:6379[1]> dbsize
(integer) 1
10、save:保存所有数据。很少在生产环境中使用,因为它会阻塞所有客户端的请求,可以使用bgsave命令代替。如果在bgsave的保存数据的子进程发生错误时,用save保存最新的数据是最后的手段。
11、info:获取本服务器的信息
12、config get:获取redis服务器配置文件的参数,支持通配符
13、flushdb:删除当前数据库的所有数据
14、flushall:删除所有数据库的所有数据
5)redis高级应用
密码保护功能
操作步骤:去redis主配置文件中找到 requirepass 在后面添加所设置的登录密码
[root@localhost ~]# vim /usr/local/redis/etc/redis.conf
# The requirepass is not compatible with aclfile option and the ACL LOAD
# command, these will cause requirepass to be ignored.
requirepass 123 #设置密码为123
[root@localhost ~]# pkill redis
[root@localhost ~]# redis-server /usr/local/redis/etc/redis.conf
#当你登录没有输入密码时,无法进行操作
[root@localhost ~]# redis-cli
127.0.0.1:6379> set name aaa
(error) NOAUTH Authentication required.
#当你登录输入密码时,可以操作
[root@localhost ~]# redis-cli -a 123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> set name aaa
OK
主从同步
redis主从服务器复制过程:
①slave 与 master 建立连接,发送sync同步命令
②master会启动一个后台进程,将数据库快照保存到文件中,同时master主进程会开始收集新的写命令并缓存。
③后台完成保存后,就将此文件发送给slave ④slave将此文件保存到硬盘上
操作步骤:
①主服务器设置好密码
②从服务器修改配置文件,用来连接主服务器
③重启从服务器,然后测试(可通过info命令获取当前服务器身份类型)
实验过程:
①进入redis主配置文件,修改配置参数(daemonize no 修改为 daemonize yes,主服务器ip、端口、密码等),并重启。![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e4620f2f86e857cf1c939c518b9d18b1.png)
②然后去主服务器上进入redis主配置文件,找到bind 127.0.0.1,并注释掉 或者 修改为本机的ip地址,并重启。
③再去从服务器上,登录测试!
[root@localhost etc]$ pkill redis #关闭进程
[root@localhost etc]$ redis-server /usr/local/redis/etc/redis.conf #开启服务
[root@localhost etc]$ redis-cli
127.0.0.1:6379> keys *
1) "sex"
2) "name"
3) "age"
结果显示,主从同步了。但此时的从只能读,不能写入。
数据持久化
①snapshotting(快照)——默认方式
RDB持久化方式能够在指定的时间间隔能对你的数据进行快照存储。是默认的持久化方式。这种方式是将内存中数据以快照的方式写入到二进制文件中,默认的文件名为dump.rdb。
Ⅰ过了900s且有1个 key 发生了改变,就会触发 save 动作
Ⅱ过了300s且有10个 key 发生了改变,就会触发 save 动作
Ⅲ过了60s且有10000个 key 发生了改变,就会触发 save 动作
结论:在redis.conf文件中dir ./定义了数据库文件的存放位置,默认是当前目录。所以每次重启redis服务所在的位置不同,将会产生新的dump.rdb文件,建议服务器搭建完成时先修改快照文件保存位置。
②append—only file (缩写aof)
使用AOF会让你的Redis更加耐久:你可以使用不同的持久化策略:每次写的时候备份、每秒备份、无备份。使用默认的每秒备份策略,redis的性能依然很好(备份是由后台线程进行处理的,主线程会尽力处理客户端请求),一旦出现故障,你最多丢失1s的数据。
操作步骤:
Ⅰ打开redis.conf配置文件开启AOF持久化
此处三种模式,
1、写入就保存:效率最慢,但最安全
2、每秒保存(默认)
3、不保存:不进行AOF备份,将数据交给操作系统处理。速度最快,最不安全
Ⅱ 重启服务,登录客户端,创建键,退出再去/usr/local/redis/datadir下查看是否生成了appendonly.aof 的文件
(而在redis7以及以后的版本中,AOF持久化的数据将存在于一个文件夹中仅追加文件名是由Redis按照特定的模式创建的。 文件名前缀是基于’appendfilename’配置的 参数,后面是关于序列和类型的附加信息。 例如,如果appendfilename设置为appendonly。 Aof,下面的文件 names可以被派生: appendonly.aof.1.base。 RDB作为基本文件。 appendonly.aof.1.incr。 aof appendonly.aof.2.incr。 作为增量文件的Aof。 将appendonly.aof.manifest作为一个manifest文件。)
[root@localhost etc]$ pkill redis
[root@localhost etc]$ redis-server /usr/local/redis/etc/redis.conf
[root@localhost etc]$
[root@localhost etc]$ redis-cli -a 123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379> mset name 123 age 18 sex man
OK
127.0.0.1:6379> exit
[root@localhost etc]$ cd ../datadir/
[root@localhost datadir]$ ls
appendonlydir dump.rdb
[root@localhost datadir]$ cd appendonlydir/
[root@localhost appendonlydir]$ ls
appendonly.aof.1.base.rdb appendonly.aof.manifest
appendonly.aof.1.incr.aof