我正在尝试使用laravel配置msql db,但是我遇到了一个奇怪的问题。在线搜索后,我仍然无法完成迁移。
我的.env中有
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT='%db_port%'
DB_DATABASE='forge'
DB_USERNAME='root'
DB_PASSWORD='password'
因此,在更改.env之后,我先执行了php artisan cache:clear
和php artisan config:cache
,然后运行了php artisan migrate
,我得到了
Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known (SQL: select * from information_schema.tables where table_schema = %db_name% and table_name = migrations)
at /Users/p/Documents/Project/hub-family-server/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the database's errors.
663| catch (Exception $e) {
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|
Exception trace:
1 Doctrine\DBAL\Driver\PDOException::("SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known")
/Users/p/Documents/Project/hub-family-server/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:31
2 PDOException::("PDO::__construct(): php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known")
/Users/p/Documents/Project/hub-family-server/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:27
Please use the argument -v to see more details.
现在使用php artisan tinker
与应用程序进行交互并检查
Psy Shell v0.9.9 (PHP 7.2.19 — cli) by Justin Hileman
>>> env('DB_USERNAME')
=> null
>>> config('database.connections.mysql.database')
=> "%db_name%"
>>> env('DB_HOST')
=> null
>>> env('DB_USERNAME')
=> null
>>>
所以我不确定现在发生了什么,我也确实用composer require doctrine/dbal
更新了dbal,但这没做任何事情。有人可以帮我了解发生了什么事。
答案 0 :(得分:1)
我认为问题出在DB_PORT上,应该是3306,或者根据服务器设置而定。 更改DB_PORT,如果所有其他数据正确,则应该可以。
答案 1 :(得分:1)
问题出在DB_PORT上,如果您使用的是Mamp服务器,则应该为3306或8000,使其为8889,并且应该可以工作
答案 2 :(得分:1)
一段时间后,我设法将mysql与应用程序连接起来。
.env无法正确加载,因此
之后 php artisan config:cache
我需要
php artisan key:generate
也缺少DB_PORT
。
答案 3 :(得分:0)
请正确更新.env文件。
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test_db
DB_USERNAME=root
DB_PASSWORD=123
如果您在本地主机上工作并且本地主机没有任何密码,请更新DB_PORT编号,删除DB_PORT,DB_DATABASE,DB_USERNAME,DB_PASSWORD中的单引号,并将“ DB_PASSWORD =“留空。