3

I am moving an application from my development machine to a test server. When connecting to my local development mysql database everything works as expected. When attempting to connect to our test server, the requests time out after 45 seconds and a 500 error is returned.

I tested that the servers can communicate and php can get results by using the basic mysqli php functionality, and results are returned as expected:

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM users";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo var_export($row, true);
    }
} else {
    echo "0 results";
}
$conn->close();

The following both fail and hit timeout limits in laravel:

$users = DB::table('users')->get();

$users = User::all();

Thoughts? Ideas? Opinions?

6
  • The first is just an attempted connection. The second retrieves all the records from the User table. They don't compare. Try select * from users in the mysql command line and see how long it takes. Then try some logging -- turn on mysql's slow log and/or general_log for the duration of the test. Commented Feb 17, 2017 at 17:20
  • @aynber Updated question after researching your comment Commented Feb 17, 2017 at 17:42
  • 2
    Next step is to find out if it's just the table, or all DB queries using those methods. Try DB::select("SELECT NOW()"); and see if that times out. Then DB::select("SHOW TABLES"); and DB::select("DESCRIBE users"); Commented Feb 17, 2017 at 18:09
  • @aynber The result is the same for each of the select statements you have suggested. Commented Feb 17, 2017 at 18:46
  • If those queries are timing out, especially the select now, there's definitely an issue between laravel and your DB. Did you run your mysqli script on the same server as the laravel app, with the exact same credentials? Commented Feb 17, 2017 at 18:48

1 Answer 1

7

I had the same issue. I did var_dump(DB::connection()); and found out that the host value was wrong because a wrong .env file was loaded.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.