最近评论

Amoeba 跟 mysql proxy的区别

Amoeba 跟 mysql proxy有区别。
主要是针对数据切分方面。
在 mysql proxy 上面如果想要读写分离并且 读集群、写集群 机器比较多情况下,用mysql proxy 需要相当大的工作量,目前mysql proxy没有现成的 lua脚本。mysql proxy根本没有配置文件,lua脚本就是它的全部,当然可以是相当方便的。那么同样这种东西需要编写大量的lua脚本才能完成一个复杂的配置。amoeba目标是走产品化这条路。只需要进行相关的配置就可以满足需求。
一、Mysql Master/Slave 结构之下的读写分离:
Master: server1 (可读写)
slaves:server2、server3、server4(3个平等的数据库。只读/负载均衡)
amoeba提供读写分离pool相关配置。并且提供负载均衡配置。
可配置server2、server3、server4形成一个虚拟的 virtualSlave,该配置提供负载均衡、failOver、故障恢复功能

Xml代码

  
<dbServer name=“virtualSlave” virtual=“true”>  
    <poolConfig>  
        <className>com.meidusa.amoeba.server.MultipleServerPool</className>  
        <!– 负载均衡参数 1=ROUNDROBIN , 2=WEIGHTBASED –>  
        <property name=“loadbalance”>1</property>  
                   
        <!– 参与该pool负载均衡的poolName列表以逗号分割 –>  
        <property name=“poolNames”>server2,server3,server4</property>  
    </poolConfig>  
</dbServer>  

<dbServer name=”virtualSlave” virtual=”true”>
<poolConfig>
<className>com.meidusa.amoeba.server.MultipleServerPool</className>
<!– 负载均衡参数 1=ROUNDROBIN , 2=WEIGHTBASED –>
<property name=”loadbalance”>1</property>

<!– 参与该pool负载均衡的poolName列表以逗号分割 –>
<property name=”poolNames”>server2,server3,server4</property>
</poolConfig>
</dbServer>
如果不启用数据切分,那么只需要配置QueryRouter属性
wirtePool=server1
readPool=virtualSlave

Xml代码

<queryRouter>  
    <className>com.meidusa.amoeba.mysql.parser.MysqlQueryRouter</className>  
    <property name=“LRUMapSize”>1500</property>  
    <property name=“defaultPool”>server1</property>  
  
    <property name=“writePool”>server1</property>  
    <property name=“readPool”>virtualSlave</property>  
  
    <property name=“needParse”>true</property>  
</queryRouter>  

<queryRouter>
<className>com.meidusa.amoeba.mysql.parser.MysqlQueryRouter</className>
<property name=”LRUMapSize”>1500</property>
<property name=”defaultPool”>server1</property>

<property name=”writePool”>server1</property>
<property name=”readPool”>virtualSlave</property>

<property name=”needParse”>true</property>
</queryRouter>
那么遇到update/insert/delete将 query语句发送到 wirtePool,将 select发送到 readPool机器中执行。
二、数据切分:
这方面amoeba显然更加容易了。
举个数据切分例子:
select * from user_event where user_id=’test’ and  gmt_create between Sysdate() -1 and Sysdate()
如果根据gmt_create 时间进行数据切分,比如 [...]

php连接mysql查询

<?php
$con = mysql_connect(“localhost”,”peter”,”abc123″);
if (!$con)
  {
  die(‘Could not connect: ‘ . mysql_error());
  }
mysql_select_db(“my_db”, $con);
$result = mysql_query(“SELECT * FROM person”);
while($row = mysql_fetch_array($result))
  {
  echo $row['FirstName'] . ” ” . $row['LastName'];
  echo “<br />”;
  }
或者
 
<?php
mysql_connect($host,$user,$password);
$result = mysql_db_query(“database”,“select * from table”);
while($row = mysql_fetch_array($result)) {
  echo $row["user_id"];
  echo $row["fullname"];
}
mysql_free_result($result);
?>

mysql查询数据库中所有表名

查询数据库中所有表名
select * from information_schema.tables 
查询指定数据库中指定表的所有字段名
select column_name from information_schema.columns  where table_schema=’YOURDATABASENAME’ and table_name=’YOURTABLENAME’

MySQL 当记录不存在时插入(insert if not exists)

 
示例一:插入多条记录
假设有一个主键为 client_id 的 clients 表,可以使用下面的语句:

INSERT INTO clients
(client_id, client_name, client_type)
SELECT supplier_id, supplier_name, ‘advertising‘
FROM suppliers
WHERE not exists (select * from clients
where clients.client_id = suppliers.supplier_id);

示例一:插入单条记录

INSERT INTO clients
(client_id, client_name, client_type)
SELECT 10345, ‘IBM‘, ‘advertising‘
FROM dual
WHERE not exists (select * from clients
where clients.client_id = 10345);

来源: techonthenet.com

关于 mysql error number 1051和 1418 错误的处理

mysql error number 1051 在网上(包括外国网) 说了一大堆,但就是没有人知道其中的缘由,这是store engine 不同造成的
例如,最开始的engine=myisam,在此基础上建了A表,那么你现在要转成innodb的方式(开启了my.cnf下的innodb的参数)现在如果再来 select A 表那么是会报不存在的,如果drop 的话会报 mysql error number 1051.这个问题该怎么处理呢? 哈哈,先关掉my.cnf的innodb参数,然后重启mysql,这样A就可以select 了,把A表的backup 下来,drop 掉A表,然后再开启innodb参数,重起mysql,restore 刚才备份的脚本,
这样就可以搞定了.
如果在create function的时候有 1418的错语的时候:那么
set global log_bin_trust_routine_creators=1;
然后怎么create function 都不会有问题(当然,你的function必段正确哦),妈的,这是mysql的一个bug,搞不懂为什么,反正这样做就OK了.
在oracle中根本就没有这种错语,如果出现了这种错语,是由于用户权限不够,OpenSource的东西就是这样的.还是oracle的好.
2006-04-26于北京朝阳 18:15
转载来源:http://blog.chinaunix.net/u/15117/showart_104301.html