【发布时间】:2015-02-17 11:58:49
【问题描述】:
我使用 Laravel 已经有一段时间了,但主要是在单个数据库服务器上。
现在,我的客户已经从同一个网络中的多个数据库服务器获得了至少 5 个数据库。
我可以使用ping 通过 ssh 确认服务器之间的通信,也可以使用
mysqli_connect 检查数据库连接。
但是,当我通过它的 database.php(mysql 配置)使用 laravel 时,例如:
// This setup is working fine (using localhost)
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'dbname',
'username' => 'user',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
// Other DB (which is failing to connect) but i can confrim the connection using a file outside
// the laravel app through mysqli
'other_db_connection' => array(
'driver' => 'mysql',
'host' => '192.168.33.241',
'database' => 'dbname',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'another_db_connection' => array(
'driver' => 'mysql',
'host' => '192.168.33.211',
'database' => 'dbname',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
并使用new PDO
// This connectiion functions with no problem
$conn1 = new PDO('mysql:host=localhost;dbname=dbname;port=3306','username','password');
if($conn1) {
echo 'connected';
}
// But this returns an error
$conn = new PDO('mysql:host=192.168.33.241;dbname=dname;port=3306','username','password');
if($conn) {
echo 'connected';
}
// Also, i have no problems using the DB:: or even the model as long as the host is the localhost
连接失败,PDO 和 laravel 默认的 DB 功能只有在主机是 localhost 时才有效。
由于某种原因,如果我使用其他服务器 ip,它会抛出这样的错误,这是我想与之通信的地方:
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '192.168.33.241' (13)
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '192.168.33.211' (13)
当我尝试使用服务器 ip 或主域名连接时。
我错过了什么吗?为什么我只能在 localhost 的本地数据库服务器上连接
而不是任何其他数据库服务器?但是,如果我使用来自 laravel 应用程序外部的 index.php 文件并使用 mysqli_connect 进行查询和建立连接,我可以毫无问题地连接到它们?
感谢您的所有帮助。
【问题讨论】:
-
你能发布完整的
database.php(删除密码)吗?另外,你是如何在你的代码中使用它们的? -
@msturdy .. 嗨,我已根据您的要求进行了编辑,希望更清晰。
-
@MarcoMura .. 我看不出这篇文章有什么帮助。我已经看到了这一点,它解决了与 MAMP 端口有关的问题。我们使用的服务器的所有端口都是 3306。我提到我在 localhost 数据库服务器上连接没有问题。一旦我出现问题尝试连接到不同的数据库服务器。谢谢。