te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>laravel - Multiple Sail project environments and database issues - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

laravel - Multiple Sail project environments and database issues - Stack Overflow

programmeradmin3浏览0评论

I am used to having just one Laravel Sail project on my machine which has been fine and I am used to using DDEV for Craft CMS project environments where each container feels more self-contained. I am now at a point where I have tried to set up a second Laravel project locally that I can run with Sail but it seems like I can't have two different database environments or something? Maybe Sail environments share the same database connection if they both use MySQL 8?

I am not trying to run the two projects simultaneously but it seems like when MySQL starts up, it loads the database for the wrong project and doesn't create the database for the new project and I don't have permission to create it manually.

I am not a Docker config expert so if someone could help me to understand how I need to set things up so that I can have two projects that each have their own database with Sail, I'd appreciate the help.

I am used to having just one Laravel Sail project on my machine which has been fine and I am used to using DDEV for Craft CMS project environments where each container feels more self-contained. I am now at a point where I have tried to set up a second Laravel project locally that I can run with Sail but it seems like I can't have two different database environments or something? Maybe Sail environments share the same database connection if they both use MySQL 8?

I am not trying to run the two projects simultaneously but it seems like when MySQL starts up, it loads the database for the wrong project and doesn't create the database for the new project and I don't have permission to create it manually.

I am not a Docker config expert so if someone could help me to understand how I need to set things up so that I can have two projects that each have their own database with Sail, I'd appreciate the help.

Share Improve this question asked Feb 17 at 16:36 Benjamin KohlBenjamin Kohl 5325 silver badges6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

It sounds like both are hitting the same ports.

In you first project, try setting the docker-compose.yml vars like;

laravel.test:
    build:
        context: ./vendor/laravel/sail/runtimes/8.3
        dockerfile: Dockerfile
        args:
            WWWGROUP: '${WWWGROUP}'
            NODE_VERSION: '22'
    image: sail-8.3/app
    extra_hosts:
        - 'host.docker.internal:host-gateway'
    ports:
        - '${APP_PORT:-8080}:80'
        - '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
        - '${XDEBUG_PORT:-9003}:${XDEBUG_PORT:-9003}'
    environment:
        WWWUSER: '${WWWUSER}'
        LARAVEL_SAIL: 1
        XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
        XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
        IGNITION_LOCAL_SITES_PATH: '${PWD}'
    volumes:
        - '.:/var/www/html'
    networks:
        - sail
    depends_on:
        - mysql
        - minio
        - mailpit
mysql:
    image: 'mysql/mysql-server:8.0'
    ports:
        - '${FORWARD_DB_PORT:-3306}:3306'
    environment:
        MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
        MYSQL_ROOT_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

And in the other project, the dockerfile;

laravel.test:
    build:
        context: ./vendor/laravel/sail/runtimes/8.3
        dockerfile: Dockerfile
        args:
            WWWGROUP: '${WWWGROUP}'
    image: sail-8.3/app
    extra_hosts:
        - 'host.docker.internal:host-gateway'
    ports:
        - '8010:80'
        - '${VITE_PORT:-5174}:${VITE_PORT:-5174}'
    environment:
        WWWUSER: '${WWWUSER}'
        LARAVEL_SAIL: 1
        XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
        XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
        IGNITION_LOCAL_SITES_PATH: '${PWD}'
    volumes:
        - '.:/var/www/html'
    networks:
        - sail
    depends_on:
        - mysql
        - mailpit
        - minio
mysql:
    image: 'mysql/mysql-server:8.0'
    ports:
        - '${FORWARD_DB_PORT:-3307}:3306'
    environment:
        MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
        MYSQL_ROOT_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

You can control this via the .env by setting the repective values of (any any other clashing ports), e.g in first project

APP_PORT=8080
FORWARD_DB_PORT=3306

so access the first on http://localhost:8080

and second

APP_PORT=8081
FORWARD_DB_PORT=3307

so access the first on http://localhost:8081

发布评论

评论列表(0)

  1. 暂无评论