解决nginx开启http2不生效问题

先感受一下http2的优势

https://http2.akamai.com/demo

nginx在1.9起就开始支持http2了, 如下配置即可开启

listen 443 ssl http2;
server_name www.koyoz.com koyoz.com;

ssl on;
ssl_certificate fullchain.pem;
ssl_certificate_key privkey.pem;

验证是否开启: Chrome 安装 HTTP/2 and SPDY indicator, 若变成蓝色闪电就表示http2开启了

如果用以上配置后, 仍未开启 那说明编译时openssl的版本太低了. 下载最新openssl-1.0.2h
并指定 –with-openssl 为刚刚下载的新版本, 重新编译nginx, 即可开启https

效果如下:
h2

用curl测速

命令

curl -o /dev/null -s -w 'DNS:%{time_namelookup}\nConnect:%{time_connect}\nStart:%{time_starttransfer}\nTotal:%{time_total}\nSpeed:%{speed_download} byte/s' https://koyoz.com/blog/

返回结果:
DNS:0.030
Connect:0.078
Start:0.503
Total:0.515
Speed:60306.000 byte/s

字段含义:
time_connect 建立到服务器的 TCP 连接所用的时间
time_starttransfer 在发出请求之后,Web 服务器返回数据的第一个字节所用的时间
time_total 完成请求所用的时间
time_namelookup DNS解析时间,从请求开始到DNS解析完毕所用时间(记得关掉 Linux 的 nscd 的服务测试)
speed_download 下载速度,单位-字节每秒。

用grep遍历文件夹, 并查找文本文件中的内容

平时用得不多, 用的时候每次都忘记, 还是记录一下

grep –color -rnH ‘mysql’ *

如果只查找某种类型的文件, 可以配合find

find ./ -name ‘*.php’ | xargs -i grep ‘mysql’ {} -rnH –color

其中
grep -r 遍历子目录
grep -n 显示行号
grep -H 显示完整路径
grep –color 高亮显示

Elasticsearch 多字段评分排序

有这样一个需求: 展示一个用户列表, 有头像的优先展示, 有认证的优先展示, 有相册的优先展示, 同时拥有这些条件越多的则优先级更高, 第二排序条件是注册时间逆序.

通常是用条件进行过滤, 但往往是将不符合的条件数据去掉了, 而不是将其排在后面. 而对于多个条件时, 过滤更无能为力.

自定义脚本, 进行多字段评分排序, 可以很方便可以实现:

1. 写入配置文件, 开启script支持, 并重启Elasticsearch

script.groovy.sandbox.enabled: true

script.inline: on
script.indexed: on
script.search: on
script.engine.groovy.inline.aggs: on
script.engine.groovy.inline.search: on

2. 创建索引

PUT /users
{
  "mappings": {
    "list" : {
      "properties": {
        "Name": {
          "type": "string",
          "analyzer": "standard",
          "index": "analyzed"
        },
        "HasAvatar": {
          "type": "long"
        },
        "HasAlbum": {
          "type": "long"
        },
        "HasAuth": {
          "type": "long"
        },
        "RegTime": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss"
        }
      }
    }
  }
}

3. 添加测试数据

POST /users/list/_bulk
{"index": {"_id": 1}}
{"Name": "Allen", "HasAvatar": 1, "HasAlbum": 1, "HasAuth": 1, "RegTime": "2015-12-01 00:00:00"}
{"index": {"_id": 2}}
{"Name": "Burnell", "HasAvatar": 0, "HasAlbum": 0, "HasAuth": 0, "RegTime": "2015-11-02 00:00:00"}
{"index": {"_id": 3}}
{"Name": "Chasel", "HasAvatar": 0, "HasAlbum": 0, "HasAuth": 0, "RegTime": "2015-11-22 00:00:00"}
{"index": {"_id": 4}}
{"Name": "Eden", "HasAvatar": 1, "HasAlbum": 0, "HasAuth": 1, "RegTime": "2015-12-02 00:00:00"}
{"index": {"_id": 5}}
{"Name": "Ford", "HasAvatar": 1, "HasAlbum": 1, "HasAuth": 0, "RegTime": "2015-12-03 00:00:00"}
{"index": {"_id": 6}}
{"Name": "Gordon", "HasAvatar": 1, "HasAlbum": 0, "HasAuth": 0, "RegTime": "2015-12-04 00:00:00"}

4. 按条件搜索

GET /users/list/_search
{
  "sort": [{
    "_script": {
      "script": "doc['HasAvatar'].value  + doc['HasAlbum'].value + doc['HasAuth'].value",
      "type": "number",
      "order": "desc"
    },
    "RegTime":{"order": "desc"}
  }]
}

看看结果是不是符合预期:

{
   "took": 8,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 6,
      "max_score": null,
      "hits": [
         {
            "_index": "users",
            "_type": "list",
            "_id": "1",
            "_score": null,
            "_source": {
               "Name": "Allen",
               "HasAvatar": 1,
               "HasAlbum": 1,
               "HasAuth": 1,
               "RegTime": "2015-12-01 00:00:00"
            },
            "sort": [
               3,
               1448928000000
            ]
         },
         {
            "_index": "users",
            "_type": "list",
            "_id": "5",
            "_score": null,
            "_source": {
               "Name": "Ford",
               "HasAvatar": 1,
               "HasAlbum": 1,
               "HasAuth": 0,
               "RegTime": "2015-12-03 00:00:00"
            },
            "sort": [
               2,
               1449100800000
            ]
         },
         {
            "_index": "users",
            "_type": "list",
            "_id": "4",
            "_score": null,
            "_source": {
               "Name": "Eden",
               "HasAvatar": 1,
               "HasAlbum": 0,
               "HasAuth": 1,
               "RegTime": "2015-12-02 00:00:00"
            },
            "sort": [
               2,
               1449014400000
            ]
         },
         {
            "_index": "users",
            "_type": "list",
            "_id": "6",
            "_score": null,
            "_source": {
               "Name": "Gordon",
               "HasAvatar": 1,
               "HasAlbum": 0,
               "HasAuth": 0,
               "RegTime": "2015-12-04 00:00:00"
            },
            "sort": [
               1,
               1449187200000
            ]
         },
         {
            "_index": "users",
            "_type": "list",
            "_id": "3",
            "_score": null,
            "_source": {
               "Name": "Chasel",
               "HasAvatar": 0,
               "HasAlbum": 0,
               "HasAuth": 0,
               "RegTime": "2015-11-22 00:00:00"
            },
            "sort": [
               0,
               1448150400000
            ]
         },
         {
            "_index": "users",
            "_type": "list",
            "_id": "2",
            "_score": null,
            "_source": {
               "Name": "Burnell",
               "HasAvatar": 0,
               "HasAlbum": 0,
               "HasAuth": 0,
               "RegTime": "2015-11-02 00:00:00"
            },
            "sort": [
               0,
               1446422400000
            ]
         }
      ]
   }
}

字段评分还能根据不同需要给予权重, 同样可以用于带搜索词的情况中, 只需要设置好对应的评分权重即可.

解决MySQL5.6版本”innodb_table_stats” not found

MySQL启动时, 在error log中报错如下

2015-01-30 17:00:45 7f4d6a59c700 InnoDB: Error: Table "mysql"."innodb_table_stats" not found.
2015-01-30 17:00:45 7f4d6a59c700 InnoDB: Error: Fetch of persistent statistics requested for table "xxx"."wp3_options" but the required system tables mysql.innodb_table_stats and mysql.innodb_index_stats are not present or have unexpected structure. Using transient stats instead.
......

以root登陆mysql

use mysql;

drop table `innodb_index_stats`;

drop table `innodb_table_stats`;

drop table `slave_master_info`;

drop table `slave_relay_log_info`;

drop table `slave_worker_info`;

执行时若报table不存在的错误,可以不用管.

然后进入mysql的datadir, 执行

rm -f innodb_* slave_*

在到mysql中, 执行

source /yourpath/five-tables.sql

附件: five-tables

重启MySQL, 错误不再有了.