You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 9 Sail MySQL端口转发报错及权限问题求助

Laravel 9 Sail MySQL端口转发后数据库连接问题排查与解决

为了保留主机MySQL的3306端口,将Laravel 9 Sail应用的MySQL端口转发到3307,执行sail up后可正常访问欢迎页面,但出现数据库连接错误,先后遇到名称解析失败和权限拒绝问题,相关信息如下:

初始错误

Illuminate\Database\QueryException

PHP 8.1.9
9.26.1

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql failed: Temporary failure in name resolution

SELECT count(*) AS aggregate FROM `users` WHERE `email` = test@test.com

.env配置

APP_URL=http://127.0.0.1
APP_PORT=81

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
FORWARD_DB_PORT=3307

docker-compose.yml配置

# For more information: https://laravel.com/docs/sail
version: '3'
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.1
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.1/app
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        ports:
            - '${APP_PORT:-81}:80'
            - '${VITE_PORT:-5174}:${VITE_PORT:-5173}'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
            XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
            XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mysql
    mysql:
        image: 'mysql/mysql-server:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3307}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '{DB_PASSWORD}'
            MYSQL_ROOT_HOST: '{DB_HOST}'
            MYSQL_DATABASE: '{DB_DATABASE}'
            MYSQL_USER: '{DB_USERNAME}'
            MYSQL_PASSWORD: '{DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 1
        volumes:
            - 'sail-mysql:/var/lib/mysql'
            - './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh'
        networks:
            - sail
        healthcheck:
            test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
            retries: 3
            timeout: 5s
networks:
    sail:
        driver: bridge
volumes:
    sail-mysql:
        driver: local

更新1:终端输出

sm_v2-laravel.test-1   "start-container"   laravel.test        exited (0)          
Shutting down old Sail processes...
[+] Running 0/1
 ⠙ Network sm_v2_sail  Creating                                                                                      0.2s
[+] Running 3/3d orphan containers ([sm_v2-service-1]) for this project. If you removed or renamed this service in your co ⠿ Network sm_v2_sail              Created                                                                           0.2s
 ⠿ Container sm_v2-mysql-1         Created                                                                           1.5s
 ⠿ Container sm_v2-laravel.test-1  Created                                                                           0.5s
Attaching to sm_v2-laravel.test-1, sm_v2-mysql-1
sm_v2-mysql-1         | [Entrypoint] MySQL Docker Image 8.0.30-1.2.9-server
sm_v2-mysql-1         | [Entrypoint] Starting MySQL 8.0.30-1.2.9-server
sm_v2-mysql-1         | 2022-08-30T15:19:04.087084Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
sm_v2-mysql-1         | 2022-08-30T15:19:04.092964Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.30) starting as process 1
sm_v2-mysql-1         | 2022-08-30T15:19:04.148193Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
sm_v2-mysql-1         | 2022-08-30T15:19:04.303213Z 1 [ERROR] [MY-012960] [InnoDB] Cannot create redo log files because data files are corrupt or the database was not shut down cleanly after creating the data files.
sm_v2-mysql-1         | 2022-08-30T15:19:04.755173Z 1 [ERROR] [MY-010334] [Server] Failed to initialize DD Storage Engine
sm_v2-mysql-1         | 2022-08-30T15:19:04.755609Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
sm_v2-mysql-1         | 2022-08-30T15:19:04.755681Z 0 [ERROR] [MY-010119] [Server] Aborting
sm_v2-mysql-1         | 2022-08-30T15:19:04.757223Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.30)  MySQL Community Server - GPL.
sm_v2-mysql-1 exited with code 1
sm_v2-laravel.test-1  | 2022-08-30 15:19:07,746 INFO Set uid to user 0 succeeded
sm_v2-laravel.test-1  | 2022-08-30 15:19:07,751 INFO supervisord started with pid 1
sm_v2-laravel.test-1  | 2022-08-30 15:19:08,756 INFO spawned: 'php' with pid 16
sm_v2-laravel.test-1  | 2022-08-30 15:19:09,759 INFO success: php entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
sm_v2-laravel.test-1  | 
sm_v2-laravel.test-1  |    INFO  Server running on [http://0.0.0.0:80].  
sm_v2-laravel.test-1  | 
sm_v2-laravel.test-1  |   Press Ctrl+C to stop the server
sm_v2-laravel.test-1  | 
sm_v2-laravel.test-1  |   2022-08-30 15:19:21 ................................................... ~ 1s
sm_v2-laravel.test-1  |   2022-08-30 15:19:23 /favicon.ico ...................................... ~ 0s
sm_v2-laravel.test-1  |   2022-08-30 15:19:23 ................................................... ~ 1s
sm_v2-laravel.test-1  |   2022-08-30 15:19:24 /build/assets/app.ac81e540.css .................... ~ 0s
sm_v2-laravel.test-1  |   2022-08-30 15:19:24 /build/assets/app.ab93cf8a.js ..................... ~ 0s
sm_v2-laravel.test-1  |   2022-08-30 15:19:24 /favicon.ico ...................................... ~ 0s
sm_v2-laravel.test-1  |   2022-08-30 15:19:27 ................................................... ~ 0s
sm_v2-laravel.test-1  |   2022-08-30 15:19:29 /favicon.ico ...................................... ~ 0s
    sm_v2-laravel.test-1  |   2022-08-30 16:07:14 ................................................... ~ 0s

更新2:新错误

SQLSTATE[HY000] [1045] Access denied for user 'root'@'192.168.128.3' (using password: YES)

解决方法

1. 修复MySQL容器启动失败(InnoDB日志损坏)

  • 停止所有Sail容器:sail down
  • 删除损坏的MySQL数据卷:docker volume rm sm_v2_sail-mysql
  • 重新启动容器:sail up -d

2. 修正docker-compose.yml的环境变量配置

MySQL服务的环境变量需正确引用.env中的变量,同时调整root用户访问权限:

mysql:
    environment:
        MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
        MYSQL_ROOT_HOST: '%'  # 允许容器网络内任意主机访问root用户
        MYSQL_DATABASE: '${DB_DATABASE}'
        MYSQL_USER: '${DB_USERNAME}'
        MYSQL_PASSWORD: '${DB_PASSWORD}'
        MYSQL_ALLOW_EMPTY_PASSWORD: 0  # 禁用空密码,避免权限冲突

3. 完善.env文件的数据库配置

确保.env中已明确设置数据库相关参数,示例:

DB_PASSWORD=your_secure_password
DB_DATABASE=laravel_app_db
DB_USERNAME=laravel_user

4. 重启容器并清理缓存

  • 重新构建容器环境:sail build --no-cache
  • 启动容器:sail up -d
  • 清除Laravel配置缓存:sail artisan config:clear

内容的提问来源于stack exchange,提问作者geeky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 16:06:29