Doctrine fails to connect to MYSQL database in linked docker container - php

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

Related

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

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',
],
],*/
];

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.

PDOException could not find driver on Zend Framework 2 + Doctrine

I'm trying to install Zend Framework 2 + Doctrine from this manual and have some problem with PDO driver. Doctrine is trying to connect to my MySQL server and then trying to create schema:
./vendor/bin/doctrine-module orm:schema-tool:create
I have this error:
[PDOException]
could not find driver
This is my config/autoload/doctrine.local.php:
return array( 'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' =>'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'port' => '3306',
'user' => 'root',
'password' => 'password',
'dbname' => 'blog',
)))));
I have PHP 5.3.5 and i have uncommented ;extension=php_pdo_mysql.dll in php.ini, also it wasn't commented. But php -m | grep -i pdo gives me:
PDO
only. I'm know that it should be pdo_mysql too, but i'm trying everything and it isn't appear.
Anyway, PDO driver is works well at my another project on this server, where I'm not using Zend and Doctrine.
Ohh yes. I'm just solved a problem. I'm going to this page and thought to find my php.ini files anywhere else perhaps PHP folder - it was one more in "C:\Users\%username%\AppData\Local\VirtualStore\Program Files (x86)\wamp\bin\php\php5.3.5" and there row
;extension=php_pdo_mysql.dll
weren't uncomment. I'm uncommented it and it works!

Cannot connect to PgSQL via Laravel SQLSTATE[08006] [7] FATAL

I'm trying to connect to pgsql via laravel and finally got everything setup (pgsql server running, pdo installed, all libs installed). I'm running on a VPS (CentOS) managed via CPanel/WHM.
Here's what I'm doing:
I'm trying to create a user database via artisan (Laravel's command line) using migrate:install.
For those that don't use Laravel, artisan uses php's PDO for pgsql to connect. Here are my settings:
'pgsql' => array(
'driver' => 'pgsql',
'host' => 'localhost',
'database' => 'dbname',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'prefix' => '',
),
I've also setup a test.php file:
$dbconn = pg_connect("host=localhost port=5432 dbname=dbname user=username password=password");
which also fails. I used phpPgAdmin to see what's up and all of the permissions are set correctly, the database shows up, the username is correct, same with password. I checked where postgre (version 8.4.12 btw) was running and phpPgAdmin tells me "localhost:5432".
The error I get via the command line is the following:
SQLSTATE[08006] [7] FATAL: no pg_hba.conf entry for host "::1", user "myusername", database "my_database", SSL OFF
Now, I tried to find the pg_hba.conf file but I'm not entirely sure where to look for it. Same goes for the error/access logs for pg and google hasn't been any help as far as this goes.
Any idea on what I can do?
localhost points to IPV6 ::1 address on your system. Postgresql makes the difference between ipv6 and ipv4 addresses when dealing with access list.
I was able to install/configure everything correctly. I changed the "host" to 127.0.0.1, not sure why that made a difference but it did :)

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