免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2862 | 回复: 8
打印 上一主题 下一主题

mysql 读写分离出现的问题~~请教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-03 13:36 |只看该作者 |倒序浏览
最近由于数据库的压力比较大,所以想使用户的对数据库的读写操作经行分离。
具体实施是 判断sql 语句
如果是匹配 select 开头的 就用 mysql_connect 连接到两台从数据库中。否则就连接主数据库。
但是期间发生过即使是写的时候也可能是停留在 从数据中 并经行写操作,使sql有误。
所以我查看了mysql手册后,相应的改进。当sql 语句中有写操作的时候,先用mysql_close关闭数据库的连接。
然后再连接到 主数据库中。经行写操作。
但是问题依然存在。写操作的时候还是有时停留在从数据库。
请教 怎样才能让写操作的时候在写数据库经行操作呢。

论坛徽章:
0
2 [报告]
发表于 2008-12-03 14:20 |只看该作者
理想的情况是先将数据库操作用类封装,然后类里建两个连接,一个读,一个写,根据情况对不同的连接操作。为以防万一,可以让从库帐号只读。

论坛徽章:
0
3 [报告]
发表于 2008-12-03 14:45 |只看该作者
不知道你具体的代码,无从下手。

论坛徽章:
0
4 [报告]
发表于 2008-12-03 15:11 |只看该作者
我是经行了封装的
具体代码

  1. function &getDb($query=''){
  2.         $db;
  3.                 $username  = $fileConfig->getValue( "db_username" );
  4.                 $password  = $fileConfig->getValue( "db_password" );
  5.                 //$host      = $fileConfig->getValue( "db_host" );
  6.                 $dbname    = $fileConfig->getValue( "db_database" );
  7.                 $dbcharset = $fileConfig->getValue( "db_character_set" );
  8.                 $dbpersistent   = $fileConfig->getValue( "db_persistent" );

  9.                                
  10.         if (preg_match ("/^select/i",ltrim($query))){
  11.                                        
  12.                 $host_array=$fileConfig->getValue( "db_host_array" );
  13.                 $host  = $host_array[array_rand ($host_array, 1)];
  14.         else{
  15.        
  16.                 $db->Close();
  17.                 $host  = $fileConfig->getValue( "db_host" );
  18.         }

  19.                 if($dbpersistent == true) {
  20.                             if( !$db->PConnect( $host, $username, $password, $dbname, $dbcharset )) {
  21.                                 $message = "Fatal error: could not connect to the database!".
  22.                                            " Error: ".$db->ErrorMsg();
  23.                                     throw( new Exception( $message ));
  24.                                     die();
  25.                             }
  26.                     }
  27.                     else {
  28.                             if( !$db->Connect( $host, $username, $password, $dbname, $dbcharset )) {
  29.                                 $message = "Fatal error: could not connect to the database!".
  30.                                            " Error: ".$db->ErrorMsg();
  31.                                     throw( new Exception( $message ));
  32.                                     die();
  33.                             }
  34.                     }
  35. }
复制代码

然后还有一个执行数据库操作的函数
  1. function Execute( $query,
  2.                           $page = DEFAULT_PAGING_ENABLED,
  3.                           $itemsPerPage = DEFAULT_ITEMS_PER_PAGE )
  4.         {
  5.             // initialize the db when we have to execute the first query,
  6.             // not earlier.
  7.             $this->_initializeDb($query);
  8.         //这里初始话的时候区分sql语句是读还是写,然后掉用上班的连接数据库的类经行读写区分
  9.             // see PDbDriverBase (or one of its drivers like PDbMySQLDriver
  10.             // for details)
  11.             $result = $this->_db->Execute( $query, $page, $itemsPerPage );
  12.                        
  13.             // if the query generated an error, write a message to the sql
  14.             // error log file
  15.             if( !$result ) {
  16.                 $this->_lastError = $this->_db->ErrorMsg();
  17.                 lt_include( PLOG_CLASS_PATH . "class/logger/loggermanager.class.php" );

  18.                 $log =& LoggerManager::getLogger( "sqlerr" );
  19.                 $error = $this->DbError();
  20.                 $log->error( "The following query = \n" .
  21.                               $query .
  22.                              "generated the following error message = \n" .
  23.                               $error );
  24.             }
  25.               
  26.             return( $result );
  27.         }
复制代码


sql语句执行都通过这个execute 函数执行

请帮忙看看,写的时候为什么还会连接着从数据库

[ 本帖最后由 ghewqqq 于 2008-12-3 15:14 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2008-12-04 09:35 |只看该作者
代码不全没看到实际操作数据库的逻辑,但可以肯定的是确实有漏洞。且不说存储过程的情况,如果往你那个Execute里一次塞两句sql的话,整个就乱套了。

论坛徽章:
0
6 [报告]
发表于 2008-12-05 10:25 |只看该作者
大家有没有试过amoeba啊,支持读写分离,分表操作.阿里巴巴的工程师开发的.似乎性能不错.

论坛徽章:
0
7 [报告]
发表于 2008-12-15 10:31 |只看该作者
MySQL Proxy 不知道性能和稳定行怎么样。一直都不敢再生产线上使用。

论坛徽章:
1
白银圣斗士
日期:2015-11-23 08:33:04
8 [报告]
发表于 2008-12-15 12:20 |只看该作者
mysql-proxy 你要是懂lua的话还是可以用到生产的。不错的东西。

论坛徽章:
0
9 [报告]
发表于 2008-12-15 12:58 |只看该作者
只从这段代码比较难找到问题~~
不过,我估计可能先读从服务器,这时写的话就是写在从DB上了
你可以试下。整个程序只写的话会不会写在从DB上。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP