PDOException : SQLSTATE[HY000] [2006] MySQL server has gone away - php

I am new to Laravel so started creating api. I already have MySQL database at local and I am trying to connect with local database but getting following logtrace.
php artisan migrate
PDOException : SQLSTATE[HY000] [2006] MySQL server has gone away
at /Users/sandeeparmal/...../vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
66| if (class_exists(PDOConnection::class) && ! $this->isPersistentConnection($options)) {
67| return new PDOConnection($dsn, $username, $password, $options);
68| }
69|
> 70| return new PDO($dsn, $username, $password, $options);
71| }
72|
73| /**
74| * Determine if the connection is persistent.
Exception trace:
1 PDO::__construct("mysql:host=127.0.0.1;port=8080;dbname=sample_server", "root", "", [])
/Users/sandeeparmal/...../vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
2 Illuminate\Database\Connectors\Connector::createPdoConnection("mysql:host=127.0.0.1;port=8080;dbname=sample_server", "root", "", [])
/Users/sandeeparmal/...../vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:100
Here is my .env file :
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8080
DB_DATABASE=sample_server
DB_USERNAME=root
DB_PASSWORD=
Here is my database.php :
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'url' => env('DATABASE_URL'),
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '8080'),
'database' => env('DB_DATABASE', 'forcity_server'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('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 => env('MYSQL_ATTR_SSL_CA'),
]) : [],
]
I tried to find solution and got solution from here but still it's not working. I applied same on local database.

I updated to laravel 6, so I updated PHP because laravel 6 uses 7.2+
by default there wasn't php.ini and when generated, extension was off so I tried migration too and found out this
Check php.ini, there is one row
;extension=pdo_mysql
make it
extension=pdo_mysql
save and restart apache

Related

Laravel Error SQLSTATE[HY000] [2002] No such file or directory in Laravel 6

i have some issue about connection on laravel into database MySQL like this.
My laravel files and database has been published on hosting.
Illuminate\Database\QueryException.
SQLSTATE[HY000] [2002] No such file or directory (SQL: SELECT * FROM tbl_master_users WHERE identity_number = '12345' AND password ='password')
I tried to looking for some reference to solve this issue, but it's not working on me.
this is .env file : (I tried to change DB_HOST into 127.0.0.1 but it still same)
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=db_users
DB_USERNAME=uname
DB_PASSWORD=password
this is my configuration on database.php file :
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'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' => '',
'prefix_indexes' => true,
'strict' => false,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
Socket :

I cannot access my second database in Laravel (.env & database.php edited !)

I have a database which runs on local that I created with TablePlus. I can access it without any problem. I tested with Insomnia and I can get the info I need. I search how to connect to a second database that is live. I changed my .env file and my database.php to create another access to the second database. But I have an error message:
SQLSTATE[HY000][1045] Access denied for user 'root'#'host' (using passowrd: YES)
where host is the name I found in PHP MyAdmin in the variables tab.
How can I acccess the second DB ? What am I doing wrong ? Do I need to change the env('DATABASE_URL') for the second connection in database.php ?
.env:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=first_db
DB_USERNAME=root
DB_PASSWORD=root
DB_SECOND_CONNECTION=mysql2
DB_SECOND_HOST=host //(again, PHP MyAdmin -> variables tab)
DB_SECOND_PORT=3306
DB_SECOND_DATABASE=second_db
DB_SECOND_USERNAME=*username*
DB_SECOND_PASSWORD=*password*
database.php:
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'first_db'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'root'),
'unix_socket' => env('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 => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'mysql2' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_SECOND_HOST', 'host'), //(again, PHP MyAdmin -> variables tab)
'port' => env('DB_SECOND_PORT', '3306'),
'database' => env('DB_SECOND_DATABASE', 'secondd_db'),
'username' => env('DB_SECOND_USERNAME', 'username'),
'password' => env('DB_SECOND_PASSWORD', 'password'),
'unix_socket' => env('DB_SECOND_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 => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
],
As suggested by #Collin above, the root user I was using didn't have the permissions. Creating a new user with all privileges in PHP MyAdmin solved the issue.
If you want to check if the database connection is actually accesable you could try the following.
First clear your config using the following command:
php artisan config:clear
Then check if the connection is accesable using the following code:
try {
DB::connection('mysql2')->getPdo();
} catch (\Exception $e) {
die("Could not connect to the database. Please check your configuration. error:" . $e );
}
If that doesn't work check if the user has the right permissions.

I can't connect to mysql DB connection refused

I have tried to connect to the DB mysql using php artisan migrate command and gives me this error
Exception trace:
1 PDOException::("SQLSTATE[HY000] [2002] Connection refused")
/Users/hamad/Desktop/web/php/test/vendor/laravel/framework/srcIlluminate/Database/Connectors/Connector.php:70
2 PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=laravel", "root", "", [])
/Users/hamad/Desktop/web/php/test/vendor/laravel/framework/src/Illuminate/Database/Connectors/.Connector.php:70
my set up in the env file is
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
and in config /database.php is
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'laravel'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('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 => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
I have tried to use localhost instead of 127.0.0.1 but did not work
thanks for your help
I changed from xampp to mamp program and worked fine
I think it is from the localserver and the port number
xampp using 192.168.64.2 as localserver
mamp uses 127.0.0.1 as localserver

Laravel - Issues trying to connect project to database

I'm trying to connect laravel to the database, but I'm having issues.
I'm getting these errors:
1 PDOException::("SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: NO)")
C:\Users\andre\Dropbox\College\Project\Rentable\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php : 68
2 PDO::__construct("mysql:host=localhost;port=3308;dbname=rentable", "root", "", [])
C:\Users\andre\Dropbox\College\Project\Rentable\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php : 68
This is my .env file
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3308
DB_DATABASE=rentable
DB_USERNAME=root
DB_PASSWORD=
This is a screenshot of the database on the localhost:
Most likely the default password for local installations is either root , secret or password, try those in your .env
Check your database parameters in config/database.php. You should have something like this
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'database_name'),
'username' => env('DB_USERNAME', 'database_username'),
'password' => env('DB_PASSWORD', 'database_password'),
'unix_socket' => env('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 => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],

Laravel 5.2 oracle database PDOException

I am working on xamp, using laravel 5.2 and trying to connect oracle database
here I want to get values in tinker:
DB::table('dept')->get();
with this error:
PDOException with message 'could not find driver'
my .env file
DB_CONNECTION=sqlsrv
DB_HOST=localhost
DB_PORT=1521
DB_DATABASE=Mydb
DB_USERNAME=db_username
DB_PASSWORD=password
my database.php
'default' => env('DB_CONNECTION', 'sqlsrv'),
...
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'Mydb'),
'username' => env('DB_USERNAME', 'db_username'),
'password' => env('DB_PASSWORD', 'password'),
'charset' => 'utf8',
'prefix' => '',
],
Install Oracle client in your machine: http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html
Install/enable Oracle for PHP. That will depend on your operation system. In windows, I believe you only need to enable on your php.ini, extension=pdo_oci.dll. In linux, you need to install, with apt get or similar in your distro, or recompile PHP with instructions on this page: http://php.net/manual/en/ref.pdo-oci.php
Use this library in your Laravel project: https://github.com/yajra/laravel-oci8
In config/database.php:
'default' => env('DB_CONNECTION', 'my_connection'),
'connections' => [
'my_connection' => [
'driver' => 'oracle',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1521'),
'service_name' => env('DB_SERVICE_NAME', ''),
'database' => env('DB_DATABASE', ''),
'username' => env('DB_USERNAME', ''),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'options' => [
PDO::ATTR_PERSISTENT => true
],
],
In your .env:
DB_CONNECTION=my_connection
DB_HOST=111.111.111.111
DB_SERVICE_NAME=service
DB_DATABASE=db
DB_USERNAME=user
DB_PASSWORD=pass

Categories