PHP连接MySQL 8.0报错

知数堂推了两期关于MySQL 8.0的公开课 https://zst.ke.qq.com/ ,很多程序员,也按耐不住了,要升级到MySQL 8.0. PHP是世界上最好的语言。 骚年肯定也不会落后。但升级到MySQL 8.0后,马上面监到两个问题。

  1.  PHP连接上MySQL。 同样的程序 MySQL 5.7没事,换了MySQL8.0 报: Access Denied for user ‘XXXX’
  2.  Server sent charset (255) unknown to the client. Please, report to the developers in … on line X . 不识别字符集

人生艰难,再次打击我们PHP程序员。 好吧! MySQL 8.0 发生重大变革了:

1. 认证方法更新为了: caching_sha2_password
2. 字符集改成了默认: utf8mb4

据说字符集的已经在php-7.20 支持了。 不过升级PHP也不容易。 我们先几个兼容支持方法吧。

关于认证和字符集兼容:

1. 配置文件[mysqld]部分

[mysqld]
default_authentication_plugin=mysql_native_password
character_set_server =utf8

上面这两个配置,把MySQL 8.0 认证更改成老的方法,字符集更改成原来的:utf8.

2. 创建基于native_password的帐号

CREATE USER 'wubx'@'%' IDENTIFIED WITH mysql_native_password BY 'zstzst';
GRANT ALL PRIVILEGES ON *.* TO 'wubx'@'%';

3. 创建DB

CREATE DATABASE IF NOT EXISTS `zst` COLLATE 'utf8_general_ci' ;
show create database zst;

确认字符集是utf8.

 

4. 使用我们新创建的帐号连接zst,应该万事Ok。 如果遇到其它问题,欢入裙交流

 

作者:吴炳锡 来源:http://wubx.net/ 联系方式: wubingxi#163.com 转载请注明作/译者和出处,并且不能用于商业用途,违者必究.

CentOS下编译XeLabs TokuDB

1. 介绍

XeLabs TokuDB 官方源码: https://github.com/XeLabs/tokudb
支持: CentOS, Ubuntu
编辑环境要求: gcc > 4.8 cmake > 3.5

2. CentOS 6下编译举例

2.1 基本准备

yum install centos-release-scl

yum install cmake3
yum install devtoolset-3-gcc-c++.x86_64
yum install devtoolset-3-gcc.x86_64
如果是CentOS7就不需要安装devtoolset这个包,系统自带的gcc版本就可以。

启动新版本gcc
source /opt/rh/devtoolset-3/enable

安装cmake3
yum install cmake3
(如果找不到cmake3 请安装epel源)

yum install zlib-devel.x86_64
yum install libaio-devel
yum install ncurses-devel
yum install readline.x86_64 readline-devel.x86_64

2.2 下载tokudb代码

git clone https://github.com/XeLabs/tokudb.git
cd tokudb
git submodule init
git submodule update

编译参考:

vim cc.sh

cmake3 .\
  -DCMAKE_BUILD_TYPE=RelWithDebInfo\
  -DBUILD_CONFIG=mysql_release\
  -DFEATURE_SET=community\
  -DWITH_EMBEDDED_SERVER=OFF\
  -DTOKUDB_VERSION=7.5.6\
  -DBUILD_TESTING=OFF\
  -DWITHOUT_ROCKSDB_STORAGE_ENGINE=1 \
  -DWITH_BOOST=extra/boost/boost_1_59_0.tar.gz\
  -DCMAKE_INSTALL_PREFIX=/usr/local/tokudb 
  • 执行参数配置初始化 sh cc.sh
  • 待执行成功后,提示Configure done
    make
    make install对于CentOS6,CentOS7的同学,如果就是想偿个鲜不想编译的话可以在下面地址下载:
    https://pan.baidu.com/s/1qYRyH3I
  • 因为TokuDB禁用hugepage:
    echo never > /sys/kernel/mm/transparent_hugepage/enabled

后面和正常基于二进制安装MySQL一样。

3. Tips

在sh cc.sh 中出错,需要删除:CMakeCache.txt
rm -rf CMakeCache.txt
有时还需要删除:
rm -rf CMakeFiles/*

如果因为出错提示缺了某个包,安装上去后,再继续。

4. 备注

devtoolset-2 目前已经有gcc的参数不支持,编译通不过(2017/12/08)
devtoolset-3 在CentOS 6上遇到gcc的段错误,如果没有段错误,devtoolset-3是可以用的。(2017/12/08)
devtoolset-4 目前在CentOS 6上测试通过(2017/12/08)
CentOS7 自带gcc可以编译通过(2017/12/08)

编译中如果遇到其它诡异问题,可以加入QQ群:579036588 联系群主获取支持。如果你做了其它版本的发行包,也可以联系群主,一块提供一些发行包。

案例推荐

TokuDB在生产环境的应用场景(zabbix也可以) http://blog.51cto.com/412166174/2050739

作者:吴炳锡 来源:http://wubx.net/ 联系方式: wubingxi#163.com 转载请注明作/译者和出处,并且不能用于商业用途,违者必究.

磁盘利用率和饱和度

导读

如何观察磁盘的IO利用率以及饱和度?

看文本文给你解药!

翻译团队:知数堂藏经阁项目 – 菜鸟盟

团队成员:菜鸟盟–hades、菜鸟盟-bruce、菜鸟盟-冰焰

译文校稿:叶师傅

原文出处:https://www.percona.com/blog/2017/08/28/looking-disk-utilization-and-saturation/

原文作者:Peter Zaitsev (Percona CEO)

在这篇文章里,会介绍磁盘利用率和饱和度相关的知识。

In this blog post, I will look at disk utilization and saturation.

在之前的博客里面,我写了一些关于CPU使用率和饱和度之间有什么实质性不同,以及CPU使用率、饱和度如何从不同维度影响响应时间(RT)的文章。
现在我们来看另一个影响数据库性能重要因素:存储子系统。在下面文章里,我会用“磁盘”代替存储子系统。

In my previous blog post, I wrote about CPU utilization and saturation, the practical difference between them and how different CPU utilization and saturation impact response times.

Now we will look at another critical component of database performance: the storage subsystem. In this post, I will refer to the storage subsystem as “disk” (as a casual catch-all).

监控IO性能最常用的工具是iostat,会显示如下的信息:

The most common tool for command line IO performance monitoring is iostat, which shows information like this:

root@ts140i:~# iostat -x nvme0n1 5
Linux 4.4.0-89-generic (ts140i)         08/05/2017      _x86_64_        (4 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
          0.51    0.00    2.00    9.45    0.00   88.04

Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util
nvme0n1           0.00     0.00 3555.57 5887.81 52804.15 87440.73    29.70     0.53    0.06    0.13    0.01   0.05  50.71

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
          0.60    0.00    1.06   20.77    0.00   77.57

Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util
nvme0n1           0.00     0.00 7612.80    0.00 113507.20     0.00    29.82     0.97    0.13    0.13    0.00   0.12  93.68

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
          0.50    0.00    1.26    6.08    0.00   92.16

Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util
nvme0n1           0.00     0.00 7653.20    0.00 113497.60     0.00    29.66     0.99    0.13    0.13    0.00   0.12  93.52

第一行(avg-cpu)显示的是自系统启动之后平均的性能。某些情况下,用当前系统的压力和平均性能作对比是很有用的。这篇文章的案例是测试环境,所以可以忽略对比这两种情况。
第二行(Device)显示的当前5秒钟的性能指标(在命令行中指定了每5秒输出一次)。

The first line shows the average performance since system start. In some cases, it is useful to compare the current load to the long term average. In this case, as it is a test system, it can be safely ignored. The next line shows the current performance metrics over five seconds intervals (as specified in the command line).

iostat命令用%util列显示利用率的信息,可以通过观察平均请求队列大小(the avgqu-sz 列)或者通过r_await和w_await列(显示平均的读和写的等待)来观察IO饱和度。如果超过正常值,设备就会过度饱和了。

The iostat command reports utilization information in the %util column, and you can look at saturation by either looking at the average request queue size (the avgqu-sz column) or looking at the r_await and w_await columns (which show the average wait for read and write operations). If it goes well above “normal” then the device is over-saturated.

和之前的文章一样,我们会执行Sysbench,然后观察iostat命令、Percona PMM的输出。

As in my previous blog post, we’ll perform some system Sysbench runs and observe how the iostat command line tool and Percona Monitoring and Management graphs behave.

我们用Sysbench测试文件IO,以便观察磁盘的变化。我创建了一个100GB的文件,因为用了DirectIO方式所以所有的请求都会直接打到磁盘。我也会用”sync”刷新模式以便更好的控制IO请求的并发度。

To focus specifically on the disk, we’re using the Sysbench fileio test. I’m using just one 100GB file, as I’m using DirectIO so all requests hit the disk directly. I’m also using “sync” request submission mode so I can get better control of request concurrency.

在这个实验中我用了一个Intel 750 NVME固态硬盘(虽然这并不重要)。

I’m using an Intel 750 NVME SSD in this test (though it does not really matter).

Sysbench FileIO 1线程

    root@ts140i:/mnt/data# sysbench  --threads=1 --time=600 --max-requests=0  fileio --file-num=1 --file-total-size=100G --file-io-mode=sync --file-extra-flags=direct --file-test-mode=rndrd run
     
    File operations:
       reads/s:                      7113.16
       writes/s:                     0.00
       fsyncs/s:                     0.00
     
    Throughput:
       read, MiB/s:                  111.14
       written, MiB/s:               0.00
     
    General statistics:
       total time:                          600.0001s
       total number of events:              4267910
     
    Latency (ms):
            min:                                  0.07
            avg:                                  0.14
            max:                                  6.18
            95th percentile:                      0.17

单线程测试结果通常可以作为基线,通常只有一个请求时的响应时间通常也最快(不过一般也不是最佳吞吐量)。

A single thread run is always great as a baseline, as with only one request in flight we should expect the best response time possible (though typically not the best throughput possible).

Iostat
Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util
nvme0n1           0.00     0.00 7612.80    0.00 113507.20     0.00    29.82     0.97    0.13    0.13    0.00   0.12  93.68

磁盘读写延迟

磁盘延迟图像证实了我们从iostat命令看到的磁盘IO延迟,它指定了特定设备。我们用它作为基线和更高并发做对比。

The Disk Latency graph confirms the disk IO latency we saw in the iostat command, and it will be highly device-specific. We use it as a baseline to compare changes to with higher concurrency.

磁盘IO使用率

即便我们只发起一个IO请求(队列深度),但磁盘IO使用率已很接近100%。这个是linux磁盘使用率显示的问题,它不像CPU利用率,由于IO设备的特殊设计机制linux不能直接显示其利用率。它真正有多少可执行单元?他们是怎么被使用的?当做了raid后,每个物理磁盘都可以被视为单独的执行单元,而SSD磁盘以及云存储(比如EBS)则可能有更多执行单元。

Disk IO utilization is close to 100% even though we have just one outstanding IO request (queue depth). This is the problem with Linux disk utilization reporting: unlike CPUs, Linux does not have direct visibility on how the IO device is designed. How many “execution units” does it really have? How are they utilized? Single spinning disks can be seen as a single execution unit while RAID, SSDs and cloud storage (such as EBS) are more than one.

磁盘负载

这个图片显示磁盘负载(或者请求队列大小),它和实际压到磁盘上线程的数量是大致匹配的。

This graph shows the disk load (or request queue size), which roughly matches the number of threads that are hitting disk as hard as possible.

饱和度(IO压力)

饱和度指标图上的IO负载几乎显示了相同的数值。唯一的不同是不像磁盘IO统计信息一样,它显示了整个系统的概要。

The IO load on the Saturation Metrics graph shows pretty much the same numbers. The only difference is that unlike Disk IO statistics, it shows the summary for the whole system.

Sysbench FileIO 4线程

现在让我们把IO提高到四个并发线程,再来看看磁盘的情况:

Now let’s increase IO to four concurrent threads and see how disk responds:

root@ts140i:/mnt/data# sysbench  --threads=4 --time=600 --max-requests=0  fileio --file-num=1 --file-total-size=100G --file-io-mode=sync --file-extra-flags=direct --file-test-mode=rndrd run

    File operations:  
       reads/s:                     26248.44  
       writes/s:                    0.00  
       fsyncs/s:                    0.00  
    Throughput:  
       read, MiB/s:                  410.13  
       written, MiB/s:               0.00  
    General statistics:  
       total time:                                       600.0002s  
       total number of events:                 15749205  
    Latency (ms):  
            min:                                  0.06  
            avg:                                  0.15  
            max:                                 8.73  
            95th percentile:               0.21  

我们看到请求数量线性增加,而请求延迟变化却很小:0.14ms vs 0.15ms。这表明设备内部有足够的执行单元并行处理负载,而且不存在其他瓶颈(如连接接口)。

We can see the number of requests scales almost linearly, while request latency changes very little: 0.14ms vs. 0.15ms. This shows the device has enough execution units internally to handle the load in parallel, and there are no other bottlenecks (such as the connection interface).

Iostat  
Device:        rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await   w_await  svctm  %util  
nvme0n1       0.00    0.00  28808.60    0.00   427668.00     0.00    29.69     4.05    0.14    0.14    0.00   0.03  99.92  

磁盘读写延迟

磁盘IO使用率

磁盘负载

饱和度(IO压力)

Sysbench FileIO 16线程

root@ts140i:/mnt/data# sysbench  --threads=16 --time=600 --max-requests=0  fileio --file-num=1 --file-total-size=100G --file-io-mode=sync --file-extra-flags=direct --file-test-mode=rndrd run

    File operations:  
       reads/s:                    76845.96  
       writes/s:                     0.00  
       fsyncs/s:                    0.00  
    Throughput:  
       read, MiB/s:                  1200.72  
       written, MiB/s:                0.00  
    General statistics:
       total time:                          600.0003s  
       total number of events:              46107727  
    Latency (ms):  
            min:                                  0.07  
            avg:                                  0.21  
            max:                                  9.72  
            95th percentile:                 0.36  
            

从4个线程到16个线程,我们再次看到吞吐量有较大提升而响应时间只轻微变大。如果你仔细观察结果,你将注意到一个更有趣的事情:平均响应时间从0.15ms增加到0.21ms(增加了40%),而95%的响应时间从0.21ms增加到0.36 ms(增加了71%)。我还进行了一个单独的测试来测量99%的响应时间,发现差异甚至更大:0.26ms vs 0.48ms(增加了84%)。

Going from four to 16 threads, we again see a good throughput increase with a mild response time increase. If you look at the results closely, you will notice one more interesting thing: the average response time has increased from 0.15ms to 0.21ms (which is a 40% increase), while the 95% response time has increased from 0.21ms to 0.36ms (which is 71%). I also ran a separate test measuring 99% response time, and the difference is even larger: 0.26ms vs. 0.48ms (or 84%).

这是一个重要的观察:一旦趋于饱和,方差可能会增加,而且其中一些请求会受到不同程度的影响(不仅仅只是我们看到的平均响应时间)。

This is an important observation to make: once saturation starts to happen, the variance is likely to increase and some of the requests will be disproportionately affected (beyond what the average response time shows).

Iostat  
Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util  
nvme0n1           0.00     0.00 82862.20    0.00 1230567.20     0.00    29.70    16.33    0.20    0.20    0.00   0.01 100.00  

磁盘读写延迟

磁盘IO使用率

磁盘负载

饱和度(IO压力)

上面的图表结果符合预期:磁盘负载和IO负载从基本饱和增长到约为16,磁盘IO利用率则保持在100%。

The graphs show an expected figure: the disk load and IO load from saturation are up to about 16, and utilization remains at 100%.

需要注意的是图形中的抖动有所增加。IO利用率飙到100%以上,磁盘IO负载峰值上升到18,而请求队列却并不是很大。这就需要先从这些信息如何去收集的角度去考虑问题。尝试每秒对这些数据进行采样,但是在有实际负载的系统中,这个采集进程也需要费些时间:我们本来希望每秒采集一次数据,而实际上间隔则是1.05 ~ 0.95秒。当数学应用于数据采集时,我们所看到的图表中就可能会有本不应该出现的波峰、波谷(或刺点)。所以从大的角度来看,是可以忽略这些波动的。

One thing to notice is increased jitter in the graphs. IO utilization jumps to over 100% and disk IO load spikes to 18, when there should not be as many requests in flight. This comes from how this information is gathered. An attempt is made to sample this data every second, but with the loaded system it takes time for this process to work: sometimes when we try to get the data for a one-second interval but really get data for 1.05- or 0.95-second intervals. When the math is applied to the data, it creates the spikes and dips in the graph when there should be none. You can just ignore them if you’re looking at the big picture.

Sysbench FileIO 64 Threads

最后,让我们用sysbench跑64个并发线程的情况:

Finally, let’s run sysbench with 64 concurrent threads hitting the disk:

root@ts140i:/mnt/data# sysbench  --threads=64 --time=600 --max-requests=0  fileio --file-num=1 --file-total-size=100G --file-io-mode=sync --file-extra-flags=direct --file-test-mode=rndrd run

    File operations:  
       reads/s:                      127840.59  
       writes/s:                     0.00  
       fsyncs/s:                     0.00  
    
    Throughput:  
       read, MiB/s:                  1997.51  
       written, MiB/s:               0.00  
    
    General statistics:  
       total time:                          600.0014s  
       total number of events:              76704744  
    
    Latency (ms):  
            min:                                  0.08  
            avg:                                  0.50  
            max:                                  9.34  
            95th percentile:                      1.25  

我们可以看到平均响应耗时从0.21ms上升到0.50ms(两倍多),此外95%的响应时间从0.36ms跃升到1.25ms。实际上,我们可以看到IO负载开始饱和了,以前我们看到随着CPU饱和度的增加,并发线程数越多则响应耗时也越久,但这次随着并发线程数的增加,我们并未看到响应耗时随着线性增加。猜测是因为我们测试IO设备有不错的内部IO并行能力,所以响应请求效率很高(即便并发线程从16增加到64)。

We can see the average has risen from 0.21ms to 0.50 (more than two times), and 95% almost tripped from 0.36ms to 1.25ms. From a practical standpoint, we can see some saturation starting to happen, but we’re still not seeing a linear response time increase with increasing numbers of parallel operations as we have seen with CPU saturation. I guess this points to the fact that this IO device has a lot of parallel capacity inside and can process requests more effectively (even going from 16 to 64 concurrent threads).

在这个测试中,当我们将并发性从1个增加到64个时,我们看到响应耗时从0.14ms增加到0.5ms(约三倍)。此时95%的响应耗时从0.17ms增加到1.25ms(约七倍)。实际上,从这时开始看到IO设备趋于饱和了。

Over the series of tests, as we increased concurrency from one to 64, we saw response times increase from 0.14ms to 0.5ms (or approximately three times). The 95% response time at this time grew from 0.17ms to 1.25ms (or about seven times). For practical purposes, this is where we see the IO device saturation start to show.

Iostat  
Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await   w_await  svctm  %util  
nvme0n1           0.00     0.00 138090.20    0.00   2049791.20     0.00    29.69    65.99    0.48    0.48    0.00   0.01 100.24  

我们将略过相关图表,因为它们基本雷同,只是在64并发线程下有更高的延迟。

We’ll skip the rest of the graphs as they basically look the same, just with higher latency and 64 requests in flight.

Sysbench FileIO 256线程

    root@ts140i:/mnt/data# sysbench  --threads=256 --time=600 --max-requests=0  fileio --file-num=1 --file-total-size=100G --file-io-mode=sync --file-extra-flags=direct --file-test-mode=rndrd run
     
    File operations:
       reads/s:                      131558.79
       writes/s:                     0.00
       fsyncs/s:                     0.00
     
    Throughput:
       read, MiB/s:                  2055.61
       written, MiB/s:               0.00
     
    General statistics:
       total time:                          600.0026s
       total number of events:              78935828
     
    Latency (ms):
            min:                                  0.10
            avg:                                  1.95
            max:                                 17.08
            95th percentile:                      3.89            
Iostat
Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util
nvme0n1           0.00     0.00 142227.60    0.00 2112719.20     0.00    29.71   268.30    1.89    1.89    0.00   0.0

最后,当并发256个线程时,从平均响应耗时的线性增长结果表明了设备已经过载,且IO请求开始排队。这里没有简单的办法判断它是由于IO总线饱和(此时IO读速率为2GB/sec),还是由于设备内部处理能力所限导致的。

With 256 threads, finally we’re seeing the linear growth of the average response time that indicates overload and queueing to process requests. There is no easy way to tell if it is due to the IO bus saturation (we’re reading 2GB/sec here) or if it is the internal device processing ability.

正如我们所见,并发数从16个到64个时,响应耗时小于线性增长,当从64到256则是线性增长,我们可以看到这个设备最佳的并发是介于16个和64之间。这时的吞吐量可以达到峰值,且没有大量的请求排队。

As we’ve seen a less than linear increase in response time going from 16 to 64 connections, and a linear increase going from 64 to 256, we can see the “optimal” concurrency for this device: somewhere between 16 and 64 connections. This allows for peak throughput without a lot of queuing.

在总结之前,我想对这次特别的测试先做一个重要的说明。这是个随机读的测试,对于很多数据库来说是个重要的工作负载模式,但可能不是最主要负载场景。可能是写为主,或者顺序IO读 (则会有相应不同的表现)。对于其它的工作负载模式,我希望本文可以帮助你在分析时提供些参考、思路。

Before we get to the summary, I want to make an important note about this particular test. The test is a random reads test, which is a very important pattern for many database workloads, but it might not be the dominant load for your environment. You might be write-bound as well, or have mainly sequential IO access patterns (which could behave differently). For those other workloads, I hope this gives you some ideas on how to also analyze them.

另一种思考饱和度的方法

Another Way to Think About Saturation

当我问起他Percona的同学对本文的反馈意见时,Yves Trudeau同学提供了另外一种思考饱和度的方法:与单线程模式相比,平均响应耗时的增长百分比作为饱和度的衡量指标 。例如:

When I asked the Percona staff for feedback on this blog post by, my colleague Yves Trudeau provided another way to think about saturation: measure saturation as percent increase in the average response time compared to the single user. Like this:

Threads Avg Response Time Saturation
1 0.14
4 0.15 1.07x or 7%
16 0.21 1.5x or 50%
64 0.50 3.6x or 260%
256 1.95 13.9x or 1290%

总结

Summary

  • 我们可以看到如何理解磁盘利用率和饱和度要比CPU复杂的多;
  • We can see how understanding disk utilization and saturation is much more complicated than for the CPU:
  • 利用率指标(由iostat和PMM输出的%Util)对理解真实的IO设备利用率是没啥作用。因为它只是衡量在运行中至少有一个请求时的耗时。如果是CPU的这个指标,它将相当于运行在至少一个核上(对于高度并发系统不是很有用);
  • The Utilization metric (as reported by iostat and by PMM) is not very helpful for showing true storage utilization, as it only measures the time when there is at least one request in flight. If you had the same metric for the CPU, it would correspond to something running on at least one of the cores (not very useful for highly parallel systems).
  • 不同于CPU,Linux工具不给我们提供关于存储设备的底层结构和在不饱和的情况下还可以处理多少并行负载的信息。更甚的是,存储设备可能有不同的底层源头会导致饱和。例如,它可能是网络连接,SATA总线,甚至是老内核的内核IO栈去处理高速存储设备(译者注:跟不上时代发展啦)。
  • Unlike a CPU, Linux tools do not provide us with information about the structure of the underlying storage and how much parallel load it should be able to handle without saturation. Even more so, storage might well have different low-level resources that cause saturation. For example, it could be the network connection, SATA BUS or even the kernel IO stack for older kernels and very fast storage.
  • 根据运行中的请求数量来测量饱和有助于判断是否饱和,但是由于我们不知道设备可以高效的并发处理多少请求,只看基础指标我们不能确定设备是否过载;
  • Saturation as measured by the number of requests in flight is helpful for guessing if there might be saturation, but since we do not know how many requests the device can efficiently process concurrently, just looking the raw metric doesn’t let us determine that the device is overloaded
  • 平均响应耗时对于观察饱和度是一个很好的指标,但是和响应耗时一样,您无法判断这个设备的响应耗时是高是低。您需要在上下文中查看它并将其与基线进行比较。当你查看平均响应耗时时,确保你分别查看读响应时间和写响应时间,并且记下平均请求队列大小以确保是相同角度的比较。
  • Avg Response Time is a great metric for looking at saturation, but as with the response time you can’t say what response time is good or bad for this device. You need to look at it in context and compare it to the baseline. When you’re looking at the Avg Response Time, make sure you’re looking at read request response time vs. write request response time separately, and keep the average request size in mind to ensure we are comparing apples to apples.

MySQL命令使用技巧|新手指引

MySQL客户端读取配置文件的顺序

MySQL这个命令可以读取配置文件如下:

#mysql --help |grep my.cnf

Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf

按顺序加载,后面的会把前面的覆盖掉。可以识别配置文件中:
[client] [mysql] ,如下:

[client]
port            = 3306
socket          = /tmp/mysql3306.sock

[mysql]
prompt="\\u@\\h [\\d]>"
#pager="less -i -n -S"
#tee=/opt/mysql/query.log
no-auto-rehash

基本使用篇

更改MySQL提示符

MySQL的传统提示符是:

mysql>

这里推荐通过修改配置文件中[MySQL]的:prompt,方便了解连接到哪个MySQL哪个DB上。如下:

[mysql]
prompt="\\u@\\h [\\d]>"

更多参考可查看:man MySQL

   +-------+--------------------------------------------------------------------+
       |Option | Description                                                        |
       +-------+--------------------------------------------------------------------+
       |\C     | The current connection identifier (MySQL 5.7.6 and up)             |
       +-------+--------------------------------------------------------------------+
       |\c     | A counter that increments for each statement you issue             |
       +-------+--------------------------------------------------------------------+
       |\D     | The full current date                                              |
       +-------+--------------------------------------------------------------------+
       |\d     | The default database                                               |
       +-------+--------------------------------------------------------------------+
       |\h     | The server host                                                    |
       +-------+--------------------------------------------------------------------+
       |\l     | The current delimiter                                              |
       +-------+--------------------------------------------------------------------+
       |\m     | Minutes of the current time                                        |
       +-------+--------------------------------------------------------------------+
       |\n     | A newline character                                                |
       +-------+--------------------------------------------------------------------+
       |\O     | The current month in three-letter format (Jan, Feb, ...)           |
       +-------+--------------------------------------------------------------------+
       |\o     | The current month in numeric format                                |
       +-------+--------------------------------------------------------------------+
       |\P     | am/pm                                                              |
       +-------+--------------------------------------------------------------------+
       |\p     | The current TCP/IP port or socket file                             |
       +-------+--------------------------------------------------------------------+
       |\R     | The current time, in 24-hour military time (0–23)                  |
       +-------+--------------------------------------------------------------------+
       |\r     | The current time, standard 12-hour time (1–12)                     |
       +-------+--------------------------------------------------------------------+
       |\S     | Semicolon                                                          |
       +-------+--------------------------------------------------------------------+
       |\s     | Seconds of the current time                                        |
       +-------+--------------------------------------------------------------------+
       |\t     | A tab character                                                    |
       +-------+--------------------------------------------------------------------+
       |\U     |                                                                    |
       |       |        Your full user_name@host_name account name                  |
       +-------+--------------------------------------------------------------------+
       |\u     | Your user name                                                     |
       +-------+--------------------------------------------------------------------+
       |\v     | The server version                                                 |
       +-------+--------------------------------------------------------------------+
       |\w     | The current day of the week in three-letter format (Mon, Tue, ...) |
       +-------+--------------------------------------------------------------------+
       |\Y     | The current year, four digits                                      |
       +-------+--------------------------------------------------------------------+
       |\y     | The current year, two digits                                       |
       +-------+--------------------------------------------------------------------+
       |\_     | A space                                                            |
       +-------+--------------------------------------------------------------------+
       |\      | A space (a space follows the backslash)                            |
       +-------+--------------------------------------------------------------------+
       |\'     | Single quote                                                       |
       +-------+--------------------------------------------------------------------+
       |\"     | Double quote                                                       |
       +-------+--------------------------------------------------------------------+
       |\\     | A literal \ backslash character                                    |
       +-------+--------------------------------------------------------------------+
       |\x     |                                                                    |
       |       |        x, for any “x” not listed above                             |
       +-------+--------------------------------------------------------------------+

动态的调整测试:

mysql> prompt (\u@\h) [\d]>\_
PROMPT set to '(\u@\h) [\d]>\_'
(user@host) [database]>
#想回到解放前原始样子,直接输入:prompt 就可以。
(user@host) [database]> prompt
Returning to default PROMPT of mysql>
mysql>

使用login-path实现无密码登录

MySQL 5.6后推出了mysql_config_editor这个命令,本地加密存储用户的密码,通过指定登录文中的某一个MySQL而不用输入密码进行登录。

使用方法: mysql_config_editor set –login-path=登录实例的名字 –host=ip –user=用户名 –password 输入密码即可。具体如下:

#mysql_config_editor set --login-path=3306 --host=127.0.0.1 --port=3306 --user=wubx --password
Enter password:


#mysql_config_editor print --all
[3306]
user = wubx
password = *****
host = 127.0.0.1
port = 3306

#mysql --login-path=3306             
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 33
Server version: 5.7.19-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

wubx@127.0.0.1:3306 [(none)]>

更多操作查看: mysql_config_editor –help

命令行执行SQL

通过命令行执行SQL方法有多种,以下三种较为常见:

  • 通过 -e 参数指定SQL
#mysql -S /tmp/mysql3306.sock -uroot -pzhishutang.com  -e "select version()"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| version()  |
+------------+
| 5.7.19-log |
+------------+
  • 通管道来执行
#echo "select version()"|mysql -S /tmp/mysql3306.sock -uroot -pwubxwubx  zst
mysql: [Warning] Using a password on the command line interface can be insecure.
version()
5.7.19-log
  • 通过login-path进行上面的方操作
#echo "select version()"|mysql --login-path=3306
version()
5.7.19-log
#mysql --login-path=3306 -e "select version()"
+------------+
| version()  |
+------------+
| 5.7.19-log |
+------------+
 echo "select version()"|mysql --login-path=3306

高级进阶篇

Pager使用

这算是MaSQL这个命令中的一个高级功能。

  • 分屏显示
wubx@127.0.0.1:3306 [(none)]>pager more
wubx@127.0.0.1:3306 [(none)]>pager less
  • 结果用md5sum比较
wubx@127.0.0.1:3306 [(none)]>pager md5sum
PAGER set to 'md5sum'
wubx@127.0.0.1:3306 [(none)]>select * from information_schema.tables;
56696bd844f2e5885ce9278d3beca750  -
  • 结果中搜索
wubx@127.0.0.1:3306 [(none)]>pager grep  Sleep|wc -l;
wubx@127.0.0.1:3306 [(none)]>show processlist;
  • 不显示查询出来结果
wubx@127.0.0.1:3306 [(none)]>pager cat >>/dev/null
wubx@127.0.0.1:3306 [(none)]>select * from information_schema.tables;
408 rows in set (0.02 sec)
  • 恢复 pager
wubx@127.0.0.1:3306 [(none)]>pager
或是
wubx@127.0.0.1:3306 [(none)]>nopager

记录MySQL输入的命令及结果

使用tee命令或是在配置文件配置,参考:

wubx@127.0.0.1:3306 [(none)]>tee /tmp/mysql.log
wubx@127.0.0.1:3306 [(none)]>select * from information_schema.tables;

输入点操作,观察一下/tmp/mysql.log吧

另外也可以通过在配置文件中,加这个配置 [mysql] tee=/tmp/mysql.log

再次登录即可 (前提这个配置文件是可以被mysql读到的)

MySQL调用系统命令

该功能只能Linux平台支持,利用system后面跟命令调用,参考:

wubx@127.0.0.1:3306 [(none)]>system top

wubx@127.0.0.1:3306 [(none)]>system ps axu |grep mysqld
mysql     5041  0.0 21.9 1077760 223936 pts/0  Sl   14:33   0:02 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3306/my3306.cnf
root      5368  0.0  0.1 106148  1052 pts/0    S+   15:41   0:00 sh -c  ps axu|grep mysqld
root      5370  0.0  0.0 103360   816 pts/0    S+   15:41   0:00 grep mysqld

感受一下吧,是不是很帅,更好的等你来发现。

执行外面的SQL

利用source命令执行外面的SQL,如:

echo "select version();" >/tmp/v.sql
wubx@127.0.0.1:3306 [(none)]>source /tmp/v.sql;
+------------+
| version()  |
+------------+
| 5.7.19-log |
+------------+
1 row in set (0.00 sec)

使用binary模型批量执行

如果你在恢复数据时遇到:

mysql -u root -p -h localhost -D zhishutang < dump.sql
ERROR: ASCII '\0' appeared in the statement, but this is not allowed unless option --binary-mode is enabled and mysql is run in non-interactive mode. Set --binary-mode to 1 if ASCII '\0' is expected. Query: 'XXXX'.

请更改为,在MySQL后面添加–binary-mode,如:

mysql --binary-mode  -u root -p -h localhost -D zhishutang < dump.sql

后语

MySQL这个客端使用较多,你还有什么好玩的方法,欢迎回复,我会整理补充到这篇文章中,让更多刚接触MySQL的人员能快速地上手。

 

作者:吴炳锡 来源:http://wubx.net/ 联系方式: wubingxi#163.com 转载请注明作/译者和出处,并且不能用于商业用途,违者必究.

我猜你一定达不到要求的《MySQL安全策略》

导读

除了MySQL自身的账号密码安全管理,系统层面、应用层面的安全策略你注意到了吗?

数据是企业核心资产,数据对企业而言是最重要的工作之一。稍有不慎,极有可能发生数据无意泄露,甚至被黑客恶意窃取的风险。每年业界都会传出几起大事件,某知名或不知名的公司被脱裤(拖库的谐音,意思是整个数据库被黑客盗取)之类的。

从数据安全上也可以分为外网安全及内部操作安全,下面分别讨论一下。

内部操作安全策略

1. 是否回收DBA全部权限

试想,如果DBA没权限了,日常DB运维的活,以及紧急故障处理,该怎么实施呢?因此,建议在没有成熟的自动化运维平台前,不应该粗暴的回收DBA的太多权限,否则可能会导致工作效率降低的,甚至DBA有一种不被信任的负面情绪。

2. MySQL层安全策略

  • 业务帐号最多只可以通过内网远程登录,而不能通过公网远程连接。
  • 增加运维平台账号,该账号允许从专用的管理平台服务器远程连接。当然了,要对管理平台部署所在服务器做好安全措施以及必要的安全审计策略。
  • 建议启用数据库审计功能。这需要使用MySQL企业版,或者Percona/MariaDB分支版本,MySQL社区版本不支持该功能。
  • 启用 safe-update 选项,避免没有 WHERE 条件的全表数据被修改;
  • 在应用中尽量不直接DELETE删除数据,而是设置一个标志位就好了。需要真正删除时,交由DBA先备份后再物理删除,避免误操作删除全部数据。
  • 还可以采用触发器来做一些辅助功能,比如防止黑客恶意篡改数据。

3. MySQL账号权限规则

  • 业务帐号,权限最小化,坚决不允许DROP、TRUNCATE权限。
  • 业务账号默认只授予普通的DML所需权限,也就是select、update、insert、delete、execute等几个权限,其余不给。
  • MySQL初始化后,先行删除无用账号,删除匿名test数据库
mysql> delete from mysql.user where user!='root' or host!='localhost'; flush privileges;

mysql> drop database test;
  • 创建备份专用账号,只有SELECT权限,且只允许本机可登入。
  • 设置MySQL账号的密码安全策略,包括长度、复杂性。

4. 关于数据备份

记住,做好数据全量备份是系统崩溃无法修复时的最后一概救命稻草
备份数据还可以用来做数据审计或是用于数据仓库的数据源拉取之用
一般来说,备份策略是这样的:每天一次全备,并且定期对binlog做增备,或者直接利用binlog server机制将binlog传输到其他远程主机上。有了全备+binlog,就可以按需恢复到任何时间点。
特别提醒:当采用xtrabackup的流式备份时,考虑采用加密传输,避免备份数据被恶意截取。

外网安全策略

事实上,操作系统安及应用安全要比数据库自身的安全策略更重要。同理,应用程序及其所在的服务器端的系统安全也很重要,很多数据安全事件,都是通过代码漏洞入侵到应用服务器,再去探测数据库,最后成功拖库。

1. 操作系统安全建议

  • 运行MySQL的Linux必须只运行在内部网络,不允许直接对公网暴露,实在有需要从公网连接的话,再通过跳板机做端口转发,并且如上面所述,要严格限制数据库账号权限级别。
  • 系统账号都改成基于ssh key认证,不允许远程密码登入,且ssh key的算法、长度有要求以确保相对安全。这样就没有密码丢失的风险,除非个人的私钥被盗。
  • 进一步的话,甚至可以对全部服务器启用PAM认证,做到账号的统一管理,也更方便、安全。
  • 关闭不必要的系统服务,只开必须的进程,例如 mysqld、sshd、networking、crond、syslogd 等服务,其它的都关闭。
  • 禁止root账号远程登录。
  • 禁止用root账号启动mysqld等普通业务服务进程。
  • sshd服务的端口号建议修改成10000以上。
  • 在不影响性能的前提下,尽可能启用对MySQL服务端口的防火墙策略(高并发时,采用iptables可能影响性能,建议改用ip route策略)。
  • GRUB必须设置密码,物理服务器的Idrac/imm/ilo等账号默认密码也要修改。
  • 每个需要登入系统的员工,都使用每个人私有帐号,而不是使用公共账号。
  • 应该启用系统层的操作审计,记录所有ssh日志,或利bash记录相应的操作命令并发送到远程服务器,然后进行相应的安全审计,及时发现不安全操作。
  • 正确设置MySQL及其他数据库服务相关目录权限,不要全是755,一般750就够了。
  • 可以考虑部署堡垒机,所有连接远程服务器都需要先通过堡垒机,堡垒机上就可以实现所有操作记录以及审计功能了。
  • 脚本加密对安全性提升其实没太大帮助。对有经验的黑客来说,只要有系统登入权限,就可以通过提权等方式轻松获得root。

2. 应用安全建议

  • 禁用web server的autoindex配置。
  • 从制度层面,杜绝员工将代码上传到外部github上,因为很可能存在内部IP、账号密码泄露的风险,真的要上传必须先经过安全审核。
  • 尽量不要在公网上使用开源的cms、blog、论坛等系统,除非做过代码安全审计,或者事先做好安全策略。这类系统一般都是黑客重点研究对象,很容易被搞;
  • 在web server层,可以用一些安全模块,比如nginx的WAF模块;
  • 在app server层,可以做好代码安全审计、安全扫描,防止XSS攻击、CSRF攻击、SQL注入、文件上传攻击、绕过cookie检测等安全漏洞;
  • 应用程序中涉及账号密码的地方例如JDBC连接串配置,尽量把明文密码采用加密方式存储,再利用内部私有的解密工具进行反解密后再使用。或者可以让应用程序先用中间账号连接proxy层,再由proxy连接MySQL,避免应用层直连MySQL;

最后我们想说,任何高明的安全策略,都不如内部员工的安全意识来的重要。以前发生过一起案例,公司内有位员工的PC不慎中毒,结果导致内网数据被盗。

安全无小事,每个人都应铭记于心。在数据安全面前,可以适当牺牲一些便利性,当然也不能太过,否则可能得不偿失。

 

From:

云DB

MySQL整型数据溢出的处理策略

作者:吴炳锡 来源:http://www.mysqlsupport.cn/ 联系方式: wubingxi#gmail.com 转载请注明作/译者和出处,并且不能用于商业用途,违者必究.

今天接到一个朋友电话说是觉的数据库被别人更改了,出现数据不对的问题 。经过很久的排查是数据类型溢出了(发生问题的版本是MySQL 5.1)。后来通过给朋友那边把MySQL 5.1升级到MySQL 5.5去解决这个问题。 这也让我有兴趣去了解一下MySQL不同版本数据类型溢出的处理机制。

先看一下MySQL支持的整型数及大小,存储空间:

Type Storage Minimum Value Maximum Value 存储大小
  (Bytes) (Signed/Unsigned) (Signed/Unsigned) byte
TINYINT 1 -128 127 1 byte
    0 255  
SMALLINT 2 -32768 32767 2 bytes
    0 65535  
MEDIUMINT 3 -8388608 8388607 3 bytes
    0 16777215  
INT 4 -2147483648 2147483647 4 bytes
    0 4294967295  
BIGINT 8 -9223372036854775808 9223372036854775807 8 bytes
    0 18446744073709551615  

另外请记着mysql的数据处理会转成bigint处理,所以这里就用bigint几个测试:

SELECT CAST(0 AS UNSIGNED) - 1;

SELECT 9223372036854775807 + 1;

MySQL 5.1 下:

mysql> SELECT CAST(0 AS UNSIGNED) - 1;
+-------------------------+
| CAST(0 AS UNSIGNED) - 1 |
+-------------------------+
|    18446744073709551615 |
+-------------------------+
1 row in set (0.01 sec)

mysql> SELECT 9223372036854775807 + 1;
+-------------------------+
| 9223372036854775807 + 1 |
+-------------------------+
|    -9223372036854775808 |
+-------------------------+
1 row in set (0.01 sec)

MySQL 5.5, 5.6, 5.7下:

mysql> SELECT CAST(0 AS UNSIGNED) - 1;
ERROR 1690 (22003): BIGINT UNSIGNED value is out of range in '(cast(0 as unsigned) - 1)'
mysql> 
mysql> 
mysql> 
mysql> SELECT 9223372036854775807 + 1;
ERROR 1690 (22003): BIGINT value is out of range in '(9223372036854775807 + 1)'

所在处理这类数据是一定要小心溢出(如早期有做弊冲Q币就是利用这个方法处理)

这个问题有可能会出现积分消息,积分相加, 或是一些钱相关的业务中出现, 主库5.1 ,从库MySQL 5.5情况也会出现不同步的问题。
建议:这类业务系统尽可能的升级到MySQL 5.5后版本 

更多详情参考: http://dev.mysql.com/doc/refman/5.7/en/out-of-range-and-overflow.html

MariaDB thread pool解读

作者:吴炳锡 来源:http://www.mysqlsupport.cn/ 联系方式: wubingxi#gmail.com 转载请注明作/译者和出处,并且不能用于商业用途,违者必究.

Thread pool是什么

MySQL是每个连接上来都要创建一个线程来执行语句。这样每一个新的连接进来即会创建一个新的线程,这种动作对MySQL本身压力比较大。Threadpool是提供一种线程代理的模型执行每个连接的语句。而MySQL内部维护一个可能接受的线程总数,减少线程太多在CPU切换等方面的压力。

使用Threadpool的好处

  1. 使用线程代理的模型在连接数已知可控的情况下,提前创建好过线程并利用操作系统的threadpool技术维护,能减少很多开销。
  2. 尽可能的使用操作系统的线程方面的管理,把线程管理开销降到最低。 3. 利用thread_pool_max_threads控制资源使用

什么情况下Thread Pool作用比较小:

  1. 并发太高及有长的语句操作。 如:内部threadpool的总数为:10个,外面并发来了1000个请求,现在都来争用这10个内部线程,这个开销也比较重。同样道理加大threadpool后,innodb内部的并发也会受到限制。
  2. 突发的大量连接。这个情况下threadpool还有一定的作用,但是效果不明显了。如果为了防止大量的连接进来。可以考虑增加thread_pool_min_threads及thread_pool_idle_timeout的值,让平时也维护一个大的threadpool
  3. 高并发的环境,有可能简单的查询也会变慢。虽然select 1;这样的查询很简单应该会很快完成。但在threadpool的下面也需要排队等待从theadpool得到可用的线程才能执行。

使用方法:

目前支持threadpool的有MariaDB, Percona, 官方MySQL的一个收费功能。配置启用。

在my.cnf

[mysqld]

#thread pool

thread_handling=pool-of-threads

更多细节实现WL#246   threadpool-in-55

从MySQL源码学习运维Innodb buffer命中率计算

作者:吴炳锡 来源:http://www.mysqlsupport.cn/ 联系方式: wubingxi#gmail.com 转载请注明作/译者和出处,并且不能用于商业用途,违者必究.
按官方手册推荐Innodb buffer Hit Ratios的计算是:

100-((iReads / iReadRequests)*100)
iReads : mysql->status->Innodb_buffer_pool_reads
iReadRequests: mysql->status->Innodb_buffer_pool_read_requests

出处: http://dev.mysql.com/doc/mysql-monitor/2.0/en/mem_graphref.html
搜”Hit Ratios”
推荐有兴趣的同学把这个页面都看一下应该也会有很大收获.
另外在hackmysql: www.hackmysql.com网站上的: mysqlsqlreport中关于buffer命中计算是:

$ib_bp_read_ratio = sprintf "%.2f",
($stats{'Innodb_buffer_pool_read_requests'} ?
100 - ($stats{'Innodb_buffer_pool_reads'} /
$stats{'Innodb_buffer_pool_read_requests'}) * 100 :0);

即:

ib_bp_hit=100-(Innodb_buffer_pool_reads/Innodb_buffer_pool_read_requests)*100

另外我们知道查看Innodb Buffer Hit Ratios的地方是:
show engine innodb status\G;
Buffer pool hit rate : XXXX/1000;
那个XXX/1000即是buffer pool hit ratios的命中.
这样也可以从代码里看一下这个bp命中计算:

storage/innobase/buf/buf0buf.c # void buf_print_io
storage/innodbase/include/buf0buf.h #struct buf_block_struct

在buf0buf.c 中的buf_print_io函数中可以看到:

void
buf_print_io(
…

if (buf_pool->n_page_gets > buf_pool->n_page_gets_old) {
fprintf(file, "Buffer pool hit rate %lu / 1000\n",
(ulong)
(1000 - ((1000 * (buf_pool->n_pages_read
- buf_pool->n_pages_read_old))
/ (buf_pool->n_page_gets
- buf_pool->n_page_gets_old))));
} else {
fputs("No buffer pool page gets since the last printout\n",
file);
}

buf_pool->n_page_gets_old = buf_pool->n_page_gets;
buf_pool->n_pages_read_old = buf_pool->n_pages_read;
…
}

结合:
storage\innobase\include\buf0buf.h中

struct buf_block_struct{
…
ulint n_pages_read; /* number read operations */
…
ulint n_page_gets; /* number of page gets performed;
also successful searches through
the adaptive hash index are
counted as page gets; this field
is NOT protected by the buffer
pool mutex */
…
ulint n_page_gets_old;/* n_page_gets when buf_print was
last time called: used to calculate
hit rate */
…
ulint n_pages_read_old;/* n_pages_read when buf_print was
last time called */
…
}

从这个来看innodb buffer hit Ratios的命中计算需要本次取的值和上次值做一个减法公式应该为

ib_bp_hit=1000 – (t2.iReads – t1.iReads)/(t2.iReadRequest – t1.iReadRequest)*1000

t(n): 时间点 两个时间间隔最少是30秒以上,在小意义不大.
iReads: Innodb_buffer_pool_reads
iReadRequest: Innodb_buffer_pool_read_requests

对innodb的输出参数有兴趣的可以关注: storage/innobase/buf/Srv0srv.c 中的:
void srv_export_innodb_status()

思考:
对于innodb_buffer_pool_read_requests, innodb_buffer_pool_reads这种累加值,当很大时进行: innodb_buffer_pool_reads/innodb_buffer_pool_read_requests 相来讲只能得到从开始到现在的命中率的表现了. 如果想得到现在近五分钟,近一分钟或是8点到9点每分钟的命中率情况,如果还是按着innodb_buffer_pool_reads/innodb_buffer_pool_read_requests 进行计算,只能得到mysqld开起累计在8点-9点的每分钟的累计平均命中情况.
所以如果想到每(五)分钟的命中情况,就需要本次取得的值和一(五)分钟前的值进行相减,然后进行运算.这样才能得到一个当下的bp命中情况.
两种方法没实质的对错的问题,但相对于源码中的那种计算方式更容让发现数据库的抖动问题.

能解决的问题:
偶而的数据库性能抖动能直观的反应出来.

MySQL不同分支版本的压力测试

作者:吴炳锡 来源:http://www.mysqlsupport.cn/ 联系方式: wubingxi#gmail.com 转载请注:译者和出处,并且不能用于商业用途,违者必究.

压力测试的目的:
通过压力测试了解一下不同发行版本的性能区别。
MySQL不的版本测试,MySQL同样的配置
具体版本如下:
MySQL-5.1.42企业版+innodb-plugin
MySQL-5.1.42企业版+默认的innodb
MySQL-5.1.43开源版+ innodb-plugin
MySQL-5.1.43 Percona
操作系统:
Redhat Enterprise 5.4
硬件: Dell R710,RAM:48G,硬盘:6块SAS做Radid10

压力设置
创建一个1kw的Innodb表,使用16个并发去进行读取写入更新事务方面的操作.
测试工具:
Sysbench
测试方法:
创建数据:

time sysbench --mysql-user=root --mysql-host=localhost --test=oltp --oltp-test-mode=complex --mysql-table-engine=innodb --oltp-table-size=10000000 --mysql-db=test --oltp-table-name=innodb_1kw --num-threads=16 --max-requests=500000 preware

测试:

time sysbench --mysql-user=root --mysql-host=localhost --test=oltp --oltp-test-mode=complex --mysql-table-engine=innodb --oltp-table-size=10000000 --mysql-db=test --oltp-table-name=innodb_1kw --num-threads=16 --max-requests=500000 run

MySQL的基本配置

innodb_buffer_pool_size = 30G
innodb_data_file_path = ibdata1:1G:autoextend
transaction_isolation = READ-COMMITTED
innodb_thread_concurrency = 16
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 8M
innodb_log_file_size = 256M
innodb_log_files_in_group = 3
innodb_log_group_home_dir=/u1/mysqlp/logs/
innodb_max_dirty_pages_pct = 75
innodb_flush_method=O_DIRECT
innodb_lock_wait_timeout = 20
innodb_file_per_table = 1
启用innodb-plugin,innodb-plugin的版本号为:1.0.6

测试结果
分别测试三次,取平均值:

版本 事务/秒 写入读取/秒 其它操作/秒
MySQL企业版Innodb 1882.32 35764.1 3764.64
MySQL企业版Innodb-plugin 2395.073 45506.45 4790.15
MySQL开源版innodb-plugin 2288.09 43473.72 4576.18
Precona-MySQL 2754.24 52330.52 5508.48

1kw 写入的速度

版本 写入1kw数据总时间 用户 系统
MySQL企业版Innodb 3m25.318s 0m1.953s 0m0.177s
MySQL企业版Innodb-plugin 3m0.077s 0m1.783s 0m0.081s
MySQL开源版Innodb-plugin 3m0.169s 0m1.882s 0m0.125s
Precona-MySQL 3m0.030s 0m1.979s 0m0.192s

结果分析
事务对比:
transcation
写入读取对比:
readwrite
其它操作对比:
other
MySQL的开源版和企业版的Innodb性能相差不大,所以这里不在单独比较。总的来看Precona的MySQL性能表现良好。针对Innodb-plugin的的比较MySQL的企业版少表现比较好一点。从数据上来看选择不同的版本性能上最大的区别基本接近2倍。

Relay log read failure的处理

作者:吴炳锡 来源:http://www.mysqlsupport.cn/ 联系方式:select unhex(’777562696E67786940676D61696C2E636F6D’); 载请注明作/译者和出处,并且不能用于商业用途,违者必究。
       众所周知MySQL5.1的Replication是比较烂的。MySQL的每一个版本更新关于同步方面每次都是可以看到一大堆。但MySQL 5.1性能是比较突出的。所以经不住诱惑使用MySQL 5.1。所以也要经常遇到一些Bug。如: 

   
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.10.118
                  Master_User: repl_wu
                  Master_Port: 3306
                Connect_Retry: 30
              Master_Log_File: mysql-bin.005121
          Read_Master_Log_Pos: 64337286
               Relay_Log_File: relay-bin.003995
                Relay_Log_Pos: 18446697137031827760
        Relay_Master_Log_File: mysql-bin.005121
             Slave_IO_Running: Yes
            Slave_SQL_Running: No
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 1594
                   Last_Error: Relay log read failure: Could not parse relay log event entry. The possible reasons are: the master's binary log is corrupted (you can check this by running 'mysqlbinlog' on the binary log), the slave's relay log is corrupted (you can check this by running 'mysqlbinlog' on the relay log), a network problem, or a bug in the master's or slave's MySQL code. If you want to check the master's binary log or slave's relay log, you will be able to know their names by issuing 'SHOW SLAVE STATUS' on this slave.
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 4
              Relay_Log_Space: 64337901
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 1594
               Last_SQL_Error: Relay log read failure: Could not parse relay log event entry. The possible reasons are: the master's binary log is corrupted (you can check this by running 'mysqlbinlog' on the binary log), the slave's relay log is corrupted (you can check this by running 'mysqlbinlog' on the relay log), a network problem, or a bug in the master's or slave's MySQL code. If you want to check the master's binary log or slave's relay log, you will be able to know their names by issuing 'SHOW SLAVE STATUS' on this slave.
1 row in set (0.00 sec)
 

        从上面可以看到是中继日值或是Master上的日值出问题了。
        首先如果是中继日值坏掉,那只需要找到同步的时间点,然后重新同步,这样就可以有新的中继日值了。如果Master上的日值坏了就麻烦了。
从经验来看,这是中继日值出问题了。处理方法:

    需要找到同步的点。

日值为:Master_Log_File: mysql-bin.005121,Relay_Master_Log_File: mysql-bin.005121以Relay_Master_Log_File为准,Master_Log_File为参考。

日值执行时间点:

Exec_Master_Log_Pos: 4

那么现在就可以:

  
      mysql>stop slave;

    mysql>change master to Master_Log_File=’mysql-bin.005121’, Master_Log_Pos=4;
   
    mysql>start slave;

    mysql>show slave status\G;

    进行确认。

 

   建议:

    在使用MySQL-5.1.36以下的版本的同学,请尽快升级到MySQL-5.1.40 & MySQL-5.1.37sp1