无法在 docker 的本地环境中连接到 cakephp 中的 mysql 数据库
Can`t connect to mysql database in cakephp in a local environment with docker
我使用 mysql、phpmyadmin、php 和 mailhog 设置了一个本地环境,但我无法使用 CakePhp 连接到数据库。
我的连接数据在config/app。php:
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => '172.21.0.4', => I tried localhost, mysql, 172.0.0.1 as well...
'username' => 'root',
'password' => 'root',
'database' => 'mydb1',
'encoding' => 'utf8mb4',
'timezone' => 'UTC',
'cacheMetadata' => true,
'flags' => [],
'log' => false,
当我通过 mysqli_connect() 测试连接时(直接在 config/app.php 中),它工作正常:
$link = mysqli_connect("mysql", "root", "root",'mydb1',"3306",'');
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made!" . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
$query = "SELECT * FROM test";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
$flag = FALSE;
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH))
{
echo $row['test']."<br>";
}
mysqli_close($link);
非常感谢!
来自奥地利的问候!
首先,确保您没有使用 app_local.php 配置文件。
如果您使用 docker,则主机使用与数据库服务相同的名称
Docker撰写
services:
db:
platform: linux/x86_64
image: mysql:5.7
app.php
'Datasources' => [
'default' => [
'host' => 'db', //<------------------
我使用 mysql、phpmyadmin、php 和 mailhog 设置了一个本地环境,但我无法使用 CakePhp 连接到数据库。
我的连接数据在config/app。php:
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => '172.21.0.4', => I tried localhost, mysql, 172.0.0.1 as well...
'username' => 'root',
'password' => 'root',
'database' => 'mydb1',
'encoding' => 'utf8mb4',
'timezone' => 'UTC',
'cacheMetadata' => true,
'flags' => [],
'log' => false,
当我通过 mysqli_connect() 测试连接时(直接在 config/app.php 中),它工作正常:
$link = mysqli_connect("mysql", "root", "root",'mydb1',"3306",'');
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made!" . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
$query = "SELECT * FROM test";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
$flag = FALSE;
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH))
{
echo $row['test']."<br>";
}
mysqli_close($link);
非常感谢! 来自奥地利的问候!
首先,确保您没有使用 app_local.php 配置文件。
如果您使用 docker,则主机使用与数据库服务相同的名称
Docker撰写
services:
db:
platform: linux/x86_64
image: mysql:5.7
app.php
'Datasources' => [
'default' => [
'host' => 'db', //<------------------