I'm working with MAMP on my local development server on my laravel application and I'm trying to figure out how I can safely setup my server so I don't have to use the following into the database connections mysql array because that should only be used when I'm on my development server. It works when I add the line into the mysql array however that isn't used if I was on a production server. Any ideas?
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
.env.development.php
<?php
return [
'DB_HOST' => '127.0.0.1',
'DB_USERNAME' => 'root',
'DB_PASSWORD' => '1234',
'DB_NAME' => 'mytable'
];
app/config/database.php
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => getenv('DB_HOST'),
'database' => getenv('DB_NAME'),
'username' => getenv('DB_USERNAME'),
'password' => getenv('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
There is even simple solution. add this to ur .env file
DB_HOST=localhost;unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock
On config/database.php:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST'),
'unix_socket' => env('UNIX_SOCKET'),
'port' => env('DB_PORT'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
On .env:
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mytable
DB_USERNAME=root
DB_PASSWORD=1234
UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
Check the environment detection part in the bootstrap/start.php. You should add your machine's name to the array that has local key. (If you don't know your machine's name, run hostname in terminal. If it's something stupid, Google how to change it. It's pretty simple.) Then copy and paste your database configurations to app/config/local/database.php. Create the file if it doesn't exists.
Make sure MAMP preference is set to Apache port: 80, Nginx Port: 80, MySQL Port: 3306
Here's what worked for me with Laravel 5.7:
go to config/database.php and find the line 54 below:
before:
'unix_socket' => env('DB_SOCKET', ''),
After:
'unix_socket' => env('DB_SOCKET', '/Applications/MAMP/tmp/mysql/mysql.sock'),
Save the file.
Then in terminal run:
php artisan config:cache
php artisan migrate
If none of the above solutions worked for you,
Try actually starting your webserver as this was the fix for me
Related
Hi I am using Laravel 5 with homestead. I am experiencing homestead first time so facing problem. I have created a database 'myDb' and imported data into this. I have installed vagrant and homestead, cloned my code and configured homestead.yaml file accordingly. I have set up my .env. When I try to run my project is is giving me unknown database myDb error. I can see my database in my phpmyadmin but I do not know why it is giving me this error. My .env file looks like
DB_HOST=localhost
DB_DATABASE=myDb
DB_USERNAME=homestead
DB_PASSWORD=secret
And I have same settings in my config/database.php too.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'myDb'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]
Its really making me mad. What Am I doing wrong? I am getting following
in Connector.php line 55
at PDO->__construct('mysql:host=localhost;dbname=myDb', 'homestead', 'secret', array('0', '2', '0', false, '0')) in Connector.php line 55
at Connector->createConnection('mysql:host=localhost;dbname=myDb', array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'myDb', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql'), array('0', '2', '0', false, '0')) in MySqlConnector.php line 22
at MySqlConnector->connect(array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'myDb', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql')) in ConnectionFactory.php line 60
at ConnectionFactory->createSingleConnection(array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'myDb', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql')) in ConnectionFactory.php line 49
One common mistake that you might be making is that you're probably accessing MySQL server on your localhost while you should remember that Homestead is a complete virtual dev. environment. It has it's own Web Server, MySQL server and etc.
To see if you have "myDB" on the Homestead MySQL server as well, try accessing homestead via the following command.
homestead ssh
Once done that, try opening the mysql console via the mysql command.
mysql -u username -p
And after that list all your databases using
show databases
If you can't see "myDb" database there, than you should try connecting to Homestead MySQL server using Navicat or MySQL Workbench and move your database from your local MySQL server to the Homestead MySQL server.
That would probably fix your problem.
If you are migrating your database, check if you are migrating the same database name inside your Homestead.yaml file and the database name in your .env file.
Inside Homestead.yaml file:
databases:
- laravel
Inside your .env file:
DB_DATABASE=laravel
I have two database configs, one for production and one for development:
// app/config/database.php
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => $_SERVER['RDS_HOSTNAME'],
'database' => $_SERVER['RDS_DB_NAME'],
'username' => $_SERVER['RDS_USERNAME'],
'password' => $_SERVER['RDS_PASSWORD'],
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
)
// app/config/development/database.php
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => $_SERVER['MYSQL_PORT_3306_TCP_ADDR'],
'database' => $_SERVER['MYSQL_ENV_MYSQL_DATABASE'],
'username' => $_SERVER['MYSQL_ENV_MYSQL_USER'],
'password' => $_SERVER['MYSQL_ENV_MYSQL_PASSWORD'],
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
)
The relevant database environment variables exist (the ones beginning with MYSQL_), and when running the migrate command:
php artisan migrate --env=development
the following exception gets thrown:
{
"error":{
"type":"ErrorException",
"message":"Undefined index: RDS_HOSTNAME",
"file":"/var/www/app/config/database.php",
"line":50
}
}
Why does Laravel care if the environment variable in my production config doesn't exist when I don't even want to use the production configuration? How do I get around this?
This problem can happen when in your .env file you have the following
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=test_db
DB_USERNAME=root
DB_PASSWORD=
Instead, you should have
RDS_CONNECTION=mysql
RDS_HOSTNAME=localhost
RDS_PORT=3306
RDS_DB_NAME=test_db
RDS_USERNAME=root
RDS_PASSWORD=
The difference is in the name of the keys. In addition to that, this might be of interest in case that problem is related with bash and AWS.
My suggestion would be to use environment variables instead, and make sure the keys match between environments (I think that's your biggest issue).
// File: .env.development.php
return [
'database_mysql_host' => '',
'database_mysql_database' => '',
'database_mysql_username' => '',
'database_mysql_password' => '',
];
Then you can remove both of your config files and just modify app/config/database.php:
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => $_ENV['database_mysql_host'],
'database' => $_ENV['database_mysql_database'],
'username' => $_ENV['database_mysql_username'],
'password' => $_ENV['database_mysql_password'],
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
)
I'm assuming for the moment that you're setting the environment variables on the production server in the vhost or some other apache configuration file. Change those keys where they are being set from the 'special' ones you picked out for your production environment to match the keys you're using in your development environment:
SetEnv database_mysql_host {your value for RDS_HOSTNAME}
SetEnv database_mysql_database {your value for RDS_DB_NAME}
SetEnv database_mysql_username {your value for RDS_USERNAME}
SetEnv database_mysql_password {your value for RDS_PASSWORD}
I've installed laravel 5 successfully by using this command:
composer create-project laravel/laravel test-laravel-5-project dev-develop --prefer-dist
I even verified the version of installed laravel by using php artisan -V command. The output was
Laravel Framework version 5.0-dev
Then I went to app/config/database.php, gave dafault db as mysql and gave configurations as
'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), 'database' => env('DB_DATABASE', 'Logintestfive'), 'username'=> env('DB_USERNAME', 'root'), 'password'=> env('DB_PASSWORD', 'manasa'), 'charset'=> 'utf-8', 'collation'=> 'utf-8_unicode_ci', prefix=> '', 'strict'=> false, ]
Then I went to localhost:8000/auth/register and filled up the form and submitted the data and this is the error which I got:
PDOException in Connector.php line 47: SQLSTATE[28000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES)
But I've neither used laravel homestead for installng laravel 5 in my system nor used vagrant to set up laravel homestead. And it tells me like this:
in Connector.php line 47
at PDO->__construct('mysql:host=localhost;dbname=homestead', 'homestead', 'secret', array('0', '2', '0', false, '0')) in Connector.php line 47
at Connector->createConnection('mysql:host=localhost;dbname=homestead', array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql'), array('0', '2', '0', false, '0')) in MySqlConnector.php line 20
at MySqlConnector->connect(array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql')) in compiled.php line 10545
at ConnectionFactory->createSingleConnection(array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql')) in compiled.php line 10541
at ConnectionFactory->make(array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false), 'mysql') in compiled.php line 10459
How can I fix those issues?
I got a similar problem. I started my server using php artisan serve command, and edited the .env file and refreshed the web-page which did not reflect any changes!
I fixed it by stopping the webserver, editing the .env.php file and restarting the webserver!
So I guess it's a caching-issue of the .env.php file.
Laravel 5.1.10
As the answers already given above by Ganesh and Yordi, But i'm combining the both of them into single one.
Issue:
When i try to create a database in phpmyAdmin and want to create schema/table through Laravel Schema builder service i got the following errors:
PDOException in Connector.php line 50: SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES)
in Connector.php line 50
at PDO->__construct('mysql:host=localhost;dbname=homestead', 'homestead', 'secret', array('0', '2', '0', false, '0'))
in Connector.php line 50
at Connector->createConnection('mysql:host=localhost;dbname=homestead', array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql'), array('0', '2', '0', false, '0')) in MySqlConnector.php line 22
at MySqlConnector->connect(array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql')) .............
Solution 1:
Then I realised that the issues were in the env file, I forgot to change the values in the env file. The .env file is located in the laravel 5 root directory
open the file and change the following values with your ones
DB_DATABASE = localhost,
DB_USERNAME = databse-username,
DB_PASSWORD = databse-user-password
Solution 2:
Or change in the database.php file without env() as Ganesh syas:
// instead of this block
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'your database name'),
'username' => env('DB_USERNAME', 'your database username'),
'password' => env('DB_PASSWORD', 'your database password'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
// use this one
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'your database name',
'username' => 'your database username',
'password' => 'your database password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]
The both solutions worked for me.
Laravel is using the variables contained in your .env file.
From: http://laravel.com/docs/master/configuration
It is often helpful to have different configuration values based on the environment the application is running in. For example, you may wish to use a different cache driver locally than you do on your production server. It's easy using environment based configuration.
Laravel utilizes the DotEnv PHP library by Vance Lucas. In a fresh Laravel installation, the root directory of your application will contain a .env.example file. If you install Laravel via Composer, this file will automatically be renamed to .env. Otherwise, you should rename the file manually.
All of the variables listed in this file will be loaded into the $_ENV PHP super-global when your application receives a request. You may use the env helper to retrieve values from these variables. In fact, if you review the Laravel configuration files, you will notice several of the options already using this helper!
Feel free to modify your environment variables as needed for your own local server, as well as your production environment. However, your .env file should not be committed to your application's source control, since each developer / server using your application could require a different environment configuration.
If you are developing with a team, you may wish to continue including a .env.example file with your application. By putting place-holder values in the example configuration file, other developers on your team can clearly see which environment variables are needed to run your application.
The default .env file looks something like:
APP_ENV=local
APP_DEBUG=true
APP_KEY=YOUR_KEY_HERE
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
CACHE_DRIVER=file
SESSION_DRIVER=file
I got the same problem. After I use the command "php artisan config:clear" to clear the old configuration cache file, it can work. Maybe you can try it.
By the way, my English is not good, hope you can understand.
Laravel is using .env file so change the .env file or change in the database.php file without env()
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'your database name',
'username' => 'your database username',
'password' => 'your database password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]
In Config->database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'DBName'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
In .env file :
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=DBName
DB_USERNAME=root
DB_PASSWORD=
Close the laravel application server and restart again for clearing the cache.
php artisan serve
Or you can do :
php artisan config:clear
This will clear the cache in config files.
Now it will definitely work! Cheers!
Laravel config/database.php uses the .env file for database name,username asnd password, Please change your .env file
DB_HOST=localhost
DB_DATABASE=my_project
DB_USERNAME=root
DB_PASSWORD=myproject
After changing this run the below command:
composer dump-autoload
then run your project
php artisan serve
I had a very similar issue and didn't know where to look. I cleared the cache, restarted server, etc., no joy. Until I found it...
Before I reverted to a previous version on the Git repo, I used to have my session driver set to use the database! Silly thing, but it's not necessarily the database connection what you need to look at but instead where you are invoking the database to be used at the first place.
I hope that somebody who'll end up on this page the way I did will find this helpful.
i have same issui, edit file .env on root folder and change database config DB_DATABASE DB_USERNAME DB_PASSWORD
Inside your laravel eg. c:/www/laravel, create a new directory called 'local' inside app/config.Copy database.php (app/config/database.php).This works for me
Yes i had the same issues where everything went well right from migration installation to php artisan migrate. But when i tried to create a user the access denied.
Solution i found was to close the ctrl + d to terminate the server and restart. I worked... :)
you can restart your server
I think is a catch error in Laravel so run your server again with "php artisan serve" and refresh your website page IT's OK.
for me
sudo service apache2 reload
did the job
I created a database "mydatabase" and I changed config>database.php to:
'mysql' => array(
'driver' => 'mysql',
'host' => 'mysite.local',
'database' => 'mydatabase',
'username' => 'myusername',
'password' => 'mypassword',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
now inside route.php i have:
Route::get('/', function()
{
$data=DB::table('user')->get();
return $data;
});
laravel sends an Exception which shows that it tries to access:
homestead.user
instead of
mydatabase.user
now if i change route.php to:
Route::get('/', function()
{
$data=DB::table('mydatabase.user')->get();
return $data;
});
it will work!
Also according to this question I changed config>local>database.php to:
'mysql' => array(
'driver' => 'mysql',
'host' => 'mysite.local',
'database' => 'mydatabase',
'username' => 'myusername',
'password' => 'mypassword',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
But this time, even
$data=DB::table('mydatabase.user')->get();
doesn't work either! This time it thrown another exception :
PDOException (2002)
SQLSTATE[HY000] [2002] Connection refused
My question is why laravel tries to use "homestead" database instead of "mydatabase"? should I change something else?
EDIT:
I changed the config/local/database.php to
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'mydatabase',
'username' => 'myusername',
'password' => 'mypassword',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
and everything works fine! (I changed mysite.local to localhost)
I've not define local host in my /etc/hosts so why laravel looks for that host?
Your host should be localhost. The term localhost means the computer which laravel is running on. mysite.local is presumably a virtual site residing on this computer. It doesn't have its own installation of Mysql. All virtual sites will share the same mysql. They will just use different databases.
Thats how my setups work anyway.
The problem is in your config/database.php with default connection, currently default connection setting is getting from .env file as
'default' => env('DB_CONNECTION', 'mysql'),
So change it to :
'default' => 'mysql',
php artisan migrate:install
{"error":{"type":"ErrorException","message":"PDO::__construct(): [2002] Connection refused (trying to connect via tcp:\/\/127.0.0.1:3306)","file":"\/Applications\/MAMP\/htdocs\/DRCSports\/vendor\/laravel\/framework\/src\/Illuminate\/Database\/Connectors\/Connector.php","line":47}}
In my database.php I have updated the information to mysql
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'Laravel_DRCSports',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
I am not sure if I am understanding the error right, but to me it looks like my laravel isn't connecting to mysql right. If that is the case I have no clue how to fix it.
The problem was that mysql is running on port 8888, while Laravel's default port value is 3306 (as it's the default port of mysql servers).
The solution is to add 'port' key to the array (For example: 'port' => 8888) and it'll
do the work.
This is what i did... in /app/config/app.php
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost:8889',
'database' => 'pic',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
and at the bottom of the php code
'redis' => array(
'cluster' => false,
'default' => array(
'host' => '127.0.0.1',
'port' => 8888,
'database' => 0,
),
),
it has to work it...
I experienced problems (Laravel 4) when I used MySQL on a port other than 3306.
A browser-run app expects the following app/config/database syntax:
'mysql' => array(
...
'host' => 'localhost',
'port' => '8889',
...
)
While the command-line run artisan expects the following syntax:
'mysql' => array(
...
'host' => 'localhost:8889',
...
)
The issue is described here:
https://github.com/laravel/laravel/issues/1182
Most articles suggest a workaround using Laravel environments, but it results in duplicate config files and violates the DRY principle (Don't Repeat Yourself), so here's another alternative:
At the top of app/config/database.php:
$my_hostname = 'localhost';
$my_port = '8889';
$my_database = 'database';
$my_username = 'username';
$my_password = 'password';
if (App::runningInConsole()) { // artisan runs from the command line
// change 'localhost' to 'localhost:8889'
$my_hostname = $my_hostname.':'.$my_port;
}
And further down:
'mysql' => array(
'driver' => 'mysql',
'host' => $my_hostname,
'port' => $my_port,
'database' => $my_database,
'username' => $my_username,
'password' => $my_password,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
Cheers
Change your database information in your config/database.php and .env file.
I did these changes and worked like a champ :
in database.php :
host : localhost:8889
Port: 8889
and my mamp has a password so I there was two way to putting that password either in database.php file or in .env file I changed the password '' value to 'forge' and then use my MAMP password in the .env file
by the way, you can see your specific information about MAMP in MAMP application in the port tab (MySQL one)
Make sure to edit this part of ".env" file.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD=root
This worked for me.
i guess you can solve this problem by add the following code inside app/config/database syntax:
'mysql' => array(
...
'pconnect' => 'TRUE',
...
)