手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表Tag:mysql

MySQL 5.0系列新的社区稳定版5.0.75发布!

MySQL 5.0从5.0.27以后,单数版本为社区版。双数版本号为企业版。如果您还不想从5.0升级到5.1的话,可以继续使用5.0的这个版本。主要还是以安全更新和bug fix居多。
官方change-log网址:http://dev.mysql.com/doc/refman/5.0/en/releasenotes-cs-5-0-75.html
简要说明:

Functionality added or changed:

  • Security Enhancement: To enable stricter control over the location from which user-defined functions can be loaded, the plugin_dir system variable has been backported from MySQL 5.1. If the value is non-empty, user-defined function object files can be loaded only from the directory named by this variable. If the value is empty, the behavior that is used prior to the inclusion of plugin_dir applies: The UDF object files must be located in a directory that is searched by your system's dynamic linker. (Bug#37428)

  • Previously, index hints did not work for FULLTEXT searches. Now they work as follows:

    For natural language mode searches, index hints are silently ignored. For example, IGNORE INDEX(i) is ignored with no warning and the index is still used.

    For boolean mode searches, index hints are honored. (Bug#38842)

 

Tags: mysql, database, community

InnoDB数据表空间文件平滑迁移

为了保持作者原有的格式,和他的URL版本的邮箱,我就不做更改了。其实世界上很多事情都是那么巧的,php focus群里的朋友还在问我数据库是否能够直接拷了就能使用。我的解释是4.0以下的版本都可以拷的,从4.1开始尽量使用mysqldump,但是sundj说,对于MYISAM类型的数据库和表,是可以直接COPY的。对于INNODB则不行。然后打开日常订阅的一些RSS,就发现了这篇文章 。。。套用范伟老师的话:缘份呐

原文如下:

作/译者:叶金荣(Email: ),来源:http://imysql.cn,转载请注明作/译者和出处,并且不能用于商业用途,违者必究。

前言

InnoDB存储引擎满足了MVCC和ACID特性,在需要支持事务的环境下必不可少。有些环境下,采用InnoDB可能效果比MyISAM还要来 的好。不过,在很多人眼中看来,InnoDB表空间文件由于无法实现跨服务器平滑迁移,因此不愿意使用。实际情况真是这样吗?本文就来探讨一下 InnoDB表空间文件的平滑迁移可能性。

如何迁移?

从MySQL文档中我们了解到,InnoDB的表空间可以是共享的或独立的。如果是共享表空间,则所有的表空间都放在一个文件 里:ibdata1,ibdata2..ibdataN,这种情况下,目前应该还没办法实现表空间的迁移,除非完全迁移,因此不在本次讨论之列;我们只讨 论独立表空间的情况。

不管是共享还是独立表空间,InnoDB每个数据表的元数据(metadata)总是保存在 ibdata1 这个共享表空间里,因此该文件必不可少,它还可以用来保存各种数据字典等信息。数据字典中,会保存每个数据表的ID号,每次发生数据表空间新增时,都会使 得该ID自增一个值(++1),例如:CREATE TABLE xx ENGINE = InnoDB / ALTER TABLE xx ENGINE = InnoDB 都会使得ID值增加。
有了上面的理解,想要实现InnoDB表空间文件的平滑迁移就很容易了,呵呵。下面是一些例子:
假定我们有2台DB主机,一个是A,一个B;现在想把A上的某个InnoDB表空间文件迁移到B上直接用。

一、迁移失败的例子

直接从A上把表空间文件 yejr.ibd 拷贝到 B 上后,导入表空间,报错,无法使用。这是由于A,B上创建该表时的顺序不一致,导致表的ID不一样,无法导入。
注意:,在这里,表空间文件直接拷贝的前提是该表空间处于"干净"状态下,也就是所有的数据均已经刷新到磁盘中,否则可能导致无法使用或部分数据丢失。
1. 在B上将旧的表空间废弃

(root@imysql.cn/17:52:47)[yejr]>ALTER TABLE yejr DISCARD TABLESPACE;
Query OK, 0 rows affected (0.00 sec)

2. 拷贝到目标机器

scp yejr.ibd B:/home/mysql/yejr/yejr.ibd
....

3. 启用该表空间

(root@imysql.cn/17:52:47)[yejr]>ALTER TABLE yejr IMPORT TABLESPACE;
ERROR 1030 (HY000): Got error -1 from storage engine

4. 查看错误

InnoDB: Operating system error number 13 in a file operation.
InnoDB: The error means mysqld does not have the access rights to
InnoDB: the directory.
InnoDB: Error: trying to open a table, but could not
InnoDB: open the tablespace file './test/b.ibd'!
InnoDB: Error: cannot reset lsn's in table `test/b`
InnoDB: in ALTER TABLE ... IMPORT TABLESPACE

5. 很明显,是权限的问题,修正过来,然后重新导入

(root@imysql.cn/17:52:47)[yejr]>ALTER TABLE yejr DISCARD TABLESPACE;
ERROR 1030 (HY000): Got error -1 from storage engine

6. 怎么还是错误?继续看日志

InnoDB: Error: tablespace id in file './yejr/yejr.ibd' is 15, but in the InnoDB
InnoDB: data dictionary it is 13.
InnoDB: Have you moved InnoDB .ibd files around without using the
InnoDB: commands DISCARD TABLESPACE and IMPORT TABLESPACE?
InnoDB: Please refer to
InnoDB: http://dev.mysql.com/doc/refman/5.0/en/innodb-troubleshooting.html
InnoDB: for how to resolve the issue.
InnoDB: cannot find or open in the database directory the .ibd file of
InnoDB: table `yejr/yejr`
InnoDB: in ALTER TABLE ... IMPORT TABLESPACE

从上面的日志得知,由于在A服务器上,yejr表的ID是15,而在B服务器上,yejr表的ID却是13,二者不一致,因此迁移失败。
既然只是因为ID不一样,而且有了上面的理论基础,我们完全可以人为的让它们的ID一致嘛,请看下面的第2次尝试。

二、人工干预下的成功迁移

1. 上面的例子中,B上面的yejr表ID为13,而A上面为15;因此只需要让B上的yejr表ID增加2就可以了。

(root@imysql.cn/17:52:47)[yejr]>ALTER TABLE yejr RENAME TO yejr1;
Query OK, 0 rows affected (0.00 sec)
#这个时候,yejr的ID变为14
(root@imysql.cn/17:52:47)[yejr]>ALTER TABLE yejr1 RENAME TO yejr;
Query OK, 0 rows affected (0.00 sec)
#这个时候,yejr的ID变为15

2. 然后,我们再导入

(root@imysql.cn/17:52:47)[yejr]>ALTER TABLE yejr IMPORT TABLESPACE;
Query OK, 0 rows affected (0.00 sec)
(root@imysql.cn/17:52:47)[yejr]>select count(*) from yejr;
+----------+
| count(*) |
+----------+
| 3 |
+----------+
1 row in set (0.00 sec)

看到了吧,成功了,呵呵。想要让他ID增加的方式也可以重复创建表,根据实际情况或者个人喜好而定了。
以上测试均在mysql 5.0.67版本下通过,只不过显示数据稍作处理了。

原文URL:http://imysql.cn/2008_12_17_migrate_innodb_tablespace_smoothly

Tags: innodb, mysql, database, 数据迁移, 叶金荣

mysql处理UTC时间

上午莫莫找我,问我有没有办法处理UTC时间,我很得意的将前两天COPY过来的,淘宝DBA的文章发给他看。说道,很简单呀,str_to_date就行了。

然而实现下来,却不正常,莫莫的时间是:05/Dec/2008:12:10:59 +0800,处理了半天都没有处理下来。因为我用的是:

SELECT STR_TO_DATE('05/Dec/2008:12:10:59 +0800' , '%d/%m/%Y');在我想来,date,month,year就行了,结果,返回给我的是NULL,死活不知道是什么问题。。。。

后来莫莫和我说,陌陌解决了这个问题,他用的是:SELECT STR_TO_DATE('05/Dec/2008:12:10:59 +0800' , '%d/%b/%Y');整个参数里就一个b和我的不一样。

才知道,也才明白,用STR_TO_DATE的时候,后面的参数和格式要和前面的参数一模一样,包括位置,包括参数的格式;虽然返回的值是:2008-12-05,但格式不对或者顺序不对,返回的肯定是NULL

记录下来,提醒一下自己,这个东西和php的strtotime不一样啊。。。

Tags: mysql, database, utc, str_to_date

Mysql时间函数

说实话,在我的应用当中,很少会用到这些MYSQL自带的时间函数,除非是在使用PHPMYADMIN的时候,于是,经常在有人问我,时间上加一天怎么办,减一天怎么办,当前月的最后一天怎么处理,上个月的今天到这个月的今天怎么搞,我都是临时翻手册来加以解释。毕竟大多数的情况下,我们都是会把这些处理交给PHP计算好再使用SQL查询,而真的很少利用自带函数。

在看到Taobao的DBA们把它们整理出来时,不禁暗暗 的YING笑了一番,以后方便了,不用找手册了。。。。。

原文:http://rdc.taobao.com/blog/dba/html/234_mysql_date_func.html

作者:丁原 

内容:

通常我们会有一些时间的转换需求,比如要统计某个时间段的收入,比如要截取某个时间的年份,比如要根据某个日期推算出是星期几等个,这些都可以通过Mysql自带的时间函数很容易实现。因为我对Mysql的函数还不熟,而通常又会调用这些时间函数,这边稍加总结以便查询。
–返回当前时间

mysql> select now(),date(now()),sysdate();
+---------------------+-------------+---------------------+
| now()               | date(now()) | sysdate()           |
+---------------------+-------------+---------------------+
| 2008-12-02 10:11:36 | 2008-12-02  | 2008-12-02 10:11:36 |
+---------------------+-------------+---------------------+
1 row in set (0.00 sec)
mysql> select curdate(),curdate()+0,curtime(),curtime()+0;       
+---------------------+-------------+---------------------+
| curdate()  | curdate()+0 | curtime() | curtime()+0   |
+---------------------+-------------+---------------------+
| 2008-12-02 | 20081202    | 10:00:33  | 100033.000000 |
+---------------------+-------------+---------------------+

–返回日期当月最后一天

mysql> select last_day('2008-12-02');
+------------------------+
| last_day('2008-12-02') |
+------------------------+
| 2008-12-31             |
+------------------------+
1 row in set (0.00 sec)

–返回日期的星期几

mysql> select dayname('2008-12-02'),dayofweek('2008-12-02');
+-----------------------+-------------------------+
| dayname('2008-12-02') | dayofweek('2008-12-02') |
+-----------------------+-------------------------+
| tuesday               |                       3 |
+-----------------------+-------------------------+
1 row in set (0.00 sec)

–返回日期的年,月,日

mysql> select month('2008-12-02'),year('2008-12-02'),day('2008-12-02');
+---------------------+--------------------+-------------------+
| month('2008-12-02') | year('2008-12-02') | day('2008-12-02') |
+---------------------+--------------------+-------------------+
|                  12 |               2008 |                 2 |
+---------------------+--------------------+-------------------+
1 row in set (0.00 sec)

–返回日期的小时,分,秒

mysql> select hour('10:05:03'),minute('10:05:03'),second('10:05:03');       
+------------------+--------------------+--------------------+
| hour('10:05:03') | minute('10:05:03') | second('10:05:03') |
+------------------+--------------------+--------------------+
|               10 |                  5 |                  3 |
+------------------+--------------------+--------------------+
1 row in set (0.00 sec)

1.subdate(d,t):起始时间加上一段时间(year,month,day…)

mysql> select date_add('1998-01-02', interval 31 day),adddate('1998-01-02', 31);     
+-----------------------------------------+---------------------------+
| date_add('1998-01-02', interval 31 day) | adddate('1998-01-02', 31) |
+-----------------------------------------+---------------------------+
| 1998-02-02                              | 1998-02-02                |
+-----------------------------------------+---------------------------+
1 row in set (0.00 sec)
mysql> select date_add('1998-01-02',interval 2 year);
+-----------------------------------------------------+
| date_add('1998-01-02', interval 2 year)
+-----------------------------------------------------+
| 2000-01-02                                 
+-----------------------------------------------------+
1 row in set (0.00 sec)
mysql> select date_add('1998-01-02', interval 2 hour);
+-----------------------------------------------------+
| date_add('1998-01-02', interval 2 hour)
+-----------------------------------------------------+
| 1998-01-02 02:00:00                   
+-----------------------------------------------------+
1 row in set (0.00 sec)

2.subdate(d,t):起始时间减去一段时间

mysql> select subdate('1998-01-02', interval 31 day),subdate('1998-01-02', 31);
+----------------------------------------+---------------------------+
| subdate('1998-01-02', interval 31 day) | subdate('1998-01-02', 31) |
+----------------------------------------+---------------------------+
| 1997-12-02                             | 1997-12-02                |
+----------------------------------------+---------------------------+
1 row in set (0.00 sec)

3.addtime(d,t):起始时间d加入时间t

mysql> select addtime('1997-12-31 23:59:50','00:00:05'), addtime('23:59:50','00:00:05') ;
+-------------------------------------------+--------------------------------+
| addtime('1997-12-31 23:59:50','00:00:05') | addtime('23:59:50','00:00:05') |
+-------------------------------------------+--------------------------------+
| 1997-12-31 23:59:55                       | 23:59:55                       |
+-------------------------------------------+--------------------------------+
1 row in set (0.00 sec)

4.subtime(d,t):起始时间d减去时间t

mysql> select subtime('1997-12-31 23:59:50','00:00:05'), subtime('23:59:50','00:00:05');     
+-------------------------------------------+--------------------------------+
| subtime('1997-12-31 23:59:50','00:00:05') | subtime('23:59:50','00:00:05') |
+-------------------------------------------+--------------------------------+
| 1997-12-31 23:59:45                       | 23:59:45                       |
+-------------------------------------------+--------------------------------+
1 row in set (0.00 sec)

5.datediff(d1,d2):返回起始时间d1和结束时间d2之间的天数

mysql> select datediff('1997-12-31 23:59:59','1997-12-30');
+----------------------------------------------+
| datediff('1997-12-31 23:59:59','1997-12-30') |
+----------------------------------------------+
|                                            1 |
+----------------------------------------------+
1 row in set (0.00 sec)

6.date_format(date,format):根据format字符串显示date值的格式

mysql> select date_format('2008-12-02 22:23:00', '%y %m %m %h:%i:%s');
+---------------------------------------------------------+
| date_format('2008-12-02 22:23:00', '%y %m %m %h:%i:%s') |
+---------------------------------------------------------+
| 2008 12 12 22:23:00                                     |
+---------------------------------------------------------+
1 row in set (0.00 sec)

7.str_to_date(str,format) 字符串转化为时间

mysql> select str_to_date('04/31/2004', '%m/%d/%y %h:%i:s');
+-----------------------------------------------+
| str_to_date('04/31/2004', '%m/%d/%y %h:%i:s') |
+-----------------------------------------------+
| 2004-04-31 00:00:00                           |
+-----------------------------------------------+
1 row in set (0.00 sec)

8.timestamp(expr) , timestamp(expr,expr2) :
对于一个单参数,该函数将日期或日期时间表达式 expr 作为日期时间值返回.对于两个参数, 它将时间表达式 expr2添加到日期或日期时间表达式 expr 中,将theresult作为日期时间值返回

mysql> select timestamp('2003-12-31'), timestamp('2003-12-31 12:00:00','12:00:00');
+-------------------------+---------------------------------------------+
| timestamp('2003-12-31') | timestamp('2003-12-31 12:00:00','12:00:00') |
+-------------------------+---------------------------------------------+
| 2003-12-31 00:00:00     | 2004-01-01 00:00:00                         |
+-------------------------+---------------------------------------------+
1 row in set (0.00 sec)

9.取当天0点0分,下一天0点0分

mysql> select timestamp(date(sysdate())),timestamp(adddate(date(sysdate()),1));     
+----------------------------+---------------------------------------+
| timestamp(date(sysdate())) | timestamp(adddate(date(sysdate()),1)) |
+----------------------------+---------------------------------------+
| 2008-12-02 00:00:00        | 2008-12-03 00:00:00                   |
+----------------------------+---------------------------------------+
1 row in set (0.00 sec)

--EOF--

Tags: mysql, database, 时间函数

PDF下载:《High Performance MySQL》

回忆未来的张宴推荐了这本书,并且在他的博客上提供了下载的链接,链接是新浪的爱问:点此下载《High Performance MySQL Second Edition》PDF电子版,并且博客上还有详细介绍:

  XML/HTML代码

  1. High Performance MySQL Second Edition  
  2. 作者: Baron Schwartz / Peter Zaitsev / Vadim Tkachenko / Jeremy Zawodny / Arjen Lentz / Derek Balling  
  3.   
  4. 副标题: Optimization, Backups, Replication, and More  
  5. ISBN: 9780596101718  
  6. 页数: 708  
  7. 定价: USD 49.99  
  8. 出版社: O'Reilly Media, Inc.  
  9. 装帧: Paperback  
  10. 出版年: 2008-06-18  
  11.   
  12. High Performance MySQL is the definitive guide to building fast, reliable systems with MySQL. Written by noted experts with years of real-world experience building very large systems, this book covers every aspect of MySQL performance in detail, and focuses on robustness, security, and data integrity.  
  13.   
  14. High Performance MySQL teaches you advanced techniques in depth so you can bring out MySQL's full power. Learn how to design schemas, indexes, queries and advanced MySQL features for maximum performance, and get detailed guidance for tuning your MySQL server, operating system, and hardware to their fullest potential. You'll also learn practical, safe, high-performance ways to scale your applications with replication, load balancing, high availability, and failover.  
  15.   
  16. This second edition is completely revised and greatly expanded, with deeper coverage in all areas. Major additions include:  
  17. * Emphasis throughout on both performance and reliability  
  18. * Thorough coverage of storage engines, including in-depth tuning and optimizations for the InnoDB storage engine  
  19. * Effects of new features in MySQL 5.0 and 5.1, including stored procedures, partitioned databases, triggers, and views  
  20. * A detailed discussion on how to build very large, highly scalable systems with MySQL  
  21. * New options for backups and replication  
  22. * Optimization of advanced querying features, such as full-text searches  
  23. * Four new appendices  
  24. The book also includes chapters on benchmarking, profiling, backups, security, and tools and techniques to help you measure, monitor, and manage your MySQL installations.  

为此,张宴还提供了一张图片:

 

大小: 5.18 K
尺寸: 106 x 139
浏览: 1836 次
点击打开新窗口浏览全图

 

整篇博客我作了一下整理和排序,想看原文的,请到:http://blog.s135.com/read.php?381,因为在这个页面上张宴采用了划词翻译,如果有看不懂英文的话,选中一下就会自动翻译了。。我没有提供。也不想再插入一些代码。呵呵

为了方便大家,我也上传了这个文件,大家可以点击下载:high.performance.mysql_second.edition.zip


Tags: mysql, database, pdf, download