显示 binlog 文件列表
show binary logs;
清理 binlog 文件
purge binary logs to 'mysql-bin.000001';
查看 binlog 内容
show binlog events in 'mysql-bin.000001' limit 1000, 10;
或者SHELL命令行
sudo mysqlbinlog mysql-bin.000001
显示 binlog 文件列表
show binary logs;
清理 binlog 文件
purge binary logs to 'mysql-bin.000001';
查看 binlog 内容
show binlog events in 'mysql-bin.000001' limit 1000, 10;
或者SHELL命令行
sudo mysqlbinlog mysql-bin.000001
query_cache_size, tmp_table_size 这两个选项一定要设置!
# The following options will be passed to all MySQL clients [client] #password = your_password port = 3306 socket = /home/work/mysql/tmp/mysql.sock # Here follows entries for some specific programs # The MySQL server [mysqld] port = 3306 bind-address = 127.0.0.1 socket = /home/work/mysql/tmp/mysql.sock #skip-locking max_allowed_packet = 1M table_open_cache = 16 sort_buffer_size = 8M read_buffer_size = 512K read_rnd_buffer_size = 256K net_buffer_length = 8K thread_stack = 128K # Size of the Key Buffer, used to cache index blocks for MyISAM tables. # Do not set it larger than 30% of your available memory, as some memory # is also required by the OS to cache rows. Even if you're not using # MyISAM tables, you should still set it to 8-64M as it will also be # used for internal temporary disk tables. key_buffer_size = 8M # If the temporary file used for fast index creation would be bigger # than using the key cache by the amount specified here, then prefer the # key cache method. This is mainly used to force long character keys in # large tables to use the slower key cache method to create the index. myisam_sort_buffer_size = 8M # Query cache is used to cache SELECT results and later return them # without actual executing the same query once again. Having the query # cache enabled may result in significant speed improvements, if your # have a lot of identical queries and rarely changing tables. See the # "Qcache_lowmem_prunes" status variable to check if the current value # is high enough for your load. # Note: In case your tables change very often or if your queries are # textually different every time, the query cache may result in a # slowdown instead of a performance improvement. query_cache_size=16M # Maximum size for internal (in-memory) temporary tables. If a table # grows larger than this value, it is automatically converted to disk # based table This limitation is for a single table. There can be many # of them. tmp_table_size=16M # Don't listen on a TCP/IP port at all. This can be a security enhancement, # if all processes that need to connect to mysqld run on the same host. # All interaction with mysqld must be made via Unix sockets or named pipes. # Note that using this option without enabling named pipes on Windows # (using the "enable-named-pipe" option) will render mysqld useless! # skip-networking server-id = 1 # Uncomment the following if you want to log updates #log-bin=mysql-bin # binary logging format - mixed recommended #binlog_format=mixed # Uncomment the following if you are using InnoDB tables #default_storage_engine=InnoDB innodb_data_home_dir = /home/work/mysql/var/ innodb_data_file_path = ibdata1:10M:autoextend innodb_log_group_home_dir = /home/work/mysql/var/ # You can set .._buffer_pool_size up to 50 - 80 % # of RAM but beware of setting memory usage too high innodb_buffer_pool_size = 32M innodb_additional_mem_pool_size = 4M # Set .._log_file_size to 25 % of buffer pool size innodb_log_file_size = 10M innodb_log_buffer_size = 16M innodb_flush_log_at_trx_commit = 1 innodb_lock_wait_timeout = 50 [mysqldump] quick max_allowed_packet = 16M [mysql] no-auto-rehash # Remove the next comment character if you are not familiar with SQL #safe-updates [myisamchk] key_buffer_size = 8M sort_buffer_size = 8M [mysqlhotcopy] interactive-timeout
这是我给部门同事做的技术分享.
当今绝大部分的软件系统都用到了关系数据库, 所以, 作为软件开发工程师, 必须掌握关系数据库应用设计能力.
最近做了一次MySQL所谓的"海量数据"查询性能分析.
dt | dt2 | dt3 | it | it2 | it3 | |
id | id | id | id | id | id | int PK |
ext1 | ext1 | varchar(256) | ||||
time | time | time | time | time | time | int/datetime KEY |
ext2 | ext2 | ext2 | ext2 | varchar(128) |
说明, MyISAM引擎, dt表示时间字段使用datetime类型, it表示时间字段使用int类型.
首先生成100K个UNIX时间戳(int), 然后随机选取10M次, 每一次往6个表里插入一条记录(当time字段是datetime类型时, 做类型转换). 所以每一个表都有10M条记录. ext1和ext2字段会用随机的字符串填充.
使用Ubuntu Linux, 编译过程提示缺啥补啥即可.
./configure --prefix=/home/work/httpd --enable-so --enable-rewrite --enable-vhost-alias
配置文件:
LoadModule php5_module modules/libphp5.so AddType application/x-httpd-php .php # PhpIniDir /home/work/php/php.ini
Yii的AR类, 可以在rules()函数中声明验证规则. 但指定了验证规则, 也可以不使用. 在调用save()方法时, 如何第一个参数为false(默认为true), 则不进行验证. 一旦验证失败, 将立即返回, 不保存数据. Yii提供的工具可以根据数据库表定义自动生成验证规则. AR类的验证规则的另一个功能是用于自动生成HTML页面表单. 但是, 根据实际使用情况, Yii所提供的这个功能在XXX项目中几乎已经成为鸡肋, 都是在开发者不知情的情况下使用了.
Yii的这个特性, 加上数据库表设计的失误, 以及代码逻辑问题, 导致某个功能出现BUG.
代码的功能是这样的: 调用AR类的save()方法保存一条记录, 然后跳转到该记录的详细信息页面. 实际操作时, 偶尔出现记录未保存, 然后跳转URL里的id参数为空. 代码逻辑如下:
if($ar->save()){ // do something } redirect($ar->id);
显然, 代码逻辑没有错误处理, 无论成功与否, 都跳转. 下面再分析保存失败的原因.
数据库表定义中, 备注字段的类型为VARCHAR(255), 如何短的字符串类型的应用范围一般很窄, 再加上所对应的业务字段是备注信息, 往往是大段的文本, 所以, 数据库设计已经失败, 出错是必然的. 另外, 程序逻辑无论是客户端还是服务器端, 都没有再做业务层的验证给出错误提示, 而是无意地使用了Yii框架的AR类的验证机制. 综合上述原因, BUG的查找花费了不少的时间.
总结:
1. 数据库设计
通过对数据库表字段的仔细选择来获取性能, 对于XXX系统, 这种方式已经过时. 一般只有如下几种数据类型: 数字, 字符串(varchar), 文本(长字符串, text), 时间.
结合 MySQL, 数字主要使用int, 像tiny int, middle int之类的整数类型, 只是节省空间考虑, 速度上反而比int类型要慢, 当数值较大时, 使用bigint. 对XXX, 除了int和bigint, 其它的整数类型都没有必要.
对于字符串和文本类型, 如果不建立索引作为查询条件, 一般使用text, 不要使用varchar, 如果文本较长, 则使用longtext. 如果字段对应的输入框是多行文本框, 那么就没有使用varchar的必要了. 建议原则是: 不建立索引的字符串使用text, 格式化输入框(HTML编辑控件)对应的字段使用longtext, 只有建立索引的字符串才使用varchar.
2. AR类设计
把AR类中的rules()函数删除.
3. 客户端验证
在客户端也做验证, 并在出错时提示用户. 如用户输入的文本过长, 等等.