Laravel Can't Connect to database - Migrations - Error 2002 - mysql

I have searched for a couple of hours now, and still am unable to find this.
I get 2 errors, if I use the database host as 'localhost', I get this error:
[PDOException]
SQLSTATE[HY000] [2002] No such file or directory
and if I change the database host to '127.0.0.1' I get this error:
[PDOException]
SQLSTATE[HY000] [2002] Connection refused
Things I have tried:
changing where the apache / mysql server is run (either user or `josh (Apache) / josh (MySQL)
changing the port that MySQL runs on in MAMP, and putting that port in the mysql array in the database.php file
changing the host of the connection from localhost to 127.0.0.1 and back.
creating a new user in phpmyadmin
turning off the firewall
Any ideas how to fix this?

I figured it out, add this after the 'host' => '127.0.0.1':
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock'
So the connection would look like this:
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'database' => 'dbname',
'username' => 'josh',
'password' => 'pass',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)

fist clear your config file by typing
php artisan config:clear
Then modify your .env file
APP_URL = 127.0.0.1
DB_HOST = 127.0.0.1
Hope this works for you.

IF you use lampp . You can try this:
Open file mysql config. (you can open lampp control panel in /opt/lampp/manager-linux-x64.run,after that open Configure of Mysql Database or open /opt/lampp/etc/my.cnf).
Find and see "socket =/opt/lampp/var/mysql/mysql.sock",
Add 'unix_socket' => '/opt/lampp/var/mysql/mysql.sock' to file database.php in laravel:
'mysql' => [
'driver' => 'mysql',
'unix_socket' => '/opt/lampp/var/mysql/mysql.sock',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'luanvan'),
'username' => env('DB_USERNAME', 'huuthang'),
'password' => env('DB_PASSWORD', '123456'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
File mysql.sock depend on your install location of lampp. I let you see if you can't find it. Good luck

I had to do the following in ubuntu 15.10
copy the file or file contents from /opt/lampp/etc/my.cnf to /etc/mysql/my.cnf
Then open database.php file in your app/config folder and add the following line below 'host' => env('DB_HOST', 'localhost')
'unix_socket' => '/opt/lampp/var/mysql/mysql.sock'
That solved my problem, could save someone else's. Please note that it is VERY IMPORTANT you first verify the directory which contains mysql.sock file before configuring the path.

Related

Connect external Database to Laravel Vapor

I need a little help connecting an external MySQL database to Laravel Vapor. The database is located on a Hetzner Server and it seems like there is a failure using a tls encrypted connection:
==> Executing Function...
Status Code: 1
Output:
In Connection.php line 712:
SQLSTATE[HY000] [2002] (SQL: SELECT * FROM KURSE_planung )
In Exception.php line 18:
SQLSTATE[HY000] [2002]
In PDOConnection.php line 40:
SQLSTATE[HY000] [2002]
In PDOConnection.php line 40:
PDO::__construct(): SSL operation failed with code 1. OpenSSL Error message
s:
error:1416F086:SSL routines:tls_process_server_certificate:certificate veri
fy failed
I already tried to disable SSL by using the following params in the database URL without any luck:
MYSQL_DATABASE_URL=mysql://username:password#sql168.your-server.de/databasename?charset=utf8mb4&sslmode=disabled&ssl-mode=disabled&useSSL=false
Does anyone know how to disable TLS when connecting to the database or what else I can do about it?
Edit:
I managed to connect to the database. My Hoster Hetzner provides a certificate which needs to be provided to the connection configuration in database.php:
'mysql' => [
'driver' => 'mysql',
'url' => env('MYSQL_DATABASE_URL'),
'host' => env('MYSQL_DB_HOST', '127.0.0.1'),
'port' => env('MYSQL_DB_PORT', '3306'),
'database' => env('MYSQL_DB_DATABASE', 'forge'),
'username' => env('MYSQL_DB_USERNAME', 'forge'),
'password' => env('MYSQL_DB_PASSWORD', ''),
'unix_socket' => env('MYSQL_DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => base_path(env('MYSQL_ATTR_SSL_CA')),
]) : [],
],
Then in the .env file one needs to set the correct path to the certificate using MYSQL_ATTR_SSL_CA.
One question remains: does anyone know how to disable SSL for MySQL on Vapor?
Best regards
Clemens
I don't know if this might help. But In my case Laravel Vapor was hosted with RedHatOS, then I defined the env with the following:
MYSQL_ATTR_SSL_CA=/etc/pki/tls/certs/ca-bundle.crt
In my case, the database is hosted on planetscale this configration a specified on the documentation

Laravel 5 on digitalocean: cannot connect to database

I have this in env file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE={db_name}
DB_USERNAME=root
DB_PASSWORD={db_password}
I config/database.php
'default' => env('DB_CONNECTION', 'mysql'),
'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', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false, //true is default
'engine' => null,
],
That's how I got the password:
cat /root/.digitalocean_password
But I get
SQLSTATE[HY000] [1698] Access denied for user 'root'#'localhost' (SQL: select * from `users` where `email` ....
I even cleared the password value in env file but ti doesn't work too. Any help please? Thanks in advance.
To backtrace your issue. try to connect via SSH on your database server . then try to do this
$ sudo mysql -uroot -p
$ password : ******** (enter your password here)
If you were able to connect, probably you shouldn't have any problem on your app level.
second option is : try to use host "localhost" instead of "127.0.0.1" as your root username might only pointed to #localhost instead of #127.0.0.1
Here's the solution I got:
I had to create another user rather than root and add all permissions to that new user which will sure have a new password. I used that username and password in .env file for database connection and it worked fine.

Laravel 5 and remote mysql not working on production

In my local environment I have working a secondary remote mysql connection specified in config/database.php without any problems:
# Secondary database connection
'mysql_remote' => [
'driver' => 'mysql',
'host' => env('DB_HOST_2', 'cherokee.websitewelcome.com'),
'port' => env('DB_PORT_2', '3306'),
'database' => env('DB_DATABASE_2', 'dbname'),
'username' => env('DB_USERNAME_2', 'dbusr'),
'password' => env('DB_PASSWORD_2', 'dbpass'),
'unix_socket' => env('DB_SOCKET_2', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
But when I run this on production server with the exact same database file I get the following error, is like the remote host (cherokee.websitewelcome.com) is not used and replace it with local server host (eu01.server.plus)
SQLSTATE[HY000] [1045] Access denied for user 'dbusr'#'eu01.server.plus' (using password: YES) (SQL: select ...)
Any ideas?
Solved by adding eu01.server.plus to authorized hosts on remote mysql server. Initially I was adding the IP and not hostname.

Laravel 5.1 - Connecting to MySQL Database (MAMP)

There are topics online that are discussing this problem however, I couldn't find any tidy explanation of the problem or any solid answers for the question. What I am trying to achieve is connecting Laravel 5.1 to MySQL Database of MAMP.
In my config>app.php:
'default' => env('DB_CONNECTION', 'mysql'),
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost:8889',
'database' => 'test',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'prefix' => '',
'strict' => false,
],
In my .env:
DB_HOST=localhost
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=root
I also have .env.example: (which I believe has no functionality)
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
I also have create_users_table.php and create_password_resets_table.php in my database>migrations (even though I did not run any migration:make)
MAMP is directing and running the server successfully as it loads the project on localhost.
Here is my MAMP settings:
And the test database is created (with tables in it which I have previously created and used in my other projects, not Laravel.)
Even though everything seems correct to me, when trying to submit Auth form, I am getting this error:
PDOException in Connector.php line 50:
could not find driver
in Connector.php line 50
at PDO->__construct ('mysql:unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock;dbname=test', 'root', 'root', array('0', '2', '0', false, false)) in Connector.php line 50
at Connector->createConnection('mysql:unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock;dbname=test', array('driver' => 'mysql', 'host' => 'localhost:8889', 'database' => 'test', 'username' => 'root', 'password' => 'root', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock', 'prefix' => '', 'strict' => false, 'name' => 'mysql'), array('0', '2', '0', false, false)) in MySqlConnector.php line 22
and so on...
On mac or unix you have to include the socket path in the configuration database.php file
i.e 'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
It was pretty simple for me, I added :8889 to the localhost in the .env file.
DB_HOST=localhost:8889
This is because in the MAMP preferences, :8889 is the default port.
The most important thing for me was defining the UNIX socket. Because I have another MYSQL on my machine - Laravel was trying to connect to a database in that MYSQL process.
Defining the UNIX for the MAMP database to be used worked perfectly. Try adding this to your MYSQL configuration in database.php
'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' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
As far as I am concerned it doesn't make any sense to set in database.php as many of them suggested.
Since this change would be mostly required in the development mode. So the proper way of setting the unix_socket is as below
file: .env
DB_SOCKET='/Applications/MAMP/tmp/mysql/mysql.sock'
By doing the above way already .env is included in .gitignore and won't create any other problem while your project is remotely deployed.
NOTE: I have tested this setting in Laravel 5.7 and above versions
Found my answer. Here is a way to fix it:
Start MAMP
On the top left, go to "MAMP" -> "Preferences"
Go to the "PHP" tab
Tick PHP 5.5.17 (or whatever you have) instead of the one which is ticked by default (5.6.1 -> 5.5.17 with he latest version of MAMP)

Laravel; "SQLSTATE[HY000] [2002] Connection refused"

I set up homestead 2.0 on OSX host and using Sequel Pro.
I can do migration and confirm that data are migrated in Sequel Pro, so it looks like no problem about DB connection.
But once I try to fetch data from Laravel 4.2 app, it fails to retrieve data and get following error.
PDOException (2002)
SQLSTATE[HY000] [2002] Connection refused
Here is my mysql configuration.
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => '33060',
'database' => 'homestead',
'username' => 'homestead',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
Sequel pro setting is same above,
Name: 127.0.0.1
Host: 127.0.0.1
Username: homestead
Password: secret
Database:
Port: 33060
I tried change host name to 'localhost', homestead destroy and up, but No work.
Why?? Any suggestion helps.
I had exactly the same issue and I'm not sure why this happens.
However changing
'host' => '127.0.0.1',
to
'host' => 'localhost',
worked for me. Always simpler than what we expect!
For other devs there who are running Laravel on it's latest version (5.3) I just change these lines:
DB_HOST=127.0.0.1
DB_PORT=3306
Into this:
DB_HOST=localhost
DB_PORT=33060
Obviously, 33060 will be converted into 3306 but can't explain by now why localhost should work.
I solved the problem substituting the address 127.0.1.1 to localhost on the host parameter in the Laravel's .env file. I am working over GNU/Linux Debian 8, Laravel 5.6.3, PHP 7.1.8, Apache 2.4.27 and (Mysql) 10.1.26-MariaDB.
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'homestead',
'username' => 'homestead',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
Try this out it might work for you
I encountered the same problem for some reason neither of the above worked. For me I tried this in .env I changed DB_HOST to homestead.local. Homstead being the name of my site. This worked.
DB_HOST=homestead.local
I solved the problem by updating mariadb. I've checked what was outdated with 'brew outdated', then updated mariadb with 'brew upgrade mariadb' reload vagrant and issue was fixed. Hope this works for you.
Check your .env file for MYSQL_SSL=true.
We use SSL for MySQL in production not on dev after commenting the #MYSQL_SSL=true migration ran successfully.