CONFIG REWRITE

Available since 2.8.0.

CONFIG REWRITE命令用于重写redis server启动时使用的配置文件,以最小的变动反映当前server的实际配置,当前的实际配置可能会由于CONFIG SET的使用而与启动时的配置文件不同.

类似 scope=spfile

重写以非常保守的方式执行:

  • 尽可能保留原始redis.conf的注释和整体结构.
  • 如果一个选项已经存在于旧的redis.conf文件中,它将被原地更新.
  • 如果某个选项尚不存在,但设置为其默认值,则不会由重写过程添加.
  • 如果某个选项尚不存在,但它被设置为非默认值,则会将其附加到文件末尾.
  • Non used lines are blanked. For instance if you used to have multiple save directives, but the current configuration has fewer or none as you disabled RDB persistence, all the lines will be blanked.

    未使用的行将被删除.例如,如果您曾经设置多个save,但是后来却通过config set只设置了更少的或者config set save ‘’,那么所有的未使用save将被删除

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    [root@cn_mu_binlog_backup ~]# redis-cli -p 6379 config get save
    1) "save"
    2) ""
    [root@cn_mu_binlog_backup ~]# cat /usr/local/redis/etc/6379_redis.conf|grep save
    # save <seconds> <changes>
    # Will save the DB if both the given number of seconds and the given
    # In the example below the behaviour will be to save:
    # Note: you can disable saving completely by commenting out all "save" lines.
    # It is also possible to remove all the previously configured save
    # points by adding a save directive with a single empty string argument
    # save ""
    save 900 1
    save 300 10
    save 60 10000
    ...
    重写
    [root@cn_mu_binlog_backup ~]# redis-cli -p 6379 config rewrite
    OK
    [root@cn_mu_binlog_backup ~]# cat /usr/local/redis/etc/6379_redis.conf|grep save
    # save <seconds> <changes>
    # Will save the DB if both the given number of seconds and the given
    # In the example below the behaviour will be to save:
    # Note: you can disable saving completely by commenting out all "save" lines.
    # It is also possible to remove all the previously configured save
    # points by adding a save directive with a single empty string argument
    # save ""
    # (at least one save point) and the latest background save failed.
    stop-writes-on-bgsave-error yes
    # If you want to save some CPU in the saving child set it to 'no' but
    # algorithms (in order to save memory), so you can tune it for speed or
    # the configured save points).
    # saving process (a background save or AOF log background rewriting) is
    # Lists are also encoded in a special way to save a lot of space.
    # order to save a lot of space. This encoding is only used when the length and

如果由于某种原因原来的配置文件不再存在,CONFIG REWRITE也可以重写配置文件. 但是,如果服务器启动时没有配置文件,CONFIG REWRITE将只返回一个错误.

原子重写过程

In order to make sure the redis.conf file is always consistent, that is, on errors or crashes you always end with the old file, or the new one, the rewrite is performed with a single write(2) call that has enough content to be at least as big as the old file. Sometimes additional padding in the form of comments is added in order to make sure the resulting file is big enough, and later the file gets truncated to remove the padding at the end.

为了确保redis.conf文件始终保持一致,即在出现错误或崩溃时总是以旧文件或新文件结束,重写将使用具有足够内容的单次写入(2)调用来执行 至少与旧文件一样大. 有时会添加注释形式的其他填充以确保生成的文件足够大,然后文件被截断以删除最后的填充.

返回值

Simple string reply: OK 当配置被正确重写. 否则,返回错误

redis-cli,Redis命令行界面

redis-cli,Redis命令行界面

redis-cli是Redis命令行界面,一个简单的程序,允许向Redis发送命令,并直接从终端读取服务器发送的回复。

它有两种主要的模式:一种是交互模式,在这种模式下,用户输入命令并获得回复的REPL(Read Eval Print Loop)另一种模式是将命令作为redis-cli的参数发送,执行并打印在标准输出上。

在交互模式下,redis-cli具有基本的行编辑功能,可以提供良好的打字体验。

但是,redis-cli不只是这一点。有些选项可以用来启动程序,以便将其放入特殊模式,以便redis-cli可以完成更复杂的任务,例如模拟slave并打印从master接收到的复制流,查看复制延迟,并显示统计数据,甚至一个ASCII艺术频谱图的延迟样本和频率,以及许多其他事情。

本指南将涵盖redis-cli的不同方面,由简入繁。

如果您要广泛使用Redis,或者您已经这么做了,那么很可能会碰巧使用redis-cli。花一些时间来熟悉它可能是一个非常好的主意,一旦你知道了命令行界面的所有技巧,你就会看到你将更有效地使用Redis。

命令行用法

如下可以直接非交互模式执行命令并在标准输出获取返回结果

1
2
redis-cli incr mycounter
(integer) 7

‘()’内为返回结果的类型.当我们需要获取返回结果作为下一个命令的输入,或者希望将结果重定向到文件中时,我们可能不需要显示类型信息.
实际上,redis-cli会自动检测,当它检测到标准输出是一个tty(a terminal basically)时,它会显示类型信息来提升可读性,否则它会启用原始输出模式,如下所示:

1
2
3
$ redis-cli incr mycounter > /tmp/output.txt
$ cat /tmp/output.txt
8

这一次当CLI检测到输出不在写入terminal后,(integer)被删除了不再显示. 你可以使用--raw选项来强制输出到终端的内容也不显示类型信息

1
2
$ redis-cli --raw incr mycounter
9

同样的,你也可以使用--no-raw强制非tty输出也显示类型信息

1
2
3
4
5
6
7
8
9
10
[root@cn_mu_binlog_backup ~]# redis-cli -p 6381 incr no
(integer) 1
[root@cn_mu_binlog_backup ~]# redis-cli -p 6381 --raw incr no
2
[root@cn_mu_binlog_backup ~]# redis-cli -p 6381 incr no > res.txt
[root@cn_mu_binlog_backup ~]# cat res.txt
3
[root@cn_mu_binlog_backup ~]# redis-cli -p 6381 --no-raw incr no > res.txt
[root@cn_mu_binlog_backup ~]# cat res.txt
(integer) 4

阅读全文

自定义函数改表关联优化一例

今天朋友丢来一个SQL,叫我帮忙优化一下.受过落总真传,我瞄了几眼就知道咋回事了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
SELECT ESS.PK_NO,
HE.EMPID,
HE.LOCAL_NAME,
ESS.ITEM_NO ITEM_NO_NO,
ESS.PERSON_ID,
GET_DEPT_NAME(HE.DEPTNO, 'zh') DEPT_NAME,
GET_GLOBAL_NAME(ESS.ITEM_NO, 'zh') ITEM_NAME,
ESS.AR_DATE_STR,
TO_CHAR(ESS.FROM_TIME, 'HH24:MI') FROM_TIME,
TO_CHAR(ESS.TO_TIME, 'HH24:MI') TO_TIME,
ESS.QUANTITY,
ESS.REMARK,
HE.EMPID,
ESS.AR_DATE_STR,
GET_GLOBAL_NAME(ESS.STATUS_CODE, 'zh') STATUS_CODE,
GET_GLOBAL_NAME(ESS.ITEM_NO, 'zh') ITEM_NO,
ESS.REMARK,
ESS.LOCK_YN
FROM AR_DETAIL_HYOSUNG_JX ESS, HR_EMPLOYEE HE
WHERE ESS.PERSON_ID = HE.PERSON_ID
AND ESS.PERSON_ID NOT LIKE '111111%'
AND ESS.ITEM_NO IN ('141454', '14015951', '141445', '141443', '190000514')
AND EXISTS (SELECT B1.DEPTID
FROM HR_DEPARTMENT B1
WHERE B1.DEPTNO = HE.DEPTNO
START WITH B1.DEPTNO in
(SELECT HRD.DEPTID
FROM HR_DEPARTMENT HRD
WHERE HRD.MANAGER_EMP_ID = '11111117')
CONNECT BY PRIOR B1.DEPTNO = B1.PARENT_DEPT_NO
UNION
SELECT AR_SUPERVISOR_INFO.DEPTNO
FROM AR_SUPERVISOR_INFO
WHERE AR_SUPERVISOR_INFO.DEPTNO = HE.DEPTNO
AND AR_SUPERVISOR_INFO.PERSON_ID = '11111117')
ORDER BY ESS.AR_DATE_STR ASC, ESS.CREATE_DATE DESC, HE.DEPTNO, HE.EMPID

我让他先执行一下SQL 看看几秒,他说6秒
OK,再把select 里面那俩自定义函数GET_DEPT_NAME,GET_GLOBAL_NAME 注释掉查一下, 他说1.5秒
那这个SQL就是慢在这俩自定义函数上呗, 这个查询每返回一行,这函数就要执行一次
那么函数可以改成 标量 , 标量可以改成 letf join.

阅读全文

PMM使用Grafana告警

PMM如何告警?

从Grafana v4.0开始增加了Alterting功能 ( PMM 1.0.7 版本时Grafana版本为4.0). 这篇文章将手把手教你如何配置你的告警

开始创建Alert

PMM部署完成后,你可以看到如下界面
image

此时你可能需要对Threads_connected / Threads_running 指标进行监控

点击对应的Graph标题,点击Edit
image

按下图方式依次点击ALert -> Create Alert 创建告警
image

不幸的是,当你尝试对A指标创建如下告警时,Grafana提示一个错误“Template variables are not supported in alert queries.”
image

首先A代表什么可以从Metrics菜单中看到,从图中可以看到对于Threads_connected值的获取表达式中包含了变量$host, 而$host是箭头所指的Host下拉菜单传递的
image
对于使用变量的Mertrics,无法创建Alert

阅读全文

Pt-table-checksum原理浅析

Pt-table-checksum原理浅析

主库建一个表

1
2
3
4
5
node1> create table fan(id int) engine=innodb;
Query OK, 0 rows affected (0.12 sec)
node1> insert into fan values(1);
Query OK, 1 row affected (0.07 sec)

从库制造不一致

1
2
3
4
5
6
7
8
9
node2> select * from fan;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
node2> update fan set id=2;

两边打开general log,然后

1
2
3
[root@node1 ~]# pt-table-checksum --nocheck-replication-filters --no-check-binlog-format h=172.16.83.17,u=root,p=mysql,P=3307 --replicate=percona.checksums --recursion-method=dsn=h=172.16.83.21,D=percona,t=dsns --databases=sysbench --tables=fan
TS ERRORS DIFFS ROWS CHUNKS SKIPPED TIME TABLE
11-06T05:12:58 0 1 1 1 0 0.344 sysbench.fan

先看两边的checksums表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
node1> select * from `percona`.`checksums` where tbl='fan';
+----------+-----+-------+------------+-------------+----------------+----------------+----------+----------+------------+------------+---------------------+
| db | tbl | chunk | chunk_time | chunk_index | lower_boundary | upper_boundary | this_crc | this_cnt | master_crc | master_cnt | ts |
+----------+-----+-------+------------+-------------+----------------+----------------+----------+----------+------------+------------+---------------------+
| sysbench | fan | 1 | 0.019798 | NULL | NULL | NULL | 42981178 | 1 | 42981178 | 1 | 2017-11-06 05:12:58 |
+----------+-----+-------+------------+-------------+----------------+----------------+----------+----------+------------+------------+---------------------+
node2> select * from `percona`.`checksums` where tbl='fan';
+----------+-----+-------+------------+-------------+----------------+----------------+----------+----------+------------+------------+---------------------+
| db | tbl | chunk | chunk_time | chunk_index | lower_boundary | upper_boundary | this_crc | this_cnt | master_crc | master_cnt | ts |
+----------+-----+-------+------------+-------------+----------------+----------------+----------+----------+------------+------------+---------------------+
| sysbench | fan | 1 | 0.019798 | NULL | NULL | NULL | 40deaf21 | 1 | 42981178 | 1 | 2017-11-06 05:12:58 |
+----------+-----+-------+------------+-------------+----------------+----------------+----------+----------+------------+------------+---------------------+
1 row in set (0.04 sec)

阅读全文

pybackup使用文档

pybackup使用文档

https://github.com/Fanduzi/pybackup
pybackup源自于对线上备份脚本的改进和对备份情况的监控需求.
原本生产库的备份是通过shell脚本调用mydumper,之后再将备份通过rsync传输到备份机.
想要获取备份状态,时间,rsync传输时间等信息只能通过解析日志.
pybackup由python编写,调用mydumper和rsync,将备份信息存入数据库中,后期可以通过grafana图形化展示和监控备份
目前不支持2.6,仅在2.7.14做过测试

参数说明

帮助信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@iZ23t8cwo3iZ backup_db]# python pybackup.py -h
Usage:
pybackup.py mydumper ARG_WITH_NO_--... (([--no-rsync] [--no-history]) | [--only-backup])
pybackup.py only-rsync [--backup-dir=<DIR>] [--bk-id=<id>] [--log-file=<log>]
pybackup.py -h | --help
pybackup.py --version
Options:
-h --help Show help information.
--version Show version.
--no-rsync Do not use rsync.
--no-history Do not record backup history information.
--only-backup Equal to use both --no-rsync and --no-history.
--only-rsync When you backup complete, but rsync failed, use this option to rsync your backup.
--backup-dir=<DIR> The directory where the backup files need to be rsync are located. [default: ./]
--bk-id=<id> bk-id in table user_backup.
--log-file=<log> log file [default: ./rsync.log]
more help information in:
https://github.com/Fanduzi

1
pybackup.py mydumper ARG_WITH_NO_--... (([--no-rsync] [--no-history]) | [--only-backup])

除了最后三个参数,使用的所有参数和mydumper -h中列出的参数相同. 只不过目前只支持长选项,并且不带’–’

阅读全文


Powered by Hexo and Hexo-theme-hiker

Copyright © 2013 - 2018 Fan() All Rights Reserved.

访客数 : | 访问量 :

#