简体   繁体   English

如何避免在mysql中同时选择第一行?

[英]How to avoid simultaneous select of first row in mysql?

I have three php scripts on server1, server2, server3. 我在server1,server2,server3上有三个php脚本。 I have remote mysql server4 with database "watch" - table "mytable" with one column "data" 我有带有数据库“ watch”的远程mysql server4-表“ mytable”和一列“ data”

All these scripts connect to db "watch" table "watch" column "data" and all do the same operation: 所有这些脚本都连接到数据库“监视”表“监视”列“数据”,并且都执行相同的操作:

  1. Select first row of column "data" 选择“数据”列的第一行
  2. Make operation with data 用数据进行操作
  3. Delete first row of column "data" 删除“数据”列的第一行
 $connect = mysql_connect("ip","user","password") OR DIE("Can't connect "); mysql_select_db("watch") or die("Can't select database"); $xv=1 do { $select = mysql_query("SELECT data FROM mytable LIMIT 1") or die("wrong select"); if (!$select) { echo 'Could not run query: ' . mysql_error(); exit;} $data = mysql_fetch_row($select); ----------- operation with first row of "data" ------------- mysql_query("DELETE FROM mytable LIMIT 1") or die("wrong delete"); } while ($xv++<100); 

For example, 例如,

first row of "data" contains uniq value "Alex". “数据”的第一行包含uniq值“ Alex”。

second row of "data" contains uniq value "Michael". “数据”的第二行包含uniq值“迈克尔”。

third row of "data" contains uniq value "George". “数据”的第三行包含uniq值“乔治”。

PHP scripts run in cron at the same time but from different servers. PHP脚本同时在cron中运行,但来自不同的服务器。

Is it possible that two or all three scripts select the same value of first row "Alex"? 是否有两个或所有三个脚本选择第一行“ Alex”的相同值?

I would like to avoid that two or three scripts select simultaneously the same value of first row. 我想避免两个或三个脚本同时选择第一行的相同值。

Use locking, unlocking of tables. 使用表的锁定,解锁。 LOCK TABLES explicitly acquires table locks for the current client session. LOCK TABLES显式获取当前客户端会话的表锁。 UNLOCK TABLES explicitly releases any table locks held by the current session. UNLOCK TABLES显式释放当前会话持有的所有表锁。 Also read this link . 另请阅读此链接

$connect = mysql_connect("ip","user","password") OR DIE("Can't connect ");
mysql_select_db("watch") or die("Can't select database");

$xv=1
do {
    mysql_query("LOCK TABLES mytable WRITE");
    $select = mysql_query("SELECT data FROM mytable LIMIT 1") or die("wrong select");
    if (!$select) {
         echo 'Could not run query: ' . mysql_error(); 
         exit;
    }
    $data = mysql_fetch_row($select);

-----------
operation with first row of "data" 
-------------

    mysql_query("DELETE FROM mytable LIMIT 1") or die("wrong delete");
    mysql_query("UNLOCK TABLES");

} while ($xv++<100);

I tnink that in mysql the only way to do it is locking table before reading or writing. 我认为在mysql中唯一的方法是在读取或写入之前锁定表。 here you have the link: http://dev.mysql.com/doc/refman/5.0/en/lock-tables.html 在这里,您具有链接: http : //dev.mysql.com/doc/refman/5.0/en/lock-tables.html

Please don't forget to unlock the table, try to catch every exception you can have or the table can be locked for a heck long time. 请不要忘记解锁表,尝试捕获所有可能遇到的异常,或者表可以长时间锁定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM