laradock下laravel项目的DB_HOST的问题
安装完成laradock和laravel项目的时候,安装dcat-admin最后一步
// 执行数据库迁移操作
php artisan admin:install
提示:
Illuminate\Database\QueryException
SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = test and table_name = migrations and table_type = 'BASE TABLE')
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:712
708▕ // If an exception occurs when attempting to run a query, we'll format the error
709▕ // message to include the bindings with SQL, which will make this exception a
710▕ // lot more helpful to the developer instead of just the database's errors.
711▕ catch (Exception $e) {
➜ 712▕ throw new QueryException(
713▕ $query, $this->prepareBindings($bindings), $e
714▕ );
715▕ }
716▕ }
+45 vendor frames
46 artisan:37
Illuminate\Foundation\Console\Kernel::handle()
根据错误提示,就是数据库没有连接上,项目的mysql配置信息有误(账号密码错误,host错等)。查看laravel项目配置信息:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=root
使用navicat工具,是可以正常连接的,但是到这里就不行,查看laradock下的.env有关mysql的配置:
EARMAN_MYSQL_HOST=localhost
# Mysql server port (Default: 3306)
GEARMAN_MYSQL_PORT=3306
# Mysql server user (Default: root)
GEARMAN_MYSQL_USER=root
# Mysql password
GEARMAN_MYSQL_PASSWORD=
# Path to file with mysql password(Docker secrets)
GEARMAN_MYSQL_PASSWORD_FILE=
# Database to use by Gearman (Default: Gearmand)
GEARMAN_MYSQL_DB=Gearmand
# Table to use by Gearman (Default: gearman_queue)
GEARMAN_MYSQL_TABLE=gearman_queue
根据这篇文章——连接laradock mysql数据库出错(已解决)所说的:
项目中的DB_HOST应该改为mysql
。
改完以后数据库就可以正常连接了。这里有不解的地方,不知道我理解的对不对:
1、使用navicat工具连接数据库属于连接宿主机,属于容器外的操作,因此应该是使用localhost或者127.0.0.1进行连接数据库;
2、项目(代码映射到的位置)处于docker的虚拟层,也就是workspace
容器内,因此是容器内连接mysql,使用的是mysql这个容器名,而不是localhost,因此项目的DB_HOST应该是mysql。
3、如果是容器外的项目需要连接数据库,那么它的DB_HOST应该是localhost或者127.0.0.1。
其他:
使用指令进入workspace容器:
// docker-compose exce 服务id bash
// 作用:进入某个服务容器内部
docker-compose exce workspace bash
推荐文章: