<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>搜我收藏 &#187; mysql</title>
	<atom:link href="http://www.sou5.cn/archives/tag/mysql/feed" rel="self" type="application/rss+xml" />
	<link>http://www.sou5.cn</link>
	<description>Web 2.0 网站架构、优化 数据库架构</description>
	<lastBuildDate>Sun, 05 Feb 2012 16:21:22 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>都是selinux惹的祸</title>
		<link>http://www.sou5.cn/archives/831.html</link>
		<comments>http://www.sou5.cn/archives/831.html#comments</comments>
		<pubDate>Sun, 25 Jul 2010 07:30:56 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[原创]]></category>
		<category><![CDATA[Errcode: 13]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[selinux]]></category>

		<guid isPermaLink="false">http://www.sou5.cn/?p=831</guid>
		<description><![CDATA[今天装了一个虚拟机，在服务器上装mysql 想把数据库目录改变一下修改完/etc/my.cnf 和/etc/init.d/mysqld
重启mysql　出现错误:
100725 23:09:50  mysqld ended
100725 23:22:44  mysqld started
100725 23:22:44 [Warning] Can&#8217;t create test file /Data/mysql_db/mysql/localhost.lower-test
100725 23:22:44 [Warning] Can&#8217;t create test file /Data/mysql_db/mysql/localhost.lower-test
/usr/libexec/mysqld: Can&#8217;t change dir to &#8216;/Data/mysql_db/mysql/&#8217; (Errcode: 13)
100725 23:22:44 [ERROR] Aborting
100725 23:22:44 [Note] /usr/libexec/mysqld: Shutdown complete
看几遍配置没错呀，又看看权限也没错呀，为什么说没有权限而报错呢，　然后google了一下　原来是selinux问题
修改/etc/selinux/config
将 SELINUX=enforcing或permissive改成disabled
然后reboot
问题解决:)
另外修改my.cnf里面的数据库路径时，要加上
[mysql]
socket=/Data/mysql_db/mysql/mysql.sock
设定mysql的mysql.sock文件路径不然连接时会出
 ERROR 2002 (HY000): Can&#8217;t connect to local MySQL server through socket &#8216;/var/lib/mysql/mysql.sock&#8217; (2)
错误
相关：
什么是SELinux 
也可以
使用setenforce [...]]]></description>
		<wfw:commentRss>http://www.sou5.cn/archives/831.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mysql使用rand随机查询记录效率测试(转)</title>
		<link>http://www.sou5.cn/archives/755.html</link>
		<comments>http://www.sou5.cn/archives/755.html#comments</comments>
		<pubDate>Tue, 18 May 2010 04:41:50 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[rand]]></category>

		<guid isPermaLink="false">http://www.sou5.cn/?p=755</guid>
		<description><![CDATA[一直以为mysql随机查询几条数据，就用
SELECT * FROM `table` ORDER BY RAND() LIMIT 5
就可以了。
但是真正测试一下才发现这样效率非常低。一个15万余条的库，查询5条数据，居然要8秒以上
查看官方手册，也说rand()放在ORDER BY 子句中会被执行多次，自然效率及很低。
You cannot use a column with RAND() values in an ORDER BY clause, because ORDER BY would evaluate the column multiple times.
搜索Google，网上基本上都是查询max(id) * rand()来随机获取数据。

SELECT *
FROM `table` AS t1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM`table`)) AS id) AS t2
WHERE t1.id >= t2.id
ORDER BY t1.id ASC [...]]]></description>
		<wfw:commentRss>http://www.sou5.cn/archives/755.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>搭建mysql master-slave replication</title>
		<link>http://www.sou5.cn/archives/606.html</link>
		<comments>http://www.sou5.cn/archives/606.html#comments</comments>
		<pubDate>Thu, 24 Dec 2009 02:53:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[master]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[slave]]></category>

		<guid isPermaLink="false">http://www.sou5.cn/?p=606</guid>
		<description><![CDATA[本篇文章记录了如何搭建一个master-slave架构的mysql数据库服务器，一般用于读写分离的场景下：master for INSERT/UPDATE/DELETE，slave for SELECT。
根据实战结果，在master-connect-retry＝5的情况下，基本可以做到读写同步（可以打开slave的log文件来观察同步结果：tail -f /var/run/mysql/mysql.log）
基本信息
主库：192.168.1.1
从库：192.168.1.2
数据文件存放位置：/data/mysql
待同步数据库：除了mysql、test库以外的所有数据库
一、设置MASTER
1、修改my.cnf
#vi /etc/my.cnf
server-id=1
log-bin
binlog-ignore-db=mysql
binlog-ignore-db=test
2、赋予SLAVE权限帐号，允许用户在MASTER上LOAD TABLE和LOAD DATA
mysql&#62; GRANT FILE,SELECT,REPLICATION SLAVE ON *.* TO slave@192.168.1.2 IDENTIFIED BY ‘password’;
3、锁主库表
mysql&#62; FLUSH TABLES WITH READ LOCK;
4、显示主库信息
记录File和Position，从库设置将会用到
mysql&#62; SHOW MASTER STATUS;
+—————+———-+————–+——————+
&#124; File          &#124; Position &#124; Binlog_do_db &#124; Binlog_ignore_db &#124;
+—————+———-+————–+——————+
&#124; hathor-bin.001 &#124; 79   &#124;              &#124; mysql,test         &#124;
+—————+———-+————–+——————+
5、打包主库
# cd /data
# tar cvfz [...]]]></description>
		<wfw:commentRss>http://www.sou5.cn/archives/606.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mysql截取函数LOCATE和POSITION</title>
		<link>http://www.sou5.cn/archives/538.html</link>
		<comments>http://www.sou5.cn/archives/538.html#comments</comments>
		<pubDate>Wed, 05 Aug 2009 09:10:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[locate]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[position]]></category>

		<guid isPermaLink="false">http://www.sou5.cn/?p=538</guid>
		<description><![CDATA[在mysql 里是没有像sqlserver的charindex函数的， 用LOCATE和POSITION函数可以实现功能！
LOCATE(substr,str)
POSITION(substr IN str)
返回子串 substr 在字符串 str 中第一次出现的位置。如果子串 substr 在 str 中不存在，返回值为 0：
mysql&#62; SELECT LOCATE(’bar’, ‘foobarbar’);
-&#62; 4
mysql&#62; SELECT LOCATE(’xbar’, ‘foobar’);
-&#62; 0
这个函数是多字节安全的。在 MySQL 3.23 中，这个函数是字母大小写敏感的，当在 MySQL 4.0 中时，如有任一参数是一个二进制字符串，它才是字母大小写敏感的。
LOCATE(substr,str,pos)
返回子串 substr 在字符串 str 中的第 pos 位置后第一次出现的位置。如果 substr 不在 str 中返回 0 ：
mysql&#62; SELECT LOCATE(’bar’, ‘foobarbar’,5);
-&#62; 7
这个函数是多字节安全的。在 MySQL 3.23 中，这个函数是字母大小写敏感的，当在 MySQL 4.0 中时，如有任一参数是一个二进制字符串，它才是字母大小写敏感的。
综合例子：
select info.* from info left join [...]]]></description>
		<wfw:commentRss>http://www.sou5.cn/archives/538.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQLdb for Python使用指南/Python的数据库操作</title>
		<link>http://www.sou5.cn/archives/529.html</link>
		<comments>http://www.sou5.cn/archives/529.html#comments</comments>
		<pubDate>Sat, 25 Jul 2009 17:53:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.sou5.cn/?p=529</guid>
		<description><![CDATA[网站就是要和数据库进行交互,否则什么都不用做了&#8230;今天我们来看一个叫MySQLdb的库,这个用来和MySQL数据库进行交互.
可以从这里获得这个库
http://sourceforge.net/projects/mysql-python
如果你不确定你的python环境里有没有这个库,那就打开python shell,输入 import MySQLdb,如果返回错误信息,那就表示你的机器上没有,赶紧去下载一个.我的机器是win xp,所以我下载了win环境下的exe那个,直接双击完成安装.
在介绍具体的操作前,先花点时间来说说一个程序怎么和数据库进行交互
1.和数据库建立连接
2.执行sql语句,接收返回值
3.关闭数据库连接
使用MySQLdb也要遵循上面的几步.让我们一步步的进行.
0.引入MySQLdb库
import MySQLdb
1.和数据库建立连接
conn=MySQLdb.connect(host=&#8221;localhost&#8221;,user=&#8221;root&#8221;,passwd=&#8221;sa&#8221;,db=&#8221;mytable&#8221;)
提供的connect方法用来和数据库建立连接,接收数个参数,返回连接对象.
比较常用的参数包括
host:数据库主机名.默认是用本地主机.
user:数据库登陆名.默认是当前用户.
passwd:数据库登陆的秘密.默认为空.
db:要使用的数据库名.没有默认值.
port:MySQL服务使用的TCP端口.默认是3306.
更多关于参数的信息可以查这里
http://mysql-python.sourceforge.net/MySQLdb.html
然后,这个连接对象也提供了对事务操作的支持,标准的方法
commit() 提交
rollback() 回滚
2.执行sql语句和接收返回值
cursor=conn.cursor()
n=cursor.execute(sql,param)
首先,我们用使用连接对象获得一个cursor对象,接下来,我们会使用cursor提供的方法来进行工作.这些方法包括两大类:1.执行命令,2.接收返回值
cursor用来执行命令的方法:
callproc(self, procname, args):用来执行存储过程,接收的参数为存储过程名和参数列表,返回值为受影响的行数
execute(self, query, args):执行单条sql语句,接收的参数为sql语句本身和使用的参数列表,返回值为受影响的行数
executemany(self, query, args):执行单挑sql语句,但是重复执行参数列表里的参数,返回值为受影响的行数
nextset(self):移动到下一个结果集
cursor用来接收返回值的方法:
fetchall(self):接收全部的返回结果行.
fetchmany(self, size=None):接收size条返回结果行.如果size的值大于返回的结果行的数量,则会返回cursor.arraysize条数据.
fetchone(self):返回一条结果行.
scroll(self, value, mode=&#8217;relative&#8217;):移动指针到某一行.如果mode=&#8217;relative&#8217;,则表示从当前所在行移动value条,如果mode=&#8217;absolute&#8217;,则表示从结果集的第一行移动value条.
下面的代码是一个完整的例子.
#使用sql语句,这里要接收的参数都用%s占位符.要注意的是,无论你要插入的数据是什么类型,占位符永远都要用%s
sql=&#8221;insert into cdinfo values(%s,%s,%s,%s,%s)&#8221;
#param应该为tuple或者list
param=(title,singer,imgurl,url,alpha)
#执行,如果成功,n的值为1
n=cursor.execute(sql,param)
#再来执行一个查询的操作
cursor.execute(&#8220;select * from cdinfo&#8221;)
#我们使用了fetchall这个方法.这样,cds里保存的将会是查询返回的全部结果.每条结果都是一个tuple类型的数据,这些tuple组成了一个tuple
cds=cursor.fetchall()
#因为是tuple,所以可以这样使用结果集
print cds[0][3]
#或者直接显示出来,看看结果集的真实样子
print cds
#如果需要批量的插入数据,就这样做
sql=&#8221;insert into cdinfo values(0,%s,%s,%s,%s,%s)&#8221;
#每个值的集合为一个tuple,整个参数集组成一个tuple,或者list
param=((title,singer,imgurl,url,alpha),(title2,singer2,imgurl2,url2,alpha2))
#使用executemany方法来批量的插入数据.这真是一个很酷的方法!
n=cursor.executemany(sql,param)
需要注意的是(或者说是我感到奇怪的是),在执行完插入或删除或修改操作后,需要调用一下conn.commit()方法进行提交.这样,数据才会真正保存在数据库中.我不清楚是否是我的mysql设置问题,总之,今天我在一开始使用的时候,如果不用commit,那数据就不会保留在数据库中,但是,数据确实在数据库呆过.因为自动编号进行了累积,而且返回的受影响的行数并不为0.
3.关闭数据库连接
需要分别的关闭指针对象和连接对象.他们有名字相同的方法
cursor.close()
conn.close()
三步完成,基本的数据库操作就是这样了.下面是两个有用的连接
MySQLdb用户指南: http://mysql-python.sourceforge.net/MySQLdb.html
MySQLdb文档: http://mysql-python.sourceforge. &#8230; MySQLdb-module.html
]]></description>
		<wfw:commentRss>http://www.sou5.cn/archives/529.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>解剖公司框架(一)</title>
		<link>http://www.sou5.cn/archives/516.html</link>
		<comments>http://www.sou5.cn/archives/516.html#comments</comments>
		<pubDate>Fri, 10 Jul 2009 03:03:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[原创]]></category>
		<category><![CDATA[memcache]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://www.sou5.cn/?p=516</guid>
		<description><![CDATA[最近比较忙一直也没有写些东西:)，今天抽点时间想把最近忙的事情讲述、分享一下,最近和同事为公司做了一套统一的开发框架。（可能不是很好 不过目前是第一个版本 还算够我们使用的，让各位路过的高手见笑了 ）
一、我们的框架起名“ qin (秦 ) ” 就是秦朝 第一个一统天下的那个朝代，主要意识是我们要
二、全站结构图：
说明：全站基础应用和对外开放Api都基于Library,这样统一接口统一数据层比较容易管理和重够。
Library基于Qin框架。

三、框架结构图：
框架 ：分为原生框架(Zend)、和Qin、www
Qin:
Dao部分使用了Pdo引擎 实现了和Cache的存储和刷新。
Cache使用了Memcache和Apc内存存储,实现缓存tag功能
File服务器使用了 MogileFS 。
Server是管理db、cache、fileServer的服务器配置,实现故障排除。
Common里面主要放一下公用的模块 比如:IO(本地文件读写)等。

今天就到这了 要工作了:) 明天继续
]]></description>
		<wfw:commentRss>http://www.sou5.cn/archives/516.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mysql insert的几点操作(DELAYED 、IGNORE、ON DUPLICATE KEY UPDATE )</title>
		<link>http://www.sou5.cn/archives/463.html</link>
		<comments>http://www.sou5.cn/archives/463.html#comments</comments>
		<pubDate>Thu, 26 Mar 2009 06:21:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[insert]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.sou5.cn/?p=463</guid>
		<description><![CDATA[INSERT语法
INSERT [LOW_PRIORITY &#124;DELAYED&#124; HIGH_PRIORITY] [IGNORE]
      [INTO]tbl_name[(col_name,...)]
      VALUES ({expr&#124; DEFAULT},&#8230;),(&#8230;),&#8230;
      [ON DUPLICATE KEY UPDATEcol_name=expr, ... ]
或：
INSERT [LOW_PRIORITY &#124;DELAYED&#124; HIGH_PRIORITY] [IGNORE]
      [INTO]tbl_name
      SETcol_name={expr&#124; DEFAULT}, &#8230;
      [ON DUPLICATE [...]]]></description>
		<wfw:commentRss>http://www.sou5.cn/archives/463.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MySQL ORDER BY 的实现分析</title>
		<link>http://www.sou5.cn/archives/457.html</link>
		<comments>http://www.sou5.cn/archives/457.html#comments</comments>
		<pubDate>Wed, 25 Mar 2009 15:40:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[group]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[order]]></category>

		<guid isPermaLink="false">http://www.sou5.cn/?p=457</guid>
		<description><![CDATA[总的来说，在 MySQL 中的ORDER BY有两种排序实现方式，一种是利用有序索引获取有序数据，另一种则是通过相应的排序算法，将取得的数据在内存中进行排序。
下面将通过实例分析两种排序实现方式及实现图解：
假设有 Table A 和 B 两个表结构分别如下：

sky@localhost : example 01:48:21&#38;gt; show create table A\G
*************************** 1. row ***************************
Table: A
Create Table: CREATE TABLE `A` (
`c1` int(11) NOT NULL default &#8216;0&#8216;,
`c2` char(2) default NULL,
`c3` varchar(16) default NULL,
`c4` datetime default NULL,
PRIMARY KEY (`c1`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
sky@localhost : example 01:48:32&#38;gt; show create table B\G
*************************** 1. row ***************************
Table: B
Create [...]]]></description>
		<wfw:commentRss>http://www.sou5.cn/archives/457.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL 中 GROUP BY 基本实现原理</title>
		<link>http://www.sou5.cn/archives/451.html</link>
		<comments>http://www.sou5.cn/archives/451.html#comments</comments>
		<pubDate>Wed, 25 Mar 2009 15:34:34 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[group]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[order]]></category>

		<guid isPermaLink="false">http://www.sou5.cn/?p=451</guid>
		<description><![CDATA[之前连着写了几篇关于 MySQL 中常用操作的一些基本实现原理，如，MySQL ORDER BY，MySQL Join，这次再写一篇 MySQL 中 GROUP BY 的基本实现原理。
由于 GROUP BY 实际上也同样会进行排序操作，而且与 ORDER BY 相比，GROUP BY 主要只是多了排序之后的分组操作。当然，如果在分组的时候还使用了其他的一些聚合函数，那么还需要一些聚合函数的计算。所以，在GROUP BY 的实现过程中，与 ORDER BY 一样也可以利用到索引。
在 MySQL 中，GROUP BY 的实现同样有多种（三种）方式，其中有两种方式会利用现有的索引信息来完成 GROUP BY，另外一种为完全无法使用索引的场景下使用。下面我们分别针对这三种实现方式做一个分析。
1.使用松散（Loose）索引扫描实现 GROUP BY
何谓松散索引扫描实现 GROUP BY 呢？实际上就是当 MySQL 完全利用索引扫描来实现 GROUP BY 的时候，并不需要扫描所有满足条件的索引键即可完成操作得出结果。
下面我们通过一个示例来描述松散索引扫描实现 GROUP BY，在示例之前我们需要首先调整一下 group_message 表的索引，将 gmt_create 字段添加到 group_id 和 user_id 字段的索引中：

sky@localhost : example 08:49:45&#62; create index [...]]]></description>
		<wfw:commentRss>http://www.sou5.cn/archives/451.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mysql 随机</title>
		<link>http://www.sou5.cn/archives/445.html</link>
		<comments>http://www.sou5.cn/archives/445.html#comments</comments>
		<pubDate>Mon, 16 Mar 2009 14:16:43 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Lecture]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.sou5.cn/?p=445</guid>
		<description><![CDATA[SELECT FROM aaa ORDER BY rand() LIMIT N
]]></description>
		<wfw:commentRss>http://www.sou5.cn/archives/445.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

