我能够访问mysql数据库从phpmyadminusinguser: admin和密码:root和url: 127.0.0.1:3310我能够在127.0.0.1:8008上加载网站但是当我尝试登录或与数据库交互时,我得到下面的错误:
SQLSTATE[HY000] [2002] Connection refusedselect * from
users
where
和我也尝试做一个迁移从应用程序docker容器但也失败了
root@58a709f18668:/var/www/html# php artisan migrate
IlluminateDatabaseQueryException
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = laravelvueblog_db and table_name = migrations and table_type = 'BASE TABLE')
下面是我的.env文件
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3309
DB_DATABASE=laravelvueblog_db
DB_USERNAME=admin
DB_PASSWORD=root
下面是我的vhost.conf文件<VirtualHost *:8008>
DocumentRoot /var/www/html/public
<Directory "/var/www/html/public">
AllowOverride all
Require all granted
</Directory>
#ErrorLog ${APACHE_LOG_DIR}/error.log
#CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
是我的Docker文件
FROM php:8-apache
USER root
RUN apt-get update -y && apt-get install -y openssl curl zip unzip git nano
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
RUN docker-php-ext-install mysqli pdo pdo_mysql opcache
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
WORKDIR /app
COPY . /app
COPY vhost.conf /etc/apache2/sites-available/000-default.conf
RUN chown -R www-data:www-data /app && a2enmod rewrite
RUN rm -rf /var/www/html && ln -s /app /var/www/html
RUN composer install
RUN php artisan optimize:clear
CMD php artisan serve --host=0.0.0.0 --port=8000
EXPOSE 8000
下面是我的docker-compose。yaml文件
version: '3.8'
services:
db:
image: mariadb:latest
container_name: db
ports:
- 3309:3306
environment:
MYSQL_DATABASE: laravelvueblog_db
MYSQL_ROOT_PASSWORD: root
MYSQL_PASSWORD: root
MYSQL_USER: admin
volumes:
- mysql_file:/docker-entrypoint-initdb.d
networks:
- appnetwork
main:
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/app
ports:
- 8008:8000
environment:
# MYSQL_DATABASE: laravelvueblog_db
# MYSQL_ROOT_PASSWORD: root
# MYSQL_PASSWORD: root
# MYSQL_USER: admin
DB_HOST: db
DB_USER: admin
DB_PASSWORD: root
DB_NAME: laravelvueblog_db
WAIT_HOSTS: db:3306
depends_on:
- db
links:
- db
networks:
- appnetwork
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- 3310:80
links:
- mysql
environment:
PMA_HOSTS: db
PMA_PORT: 3306
depends_on:
- db
networks:
- appnetwork
volumes:
mysql_file:
driver: local
networks:
appnetwork:
driver: bridge
经过几次尝试后,我发现了问题,因为所有的服务都在同一个网络上(appnetwork))
我在laravel .env文件中使用了内部端口,如下所示
DB_PORT=3306
现在运行正常