Laravel [1045] Access denied for user 'homestead'@'localhost' .env沒有配置
laravel 連接數據庫出現錯誤
PDOException in Connector.php line 55:SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES)
1. 確認./config/database.php
<?有配phpreturn [ 'default' => env('DB_CONNECTION', 'mysql'), 'connections' => [ 'sqlite' => [ 'driver' => 'sqlite', 'database' => env('DB_DATABASE', database_path('database.sqlite')), 'prefix' => '', ], 'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'studyonline'), 'username' => env('DB_USERNAME', 'root'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8', 'collation' => 'utf8_general_ci', 'prefix' => '', 'strict' => true, 'engine' => null, ], // ... ]]
2. 檢查.env文件
默認的配置導致的錯誤
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
APP_NAME=LaravelAPP_ENV=localAPP_KEY=base64:cdQLOKNt6JfRuIwfABQ/rqDuR42lgFi8qCC5b4R/KsI=APP_DEBUG=trueAPP_URL=http://localhostLOG_CHANNEL=stackDB_CONNECTION=mysqlDB_HOST=127.0.0.1DB_PORT=3306DB_DATABASE=studyonlineDB_USERNAME=rootDB_PASSWORD=
3. 重啟服務
php artisan cache:clearphp artisan config:clearphp artisan serve
cd ./publicphp -S localhost:9000
laravel artisan 一些命令
* 創建model
php artisan make:model Video
執行后生成 ./app/Video.php
<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Video extends Model{ //}
* 創建Controller
php artisan make:controller UserController
執行后生成 ./app/Http/Controllers/UserController.php
<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;class UserController extends Controller { }
* 創建數據庫消息隊列的數據表遷移文件
php artisan queue:table
=> database/migrations/2018_07_21_033228_create_jobs_table.php
![Laravel [1045] Access denied for user 'homestead'@'localhost' .env沒有配置 _ JavaClub全棧架構師技術筆記 Laravel [1045] Access denied for user 'homestead'@'localhost' .env沒有配置 _ JavaClub全棧架構師技術筆記](https://www.javaclub.cn/zb_users/theme/tpure/style/images/lazyload.png)
<?phpuse Illuminate\Support\Facades\Schema;use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;class CreateJobsTable extends Migration{ /** * Run the migrations. * * @return void */ public function up() { Schema::create('jobs', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('queue')->index(); $table->longText('payload'); $table->unsignedTinyInteger('attempts'); $table->unsignedInteger('reserved_at')->nullable(); $table->unsignedInteger('available_at'); $table->unsignedInteger('created_at'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('jobs'); }}遷移文件
php artisan migrate
=> 數據表結構 jobs表
![Laravel [1045] Access denied for user 'homestead'@'localhost' .env沒有配置 _ JavaClub全棧架構師技術筆記 Laravel [1045] Access denied for user 'homestead'@'localhost' .env沒有配置 _ JavaClub全棧架構師技術筆記](https://www.javaclub.cn/zb_users/theme/tpure/style/images/lazyload.png)
-- MySQL dump 10.16 Distrib 10.1.31-MariaDB, for osx10.6 (i386)---- Host: localhost Database: laravel-- -------------------------------------------------------- Server version 10.1.31-MariaDB/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;/*!40101 SET NAMES utf8 */;/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;/*!40103 SET TIME_ZONE='+00:00' */;/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;---- Table structure for table `jobs`--DROP TABLE IF EXISTS `jobs`;/*!40101 SET @saved_cs_client = @@character_set_client */;/*!40101 SET character_set_client = utf8 */;CREATE TABLE `jobs` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `queue` varchar(255) NOT NULL, `payload` longtext NOT NULL, `attempts` tinyint(3) unsigned NOT NULL, `reserved_at` int(10) unsigned DEFAULT NULL, `available_at` int(10) unsigned NOT NULL, `created_at` int(10) unsigned NOT NULL, PRIMARY KEY (`id`), KEY `jobs_queue_index` (`queue`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;/*!40101 SET character_set_client = @saved_cs_client */;---- Dumping data for table `jobs`--LOCK TABLES `jobs` WRITE;/*!40000 ALTER TABLE `jobs` DISABLE KEYS */;/*!40000 ALTER TABLE `jobs` ENABLE KEYS */;UNLOCK TABLES;/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;-- Dump completed on 2018-07-21 11:39:29jobs.sql
假設數據庫名為 laravel, 導出這個表
mysqldump -uroot -hlocalhost -p --databases laravel --tables jobs > jobs.sql
創建controller
php artisan make:controller WelcomeController
ERROR:
Illuminate \ Database \ QueryException(42S22)
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'updated_at' in 'field list' (SQL: update `t_video` set `location` = http://video.tfjyzx.com/cd16bf18f8394f2aa25fb1efc814a09e/e2ac5e5effd8490d819f6025b37c38c2-5287d2089db37e62345123a1be272f8b.mp4,有配 `updated_at` = 2018-07-14 08:43:48 where `id` = 1) @Ref: https://docs.golaravel.com/docs/5.6/eloquent/Timestamps
By default, Eloquent expects created_at
and updated_at
columns to exist on your tables. If you do not wish to have these columns automatically managed by Eloquent, set the $timestamps
property on your model to false
:
<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Video extends Model { protected $table = 't_video'; protected $primaryKey = 'id'; public $timestamps = false; /* Indicates if the model should be timestamped. */}
作者:zhanghui_ming
來源鏈接:https://www.cnblogs.com/mingzhanghui/p/9308792.html