Artisan-commands need different hostname in db-config than Laravel itself - php

This is a strange one I've never been able to solve ever since I started using Laravel.
When I issue artisan-commands like 'php artisan migrate' in my db-config (I'm using MySQL via MAMP) the host line has to be:
'host' => 'localhost:8889',
However when loading a page in the web-browser I get:
SQLSTATE[HY000] [2005] Unknown MySQL server host 'localhost:8889'
It magically works when I change it to:
'host' => 'localhost',
On the live-server I'm running the application on I don't have this problem.
What is going wrong here?

This is most likely happening because you just cd'd into the directory using Terminal and ran the php artisan migrate command from there. You'll need to ssh into your vagrant box using vagrant ssh, then navigate to your project folder and run the migrate command from there.

What port is your MySQL server running on locally? By default, MySQL looks for a server on :3306, so you'll get the error unless something is there to serve it or re-direct it
My guess is the prod server is running MySQL on :8889 and your local environment is running MySQL on :3306
In your local environment, You could either change the port to 8889:
http://www.mamp.info/en/documentation/#q11
Or forward the port:
http://www.chrisvanpatten.com/port-forwarding-mavericks

Related

PDO gives access denied error when running PHP in command line but works in Apache

I'm facing a strange problem while doing maintenance in a code that was working flawlessly a couple weeks ago.
When I run a PHP script through command line (a CRON job), the code that connects to the database (I'm using MySQL 5.7) gives the following error:
SQLSTATE[HY000] [1045] Access denied for user 'user'#'localhost' (using password: YES)
This happens to all users, no matter with or without passwords, even root.
The credentials ARE CORRECT. The same code (the database connection class) is used to generate some web content and it works fine. I'm using Apache 2.4, also I can connect via mysql cli or MySQL Workbench, so credentials and permissons seems not to be the problem here.
The line that creates a PDO instance is simple as this:
new \PDO($dsn, $username, $password, $options);
and the dsn is like:
mysql:host=localhost;dbname=dbname;charset=utf8
(also tried with 127.0.0.1 instead of localhost but had the same problem)
There's just one MySQL, PHP and Apache in the system, running Ubuntu 16.04, everything is in the same machine.
The only thing that changed during this time was an OpenSSL update, but I don't know if this could be related. I've seen some questions about this error but they are the opposite, works in CLI but not in apache, so I decided to make a new question.
More info:
Both Apache and the CRON job itself are being executed by my user (faelv)
The CRON job command is: php dispatchCLI.php task=first
The apache and cli scripts uses the same includes (they both instantiates a class)
sudo changes nothing

(HY000/2002): No connection could be made because the target machine actively refused it [duplicate]

An error suddenly occurred while I was debugging my code.
It has this series of errors regarding the connection to database.
ERROR: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
( ! ) Fatal error: Uncaught exception 'PDOException' with message ' in C:\wamp\www\web\main\users.php on line 15
( ! ) PDOException: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it. in C:\wamp\www\web\main\users.php on line 15
this is the code of where the error is pointing
function __construct()
{
$this->con = new PDO("mysql:host=".$this->host.";dbname=".$this->db,$this->user,$this->pass);
}
I don't know what to do since I do not usually get problems like this so I haven't studied much about WAMP.
Thank You for your help.
If the WAMP icon is Orange then one of the services has not started.
In your case it looks like MySQL has not started as you are getting the message that indicates there is no server running and therefore listening for requests.
Look at the mysql log and if that tells you nothing look at the Windows event log, in the Windows -> Applications section. Error messages in there are pretty good at identifying the cause of MySQL failing to start.
Sometimes this is caused by a my.ini file from another install being picked up by WAMPServers MySQL, normally in the \windows or \windows\system32 folders. Do a search for 'my.ini' and 'my.cnf' and if you find one of these anywhere outside of the \wamp.... folder structure then delete it, or at least rename it so it wont be found. Then restart the MySQL service.
Possibly using different Port for MySQL than using in your Code
$conn = new PDO("mysql:host=".SERVER_NAME.";port=3307;dbname=".DB_NAME, DB_USER, DB_PASS);
Both ports should be same.
I had a similar issue when trying to migrate a Drupal website from one local host to another. From Mac running XAMMP to Windows running WAMP.
This was the error message I kept getting when trying to access the pages of the website.
PDOException: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
in drupal_get_installed_schema_version() (line 155 of C:\wamp\www\chia\includes\install.inc).
In settings.php, I've changed everything correctly, database name, user and password.
$databases = array (
'default' =>
array (
'default' =>
array (
'database' => 'mydatabasename',
'username' => 'mydbusername',
'password' => 'mydbpass',
'host' => 'localhost',
'port' => '8889',
'driver' => 'mysql',
'prefix' => '',
),
),
);
After a couple of hours of mindless google searching I've changed the port to a empty value:
'port' => '',
And after that the site loaded properly.
Check whether MySQL server is running at all. If you not run apache server(or any server you use) this could happen. After run your local server and refresh the page.
Restart your wampServer... that should solve it.
and if it doesn't..
Resta
Ensure your WAMP Server (or XAMP) is working, i.e. the wamp icon should be green.
Delete the following files from the directory:
\wamp\bin\mysql\mysql5.6.17\ib_logfile0
\wamp\bin\mysql\mysql5.6.17\ib_logfile1
\wamp\bin\mysql\mysql5.6.17\ibdata1
This solved my problem.
You should restart your Xampp or whatever server you're runnung, make sure sq
I've had the same issue too, I didn't remember to start my WAMPSERVER, and so the connection couldn't be made...
All you need to do is to start or restart your WAMPSERVER or your XAMMP again and it will work....
All you have to do it insert you exact db details and restart your mysql server
if you are using XAMPP and WAMP together in the same machine add sql server port number
<connection>
<host><![CDATA[localhost:3390]]></host>
<username><![CDATA[root]]></username>
<password><![CDATA[]]></password>
<dbname><![CDATA[sritoss_1910]]></dbname>
<initStatements><![CDATA[SET NAMES utf8]]></initStatements>
<model><![CDATA[mysql4]]></model>
<type><![CDATA[pdo_mysql]]></type>
<pdoType><![CDATA[]]></pdoType>
<active>1</active>
</connection>
I had the same problem, I just deleted all log files in mysql/data files like:
mysql-relay-bin - 2020#002d...
Just these files and it worked.
I hope this helps.
Just restart your wamp server and then run php bin/magento cache:clean
I had the same problem and I searched a lot in web but no solution worked for me .finally I noticed by chance that mysql is using port 3308 instead of port 3306 which seems to be default ,,, I changed the port to 3306 and surprisingly it worked :) .... (my problem was in connecting php to mysql database, php my admin page was shown perfectly)
'dsn' => 'mysql:host=localhost;port=3307;dbname=yourdbname',
I was getting this error attempting to run "php artisan migrate" on Windows with a virtual box / vagrant / homestead installation.
The documentation said I had to run this command on the virtual machine.
This worked!!!
make sure to do it inside your current project folder.

How to run a Laravel application that was not created by me on my personal server

I need to alter some of the front-end features of a Laravel application that was not created by me. The application was created using Laravel and am not sure how to get it up and running so I can alter the front-end code. I was given a copy of the source files. I then put that source files in to my xampp/htdocs folder. I started Xampp, opened the folder in Gitbash and entered PHP artisan serve. The application does not appear to be running.
Is there something I am missing or doing wrong? Please take into consideration that I did not create this application, for your information, I do know how to create and run a Laravel app locally, but am wondering if the process is different if it was not created by me personally?
Thank you all for helping me out, I appreciate you personally and this community.
Here is the error I am receiving in Gitbash:
PHP Warning:
require(C:\xampp\htdocs\appoets\bootstrap/../vendor/autoload.php):
failed to open stream: No such file or directory in C:\xampp\htdocs\appoets\bootstrap\autoload.php on line 17
Warning: require(C:\xampp\htdocs\appoets\bootstrap/../vendor/autoload.php):
failed to open stream: No such file or directory in
C:\xampp\htdocs\appoets\bootstrap\autoload.php on line 17
PHP Fatal error: require(): Failed opening required
'C:\xampp\htdocs\appoets\bootstrap/../vendor/autoload.php'
(include_path='C:\xampp\php\PEAR') in
C:\xampp\htdocs\appoets\bootstrap\autoload.php on line 17
Fatal error: require(): Failed opening required 'C:\xampp\htdocs\appoets\bootstrap/../vendor/autoload.php'
(include_path='C:\xampp\php\PEAR') in
C:\xampp\htdocs\appoets\bootstrap\autoload.php on line 17
Please solved this for me. If you run into the same error. Have the correct file path open in Gitbash and then run "composer install"
After that was fixed another problem popped up. I went to load Laravel in my browser and it gave a general error message no error number or anything. To fix this go to your env.example file and rename it. .env then go to the terminal or your command line and run php artisan key:generate. After this hit save all and go refresh. All should be fixed!
The errors your getting are because you haven't installed your composer dependencies. Install them with composer before running the application:
composer install
Here's the old answer before that callstack was provided. I think it's still interesting background information for you, but not immediately relevant.
It looks like you're running two web servers so I suspect you're just
confused about where the application is running.
XAMPP ships with Apache. I'd expect that running it will spin up its
web server on port 80, the default port that web browsers use.
Therefore, if you're trying to get to the application at
http://localhost, you're hitting this webserver, which might have a
different (and not set up) application on it.
Running php artisan serve will start a second web server utilizing
PHP's built in web server. According to the documentation, the
default port that this command will use is 8000:
By default the HTTP-server will listen to port 8000. However if that
port is already in use or you wish to serve multiple applications this
way, you might want to specify what port to use. Just add the --port
argument:
Therefore, you can access the application without XAMPP though this
url: http://localhost:8000
Try both URLs.
Note that php artisan serve doesn't start up a MySQL server. You
will still need XAMPP to start the MySQL server if you're using a
database.
Checklist for your issue
1. composer update --no-scripts or composer install
2. php artisan key:generate
3. rename example.env to .env
4. Change database credentials, set debug mode! Change app url
4. php artisan migrate(make sure u have the database running in the server)
5. php artisan config:clear
6. you are good to go!

mysql server has gone away error during installing migration (laravel)

So I am using my cmd on my laravel folder and I tried to do (php artisan migrate:install). 2 errors came up.
[PDOException] SQLSTATE[HY000] [2006] MySQL server has gone away
[ErrorException] PDO::__construct(): MySQL server has gone away
Can anyone please explain what I did wrong?
You have Lost SQL connection to server during query. It is temporally issue. This is because of very low default setting of max_allowed_packet.
Raising max_allowed_packet in my.cnf (under [mysqld]) to 8 or 16M usually fixes it.
[mysqld]
max_allowed_packet=16M
NOTE: This can be set on your server as it's running. You need to restart the MySQL service once you are done.
Use: set global max_allowed_packet=104857600. My value sets it to 100MB.
This is not a Laravel issue, but a general MySQL Issue. Maybe the server is not running. Are you sure you're running MySQL in the background?
Check this link: MySQL Gone Away
Do the following checks in your system:
The Database Engine is running
You have created your database
You have created an user and granted permissions to the database
You have setup the user and the database in your Laravel's .env file.
After this, try to run the migrations command again, which is:
php artisan migrate
As explained Here
Let us know if that helps :).
For me the problem seemed to be that I assigned the wrong port to my Laravel project's .env file. Later, when I matched it with the my.cnf file, it worked.
I'm using Ubuntu 16.04 + nginx + MariaDB + Laravel project.
I encountered the same problem. The solution was to delete the mysql port number from 3306 or 80, and leave it empty
In files
.env DB_PORT=3306 to DB_PORT=
and on
database.php 'port' => env('DB_PORT', '3306'), to 'port' => env('DB_PORT', ''),
In my case the problem was I changed DB_HOST to localhost but it was fixed by keeping it default ie 127.0.0.1 and the port to default 3306.
Here is the configuration for localhost in xampp:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=hmb
DB_USERNAME=root
DB_PASSWORD=
Although I use localhost:8081/phpmyadmin to access my db
In addition to other answers:
Try changing localhost domain to 127.0.0.1 both in .env and config/database.php.
Also If you're using git then check git status and see if any unwanted files have been changed.
Because In my case options array for mysql connection configuration in file config/database.php was blank array somehow. I checked out git checkout config/database.php file and it starting to work fine.
If someone is looking for a solution, try to clean all cache you have, you can manually delete cache files in bootstrap/cache folder.
My solution is due to fix the DB_HOST parameter. Indeed I use PHPStorm and I set the public url in .env in order to use DB tools from the IDE and this broken Laravel.
So I changed DB_HOST to localhost and Laravel now works fine.

ERROR: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it

An error suddenly occurred while I was debugging my code.
It has this series of errors regarding the connection to database.
ERROR: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
( ! ) Fatal error: Uncaught exception 'PDOException' with message ' in C:\wamp\www\web\main\users.php on line 15
( ! ) PDOException: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it. in C:\wamp\www\web\main\users.php on line 15
this is the code of where the error is pointing
function __construct()
{
$this->con = new PDO("mysql:host=".$this->host.";dbname=".$this->db,$this->user,$this->pass);
}
I don't know what to do since I do not usually get problems like this so I haven't studied much about WAMP.
Thank You for your help.
If the WAMP icon is Orange then one of the services has not started.
In your case it looks like MySQL has not started as you are getting the message that indicates there is no server running and therefore listening for requests.
Look at the mysql log and if that tells you nothing look at the Windows event log, in the Windows -> Applications section. Error messages in there are pretty good at identifying the cause of MySQL failing to start.
Sometimes this is caused by a my.ini file from another install being picked up by WAMPServers MySQL, normally in the \windows or \windows\system32 folders. Do a search for 'my.ini' and 'my.cnf' and if you find one of these anywhere outside of the \wamp.... folder structure then delete it, or at least rename it so it wont be found. Then restart the MySQL service.
Possibly using different Port for MySQL than using in your Code
$conn = new PDO("mysql:host=".SERVER_NAME.";port=3307;dbname=".DB_NAME, DB_USER, DB_PASS);
Both ports should be same.
I had a similar issue when trying to migrate a Drupal website from one local host to another. From Mac running XAMMP to Windows running WAMP.
This was the error message I kept getting when trying to access the pages of the website.
PDOException: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
in drupal_get_installed_schema_version() (line 155 of C:\wamp\www\chia\includes\install.inc).
In settings.php, I've changed everything correctly, database name, user and password.
$databases = array (
'default' =>
array (
'default' =>
array (
'database' => 'mydatabasename',
'username' => 'mydbusername',
'password' => 'mydbpass',
'host' => 'localhost',
'port' => '8889',
'driver' => 'mysql',
'prefix' => '',
),
),
);
After a couple of hours of mindless google searching I've changed the port to a empty value:
'port' => '',
And after that the site loaded properly.
Check whether MySQL server is running at all. If you not run apache server(or any server you use) this could happen. After run your local server and refresh the page.
Restart your wampServer... that should solve it.
and if it doesn't..
Resta
Ensure your WAMP Server (or XAMP) is working, i.e. the wamp icon should be green.
Delete the following files from the directory:
\wamp\bin\mysql\mysql5.6.17\ib_logfile0
\wamp\bin\mysql\mysql5.6.17\ib_logfile1
\wamp\bin\mysql\mysql5.6.17\ibdata1
This solved my problem.
You should restart your Xampp or whatever server you're runnung, make sure sq
I've had the same issue too, I didn't remember to start my WAMPSERVER, and so the connection couldn't be made...
All you need to do is to start or restart your WAMPSERVER or your XAMMP again and it will work....
All you have to do it insert you exact db details and restart your mysql server
if you are using XAMPP and WAMP together in the same machine add sql server port number
<connection>
<host><![CDATA[localhost:3390]]></host>
<username><![CDATA[root]]></username>
<password><![CDATA[]]></password>
<dbname><![CDATA[sritoss_1910]]></dbname>
<initStatements><![CDATA[SET NAMES utf8]]></initStatements>
<model><![CDATA[mysql4]]></model>
<type><![CDATA[pdo_mysql]]></type>
<pdoType><![CDATA[]]></pdoType>
<active>1</active>
</connection>
I had the same problem, I just deleted all log files in mysql/data files like:
mysql-relay-bin - 2020#002d...
Just these files and it worked.
I hope this helps.
Just restart your wamp server and then run php bin/magento cache:clean
I had the same problem and I searched a lot in web but no solution worked for me .finally I noticed by chance that mysql is using port 3308 instead of port 3306 which seems to be default ,,, I changed the port to 3306 and surprisingly it worked :) .... (my problem was in connecting php to mysql database, php my admin page was shown perfectly)
'dsn' => 'mysql:host=localhost;port=3307;dbname=yourdbname',
I was getting this error attempting to run "php artisan migrate" on Windows with a virtual box / vagrant / homestead installation.
The documentation said I had to run this command on the virtual machine.
This worked!!!
make sure to do it inside your current project folder.

Categories