简体   繁体   English

如何强制 Laravel 使用不同的 mysql 数据库?

[英]How can I force Laravel to use a different mysql database?

I'm running a Laravel 5.2 app on an EC2 instance with Ubuntu.我正在使用 Ubuntu 在 EC2 实例上运行 Laravel 5.2 应用程序。 For some reason the app is telling me I'm using a database other then what I've defined on the env file.出于某种原因,应用程序告诉我我正在使用一个数据库,而不是我在env文件中定义的数据库。 My .env file is as such:我的.env文件是这样的:

APP_ENV=production
APP_DEBUG=true
APP_KEY=snipsnip
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=paidfor
DB_PORT=
DB_DATABASE=paidfor
DB_USERNAME=root
DB_PASSWORD=snipsnip

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

But the application is throwing the error但是应用程序抛出错误

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'phpmyadmin.products_list` SQLSTATE[42S02]:未找到基表或视图:1146 表 'phpmyadmin.products_list`

I've ran我跑了

php artisan optimize

which yield Generating optimized class loader and then I ran这产生了生成优化的类加载器,然后我跑了

php artisan migrate

which resulted in Nothing to migrate .这导致了Nothing to migrate

I've several databases on MySQL server as listed below and I want the application to use the database paidfor and not phpmyadmin .我在 MySQL 服务器上有几个数据库,如下所列,我希望应用程序使用paidfor数据库而不是phpmyadmin

information_schema
mysql
paidfor
performance_schema
phpmyadmin

How can I fix this issue?我该如何解决这个问题?

  MySqlConnection {#148 ▼
  #pdo: PDO {#158 ▶}
  #readPdo: null
  #reconnector: Closure {#153 ▶}
  #queryGrammar: MySqlGrammar {#147 ▶}
  #schemaGrammar: null
  #postProcessor: MySqlProcessor {#151}
  #events: Dispatcher {#5 ▶}
  #fetchMode: 8
  #fetchArgument: null
  #fetchConstructorArgument: []
  #transactions: 0
  #queryLog: []
  #loggingQueries: false
  #pretending: false
  #database: "phpmyadmin"
  #doctrineConnection: null
  #tablePrefix: ""
  #config: array:12 [▼
    "driver" => "mysql"
    "host" => "127.0.0.1"
    "port" => "3306"
    "database" => "phpmyadmin"
    "username" => "phpmyadmin"
    "password" => "snipsnip"
    "charset" => "utf8"
    "collation" => "utf8_unicode_ci"
    "prefix" => ""
    "strict" => false
    "engine" => null
    "name" => "mysql"
  ]
}

If you updated the .env file and ran php artisan optimize and it still doesn't work, then there's one of two things that could happen:如果您更新了.env文件并运行了php artisan optimize并且它仍然不起作用,那么可能会发生以下两种情况之一:

  1. You're not using the values of .env for the database connection.您没有将.env的值用于数据库连接。

    Open the file config/database.php and look for the connections key.打开文件config/database.php并查找connections键。 Verify that you're using the values of env on the connection name (in your case mysql ).验证您是否在连接名称上使用 env 的值(在您的情况下为mysql )。 You should have something like:你应该有类似的东西:

     'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'engine' => null, ],
  2. Your model is using another connection rather than mysql .您的模型正在使用另一个连接而不是mysql Check the model for something like检查模型是否有类似的东西

     $this->connection = 'other-connection-name';

    or something like或类似的东西

    $this->setConnection('other-connection-name');

If you wish to use paidfor database in your system, and access from phpmyadmin, create a new user for application with these privileges: ALTER, CREATE, DELETE, DROP, INDEX, INSERT, SELECT, UPDATE (you could, also, create two users, one for migrations only and other for application).如果您希望在您的系统中使用paidfor数据库,并从 phpmyadmin 访问,请为具有以下权限的应用程序创建一个新用户: ALTER, CREATE, DELETE, DROP, INDEX, INSERT, SELECT, UPDATE (您也可以创建两个用户,一个仅用于迁移,另一个用于应用程序)。

With you root user, run in your PhpMyAdmin:使用 root 用户,在您的 PhpMyAdmin 中运行:

CREATE USER 'myuser'@'myhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON paidfor TO 'myuser'@'myhost';

After that, use your new user in your .env file.之后,在.env文件中使用新用户。

Then when you access PhpMyAdmin, with your new user, you will have access only to this database.然后,当您使用新用户访问 PhpMyAdmin 时,您将只能访问此数据库。

Don't ever never ever use root user in your application永远不要在你的应用程序中使用 root 用户

A solution could not be found with the instance I was running because the server was ignoring the .emv file.我正在运行的实例找不到解决方案,因为服务器忽略了.emv文件。 The only workaround I could figure out was to boot up a brand new EC2 instance, reinstall Composer, Laravel, and try again.我能想到的唯一解决方法是启动一个全新的 EC2 实例,重新安装 Composer、Laravel,然后重试。

Special thanks to @milz for spending about 2 hours working through this with me!特别感谢@milz 花了大约 2 个小时和我一起解决这个问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM