通过 MySQL 套接字与 Zend Framework 连接

发布于 2024-07-30 19:37:11 字数 656 浏览 4 评论 0 原文

我最近开始使用 Zend 框架。 我开始在本地 XAMPP 服务器上进行开发,一切顺利,但是当我上传到我的 1and1 帐户时,我不断收到以下错误:

消息:SQLSTATE[HY000] [2002] 不能 通过以下方式连接到本地 MySQL 服务器 套接字'/var/run/mysqld/mysqld.sock' (2)

我尝试将 application.ini 文件中的 resources.db.params.unix_socket 行更改为 phpinfo 中提到的不同行,但没有成功。 我发现这里有人回答了

MySQL服务器是否在同一主机上 作为网络应用程序? 如果没有,你 无法使用套接字连接,并且 需要通过 TCP 连接。

我认为我的网络主机就是这种情况。 如何将 ZF 更改为通过 TCP 连接? 我目前正在使用 PDO_MYSQL。

I have recently started using Zend Framework. I started developing on my local XAMPP server which went fine but when I uploaded to my 1and1 account, I keep getting the following error:

Message: SQLSTATE[HY000] [2002] Can't
connect to local MySQL server through
socket '/var/run/mysqld/mysqld.sock'
(2)

I have tried changing the resources.db.params.unix_socket line in my application.ini file to the different ones mentioned in phpinfo but with no success. I found here that someone answered

Is the MySQL server on the same host
as the web application? If not, you
can't use a socket connection, and
will need to connect via TCP.

I think that is the case with my web host. How can I change ZF to connect via TCP instead? I am currently using PDO_MYSQL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鸠书 2024-08-06 19:37:11

连接 MySQL 的方式取决于传递给 mysql_connect() 的参数。 如果你向它传递一个文件路径,即。 /var/run/mysqld/mysqld.sock,它将尝试套接字连接。 如果您向其传递主机名,它将尝试建立 TCP 连接。

您需要找到 mysql 服务器的主机名(或 IP),然后像这样连接到它:

mysql_connect('127.0.0.1:3306',$username,$password);

ZF 的赔率,您只需在存储数据库设置的配置文件中指定此项,或者在使用 host 设置连接到数据库时指定:

$db = new Zend_Db_Adapter_Pdo_Mysql(array(
    'host'     => '127.0.0.1',
    'username' => 'webuser',
    'password' => 'xxxxxxxx',
    'dbname'   => 'test'
));

编辑:如果您正在将正确的主机名/端口作为主机参数传递给 mysql_connect() 并获取此消息,那么很可能是服务器配置问题。

尝试在 php.ini 中或使用 设置>ini_set()

ini_set('mysql.default_socket','/path/to/real/socket.sock');

你必须找出套接字文件在哪里。 通常它是“/var/lib/mysql/mysql.sock”,但我认为它依赖于系统。

How you connect to MySQL depends on the parameters you pass to mysql_connect(). If you pass it a file path, ie. /var/run/mysqld/mysqld.sock, it's going to attempt a socket connection. If you pass it a hostname, it will attempt a TCP connection.

You need to find the hostname (or IP) of the mysql server, and connect to it like this:

mysql_connect('127.0.0.1:3306',$username,$password);

Odds are for ZF, you can just specify this in the configuration file where you store your database settings, or when you connect to the database using the host setting:

$db = new Zend_Db_Adapter_Pdo_Mysql(array(
    'host'     => '127.0.0.1',
    'username' => 'webuser',
    'password' => 'xxxxxxxx',
    'dbname'   => 'test'
));

Edit: If you are passing a proper hostname/port as the host parameter to mysql_connect() and getting this message, then it is most likely a server configuration issue.

Try adjusting the mysql.default_socket setting, either in php.ini or at the top of your application code using ini_set()

ini_set('mysql.default_socket','/path/to/real/socket.sock');

You'll have to figure out where the socket file is. Often it's '/var/lib/mysql/mysql.sock', but I think it's system dependent.

安人多梦 2024-08-06 19:37:11

我已将行:

defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));

按原样保留在我的index.php中,但已将 public/.htaccess 更改为

SetEnv APPLICATION_ENV production

看起来 .htaccess 未被读取。 我将 index.php 中的 else 语句更改为“生产”以使用生产数据库设置,并且它有效。 相当令人沮丧。 我想知道为什么没有读取环境变量。 感谢您的帮助。

I had left the line:

defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));

in my index.php as is but had changed public/.htaccess to

SetEnv APPLICATION_ENV production

It looks like the .htaccess was not being read. I changed the else statement in index.php to 'production' to use the production database settings and it worked. Quite frustrating. I wonder why the environment variable was not being read. Thanks for your help.

冰火雁神 2024-08-06 19:37:11

Zend Framework 在 db params unix_socket 中有选项,只需使用它

 $config= array(
 'db' => array(
'host'   => 'localhost',
'username' => 'user',
'password' => 'pass',
'dbname' => 'dbname',
'dbprefix' => '',
'unix_socket' => '/var/lib/mysqld/mysqld.sock'
  )
);

Zend Framework has option in db params unix_socket, simply use it

 $config= array(
 'db' => array(
'host'   => 'localhost',
'username' => 'user',
'password' => 'pass',
'dbname' => 'dbname',
'dbprefix' => '',
'unix_socket' => '/var/lib/mysqld/mysqld.sock'
  )
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文