gpt4 book ai didi

php - Docker无法连接到蛋糕PHP

转载 作者:行者123 更新时间:2023-12-02 19:48:10 26 4
gpt4 key购买 nike

嗨,我在安装程序上有一个docker容器,但我收到一个错误,提示系统无法连接到数据库,我无法弄清楚这是怎么回事。

我尝试连接到localhost,127.0.0.1和mysql,但出现相同的错误:

CakePHP无法连接到数据库。
无法建立与Mysql的连接:SQLSTATE [HY000] [2002]没有此类文件或目录

似乎无法在日志文件中找到任何内容(apache和cake),我缺少什么?

php代码:

 'default' => [
//'className' =>'Cake\Database\Connection',
'className' => Connection::class,
'driver' => Mysql::class,
//'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'timezone' => 'UTC',
'encoding' => 'utf8',
'host' => 'mysql',
'username' => 'myusername',
'password' => 'mypassword',
'database' => 'myadatabase',
'flags' => [],
'cacheMetadata' => true,
'log' => false,
'quoteIdentifiers' => false,
],

docker-compose.yml
#File version 2.1
version: "3"


services:
webserver:
build:
context: ./bin/webserver
container_name: '7.3.x-webserver'
restart: 'always'
ports:
- "${HOST_MACHINE_UNSECURE_HOST_PORT}:80"
- "${HOST_MACHINE_SECURE_HOST_PORT}:443"
links:
- mysql
volumes:
- ${DOCUMENT_ROOT-./www}:/var/www/html
- ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/php.ini
- ${VHOSTS_DIR-./config/vhosts}:/etc/apache2/sites-enabled
- ${LOG_DIR-./logs/apache2}:/var/log/apache2
mysql:
build:
context: "./bin/${DATABASE}"
container_name: '5.7-mysql'
restart: 'always'
ports:
- "${HOST_MACHINE_MYSQL_PORT}:3306"
volumes:
- ${MYSQL_DATA_DIR-./data/mysql}:/var/lib/mysql
- ${MYSQL_LOG_DIR-./logs/mysql}:/var/log/mysql
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: 'sc-phpmyadmin'
links:
- mysql
environment:
PMA_HOST: mysql
PMA_PORT: 3306
PMA_USER: ${MYSQL_USER}
PMA_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
ports:
- '8080:80'
volumes:
- /sessions
# redis:
# container_name: 'sc-redis'
# image: redis:latest
# ports:
# - "${HOST_MACHINE_REDIS_PORT}:6379"


docker构建脚本:
FROM php:7.3-apache-stretch

# suppression of debconfig complaints of install apt packages interactively
ARG DEBIAN_FRONTEND=noninteractive

# Update
RUN apt-get -y update --fix-missing && \
apt-get upgrade -y && \
apt-get --no-install-recommends install -y apt-utils && \
rm -rf /var/lib/apt/lists/*


# Install useful tools and install important libaries
RUN apt-get -y update && \
apt-get -y --no-install-recommends install nano wget dialog libsqlite3-dev libsqlite3-0 && \
apt-get -y --no-install-recommends install mysql-client zlib1g-dev libzip-dev libicu-dev && \
apt-get -y --no-install-recommends install --fix-missing apt-utils build-essential git curl && \
apt-get -y --no-install-recommends install --fix-missing libcurl3 libcurl3-dev zip openssl && \
rm -rf /var/lib/apt/lists/* && \
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Install xdebug
RUN pecl install xdebug-2.7.2 && \
docker-php-ext-enable xdebug

# Install redis
RUN pecl install redis-5.0.2 && \
docker-php-ext-enable redis

# Other PHP7 Extensions

RUN docker-php-ext-install pdo_mysql && \
docker-php-ext-install pdo_sqlite && \
docker-php-ext-install mysqli && \
docker-php-ext-install curl && \
docker-php-ext-install tokenizer && \
docker-php-ext-install json && \
docker-php-ext-install zip && \
docker-php-ext-install -j$(nproc) intl && \
docker-php-ext-install mbstring && \
docker-php-ext-install gettext

# Install Freetype
RUN apt-get -y update && \
apt-get --no-install-recommends install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev && \
rm -rf /var/lib/apt/lists/* && \
docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ && \
docker-php-ext-install -j$(nproc) gd

# Enable apache modules
RUN a2enmod rewrite headers

# Cleanup
RUN rm -rf /usr/src/*

最佳答案

hy我用env变量更改了app.php,更改了数据库的用户名和密码。

我删除了./data/mysql的数据以重建容器,但仍然是相同的错误,我猜它与主机名有关

也尝试使用变量PMA_HOST,但仍然没有成功。 (本地主机也不起作用)

 'host' => env('PMA_HOST'),

如果我登录到phpmyadmin,我可以毫无问题地创建表。

如果我登录到容器,则可以登录并执行查询

我看到通过grepping php设置未填充套接字,

pdo_mysql.default_socket => /var/run/mysqld/mysqld.sock => /var/run/mysqld/mysqld.sock

现在,它已被填充,但仍然无法与mysql进行php连接(在蛋糕上,在我的数据库测试脚本中,我具有连接)。

关于php - Docker无法连接到蛋糕PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62376971/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com