Laravel 5.3 wont connect to mysql - php

I created a new user on my Macbook Air (OS X Version 10.11.2). The new user has full administrator access. I'm using MAMP. The MySQL port is set to 3306. I can connect to MySQL with everything else just fine. All of my Laravel projects stopped connecting to MySQL. When i try to use
php artisan migrate
I get:
[PDOException]
SQLSTATE[HY000] [2002] Connection refused
Here is the database portion of my .env:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=store
DB_USERNAME=clerk
DB_PASSWORD=inventory
I have tried using the root user and password. I have tried using different ports. I have tried switching the host to localhost. I have verified that the username and password are correct, and that the user has access to the database.
Here is the MySQL portion of my config/database.php file:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'store'),
'username' => env('DB_USERNAME', 'clerk'),
'password' => env('DB_PASSWORD', 'inventory'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
I have been searching for a few days, and i can't seem to find the answer. My old Laravel 5.2 projects won't connect either. I'm pretty sure that this was caused by creating a new user, but i don't know enough about MySQL to figure out the problem.

Having followed troubleshooting processes as I've suggested in comments below your question, all should be working as expected.
Should that not be the case, you must be missing something; ... a PATH issue I presume.
I suggest you export your database, uninstall your MySQL, then download a new one at here.
Follow the approach elaborated here for a possible fix.
With this, your challenge will surely be addressed.

Try changing your db_host = localhost:3306

You define an engine = null. The engine is used inside the MySqlGrammar.php specifically when compiling a create query as far as I know. I have only seen null engines for VIEWS.
Try the following config:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'store'),
'username' => env('DB_USERNAME', 'clerk'),
'password' => env('DB_PASSWORD', 'inventory'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
You did mention it in your question and take it that the following works from a terminal
mysql -uclerk -pinventory -h127.0.0.1 -P3306 store
Could be that it needs to be
mysql -uclerk -pinventory -hlocalhost -P3306 store
Something else you can check with just an info.php file in your project to double check that everything seems fine with the reference to PDO as a module.
<?php
phpinfo();
Then also play around with just a little PDO php script to see if your connection is working properly. Something like:
<?php
$servername = "127.0.0.1";
$username = "clerk";
$password = "inventory";
try {
$conn = new PDO("mysql:host=$servername;port=3306;dbname=store;", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
Something else you can also check inside a dummy route what env('DB_DATABASE') etc gives you.
Updated things to check 2016.09.15
To make sure that env() is fetching the correct information include the route below and then include the output of the following:
Route::get('dbtest', function () {
$config = [
'host' => env('DB_HOST'),
'port' => env('DB_PORT'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
];
dd($config);
});
Then also access mysql as the root user and include the output of the select statement:
mysql> select host, user, password, authentication_string, password_expired from mysql.user;
As you are using MAMP make sure to use the mysql commandline tool from /Applications/MAMP/Library/bin/mysql

It's been solved. After a few hours of pulling my hair out, i decided to take #nyedidikeke advice and re-install mysql. I was using MAMP, and I have been wanting to stop using it for a while. I uninstalled it, and installed mysql through Homebrew. Everything is working just fine, thanks for all your help!

Related

Remote Connection to Mysql Server From Laravel Application

I am trying to connect a laravel application to a remote database.The connection works successfully on my local machine but does not work in the production environment.The laravel application is connected to its own database hosted in the cloud but i want to access another database hosted by another provider.Hence,I did the following:
In Config/database file in laravel folder,I added another mysql connection
config the .env file as well.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'mysql3' => [
'driver' => 'mysql',
'host' => env('DB_HOST_THIRD','localhost'),
'port' => env('DB_PORT_THIRD'),
'database' => env('DB_DATABASE_THIRD',''),
'username' => env('DB_USERNAME_THIRD',''),
'password' => env('DB_PASSWORD_THIRD',''),
],
Then in .env file:
DB_CONNECTION=mysql3
DB_HOST_THIRD=remote IP
DB_PORT_THIRD=3306
DB_DATABASE_THIRD=Remote DB Name
DB_USERNAME_THIRD=Username
DB_PASSWORD_THIRD=password
With the above setting, i can connect to the remote database successfully on my local machine.
Upon deployment, I am unable to connect to this remote database,even though a designated user has been created and access granted through the control panel.This remote database is hosted by GoDaddy and i have called but they said nothing is blocking my application, that everything is ok from their end. Below are what I have done:
I am able to connect the remote Database using Sqlyog, Mysql client successfully.
I connected to Terminal on the Remote Cpanel and try to grant all access to the User at its IP but the response is: "Access denied for user 'username#Remote IP'(using password:YES)".
From web application, the error is : "SQLSTATE[HY000] [2002] Connection refused".
I have telnet the remote IP with port 3306, which respond well. Please, can someone help me, perhaps there are things i am missing or is this kind of issue peculiar to Godaddy shared hosting?. I have checked the following from Stackoverflow:
Enable remote MySQL connection: ERROR 1045 (28000): Access denied for user

Laravel / MySQL Error: SQLSTATE[HY000]: General error: 1835 Malformed communication packet

Good morning. I'm using Laravel on VPS server.
Short situation description: Yesterday everything was working fine (for months our website were working fine), this morning I got woke up from my colleagues that both of our websites are down.
When trying to access them we receive error:
SQLSTATE[HY000]: General error: 1835 Malformed communication packet (SQL: select * from users where id = 1 limit 1)
I have checked online and can't find a solution. I tried to upgrade MySQL to newest version (Maria DB 10.3)
I tried to reset password for database user. (Also no changes)
I checked and tried sollution to set read_rnd_buffer_size=256K in my.cnf file for mysql settings
When I try to call this function directly in phpMyAdmin select * from users where id = 1 limit 1 it returns expected results.
I will appreciate every help, as all of our business depends on these platforms, I need to make them work as soon as possible.
With greetings, Artis.
Edit:
When I try to disable function that causes error, it just shows next function, and all over like that. So I believe that Laravel can't conect with mysql at all.
After deeper research I found out that only Laravel can't connect to database. On same server I have 2x Laravel applications, Codeigniter and wordpress. Both Laravel applications stopped to work at same time, but codeigniter and wordpress works as expected.
for a quick fix just add this
'options' => [PDO::ATTR_EMULATE_PREPARES => true]
to config/database.php in
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
'options' => [PDO::ATTR_EMULATE_PREPARES => true],
],
the proper solution is to upgrade your php from 7.2 to 7.4 or downgrade your mariadb
The new update requires PHP 7.3 to connect with MariaDB.
Since the issue is caused by the latest upgrade from MariaDB MDEV-24121, a more suitable solution is to downgrade MariaDB and yum-locking the MariaDB packages in place to avoid the packages from being updated and unlock them when they're patched. More details here on how to do it: Updating MariaDB to v10.2.35 or v10.3.26, causes MySQL Databases interface to show MySQL as offline
If you cannot downgrade your MariaDB or cannot update your PHP, a possible workaround is to set "PDO::ATTR_EMULATE_PREPARES" to true.

Laravel app hosted on Heroku with a Amazon RDS MySQL Database results in a connection time out

I have a Heroku app where I'm hosting my Laravel app. I started the development initially with MySQL, so I wanted to continue doing so using Amazon's RDS service. I create the instance there and managed to successfully connect via my MySQL client, the console etc.
The problem is that the Laravel app can't connect the database after numerous desperate attempts for me to fix it. I have found some articles suggesting the use of DATABASE_URL environment variable is mandatory, so I added it via the Heroku app settings. It looks like so:
mysql://myusername:mypass#myhostnamefromamazon/mydb?sslca=/app/storage/certs/amazon-rds-ca-cert.pem
I found this solution on Heroku's website. I have placed the amazon-rds-ca-cert.pem file on my Laravel's storage folder, like so: /app/storage/certs/amazon-rds-ca-cert.pem
This didn't solve my issue, so then I kept looking and found a Stackoverflow question which had this issue on Lumen. I adjusted my config/database.php according to the answer, but it's still not working for me!
<?php
$credentials = get_db_credentials();
$config = [
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', $credentials->host),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', $credentials->database),
'username' => env('DB_USERNAME', $credentials->username),
'password' => env('DB_PASSWORD', $credentials->password),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],
];
if (env('APP_ENV') == 'production') {
$config['connections']['mysql']['options'] = [PDO::MYSQL_ATTR_SSL_CA => '../storage/certs/amazon-rds-ca-cert.pem'];
}
return $config;
The get_db_credentials() function simply parses the DATABASE_URL environment variable.
The exact exception that I get is:
[2018-10-25 19:32:16] production.ERROR: SQLSTATE[HY000] [2002] Connection timed out {"exception":"[object] (Doctrine\\DBAL\\Driver\\PDOException(code: 2002): SQLSTATE[HY000] [2002] Connection timed out at /tmp/build_05920c42a6de0a378402b798320d3f04/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:50
I'm totally lost on this and unsure how to proceed.
Your AWS Security Groups must permit traffic from Heroku's IP address range.
https://devcenter.heroku.com/articles/amazon-rds
You must grant Heroku dynos access to your RDS instance. The recommended way to do this is to configure the RDS instance to only accept SSL-encrypted connections from authorized users and configure the security group for your instance to permit ingress from all IPs, eg 0.0.0.0/0.

Phalcon, Docker MySQL connection fails

So I have 3 docker containers, one with PHP+PhalconPHP, one with MySQL and another with the webserver. Now on my host system I can connect to the MySQL in the docker container and I can also connect with PDO using the following code
new PDO('mysql:host=mysql;port=1024;dbname=database', 'root', 'password');
But for some reason the phalcon framework is unable to connect to the MySQL database using the config.php file like so
return new \Phalcon\Config([
'database' => [
'adapter' => 'mysql',
'port' => 1024,
'host' => 'mysql',
'username' => 'root',
'password' => 'password',
'dbname' => 'database',
'charset' => 'utf8',
],
]);
In this case, for some reason I am getting a Can't connect to MySQL server on 'mysql' (111 "Connection refused) even though I am 100% sure I can actually connect (as proven before with the PDO connection). I also tried hardcoding the actual MySQL container ip address but no luck either.
Any ideas?
So it appeared to be kind of a stupid "mistake" but here's the solution.
Apparently when you use the Phalcon Developer Tools to scaffold your project, it does not initialize the database with the port configuration as it outright ignores it. You can easily fix this by going into the app/config/services.php file and changing the db service to also include the port configuration.
$connection = new $class([
'host' => $config->database->host,
'port' => $config->database->port,
'username' => $config->database->username,
'password' => $config->database->password,
'dbname' => $config->database->dbname,
'charset' => $config->database->charset
]);

Forge not reading my Laravel 5 environment variables

This should be really easy, but it's not. Here is the default settings for a MySQL database in Laravel 5.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
I ran php artisan env on the production server and got production. I set my environment variables with the correct keys on Forge. But when I run artisan on the production server, I get an error: 'Access denied for user 'forge'#'localhost' (using password: NO)' The reason I think it's saying password: NO is that an empty string is the default for DB_PASSWORD if the environment variable is not set.
I think what is happening is that Forge is not reading my environment variables and is using the defaults specified. If I change the values of my environment variables, I get the same error message, which seems to confirm this. I have restarted the Digital Ocean production server, thinking that would force the server to read the variables. But nada. Do I need to do something else in Forge to get it to read the variables. Jeffrey Way's Laracast doesn't indicate that I need to.
What's going on here?
Laravel 5 is not yet in beta.
Taylor has already mentioned in a recent podcast that he'll need to make changes to Forge to accommodate Laravel 5 environmental settings.
I think from memory he mentioned the ability to just edit the .env file from Forge.
For now - you should just put all your settings into a .env file in the root of your web server and it will work.
In case anyone else finds this and is still experiencing it, the .env on both Laravel 5 and Forge now work. You must however make sure your database credentials are correct. This should work for most of the defaults as of the writing of this post (Laravel 5.2)
DB_HOST=localhost
DB_DATABASE=database_name
DB_USERNAME=forge
DB_PASSWORD=the_provided_password

Categories