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

时间:2016-05-30 19:41:14

标签: php laravel

我正在使用Ubuntu在EC2实例上运行Laravel 5.2应用程序。出于某种原因,应用程序告诉我我正在使用数据库,而不是我在env文件中定义的数据库。我的.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

但是应用程序正在抛出错误

  

SQLSTATE [42S02]:未找到基表或视图:1146表'phpmyadmin.products_list`

我跑了

php artisan optimize

产生生成优化的类加载器然后我运行

php artisan migrate

导致无法迁移

我在MySQL服务器上有几个数据库,如下所示,我希望应用程序使用数据库paidfor而不是phpmyadmin

information_schema
mysql
paidfor
performance_schema
phpmyadmin

如何解决此问题?

  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"
  ]
}

3 个答案:

答案 0 :(得分:1)

如果您更新了<head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <meta charset="UTF-8" /> <title>Google Map</title> <style> #map, html, body { padding: 0; margin: 0; height: 100%; } </style> <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=drawing"></script> <script type="text/javascript"> var drawingManager; var selectedShape; var all_overlays = []; function setSelection(shape) { clearSelection(); selectedShape = shape; shape.setEditable(true); } function clearSelection() { if (selectedShape) { selectedShape.setEditable(false); selectedShape = null; } } function getPolygonCoords() { var coordinates = new Array(); if (!selectedShape) { return false; } else { var len = selectedShape.getPath().getLength(); for (var i = 0; i < len; i++) { coordinates.push(selectedShape.getPath().getAt(i).toUrlValue(5)); } return coordinates; } } function initialize() { var map = new google.maps.Map(document.getElementById('map'), { zoom: 10, center: new google.maps.LatLng(33.619003, -83.867405), mapTypeId: google.maps.MapTypeId.ROADMAP, disableDefaultUI: true, zoomControl: true }); var polyOptions = { fillColor: '#0099FF', fillOpacity: 0.7, strokeColor: '#AA2143', strokeWeight: 2, editable: true }; // Creates a drawing manager attached to the map that allows the user to draw Polygons drawingManager = new google.maps.drawing.DrawingManager({ drawingControlOptions: { drawingModes: [ google.maps.drawing.OverlayType.POLYGON ] }, polygonOptions: polyOptions, map: map }); google.maps.Polygon.prototype.Contains = function(point) { var crossings = 0, path = this.getPath(); // for each edge for (var i = 0; i < path.getLength(); i++) { var a = path.getAt(i), j = i + 1; if (j >= path.getLength()) { j = 0; } var b = path.getAt(j); if (rayCrossesSegment(point, a, b)) { crossings++; } } // odd number of crossings? return (crossings % 2 == 1); function rayCrossesSegment(point, a, b) { var px = point.lng(), py = point.lat(), ax = a.lng(), ay = a.lat(), bx = b.lng(), by = b.lat(); if (ay > by) { ax = b.lng(); ay = b.lat(); bx = a.lng(); by = a.lat(); } // alter longitude to cater for 180 degree crossings if (px < 0) { px += 360; } if (ax < 0) { ax += 360; } if (bx < 0) { bx += 360; } if (py == ay || py == by) py += 0.00000001; if ((py > by || py < ay) || (px > Math.max(ax, bx))) return false; if (px < Math.min(ax, bx)) return true; var red = (ax != bx) ? ((by - ay) / (bx - ax)) : Infinity; var blue = (ax != px) ? ((py - ay) / (px - ax)) : Infinity; return (blue >= red); } }; google.maps.event.addListener(drawingManager, 'polygoncomplete', function(e) { var point = new google.maps.LatLng(33.619003, -83.867405); var polygon = new google.maps.Polygon(getPolygonCoords()); if (polygon.Contains(point)) { console.log('YES'); } else { console.log('NO'); } }); google.maps.event.addListener(drawingManager, 'overlaycomplete', function(e) { all_overlays.push(e); if (e.type != google.maps.drawing.OverlayType.MARKER) { // Switch back to non-drawing mode after drawing a shape. drawingManager.setDrawingMode(null); // Add an event listener that selects the newly-drawn shape when the user mouses down on it. var newShape = e.overlay; newShape.type = e.type; google.maps.event.addListener(newShape, 'click', function() { setSelection(newShape); }); setSelection(newShape); } }); // Clear the current selection when the drawing mode is changed, or when the map is clicked. google.maps.event.addListener(drawingManager, 'drawingmode_changed', clearSelection); google.maps.event.addListener(map, 'click', clearSelection); var marker = new google.maps.Marker({ position: { lat: 33.619003, lng: -83.867405 }, map: map }); } google.maps.event.addDomListener(window, 'load', initialize); </script> </head> <body> <div id="map"> </div> </body> </html> 文件并运行了.env但仍然无效,那么可能会发生以下两种情况之一:

  1. 您没有将php artisan optimize的值用于数据库连接。

    打开文件.env并查找config/database.php键。验证您是否在连接名称上使用了env的值(在您的情况下为connections)。你应该有类似的东西:

    mysql
  2. 您的模型正在使用其他连接,而不是'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, ], 。检查模型是否类似

    mysql

    或类似

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

答案 1 :(得分:0)

如果您希望在系统中使用paidfor数据库,并从phpmyadmin访问,请为具有以下权限的应用程序创建新用户:ALTER, CREATE, DELETE, DROP, INDEX, INSERT, SELECT, UPDATE(您也可以创建两个用户,一个用于仅限迁移和其他应用程序)。

以root用户身份运行PhpMyAdmin:

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

之后,在.env文件中使用新用户。

然后,当您使用新用户访问PhpMyAdmin时,您将只能访问此数据库。

永远不要在您的应用程序中使用root用户

答案 2 :(得分:0)

在我运行的实例中找不到解决方案,因为服务器忽略了.emv文件。我能想到的唯一解决方法是启动一个全新的EC2实例,重新安装Composer,Laravel,然后重试。

特别感谢@milz花了大约2个小时与我一起工作!