【问题标题】:Laravel - How to create database tablesLaravel - 如何创建数据库表
【发布时间】:2017-12-21 20:07:49
【问题描述】:

我在创建数据库表时遇到问题。

首先我创建模型:

php artisan make:model Company -m

其次我更新迁移公司文件,添加我需要的列

第三,我要去phpMyAdmin创建一个名为test的新数据库

第四次我运行命令php artisan migrate 或php artisan migrate:install 并出现下一个错误:

[Illuminate\Database\QueryException]SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) (SQL: select * from information_schema.tables where table_schema = test and table_name = migrations)

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

在database/config.php我将mysql驱动编辑成这样:

'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'test'),
        'username' => env('DB_USERNAME', 'root'),
        'password' => env('DB_PASSWORD', 'pass'),
        'charset' => 'utf8',
        'collation' => 'utf8_general_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

还有.env file:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=pass

我还尝试使用 php artisan config:clear 清理缓存。

有人可以帮助我吗?我不明白这是什么问题。

谢谢

【问题讨论】:

  • 重启你的服务器再试一次
  • 您没有密码来登录 phpmyadmin。删除密码,在 env 文件中保持空白
  • @Exprator phpmyadmin 与它无关,顺便说一句,mysql 配置为使用该用户的密码。这可能是错误的
  • 这是我必须输入的mysql密码。 @Exprator
  • @Md.Abutaleb ,我已经这样做了,但不起作用!

标签: php mysql laravel phpmyadmin laravel-migrations


【解决方案1】:

您的“root”密码不正确或可能没有所需的权限。

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

重置“root”密码:

https://www.howtoforge.com/setting-changing-resetting-mysql-root-passwords

授予“root”用户权限:

Ubuntu:https://www.liquidweb.com/kb/how-to-add-a-user-and-grant-root-privileges-on-ubuntu-14-04/

Centos:https://www.liquidweb.com/kb/how-to-add-a-user-and-grant-root-privileges-on-centos-7/

然后再试一次:)

OP (OSX) 更新:

Setting the MySQL root user password on OS X

【讨论】:

  • 我认为它试图在information_schema中创建迁移表...这正常吗? @NorbertBoros
  • @user3242861 - 它不应该那样做......现在连接工作了吗?
  • 发生这种情况是因为我只在创建迁移后定义了连接吗? @NorbertBoros
  • @user3242861 - 可能是的,但我看到你已经解决了你的问题。您应该接受我修复连接的答案g :)
【解决方案2】:

在 config/database.php 文件中将密码字段编辑为 "password' => env('DB_PASSWORD', ''),并在 .env 文件中 DB_PASSWORD = 保持该字段为空

【讨论】:

  • 你怎么知道他不用密码?
猜你喜欢
  • 2015-11-18
  • 1970-01-01
  • 2016-04-25
  • 1970-01-01
  • 2023-03-16
  • 2013-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多