PDO is not working with CakePHP installation - php

I am configuring a cakePHP application, but the app doesn't seems to connect successfully to DB, I got the following error:
Cake is NOT able to connect to the database.
Database connection "Mysql" is missing, or could not be created.
I renamed the database.php.default to database.php and here is the code:
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost:8889',
'login' => 'root',
'password' => 'root',
'database' => 'cakephpdb',
'prefix' => '',
'encoding' => 'utf8',
);
}
Actually, I know CakePHP use PDO driver to connect to DB, so I tried to run a separate file just to test whether the PDO is true of false:
<?php
var_dump( extension_loaded('pdo_mysql') );
?>
And it's false:
bool(false)
I tried to enable PDO in php.ini file:
extension=php_pdo_mysql.so
Restarted MAMP, but still the same error for cakePHP app. Am I missing something? how do you make sure PDO is enabled in your cases? Thanx in advance.
EDIT:
When running phpinfo(), the PDO_MySQL is not enabled:
The php.ini path is the follwoing, according to phpinfo() output:
/Applications/MAMP/bin/php/php5.4.10/conf
So I tried to edit the php.ini file at that path and here is the relevant settings:
extension=imap.so
extension=yaz.so
extension=mcrypt.so
extension=gettext.so
extension=pgsql.so
extension=pdo_pgsql.so
extension=pdo_mysql.so
pdo_mysql.default_socket=/tmp/mysql.sock
mysql.default_socket = /tmp/mysql.sock
Also, I tried to test the DB connection in a php script:
try {
$dbh = new PDO('mysql:host=localhost;dbname=db_name', 'localhost', 'root');
echo '<br /> success';
} catch (PDOException $e) {
print "Error !: " . $e->getMessage() . "<br/>";
die();
}
And I gto always the following error:
Error !: could not find driver
So the PDO_Mysql driver is not loaded, and that's clear from the phpinfo() output. Out to fix that?

Have you confirmed whether you're updating the correct php.ini? For XAMPP, there is a php.ini for the console, and another for the webserver. You might be updating the incorrect one.

Related

"Unable to connect to your database server using the provided settings."

how to import mysqldb into heroku postgres?
I am getting an error In codeigniter
these are my settings:
$active_group = 'default';
$query_builder = TRUE;
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'whatsapp',
'dbdriver' => 'mysqli',
'dbprefix' => '',
You should post the error you are getting, this way others can maybe help you.
I found also this solution that, may or may not be helpful:
For me the issue was in the php.ini file. The property mysql.default_socket was pointing to file in a non-existent directory. The property was pointing to /var/mysql/mysql.sock but in OSX, the file was located in /tmp/mysql.sock.
Source: CodeIgniter: Unable to connect to your database server using the provided settings Error Message
You can use mysql-postgresql-converter Dump MySQL database in PostgreSQL-compatible format
mysqldump -u username -p --compatible=postgresql databasename > database.sql
then use the converter to transfer data into *.psql file. then load new dump into a fresh PostgreSQL database

Laravel 5 with Postgres SQL

I’m working on Laravel 5 with postgres as database. I’ve configured postgres 9.4 and pgAdmin III and these are working normally. When I try to run migrate it is giving me error:
[PDOException]
could not find driver
This is my database.php
'default' => 'pgsql',
'pgsql' => [ 'driver' => 'pgsql',
'host' => '127.0.0.1',
'database' => 'fms',
'username' => 'postgres',
'password' => 'root',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public', ],
Initially I though, it was due to configuration of postgres on windows 7 but I tried with plain php it works perfect
<?php
$host = "host=127.0.0.1";
$port = "port=5432";
$dbname = "dbname=fms";
$db = pg_connect( "$host $port $dbname user=postgres password=root" );
if(!$db){
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
?>
I’ve enabled php_pgsql and php_pdo_sql in wamp as well. I’m not sure how to fix this on laravel 5.
As you said you already choosed Default Database as Postgres SQL
'default' => 'pgsql',
It is a must that you need to uncomment the pdo and postgres shared object in your php configuration settings (php.ini)
i.e., You need to uncomment the following lines in your php.ini
extension=pdo_pgsql.so
extension=pgsql.so
Note :
Don't forget to stop and start your apache after doing this changes (or php-fpm if using that instead).
I had the same problem with Laravel-WAMP-PostgreSql driver not found exception. I even successfully established direct connection to Postgre as you have, but with no luck with the "php artisan migrate" command.
After long research I found out that there are multiple php.ini files in which "extension=php_pdo_pgsql.dll" and "extension=php_pgsql.dll" are comented out.
The solution is (of course) to uncoment the extensions in the following files:
~/wamp/bin/php/php5.5.*/php.ini
~/wamp/bin/php/php5.5.*/phpForApache
~/wamp/bin/php/php5.5.*/php.ini.install
~/wamp/bin/php/php5.5.*/php.ini-development
~/wamp/bin/php/php5.5.*/php.ini-production
and
~/wamp/bin/apache/apache2.4.9/php.ini
** You can leave off the "php.ini-development" and "php.ini-production" (don't need to uncoment these files).
Like #jeff said, this is probably caused by not setting DB_CONNECTION=pgsql in the .env-file. The .env-file has MySQL preconfigured, so you must edit this file.
You have to make DB related changes in
config/database.php
.env file
and other settings in
php.ini settings
If you are still getting error, then clear cache and config
php artisan cache:clear
php artisan config:clear
It should work now!
Run this command to easily uncomment the lines extension=pdo_pgsql.so and extension=pgsql.so from php.ini
sed -ri -e 's!;extension=pdo_pgsql!extension=pdo_pgsql!' $PHP_INI_DIR/php.ini
sed -ri -e 's!;extension=pgsql!extension=pgsql!' $PHP_INI_DIR/php.ini
If you do not have the drivers already installed, or it gives error like unable to load dynamic library PGSQL run this:
apt-get update && apt-get install -y libpq-dev && docker-php-ext-install pdo pgsql pdo_pgsql
This will install the pgsql and pdo_pgsql drivers.
you gotta modify .env file , then go to config>database.php and change the default to pgsql, and also in all your models file you need to change $protected $connection= 'pgsql'.
I'm using PHP7.3 and this worked for me, I forgot this:
apt-get install php7.3-pgsql
Everything runs smoothly afterwards. Just use whatever version of PHP you are using.

PDOException: SQLSTATE[HY000] [2002] No such file or directory

I have put PushChatServer dir in htdocs folder and create database puschat try to run #"http://localhost/PushChatServer/api/test/database.php"
Then I got following exception.
I want do same thing to explain this link http://www.raywenderlich.com/32963/apple-push-notification-services-in-ios-6-tutorial-part-2
I have done all that but I got this exception
Could not connect to the database. Reason: exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No such file or directory' in /Applications/MAMP/htdocs/PushChatServer/api/test/database.php:17 Stack trace: #0 /Applications/MAMP/htdocs/PushChatServer/api/test/database.php(17): PDO->__construct('mysql:host=loca...', 'root', 'root', Array) #1 {main}
You need to change host from localhost to 127.0.0.1
Laravel 4: In your app/config/database.php try changing host from localhost to 127.0.0.1
Laravel 5: In the .env file, change DB_HOST from localhost to 127.0.0.1
Source: PDOException SQLSTATE[HY000] [2002] No such file or directory
Quick test (run in shell):
php -r "new PDO('mysql:host=localhost;dbname=test', 'username', 'password');"
SQLSTATE[HY000] [2002] No such file or directory means php cannot find the mysql.default_socket file. Fix it by modifying php.ini file. On Mac it is mysql.default_socket = /tmp/mysql.sock (See MySQL connection not working: 2002 No such file or directory)
SQLSTATE[HY000] [1044] Access denied for user 'username'#'localhost' CONGRATULATION! You have the correct mysql.default_socket setting now. Fix your dbname/username/password.
Also see Error on creating connection to PDO in PHP
I had the same error using PHP 5.6.30 (macOS Sierra) with the simple PDO code like:
$pdo = new PDO('mysql:host=localhost;dbname=db_php', 'root', '');
And I received the same error message. To fix, I changed "localhost" for IP (loopback) "127.0.0.1" in my code:
$pdo = new PDO('mysql:host=127.0.0.1;dbname=db_php', 'root', '');
To test the connection:
$ php -r "new PDO('mysql:host=127.0.0.1;dbname=db_php', 'root', '');"
This work's for me!
Restart your database and local web server:
sudo service mysqld restart
It should work!
I'm not sure if this will work for you; but I use CakePHP, and I get this error whenever I forget to put the port number at the end of the 'host'.
Hope this helps!
Before
'test' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'root',
'password' => 'root',
'database' => 'mylogin',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
'quoteIdentifiers' => false,
'log' => false,
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
'url' => env('DATABASE_TEST_URL', null),
]
After
'test' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost:8080',
'username' => 'root',
'password' => 'root',
'database' => 'mylogin',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
'quoteIdentifiers' => false,
'log' => false,
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
'url' => env('DATABASE_TEST_URL', null),
]
PDO treats localhost very particularly:
From http://php.net/manual/en/ref.pdo-mysql.connection.php:
Note: Unix only: When the host name is set to "localhost", then the
connection to the server is made thru a domain socket. If PDO_MYSQL is
compiled against libmysqlclient then the location of the socket file
is at libmysqlclient's compiled in location. If PDO_MYSQL is compiled
against mysqlnd a default socket can be set thru the
pdo_mysql.default_socket setting.
That why PDO and mysql_connect will give different behavior for localhost.
So, if you want to use a TCP connection with PDO, never use localhost but 127.0.0.1.
I had the same error for Mysql PDO, this code works for me!
<?php
$dsn = 'mysql:host=127.0.0.1;dbname=testdb';
$username = 'username';
$password = 'password';
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
$dbh = new PDO($dsn, $username, $password, $options);
?>
got this code from : http://php.net/manual/en/ref.pdo-mysql.connection.php
Run the following command in shell, I think it will help.
php artisan cache:clear
php artisan config:clear
php artisan view:clear
On Mac OSX/MAMP you may find a mysql.sock.lock file in your /Applications/MAMP/tmp/mysql folder.
You can safely remove the file mysql.sock.lock and you should be in good shape.
Just ran into this same issue and the problem is the password. In the tutorial the author lists the password as:
"d]682\#%yl1nb3"
So as per the suggestion given by #Marki555, I looked in the config file - api_config.php. and the password listed there is:
"d]682\#%yI1nb3"
The upper case 'I' is what caused the issue because the password you set for user on the db has the password with the lowercase l but it really is looking for the uppercase I. After I changed the pushchat user's password to have an uppercase i, it worked!

Cake Console won't work on Mac OS X Lion

I just downloaded CakePHP 2.1 and did all the setup for the database and all the rest, but I'm unable to use the cake console, I get the following error:
Error: Database connection "Mysql" is missing, or could not be created.
If I open the site on my browser I see this:
This is my database.php:
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => '127.0.0.1',
'login' => 'user',
'password' => 'password',
'database' => 'database_schema',
'prefix' => '',
'encoding' => 'utf8',
'port' => ' /Applications/xampp/xamppfiles/var/mysql/mysql.sock',
);
I'm using XAMPP 1.7.3, I read that the issue might be related to PDO, but I have no idea how to set it up properly, any suggestions?
You should enable the php_pdo_extension in php.ini. The file is located at /Applications/XAMPP/etc/php.ini by default.
Mine was Mac OS Yosemite ,MAMP PHP 5.6.1 and spent almost two days trying all the fixes available..finally it was something do with php.ini extension_dir
Old value was '.../no-debug-non-zts-20121212' which was not there in the path specified , so i changed the path to available directory as below...and success!!
php.ini located in MAMP - /Applications/MAMP/bin/php/php5.6.1/conf (This can also be found using phpinfo() under 'Configuration File (php.ini) Path')
fixed path in php.ini
; Directory in which the loadable extensions (modules) reside.
extension_dir = "/Applications/MAMP/bin/php/php5.6.1/lib/php/extensions/no-debug-non-zts-20131226/"

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