PDOException Error - Can't connect to database - laravel

I'm using PHP 7.1, MySQL 5.7, and Laravel 5.7.12. And on a Mac.
I'm new to Laravel. I'm building my application on a remote server (a Cloudways Server). I know most people build their project locally and then deploy their application onto a remote server, but I'm just building my application on a live remote server and I'm using SFTP.
I've configured my .env file to reflect my server's connection details.
When I try to run php artisan migrate I get this error:
Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = ********* and table_name = migrations)
at /Users/newadmin/Documents/Sites/Quipper/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the database's errors.
663| catch (Exception $e) {
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|
Exception trace:
1 PDOException::("SQLSTATE[HY000] [2002] No such file or directory")
/Users/newadmin/Documents/Sites/Quipper/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
2 PDO::__construct("mysql:host=localhost;dbname=*********", "*********", "**********", [])
/Users/newadmin/Documents/Sites/Quipper/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
Above I've placed ********** where my database name, username and password go, I guess just for security reasons.
I think it's worth noting on that second exception trace it says "mysql:host=localhost;", when in my .env file I have put in my DB_HOST address, which is a remote server address. So it shouldn't be saying localhost. That seems like it could be part of the problem?
Any help would be greatly appreciated!
I've already tried running in my terminal:
php artisan cache:clear
php artisan config:cache
And that didn't work.

From the log file, look at the path to the PHP script
/Users/newadmin/Documents/Sites/Quipper
This is in your local Mac or MacBook computer, which means that you need to SSH into your Cloudways server and then run the php artisan migrate command
By SSH I mean running something like
ssh cloudways_user#server_IP_ADDRESS
cd Quipper && php artisan migrate

Related

Laravel pp db error ( the database name is incorrect

Good day this is the error i got when trying to run my laravel php artisan serve
The error i first got + the second error i got.
Not sure what to do?
Error Statement:
SQLSTATE[HY000] [1698] Access denied for user 'root'#'localhost' error generated when running :127.0.0.1:8001
Solution tried:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3630
DB_DATABASE=yourdbname
DB_USERNAME=admin
DB_PASSWORD=adminspassword
and
$ sudo mysql -u root
CREATE USER 'admin'#'localhost' IDENTIFIED BY 'adminspassword';
GRANT ALL PRIVILEGES ON `*`.`*` TO 'admin'#'localhost';
FLUSH PRIVILEGES
Final Error Statment:
I got this
SQLSTATE[HY000] [1049] Unknown database 'laravel'
The problem is that you have not created a database.
So, what you need to do is:
sudo mysql
CREATE DATABASE yourdbname;
now you can start building user and permissions in the db.

connecting to database SQLSTATE[HY000] [1045] on new server

I have recently changed server and uploaded my database to the server, for the live website i made no changes to my env and everything works the same. But in my php storm development i have had to change the ip(db host) and despite the username and password being the same i am getting an error.
SQLSTATE[HY000] [1045] Access denied for user 'connect'#'xxxxxx' (using password: YES)
I have cleared config and cache and tried launching php artisan again but get the same issue. i have even brought in the env from the server(live website) and changed the DB Host to match the IP of the server but then the error occurs. Does anyone know how i fix this error, could it be an issue with the fact the database was imported from another server?

Deploying Laravel to Elastic Beanstalk - DB Connection Refused

I've been developing Laravel app and deploying regularly to an Elastic Beanstalk instance without issue, but suddenly, and without warning, deployments have begun to fail.
Obviously I immediately wondered what I might have changed in the code to cause this, but I haven't made any changes to the core configuration. I rolled back to an earlier commit just to make sure it wasn't my code, and I got the same error.
It's failing after I run eb deploy and running the .ebextensions configs.
Here's the error message from the logs:
[2018-01-08T10:50:34.672Z] INFO [9457] : Running 4 of 5 actions: EbExtensionPostBuild...
[2018-01-08T10:50:35.523Z] ERROR [9457] : Command execution failed: Activity failed. (ElasticBeanstalk::ActivityFatalError) caused by:
In Connection.php line 664:
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = custom and table_name
= migrations)
In Connector.php line 67:
SQLSTATE[HY000] [2002] Connection refused
(ElasticBeanstalk::ExternalInvocationError)
The DB connection credentials are absolutely correct (and haven't changed either).
The command that it's attempting when it fails is simply:
php artisan migrate --force
I've tried connecting over SSH to the server, and I can manually run the same command without issue.
I just can't understand why the deployment is suddenly having its connection refused. Could it be something to do with the AWS security configuration? Could it expire or something? :-/
It turns out the issue was caused by local settings becoming cached and then deployed to the server in: bootstrap/cache/config.php.
Clearing the cache with php artisan config:clear deleted the file and solved the problem.

PDOException:SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket

whenever i run the following command php artisan migrate:install, i will get the following error in my ubuntu 12.04 terminal...
[PDOException]
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket ' /var/run/mysqld/mysqld.sock' (2).
I install laravel 5 in ubuntu 12.04. In my /var/run/mysqld folder, i don't have any mysqld.sock file. Everyone says check out your .env file but i don't have any .env.example file but yes i have app.php, database.php mail.php, cache.php files in my app/config folder.In the whole process i have conclude on thing that their is a problem with connectivity with database because whenever i want to migrate any table in database i will not migrate or not shown in database but i will create table file in my app/controller/http folder. So the problem is table is not displayed in database after i run php artisan migrate command else it will show error above mentioned. So plz help me out. Thankss.......
make sure that MySQL server is up and running, e.g. telnet 127.0.0.1 3306
replace 'localhost' with '127.0.0.1' in env('DB_HOST',

Laravel Homestead- MySQL default credentials and database

I have setup Laravel and trying to run the artisan migrate command however I am getting the error below
[PDOException] SQLSTATE[HY000] [2002] Connection refused.
I am not sure how db is setup in Homestead. So I got the below questions.
Is default database created automatically by artisan migrate or Homestead?
If it is, what is the name of it?
If its not created by default, should we create before running migration?
I tried logging in to MySQL db by connecting to Homestead VM using ssh and then running MySQL. However I get error Access denied for user.... for user name vagrant, Homestead and forge.
What is the default credentials?
I understand that creating MySQL db is out of the scope of Laravel tutorial; So it would be helpful if anyone can answer these questions and point me in right direction.
Homestead comes with a default database called homestead. Your app can either choose to hook into that database, or you will have to go and make a new database manually. You can either use a GUI (like Sequel Pro on Mac) or perform it via the command line through Vagrant.
// SSH into the box
vagrant ssh
// Connect to MySQL as the homestead user (password is: secret)
mysql -u homestead -p
// Create a new database in MySQL
CREATE DATABASE your_app_name;
// Leave MySQL
exit;
You can then migrate the database as usual, php artisan migrate.
If you need to do this with Postgres instead, it's pretty similar.
// Connect to Postgres (password is: secret)
psql -U homestead -h localhost
// Create a new database in Postgres
CREATE DATABASE your_app_name;
// Leave Postgres
\q

Resources