Laravel failing to connect to DB after 4.2 upgrade - mysql

I just upgraded my Laravel install from 4.1.(something) to 4.2.7 using the steps recommended here: http://laravel.com/docs/upgrade
Now I'm getting this error on every page:
PDOException (2002)
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '' (111)
MySQL is not running locally, but it's not supposed to be. I don't have any configuration for connecting to local MySQL, my development SQL server is remote. Why is it trying to connect to local?
Is there some config change that isn't mentioned in the upgrade guide? Everything was peachy in 4.1.
From my app/config/database.php file:
'default' => 'mysql',
...
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'myrealdb.us-east-1.rds.amazonaws.com',
'database' => 'myrealdbname',
'username' => 'myrealuser',
'password' => 'myrealpass',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'database_port' => '3306',
'unix_socket' => '',
),
...
);
I tried php artisan clear-compiled and php artisan dump-autoload just in case. No effect.

Edit: I submitted a fix for this that has been merged into the 4.2 branch. You shouldn't have to worry about this error anymore.
I got it! This appears to be a change in the way Laravel uses the database configuration, I hope this answer helps others.
The short version is: if your connection is configured like mine (in the question), delete the unix_socket entry from the array.
Previously, I always copied and edited the default entries in the connections array, leaving in the unix_socket parameter as empty. Apparently now there's a check that assumes if unix_socket is present, it should use a socket DSN string. The empty string in my config passed the check. You can see how this happens in /vendor/laravel/framework/src/Illuminate/Database/Connectors/MySqlConnector.php. The functions getDsn, getSocketDsn, and getHostDsn tell the story.
Pasted because this will eventually change:
protected function getDsn(array $config)
{
return isset($config['unix_socket']) ? $this->getSocketDsn($config) : $this->getHostDsn($config);
}
...
protected function getSocketDsn(array $config)
{
extract($config);
return "mysql:unix_socket={$config['unix_socket']};dbname={$database}";
}
...
protected function getHostDsn(array $config)
{
extract($config);
return isset($config['port'])
? "mysql:host={$host};port={$port};dbname={$database}"
: "mysql:host={$host};dbname={$database}";
}

Related

CDbConnection failed to open the DB connection Yii after moving to aws

I am new into yii. I was transferring a premade yii website to my aws server. After adding the updated database info into protected/config/main.php I am getting this error. None of the references worked. Please help.
Site Url : http://multilingualbabies.com
Possible Issue:
1) PDO driver maybe its not enabled
2) your dsn connection not truth, make sure your configuration somthing like this:
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=dbName',
'username' => 'root',
'password' => 'yourPAssword',
'charset' => 'utf8',
];
Note: make sure mysql: is set in dsn.
3) make sure your mysql port is 3306, if other one try to change dsn by adding port like this 'dsn' => 'mysql:host=localhost;port=portNumber;dbname=dbName',.
Good Luck

Laravel 5 Testing Database MySQL

In Laravel 4, I could specify the testing database through the config/testing/database file.
This file has, of course, been removed from Laravel 5 just to make things pointlessly more difficult.
So, how do I set up a MySQL test database in Laravel 5.
I do not want to use a SQLite database.
I would suggest some code but there isn't anything on SO or through Google that even attempts to answer this question.
Thank you.
If you check out the documentation Testing - Test Environment this mentions you set environment variables in the phpunit.xml
So for example you would add
<phpunit>
<php>
<env name="DB_CONNECTION" value="sqlite"/>
</php>
</phpunit>
to set the DB_CONNECTION I know you said you don't want to use SQLite but this is just an example you can set any of the values you would normally set in your .env file.
See Using Environment Variables to Add Flexibility to PHPUnit Tests for some more details on the use of environmental variables with PHPUnit.
You could go with the following approach:
Set your default databse connection to 'testing' when the app environment is also 'testing' in your database.php file:
'default' => app()->environment() == "testing" ?
env('DB_CONNECTION_TESTING', 'testing') :
env('DB_CONNECTION', 'mysql'),
Then also in your database.php file add the testing connection:
'testing' => [
'driver' => 'mysql',
'host' => env('DB_HOST_TESTING', 'localhost'),
'database' => env('DB_DATABASE_TESTING', 'forge'),
'username' => env('DB_USERNAME_TESTING', 'forge'),
'password' => env('DB_PASSWORD_TESTING', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
In your .env file you have to add the corresponding lines:
DB_HOST_TESTING=localhost
DB_DATABASE_TESTING=database_testing
DB_USERNAME_TESTING=username_testing
DB_PASSWORD_TESTING=password_testing
So every time the app environment is testing ( which is the case in phpunit tests for example ) your application uses the testing database connection.
Of course you could also modify the phpunit.xml file like mentioned in the answer of Mark Davidson.

Laravel Migration - Says unknown database, but it is created

when I use php artisan migrate, i get the error SQLSTATE[42000] [1049] Unknown database 'databaseName'.
But the database DOES exists! I even tried going back into terminal, logged into mysql and created the database again, and it said that database already exists!
Why is this giving me this error?
I have the same problem. When I run: php artisan migrate.
In my case I use Vagrant with scotch box.
To solve this, enter:
vagrant ssh
cd /var/www/yourproject
php artisan migrate
And all work well.
In your app/config/database.php file change the default value from databaseName to a real database name that you are trying to use in your application, something like this (for mysql driver):
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'your database name', //<-- put the database name
'username' => 'your user name',
'password' => 'your password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
One thing could be your casing. If you read the docs on case sensitivity it says:
the case sensitivity of the underlying operating system plays a part
in the case sensitivity of database and table names. This means
database and table names are not case sensitive in Windows, and case
sensitive in most varieties of Unix. One notable exception is Mac OS
X, which is Unix-based but uses a default file system type (HFS+) that
is not case sensitive.
Then go and check your application database setting found # app/config/database.php.
Something that looks like this:
'mysql' => array(
'read' => array(
'host' => '192.168.1.1',
),
'write' => array(
'host' => '196.168.1.2'
),
'driver' => 'mysql',
'database' => 'databaseName',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
And double check everything.
Also try using snake_case rather than camelCase for you database name.
In my case I had MySQL installed on this port: 3308, and in Laravel env file there was 3306.

DB connection Yii Framework [duplicate]

This question already has answers here:
Database connection error in Yii
(2 answers)
Closed 9 years ago.
I've got a new domain & hosting but I can't connect to the new db.
What I tried:
'db'=>array(
'connectionString' => 'mysql:host=ascodcurro.com.mysql;dbname=ascodcurro_com',
'emulatePrepare' => true,
'username' => 'ascodcurro_com',
'password' => 'xxxx',
'charset' => 'utf8',
),
If I try localhost as always it works fine.
User info from host:
MySQL
server: ascodcurro.com.mysql
DB: ascodcurro_com
user: ascodcurro_com
pw: *****
PhpMyAdmin
PhpMyAdmin: https://dbadmin.one.com/
user: ascodcurro_com
pw: ******
During the initial startup and working with Yii, the first problem that I faced was connecting to the database.
I was getting a really weird error and I was not able to connect to my DB on my Mac OSX Mountain Lion on my XAMPP stack. After countless searches, I finally figured out a method that worked. This is the only way I am able to connect to my DB for Yii.
First check if you can connect to the database normally using the
default configuration by uncommenting the 'db' configuration in the
main.php file. Example :
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=ascodcurro_com',
'emulatePrepare' => true,
'username' => 'username',
'password' => 'pass',
'charset' => 'utf8',
'tablePrefix'=>'',
'enableProfiling'=>true,
'enableParamLogging'=>true,
),
However, if you are still not able to connect to the database even after this, as I wasn't able to connect to mine, try taking this approach :
Make a new php file and type :
<?php phpinfo() ?>
check that page out, find out where 'mysql' is located and find out the 'MYSQL_SOCKET' in it and note the location.
After noting the location down, try this as your 'db' connection string(for example, for me the location is "/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock") :
'connectionString' => 'mysql:unix_socket=/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock;dbname=practice',
And the rest as the basic parameters that you use.
And then try connecting again.
Hopefully, that should connect you to your DB.
Regards,
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=basename',
'emulatePrepare' => true,
'username' => 'username',
'password' => 'pass',
'charset' => 'utf8',
'tablePrefix'=>'',
'enableProfiling'=>true,
'enableParamLogging'=>true,
),
its my config. You get any errors?

CakePHP 2.1 doesn't work on localhost

I deployed my app on a remote host and everything works as expected. But when I try to test my code on localhost, it gives me the following error, without any change to the code working on the host:
Fatal error: Class 'AppHelper' not found in [path]
I am using CakePHP 2.1 and MySQL as my default datasource.
I connect to my local database just like to the remote one (with authentication changes):
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'database',
'prefix' => '',
'encoding' => 'utf8',
);
Why isn't this working on my localhost? Thank you
Two possible things:
either you didnt know about the AppHelper requirement for 2.1:
http://book.cakephp.org/2.0/en/appendices/2-1-migration-guide.html
or you forget to declare the helper at the very top of your class:
App::uses('AppHelper', 'View/Helper');
Although the second one is highly unlikely if you are not running any unit tests.
So my bet is on the first one.