当服务器处于 Unix 套接字时如何连接到数据库?
我在连接到数据库 (phpmyadmin) 时遇到问题,这只是因为我的服务器在 UNIX 套接字中.我不知道如何连接到它(我站在窗户上).下面的代码工作正常,如果我的服务器是 TCP/IP.
I have a problem with the connection to database (phpmyadmin), it just because my server is in UNIX socket. I don't know how to connect to it (I'm stand on window). The code below is work fine, if my server is in TCP/IP.
我的代码
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'xxxxxx');
define('DB_DATABASE', 'sample_db');
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$database = mysql_select_db(DB_DATABASE, $connection) or die(mysql_error());
mysql_set_charset("utf8", $connection);
?>
如果服务器在 UNIX 套接字中,谁能告诉我如何连接到数据库?
Can anyone tell me how to connect to database, if the server is in UNIX socket?
提前致谢
推荐答案
我对你的问题有点困惑.
I'm a bit confused by your question.
我站在窗边
您的意思是您的 PHP 代码运行在 MSWindows 机器上吗?
Do you mean that your PHP code is running on a MSWindows machine?
我的服务器在 UNIX 中
my server is in UNIX
如果数据库服务器和数据库客户端 (PHP) 运行在不同的机器上,则它们无法通过 UNIX 文件系统套接字进行通信.
If the database server and the database client (PHP) are running on different machines then they cannot communicate via UNIX filesystem sockets.
下面的代码工作正常,如果我的服务器是 TCP/IP.
The code below is work fine, if my server is in TCP/IP.
不,您对事件的解释或描述不正确.如果在 libmysql 客户端(包括 PHP 的 mysql_ 扩展)中指定 'localhost' 作为目标主机,则客户端将尝试通过(Unix)文件系统套接字进行连接.奥托.如果您指定 127.0.0.1,它将使用 TCP 套接字.
No, either your interpretation or your description of events is incorrect. If you specify 'localhost' as the target host in a libmysql client (including PHP's mysql_ extension) then the client will try to connect via the (Unix) filesystem socket. OTOH. if you specify 127.0.0.1, it will use a TCP socket.
在前一种情况下,客户端从 ~/.my.cnf 获取套接字的路径,或者在没有该文件/etc/my.cnf 的情况下,或者如果您自己编译了客户端库,$前缀/etc/my.cnf
In the former scenario, the client gets the path to the socket from ~/.my.cnf, or in the absence of that file /etc/my.cnf, or if you've compiled the client lib yourself, $PREFIX/etc/my.cnf
相关文章