SQLSTATE[HY000] [2002] No such file or directory in yii2 - php

I use ubuntu 16.04.
PHP Version 7.0.4-7ubuntu2.
Apache/2.4.18 (Ubuntu) .
PHP extension: mysqli (in phpmyadmin Written).
I got Upgrade my ubuntu from 15.10 to 16.04 and I have this error:
My project correctly run in my server but I can't run that in my os:
Database Exception – yii\db\Exception
SQLSTATE[HY000] [2002] No such file or directory
↵
Caused by: PDOException
SQLSTATE[HY000] [2002] No such file or directory
in /var/www/html/iicitySite/vendor/yiisoft/yii2/db/Connection.php at line 579

Changing "localhost" to "127.0.0.1" as your host
return [
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=127.0.0.1;dbname=abc',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],

For MAMP users solution is
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;port=8889;dbname=mydbname;unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock',
'username' => 'myuser',
'password' => 'mypassword',
'charset' => 'utf8',
],
],

Hope this answer will help You:
Change the Host name from localhost to 127.0.0.1
This is inside backend\common\config\main-local.php
Now you run php yii migrate .
Hope, It will successfully create the tables in Database

if you use mamp,don't use "php" command in MAC OS, but use "php" in mamp, such as /Applications/MAMP/bin/php/php5.6.30/bin/php yii migrate.

I had this same problem too. Changing localhost didn't solve my problem. Instead, add your db port like this:
'dsn'=>'mysql:host=localhost:3307;dbname=geep'

I'm running on MAMP environment and works well using 2 solutions above
change localhost to 127.0.0.1
remain as localhost and define mysql port even default port been used (localhost:3306)

For PHP 7.2.24-0ubuntu0.18.04.3,
Get mysql socket path
add socket info to the database config of yii2
To get the socket path login to the mysql and perform following steps
Open Terminal and perform following steps
mysql -u root -p
mysql> show variables like '%sock%';
+-----------------------------------------+------------------------------------------------------+
| Variable_name | Value |
+-----------------------------------------+------------------------------------------------------+
| mysqlx_socket | /tmp/mysqlx.sock |
| performance_schema_max_socket_classes | 10 |
| performance_schema_max_socket_instances | -1 |
| socket | /opt/packages/lampstack-7.3.9-0/mysql/tmp/mysql.sock |
+-----------------------------------------+------------------------------------------------------+
4 rows in set (0.00 sec)
exit
Next add following config information
'class' => 'yii\db\Connection',
'dsn' => 'mysql:unix_socket=/opt/packages/lampstack-7.3.9-0/mysql/tmp/mysql.sock;dbname=basketmantra',
'username' => 'root',
'password' => 'root123',
'charset' => 'utf8',
I hope this helps
In Some cases you may use httpd or apache or lampp then also make sure to check php and mysql commands in terminal are same as versions web servers are using.
phpinfo() in web server is helpful to find out the versions web server is using
<?php
phpinfo();
?>
For commands
$ type php
php is hashed (/usr/bin/php)
$ type mysql
mysql is /opt/packages/lampstack-7.3.9-0/mysql/bin/mysql

Using yii2, my solution is to comment codes in common/main-local.php
For some reason, yii2 try to get main-local.php in production instead of main.php in the folder common, but when is commented it works (get DB configs on common/main.php
<?php
// common/main-local.php
return [
/* 'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=172.17.0.2;dbname=ememariadb',
'username' => 'dbsenha',
'password' => 'dbUser',
'charset' => 'utf8',
],
],*/
];

Related

PDOException with message 'could not find driver' Error for Laravel and Postgresql

First of all, the problem is about Laravel not postgresql or PHP. I can connect postgresql with a simple PHP file. But Laravel can't do it somehow.
When I try to connect postgresql server in my computer with laravel I get "PDOException with message 'could not find driver'" error. I am getting this error when I run DB::connection()->getPdo(); command at artisan tinker.
If I run php artisan migrate command, the error is Illuminate\Database\QueryException : could not find driver (SQL: select * from information_schema.tables where table_schema = public and table_name = migrations and table_type = 'BASE TABLE')
My configuration is below:
Windows 10
Wamp Server 3.1.4
Apache 2.4.35
PHP 7.2.10
Laravel Framework 6.0.3
Related lines of Laravel .env file:
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=laravel_dnm1
DB_USERNAME=postgres
DB_PASSWORD=mrd.BE.265
Related lines of Laravel database.php file:
'default' => env('DB_CONNECTION', 'pgsql'),
...
'pgsql' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'schema' => 'public',
'sslmode' => 'prefer',
],
When I run print_r(PDO::getAvailableDrivers()); at my server I get below output:
Array ( [0] => mysql [1] => pgsql [2] => sqlite )
Related lines of php info is below:
NOTE: There is no problem when I use mysql instead of postgresql.
NOTE2: I can connect the DB when I use regular PHP. Only Laravel gives this error.
install postgresql
sudo apt-get install php-pgsql
then uncomment pgsql and pdo-pgsql extensions in etc/php/$PHP_VERSION/apache2/php.ini file
then restart apache2
Verify your PHP version with php -v
Install php7.2-pgsql when needed.
I solved the issue. It is a bug at WAMP I think. When I edit php.ini from WAMP's menu it opens a different php.ini than active PHP version's. So I opened php.ini from file system and edited it from there.
There are different php version installed on your WAMP SERVER. when you click on WAMP icon it will show that pgsql and pdo_pgsql are active but for different php version. You need to check your php version using php -v command and then go to the WAMP folder and find that php version and edit php.ini file and enable pgsql extensions there.

Doctrine fails to connect to MYSQL database in linked docker container

I am using two Docker container, one having ubuntu and an apache webserver running, the other one a mysql server. The containers are linked and i can connect from the ubuntu container onto the mysql server. For the connection I use in the ubuntu container:
mysql -u root -h mysql
where the second 'mysql' is the name of the container. I can connect to it through the container id as well, so the connection works as well as connecting onto the database from the windows environment.
What doesnt work is the connection from doctrine to the database within the PHP application which is in the ubuntu container.
The config looks like this:
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => 'mysql',
'port' => '3306',
'user' => 'root',
'password' => '',
'dbname' => 'db_name',
'charset' => 'utf8',
)
)
),
)
But I get the Error message
Uncaught PDOException: could not find driver in /var/www/vendor/zendframework/zend-servicemanager/src/ServiceManager.php
and
Zend\ServiceManager\Exception\ServiceNotCreatedException: An abstract factory could not create an instance of doctrine.entitymanager.ormdefault(alias: doctrine.entitymanager.orm_default). in /var/www/vendor/zendframework/zend-servicemanager/src/ServiceManager.php
Does anyone have any idea how to solve this and where exactly the error comes from?
I have already tried to put in the container ID as 'host' and commented out the 'password' field as it is not used.
Thanks in advance
Jonathan
You need to have pdo_mysql allowed on your system, it doesn't seem a connectivity issue but a php configuration problem.
Can you try to do this command inside your php container
php -i | grep pdo_mysql
Just to understand if php has this module

Laravel 5.1 Forge - SQLSTATE[HY000] [1045] Access denied for user ''#'localhost' (using password: NO)

Ive just built my new app in Laravel 5.1 and im trying to push it live. When i try to composer install i am getting this error.
[PDOException]
SQLSTATE[HY000] [1045] Access denied for user ''#'localhost' (using password: NO)
Script php artisan clear-compiled handling the post-install-cmd event returned with an error
[RuntimeException]
Error Output:
I am using a environment file on both local and production so i have no idea whats going on. Any ideas?
Its like my environment file is not getting picked up on forge.....
config/datasbase.php
<?php
return [
/*
|--------------------------------------------------------------------------
| PDO Fetch Style
|--------------------------------------------------------------------------
|
| By default, database results will be returned as instances of the PHP
| stdClass object; however, you may desire to retrieve records in an
| array format for simplicity. Here you can tweak the fetch style.
|
*/
'fetch' => PDO::FETCH_CLASS,
/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/
'default' => env('DB_CONNECTION', 'mysql'),
/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => storage_path('database.sqlite'),
'prefix' => '',
],
'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,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
],
/*
|--------------------------------------------------------------------------
| Migration Repository Table
|--------------------------------------------------------------------------
|
| This table keeps track of all the migrations that have already run for
| your application. Using this information, we can determine which of
| the migrations on disk haven't actually been run in the database.
|
*/
'migrations' => 'migrations',
/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer set of commands than a typical key-value systems
| such as APC or Memcached. Laravel makes it easy to dig right in.
|
*/
'redis' => [
'cluster' => false,
'default' => [
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
],
],
];
The reason this is happening is because forge is running the post install command on your project and your are probably accessing the database in one of your service providers to for may be a a variable you want to attach to your views, try commenting the code in the service provider, install the repository and then push the code up again it should work fine
The reason this was happening is because i was using the 'Install Repo' functionality that forge provides to install my full project. Now because there was no environment set on the server it was failing upon install.
To get around it, i had to hardcode the production database credentials, install the repo then create the env file. Then once this was done remove the hardcoded credentials (not great i know).
It seems setting the .env before the install doesn't work either. So you wouldn't run into this sort of issue if you had your repo installed on a server from the beginning of your build.
But my project is now live and running correctly. Thanks for the replies.
try this
1
in app/database.php
Replace localhost with 127.0.0.1
'host'=> env('DB_HOST', 'localhost') -->'host' => env('DB_HOST', '127.0.0.1')
Also, in .env
DB_HOST=localhost --> DB_HOST=127.0.0.1
2
Try specify environment
php artisan migrate --env=local
3
Check to see if the MySQL is running by run
mysqladmin -u homestead -p status Enter password: secret
I got
Uptime: 21281 Threads: 3 Questions: 274 Slow queries: 0 Opens: 327 Flush tables: 1 Open tables: 80 Queries per second avg: 0.012
Which mean it's running.
4
Check MySQL UNIX Socket
5
make sure that your .env file looks something like this:
APP_ENV=local
APP_DEBUG=true
APP_KEY=YOURAPPKEY
DB_HOST=localhost
DB_DATABASE=YOURDBNAME
DB_USERNAME=YOURPHPMYADMINUSERNAME
DB_PASSWORD=YOURPHPMYADMINPASS
6
try changing localhost into 127.0.0.1 in .env DB_HOST or try add your mysql port
Make sure MySQL user has all accessing privileges for given ip while connecting the MySQL. For further reference please go through the below link.
http://tech2smooth.blogspot.in/2014/09/create-mysql-user-and-grant-permission.html
It's hard to say anything for sure without seeing the .env file, but it looks like it's picking up the username from somewhere - the default is 'forge' and yet the error message has the username as an empty string - ''#localhost.
Check your env file, make sure there are no spaces after the = and that the variable names match the ones in the database config. I've also had issues where I needed to wrap passwords and other values that had special characters in ''. Finally, remember that case matters - I've seen problems where everything worked locally and failed on the dev server because someone had changed the case of a variable. My mac didn't care, but the ubuntu server did.

setup PostgreSQL with Laravel in MAMP

I am using MAMP on my MAC. As it comes with MySQL by default. But now I need to use PostgreSQL in one of my project. How can I setup postgreSQL in MAMP for Laravel project?
Ok if you are set on using postgreSQL over mySQL that comes with MAMP you have to manually install postgreSQL on you location machine the OSX packages can be found here,
If you don't want to do a full install i recommend this Postgres App just download an extract to your applications folder then when you launch it the port number will be displayed in the menu bar like so:
create a database:
go to menu above Click on Open psql
In the command line create you database like so: CREATE DATABASE your_database_name;
should return CREATE DATABASE
Tip to exit postgreSQL CLI use \q then ENTER
You now need to plug these settings into Laravels configuration:
open file: %laravel_directory%/app/config/database.php
in the array replace 'default' => 'mysql', with 'default' => 'pgsql',
now with the information from before edit the 'connections' array like so:
'connections' => array(
'pgsql' => array(
'driver' => 'pgsql',
'host' => 'localhost',
'database' => 'your_database_name',
'username' => '',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
),
)
save file
You should now have a functioning database that Laravel can talk to.
Note you do not need the username or password in database config if you are using the Postgres App.

CakePHP: Can't access MySQL database

I'm new to CakePHP and am just running through the configuration process, but am stumped why Cake can't access my MySQL database. The Cake info page says my tmp directory is writable, the FileEngine is being used for caching (don't know what this means), and my database configuration file is present, but that CakePHP cannot connect to the database.
Here are my setup details:
PHP 5.3 (pre-installed on Snow Leopard)
MySQL 5.1.40 64-bit
CakePHP 1.2.4.8284
Here are the steps I went through:
Created a MySQL schema called cake_blog
Created a MySQL user called cake_blog_user
Granted cake_blog_user the appropriate permissions on cake_blog#localhost and cake_blog#%
Copied the database.php.default file to database.php and edited the database connection details as appropriate
Here is the relevant configuration data from database.php:
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'cake_blog_user',
'password' => 'cake_blog_password',
'database' => 'cake_blog',
'prefix' => '',
);
Am I missing something here? I should also mention that if I insert an echo mysql_error(); into the /cake/libs/view/pages/home.ctp file right before it tests the database connection, the error displayed is "No such file or directory." I have no idea what file or directory it's talking about.
Thanks!
What usually bites me in it's that MySQL thinks of 'localhost' as 'connect thru the unix socket' and '127.0.0.1' 'connect thru TCP port'. With things like XAMPP (at least on mac) the unix socket file isn't there. Just use 127.0.0.1 instead.
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => '127.0.0.1',
'login' => 'cake_blog_user',
'password' => 'cake_blog_password',
'database' => 'cake_blog',
'prefix' => '',
);
Should work all the time.
If it is the socket, just edit /etc/php.ini to reflect the following
pdo_mysql.default_socket=/tmp/mysql.sock
and
mysql.default_socket = /tmp/mysql.sock
I believe you can also do the following
<?php
public $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'cake_blog_user',
'password' => 'cake_blog_password',
'database' => 'cake_blog',
'prefix' => '',
'port' => '/tmp/mysql.sock',
)
?>
doing this might mean you need to edit the database.php file when you go live on the production server.
Thanks everyone for pointing me in the right direction. The mysql.sock file has been moved to /tmp/mysql.sock instead of its default location at /var/mysql/mysql.sock. Editing the php.ini file to reflect this has fixed the problem.
check your phpinfo and use the socket listed. that worked for me.
On Ubuntu, if you installed both 7.0 and 5.6 versions of PHP this wont work.
Youll need to switch if you have both versions:
Look first if is 7.0 version: the command is php -v.
Next do
sudo a2dismod php7.0
sudo a2enmod php5.6
sudo service apache2 restart

Categories