当我使用 pdo/dblib 从我的 Mac 连接到远程 SQL Server 数据库时,不使用 dns 字符串中的 dbname 值。无论分配给登录凭据的默认数据库如何,都设置为上下文数据库。
例如,我创建了一个名为 DB1 的新数据库和一个映射为 db_owner 的新登录名 PDO/PDO,将默认数据库设置为“master”。然后连接到 SQL Server
"dblib:host=10.44.68.187;dbname=DB1", "PDO", "PDO"
运行语句
SELECT DISTINCT CATALOG_NAME FROM INFORMATION_SCHEMA.SCHEMATA
并得到结果
[catalog_name] => master
应该是 DB1。我可以在 Win2008/R3 上使用相同的脚本,切换到 pdo/sqlsrv 并返回正确的数据库。
在 Mac 上是否有任何设置会强制 dblib 使用 dns 字符串中的 dbname 值?
TIA
测试脚本:
<?php
try {
$d = "dblib"; $h = "host"; $n = "dbname";
if ( isset( $_SERVER['OS'] ) && preg_match( '/Win/', $_SERVER['OS'] ))
{ $d = "sqlsrv"; $h = "Server"; $n = "Database"; }
$pdo = new PDO( "$d:$h=10.44.68.187;$n=DB1", "PDO", "PDO" );
foreach( $pdo->query( "select distinct catalog_name from information_schema.schemata", PDO::FETCH_ASSOC ) as $r ) {
print_r($r);
}
} catch (PDOException $e) { print $e."\n"; }
?>
从登录到语句执行请求的freeTDS日志:
log.c:190:Starting log file for FreeTDS 0.82
on 2013-10-10 11:21:52 with debug flags 0x4fff.
iconv.c:197:names for ISO-8859-1: ISO-8859-1
iconv.c:197:names for UTF-8: UTF-8
iconv.c:197:names for UCS-2LE: UCS-2LE
iconv.c:197:names for UCS-2BE: UCS-2BE
iconv.c:363:iconv to convert client-side data to the "ISO-8859-1" character set
iconv.c:516:tds_iconv_info_init: converting "ISO-8859-1"->"UCS-2LE"
iconv.c:516:tds_iconv_info_init: converting "ISO-8859-1"->"UCS-2LE"
net.c:210:Connecting to 10.44.68.187 port 1433 (TDS version 8.0)
net.c:264:tds_open_socket: connect(2) returned "Operation now in progress"
net.c:303:tds_open_socket() succeeded
util.c:162:Changed query state from DEAD to IDLE
login.c:735:quietly sending TDS 7+ login packet
token.c:312:tds_process_login_tokens()
net.c:592:Received header
0000 04 01 01 85 00 34 01 00- |.....4..|
net.c:671:Received packet
0000 e3 1b 00 01 06 6d 00 61-00 73 00 74 00 65 00 72 |.....m.a .s.t.e.r|
0010 00 06 6d 00 61 00 73 00-74 00 65 00 72 00 ab 6c |..m.a.s. t.e.r..l|
0020 00 45 16 00 00 02 00 25-00 43 00 68 00 61 00 6e |.E.....% .C.h.a.n|
0030 00 67 00 65 00 64 00 20-00 64 00 61 00 74 00 61 |.g.e.d. .d.a.t.a|
0040 00 62 00 61 00 73 00 65-00 20 00 63 00 6f 00 6e |.b.a.s.e . .c.o.n|
0050 00 74 00 65 00 78 00 74-00 20 00 74 00 6f 00 20 |.t.e.x.t . .t.o. |
0060 00 27 00 6d 00 61 00 73-00 74 00 65 00 72 00 27 |.'.m.a.s .t.e.r.'|
0070 00 2e 00 0b 49 00 4e 00-46 00 4f 00 52 00 31 00 |....I.N. F.O.R.1.|
0080 30 00 2d 00 49 00 4d 00-53 00 00 01 00 e3 08 00 |0.-.I.M. S.......|
0090 07 05 09 04 d0 00 34 00-e3 17 00 02 0a 75 00 73 |......4. .....u.s|
00a0 00 5f 00 65 00 6e 00 67-00 6c 00 69 00 73 00 68 |._.e.n.g .l.i.s.h|
00b0 00 00 ab 70 00 47 16 00-00 01 00 27 00 43 00 68 |...p.G.. ...'.C.h|
00c0 00 61 00 6e 00 67 00 65-00 64 00 20 00 6c 00 61 |.a.n.g.e .d. .l.a|
00d0 00 6e 00 67 00 75 00 61-00 67 00 65 00 20 00 73 |.n.g.u.a .g.e. .s|
00e0 00 65 00 74 00 74 00 69-00 6e 00 67 00 20 00 74 |.e.t.t.i .n.g. .t|
00f0 00 6f 00 20 00 75 00 73-00 5f 00 65 00 6e 00 67 |.o. .u.s ._.e.n.g|
0100 00 6c 00 69 00 73 00 68-00 2e 00 0b 49 00 4e 00 |.l.i.s.h ....I.N.|
0110 46 00 4f 00 52 00 31 00-30 00 2d 00 49 00 4d 00 |F.O.R.1. 0.-.I.M.|
0120 53 00 00 01 00 ad 36 00-01 71 00 00 01 16 4d 00 |S.....6. .q....M.|
0130 69 00 63 00 72 00 6f 00-73 00 6f 00 66 00 74 00 |i.c.r.o. s.o.f.t.|
0140 20 00 53 00 51 00 4c 00-20 00 53 00 65 00 72 00 | .S.Q.L. .S.e.r.|
0150 76 00 65 00 72 00 00 00-00 00 0b 00 0c 38 e3 13 |v.e.r... .....8..|
0160 00 04 04 34 00 30 00 39-00 36 00 04 34 00 30 00 |...4.0.9 .6..4.0.|
0170 39 00 36 00 fd 00 00 00-00 00 00 00 00 |9.6..... .....|
token.c:316:looking for login token, got e3(ENVCHANGE)
token.c:108:tds_process_default_tokens() marker is e3(ENVCHANGE)
dblib.c:325:db_env_chg(0x7fefe99cd960, 1, master, master)
token.c:316:looking for login token, got ab(INFO)
token.c:108:tds_process_default_tokens() marker is ab(INFO)
token.c:2451:tds_process_msg() reading message from server
token.c:2516:tds_process_msg() calling client msg handler
dbutil.c:86:_dblib_handle_info_message(0x7fefe997cb30, 0x7fefe99cd960, 0x7fff5f597140)
dbutil.c:87:msgno 5701: "Changed database context to 'master'."
token.c:2529:tds_process_msg() returning TDS_SUCCEED
token.c:316:looking for login token, got e3(ENVCHANGE)
token.c:108:tds_process_default_tokens() marker is e3(ENVCHANGE)
token.c:2281:tds_process_env_chg(): 5 bytes of collation data received
token.c:2282:tds->collation was
0000 00 00 00 00 00 - |.....|
iconv.c:985:setting server single-byte charset to "CP1252"
iconv.c:516:tds_iconv_info_init: converting "ISO-8859-1"->"CP1252"
token.c:2292:tds->collation now
0000 09 04 d0 00 34 - |....4|
token.c:316:looking for login token, got e3(ENVCHANGE)
token.c:108:tds_process_default_tokens() marker is e3(ENVCHANGE)
dblib.c:325:db_env_chg(0x7fefe99cd960, 2, , us_english)
token.c:316:looking for login token, got ab(INFO)
token.c:108:tds_process_default_tokens() marker is ab(INFO)
token.c:2451:tds_process_msg() reading message from server
token.c:2516:tds_process_msg() calling client msg handler
dbutil.c:86:_dblib_handle_info_message(0x7fefe997cb30, 0x7fefe99cd960, 0x7fff5f597140)
dbutil.c:87:msgno 5703: "Changed language setting to us_english."
token.c:2529:tds_process_msg() returning TDS_SUCCEED
token.c:316:looking for login token, got ad(LOGINACK)
token.c:316:looking for login token, got e3(ENVCHANGE)
token.c:108:tds_process_default_tokens() marker is e3(ENVCHANGE)
dblib.c:325:db_env_chg(0x7fefe99cd960, 4, 4096, 4096)
token.c:316:looking for login token, got fd(DONE)
token.c:108:tds_process_default_tokens() marker is fd(DONE)
token.c:2201:tds_process_end: more_results = 0
was_cancelled = 0
error = 0
done_count_valid = 0
token.c:2217:tds_process_end() state set to TDS_IDLE
token.c:2232: rows_affected = 0
token.c:393:leaving tds_process_login_tokens() returning 1
mem.c:563:tds_free_all_results()
util.c:162:Changed query state from IDLE to QUERYING
write.c:136:tds_put_string converting 16 bytes of "set textsize -1 "
write.c:164:tds_put_string wrote 32 bytes
util.c:162:Changed query state from QUERYING to PENDING
net.c:779:Sending packet
0000 01 01 00 28 00 00 00 00-73 00 65 00 74 00 20 00 |...(.... s.e.t. .|
0010 74 00 65 00 78 00 74 00-73 00 69 00 7a 00 65 00 |t.e.x.t. s.i.z.e.|
0020 20 00 2d 00 31 00 20 00- | .-.1. .|
token.c:495:tds_process_tokens(0x7fefe99cd960, 0x7fff5f597234, 0x7fff5f597230, 0x100)
util.c:162:Changed query state from PENDING to READING
net.c:592:Received header
0000 04 01 00 11 00 34 01 00- |.....4..|
net.c:671:Received packet
0000 fd 00 00 be 00 00 00 00-00 |........ .|
token.c:510:processing result tokens. marker is fd(DONE)
token.c:2201:tds_process_end: more_results = 0
was_cancelled = 0
error = 0
done_count_valid = 0
token.c:2217:tds_process_end() state set to TDS_IDLE
util.c:162:Changed query state from READING to IDLE
token.c:2232: rows_affected = 0
util.c:110:logic error: cannot change query state from IDLE to PENDING
token.c:495:tds_process_tokens(0x7fefe99cd960, 0x7fff5f597234, 0x7fff5f597230, 0x100)
token.c:498:tds_process_tokens() state is COMPLETED
dblib.c:237:dblib_add_connection(0x102df9a00, 0x7fefe99cd960)
dblib.c:4208:dbsetopt(0x7fefe99cd600, 7, 2147483647, -1)
dblib.c:4323:UNIMPLEMENTED dbsetopt(option = 7)
dblib.c:4208:dbsetopt(0x7fefe99cd600, 17, 2147483647, -1)
dblib.c:4323:UNIMPLEMENTED dbsetopt(option = 17)
dblib.c:4208:dbsetopt(0x7fefe99cd600, 35, 1, -1)
dblib.c:5608:dbsetuserdata(0x7fefe99cd600, 0x1017651a0)
dblib.c:3093:dbcancel(0x7fefe99cd600)
query.c:1934:tds_send_cancel: not in_cancel and idle
dblib.c:1243:dbcmd(0x7fefe99cd600, select distinct catalog_name from information_schema.schemata)