服务器在Unix套接字中时如何连接数据库

服务器在Unix套接字中时如何连接数据库

本文介绍了服务器在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?

如果数据库服务器和数据库客户端(PHP)在不同的计算机上运行,​​则它们将无法通过UNIX文件系统套接字进行通信.

If the database server and the database client (PHP) are running on different machines then they cannot communicate via UNIX filesystem sockets.

否,您对事件的解释或描述不正确.如果您在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的情况下,或者如果您自己编译了客户端lib,则$ PREFIX/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

这篇关于服务器在Unix套接字中时如何连接数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:37