锟斤拷前锟芥本锟侥诧拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟饺拷模锟斤拷锟轿拷锟斤拷锟矫伙拷锟绞讹拷锟饺拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 SQL 锟斤拷锟斤拷元锟斤拷锟节碉拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟叫的★拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟叫э拷锟街拷锟斤拷锟斤拷锟皆� 锟斤拷时锟斤拷始锟斤拷锟斤拷时锟斤拷止锟斤拷锟斤拷锟皆o拷锟斤拷锟斤拷锟斤拷元锟叫o拷锟斤拷锟斤拷锟斤拷锟捷匡拷锟斤拷锟接会被锟叫伙拷锟斤拷
锟斤拷锟接︼拷锟矫伙拷锟斤拷瓒拷锟斤拷锟皆拷嗉拷锟斤拷锟矫疵伙拷锟斤拷魏锟� MySQL 锟斤拷锟截撅拷锟斤拷锟杰癸拷锟斤拷锟斤拷锟斤拷锟�
锟斤拷锟斤拷通锟斤拷 SQL hints 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟狡★拷锟斤拷锟斤拷选锟斤拷锟皆的碉拷锟斤拷锟斤拷锟斤拷 API 锟斤拷锟叫硷拷兀锟� 然锟斤拷锟斤拷锟斤拷 API 执锟叫匡拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
Example #1 锟斤拷锟斤拷一锟斤拷 master 锟斤拷一锟斤拷 slave 锟侥诧拷锟�
[myapp] { "myapp": { "master": { "master_0": { "host": "localhost", "socket": "\/tmp\/mysql.sock" } }, "slave": { "slave_0": { "host": "192.168.2.27", "port": "3306" } } } }
Example #2 锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 SQL hints
<?php
$mysqli = new mysqli("myapp", "username", "password", "database");
if (!$mysqli)
/* Of course, your error handling is nicer... */
die(sprintf("[%d] %s\n", mysqli_connect_errno(), mysqli_connect_error()));
/* 锟斤拷锟斤拷 SELECT 锟斤拷头锟斤拷锟斤拷锟斤拷使锟斤拷 master */
if (!$mysqli->query("START TRANSACTION")) {
/* Please use better error handling in your code */
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
}
/* 锟斤拷止锟斤拷锟斤拷锟叫伙拷*/
if (!$mysqli->query(sprintf("/*%s*/INSERT INTO test(id) VALUES (1)", MYSQLND_MS_LAST_USED_SWITCH)))) {
/* Please do proper ROLLBACK in your code, don't just die */
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
}
if ($res = $mysqli->query(sprintf("/*%s*/SELECT COUNT(*) AS _num FROM test", MYSQLND_MS_LAST_USED_SWITCH)))) {
$row = $res->fetch_assoc();
$res->close();
if ($row['_num'] > 1000) {
if (!$mysqli->query(sprintf("/*%s*/INSERT INTO events(task) VALUES ('cleanup')", MYSQLND_MS_LAST_USED_SWITCH)))) {
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
}
}
} else {
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
}
if (!$mysqli->query(sprintf("/*%s*/UPDATE log SET last_update = NOW()", MYSQLND_MS_LAST_USED_SWITCH)))) {
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
}
if (!$mysqli->query(sprintf("/*%s*/COMMIT", MYSQLND_MS_LAST_USED_SWITCH)))) {
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
}
$mysqli->close();
?>
锟皆达拷 PHP 5.4.0 锟芥本锟斤拷始锟斤拷mysqlnd library 锟斤拷锟斤拷锟斤拷锟斤拷锟� autocommit 模式锟铰碉拷状态锟斤拷锟斤拷锟斤拷锟斤拷锟� SET AUTOCOMMIT=0 锟斤拷锟斤拷锟斤拷锟斤拷洌拷饨拷貌锟斤拷锟斤拷始锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷涂锟斤拷圆锟斤拷锟绞癸拷锟� SQl hints锟斤拷
锟斤拷 PHP 5.4.0 锟芥本锟斤拷始锟斤拷锟斤拷锟斤拷 API autocommit 模式锟斤拷锟斤拷锟斤拷瓒拷锟斤拷锟� trx_stickiness=master锟斤拷 锟斤拷么锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷远锟斤拷锟街癸拷锟斤拷鼐锟斤拷锟斤拷锟斤拷锟斤拷锟叫伙拷锟斤拷锟斤拷 autocommit 锟斤拷锟矫碉拷锟斤拷锟矫碉拷锟叫o拷 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟叫斤拷锟斤拷锟叫碉拷锟斤拷浞拷透锟� master锟斤拷锟斤拷锟矫革拷锟截撅拷锟解。锟斤拷 autocommit 锟斤拷锟斤拷锟斤拷锟斤拷锟皆后,诧拷锟斤拷锟斤拷锟斤拷驴锟绞硷拷锟斤拷鼐锟斤拷锟斤拷锟斤拷械锟斤拷锟戒。
锟斤拷 PHP 5.5.0 锟斤拷 PECL/mysqlnd_ms 1.5.0 锟芥本锟斤拷锟斤拷锟街硷拷榻拷锟斤拷锟斤拷锟斤拷锟斤拷 mysqli_autocommit() 锟斤拷锟斤拷锟斤拷 mysql_commit() 锟斤拷 mysql_rollback()锟斤拷
Example #3 锟斤拷锟斤拷锟斤拷馗锟斤拷鼐锟斤拷锟斤拷碌锟� trx_stickiness 锟斤拷锟斤拷
{ "myapp": { "master": { "master_0": { "host": "localhost", "socket": "\/tmp\/mysql.sock" } }, "slave": { "slave_0": { "host": "127.0.0.1", "port": "3306" } }, "trx_stickiness": "master" } }
Example #4 Transaction aware
<?php
$mysqli = new mysqli("myapp", "username", "password", "database");
if (!$mysqli)
/* Of course, your error handling is nicer... */
die(sprintf("[%d] %s\n", mysqli_connect_errno(), mysqli_connect_error()));
/* 锟斤拷锟斤拷 autocommit锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟戒发锟酵革拷 master */
$mysqli->autocommit(FALSE);
if (!$mysqli->query("INSERT INTO test(id) VALUES (1)")) {
/* Please do proper ROLLBACK in your code, don't just die */
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
}
if ($res = $mysqli->query("SELECT COUNT(*) AS _num FROM test")) {
$row = $res->fetch_assoc();
$res->close();
if ($row['_num'] > 1000) {
if (!$mysqli->query("INSERT INTO events(task) VALUES ('cleanup')")) {
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
}
}
} else {
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
}
if (!$mysqli->query("UPDATE log SET last_update = NOW()")) {
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
}
if (!$mysqli->commit()) {
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
}
/* 锟斤拷锟斤拷卸锟斤拷锟斤拷锟斤拷锟街癸拷锟斤拷锟斤拷锟斤拷锟斤拷酶锟斤拷鼐锟斤拷锟� */
$mysqli->autocommit(TRUE);
$mysqli->close();
?>
Note: 锟芥本锟斤拷锟斤拷
trx_stickiness=master 锟斤拷锟斤拷锟斤拷要 PHP 5.4.0 锟斤拷锟较版本锟斤拷
锟斤拷锟皆参匡拷锟斤拷锟斤拷锟斤拷锟斤拷锟� transaction handling 锟铰斤拷锟叫★拷