Tag Archive: mysql

长期维护生产环境的数据库,恐怕很多人会犯这样的错误:

update tableA set field1value = VVVV (where …);

忘记了加括号中的约束条件,等反应过来,Shell 中显示已经更新了几万条数据。

如何挽救这样的失误呢?

  1. 每天进行数据备份,我们的方案是每天 dump 整库,保存在 S3 中,服务器上只保存昨天的数据
  2. 假如有前一天的备份数据,可以选择性恢复除了今天以外的数据。Shell 中命令行失误往往被损坏的数据都涉及一张表的一个字段或者几个字段。
  3. Drupal 中可以利用其支持多库的特性,新建一个旧数据的库,从旧数据库中读取数据,覆盖到新库上。以下是恢复数据的脚本例子

假如不是 Drupal 系统,也可以直接用 PHP 连接2个数据库,进行类似操作。

Tips About Mysql And SimpleDB

Zerofill feature of Mysql:

All rows of the field store the integer value in the same length, this feature is usable for generating invoice ID or SKU number:

create table t (t int(3) zerofill);

insert into t set t = 10;

select * from t;

+——+
| t       |
+——+
| 010|
+——+

Something about Amazon SimpleDB

Sort feature added to SimpleDB. You can use the query ORDER BY. But if you want to sort the result set by a attribute , you should judge the exist of the attribute first WHERE A IS NOT NULL AND ORDER BY A DESC.

Eventual Consistency, that means when you put some data the latency maybe 1 second or longer, you can not get the data immediately.

A cache layer is needed if your system require returning result real time.

The default collation in mysql is not always utf-8. Because the company of mysql localed in Sweden, so the encoding or collation is latin1_swedish_ci by default. You should change this feature after you installed the mysql DB server. But If you have  operated your web site for a long time, then you want to update your site for reasons. There are lots of data existed in Mysql in the encoding latin or other not UTF8. How to change the encoding or Collation to UTF-8 ?

Just download this tool to change the collation of tables or data to UTF8:

http://www.phoca.cz/phoca-changing-collation/

The Phoca Changing collation tool changes the database collation of database, tables and columns. And it is easy to use.