Asked  7 Months ago    Answers:  5   Viewed   46 times

I am receiving the following error on my localhost for Laravel 4.1 (using MAMP)

SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (61)

It points to:

/Applications/MAMP/htdocs/crowdsets/laravel-master/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php

This is the function it is pointing to:

public function createConnection($dsn, array $config, array $options)
    {
        $username = array_get($config, 'username');

        $password = array_get($config, 'password');

        return new PDO($dsn, $username, $password, $options);
    }

Up to this point, I had not received this error.

I have a local environment and production environment set up (the default).

in config/local/database.php I have:

'mysql' => array(
            'driver'    => 'mysql',
            'host'      => '127.0.0.1',
            'database'  => 'database',
            'username'  => 'root',
            'password'  => 'root',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

 Answers

75

An error like that means the server itself is not even reachable. Did you start MySQL in MAMP?

Also, how have you started MAMP? With the standard MySQL 3306 port? Or the alternative port MAMP uses for non-admins: 8889?

I bet your server is running, but is attempting to connect to 3306 so you need to set the port to 8889. Change your config to be like this; note the addition of the port param:

'mysql' => array(
            'driver'    => 'mysql',
            'host'      => '127.0.0.1',
            'port'      => '8889',
            'database'  => 'database',
            'username'  => 'root',
            'password'  => 'root',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

EDIT: I just found this question thread that addresses the issue of connecting Laravel to MAMP via port 8889.

Wednesday, March 31, 2021
 
shin
answered 7 Months ago
56

This can be resolved by specifying the specific column names desired from the specific table like so:

$c = Customer::leftJoin('orders', function($join) {
      $join->on('customers.id', '=', 'orders.customer_id');
    })
    ->whereNull('orders.customer_id')
    ->first([
        'customers.id',
        'customers.first_name',
        'customers.last_name',
        'customers.email',
        'customers.phone',
        'customers.address1',
        'customers.address2',
        'customers.city',
        'customers.state',
        'customers.county',
        'customers.district',
        'customers.postal_code',
        'customers.country'
    ]);
Wednesday, March 31, 2021
 
redrom
answered 7 Months ago
72

We have now solved this generically for all models by extending our base model with the following functionaly:

  • We define an array of attributes that hold geometric data.
  • We decide on a per-model-basis if we want this to be auto-loaded as text.
  • We change the default query builder to select the geometry attributes as text from the database.

Here is an excerpt from the base model we now use:

/**
 * The attributes that hold geometrical data.
 *
 * @var array
 */
protected $geometry = array();

/**
 * Select geometrical attributes as text from database.
 *
 * @var bool
 */
protected $geometryAsText = false;

/**
 * Get a new query builder for the model's table.
 * Manipulate in case we need to convert geometrical fields to text.
 *
 * @param  bool  $excludeDeleted
 * @return IlluminateDatabaseEloquentBuilder
 */
public function newQuery($excludeDeleted = true)
{
    if (!empty($this->geometry) && $this->geometryAsText === true)
    {
        $raw = '';
        foreach ($this->geometry as $column)
        {
            $raw .= 'AsText(`' . $this->table . '`.`' . $column . '`) as `' . $column . '`, ';
        }
        $raw = substr($raw, 0, -2);
        return parent::newQuery($excludeDeleted)->addSelect('*', DB::raw($raw));
    }
    return parent::newQuery($excludeDeleted);
}
Wednesday, March 31, 2021
 
tika
answered 7 Months ago
97

It looks like your script is trying to connect "domain_name", but it can't find that server because it's not listed on the DNS's. Is your server on the same machine? In that cause you need to replace domain_name with localhost, 127.0.0.1 or the ip of the server.

ALSO MAKE SURE TO SANITIZE YOUR INPUTS BEFORE YOU RUN THE SQL COMMANDS!!!!

This should help.

Wednesday, March 31, 2021
 
Zigglzworth
answered 7 Months ago
24

You'll probably have to grant 'localhost' privileges to on the table to the user. See the 'GRANT' syntax documentation. Here's an example (from some C source).

"GRANT ALL PRIVILEGES ON %s.* TO '%s'@'localhost' IDENTIFIED BY '%s'";

That's the most common access problem with MySQL.

Other than that, you might check that the user you have defined to create your instance has full privileges, else the user cannot grant privileges.

Also, make sure the mysql service is started.

Make sure you don't have a third party firewall or Internet security service turned on.

Beyond that, there's several pages of the MySQL forum devoted to this: http://forums.mysql.com/read.php?11,9293,9609#msg-9609

Try reading that.

Thursday, June 3, 2021
 
JohnnyW
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :