CakePHP: Can't access MySQL database - php

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

Related

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.

Install of laravel 4 from pagodabox breaks on localhost

How do I get laravel to work on local host from pagodabox? I've installed laravel 4 through pagoda box, then cloned it to localhost. I then ran composer install to get all the dependencies and updates. When I try to navigate the URI to the public directory, it doesn't show me a "you have arrived" screen. Instead I get the following error message:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. [tcp://tunnel.pagodabox.com:6379]
I then looked in "database.php" and noticed that the redis array was modified, so I copied the same one from a fresh installation of Laravel, but then I got the following error:
No connection could be made because the target machine actively refused it. [tcp://127.0.0.1:6379]
I Had the same problem, just change the bootstrap > start.php file, search for
'local' => array('your-machine-name')
and change it with your machine name.
This video help me to deal with that: http://www.youtube.com/watch?v=CJoU-LO8Ufo , in the video he changes it to the virtual host but that didn't work for me, i had to put my computer name.
On Mac the computer name can be found by typing hostname in the terminal.
Joshdcid's answer is right for some, but not all. I found this in my bootstrap > start.php file:
'local' => array('homestead')
..., and changing that variable in any way caused my laravel app to not load at all. Not only that, but in a fresh install of laravel, this local variable had the same value of 'homestead'.
After spending a bit of time in WinMerge, I found that you should use Wayne's tip of changing
'redis' => array(
'cluster' => false,
'default' => array(
'host' => 'tunnel.pagodabox.com',
'port' => 6379,
'database' => 0,
),
),
to
'redis' => array(
'cluster' => false,
'default' => array(
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
),
),
at the bottom of the app > config > database.php file, then you should also go to the top of the
app > config > session.php file and change
'driver' => 'redis',
to
'driver' => 'file',
..., just as a fresh install would have. You should be able to view your app now!

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/"

How to connect Cakephp 1.3 with MS SQLServer 2008?

I can't connect to MSSQL Server. I've been researching for 2 days, yet I can't find any useful resources. This is the error I'm getting:
URL rewriting is not properly configured on your server.
Help me configure my database. I don't / can't use URL rewriting:
Your tmp directory is writable.
The FileEngine is being used for caching. To change the config edit APP/config/core.php
Your database configuration file is present.
Here's my database config file:
class DATABASE_CONFIG {
var $default = array(
'driver' => 'mssql',
'persistent' => false,
'host' => 'Charmae-PC\Charmae',
'login' => 'sa',
'password' => 'pass',
'database' => 'obbm',
'prefix' => '',
'port' => '',
);
}
What should I do?
You need to configure your database with cakephp[windows php driver]. I don't know which version cakephp are you using. But for any version of cakephp and wampserver windows php drivers are different. for more information
Download the sqlsrv driver from here
and then the rest of configuration.
By the way you have to use sqlsrv driver for config/database.php
var $default = array(
'driver' => 'sqlsrv',
...

How do I get CakePHP bake to find mysql.sock and recognize MySQL while using MAMP on Mac OSX?

I am currently reading "Beginning CakePHP:From Novice to Professional" by David Golding. At one point I have to use the CLI-command "cake bake", I get the welcome-screen but when I try to bake e.g. a Controller I get the following error messages:
Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (2) in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 117
Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 122
Warning: mysql_get_server_info(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 130
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 154
Error: Your database does not have any tables.
I suspect that the error-messages has to do with php trying to access the wrong mysql-socket, namely the default osx mysql-socket - instead of the one that MAMP uses. Hence I change my database configurations to connect to the UNIX mysql-socket (:/Applications/MAMP/tmp/mysql/mysql.sock):
class DATABASE_CONFIG {
var $default = array(
'driver' => 'mysql',
'connect' => 'mysql_connect',
'persistent' => false,
'host' =>':/Applications/MAMP/tmp/mysql/mysql.sock', // UNIX MySQL-socket
'login' => 'my_user',
'password' => 'my_pass',
'database' => 'blog',
'prefix' => '',
);
}
But I get the same error-messages with the new socket:
Warning: mysql_connect(): Can't connect to local MySQL server through socket '/Applications/MAMP/tmp/mysql/mysql.sock:3306' (2) in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 117
Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 122
Warning: mysql_get_server_info(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 130
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 154
Error: Your database does not have any tables.
Also, even though I use the UNIX-socket that MAMP show on it's welcome-screen, CakePHP loses the database-connection, when using this socket instead of localhost.
Any ideas on how I can get bake to work?
-- Edit 1 --
Thank you guys for helping me out! :)
I have a problem figuring out where in my.cnf to edit to get MySQL to listen to TCP/IP request. The only paragraph I can find where TCP/IP is mentioned is the following:
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking
That allows me to turn off TCP/IP completely, which is the opposite of my intention. I don't know how to go about what you suggest, if you could be more elaborate it would be great. I am a total n00b on these matters :S
Reg. connecting to a local socket: I removed the leading colon in the host-parameter, same result.
I find the solution to this problem :
Add a socket config in the cakephp app/config/database.php file
class DATABASE_CONFIG {
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'port' => '/Applications/MAMP/tmp/mysql/mysql.sock', // here is the key !
'login' => 'you',
'password' => 'yourpass',
'database' => 'yourdb',
'prefix' => '',
);
From the error, it looks like it's trying to connect to an actual IP address and not a UNIX socket, look:
'/Applications/MAMP/tmp/mysql/mysql.sock:3306'
It's appending a port to the socket, which is wrong.
So, I'd first try to configure MySQL to listen to TCP/IP requests (edit the proper section in my.cnf) and try providing 127.0.0.1 instead of the socket.
In case you won't scroll down:
To fix it at CakePHP level, change host on database.php to 'localhost' and add a port directive with its value set to the socket name '/Applications/MAMP/tmp/mysql/mysql.sock'
For anyone running into this problem when using CakePHP 2.0: for me the above database configuration files didn't do the trick. Found the 'unix_socket' property though, that worked for me:
<?php
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'unix_socket' => '/tmp/mysql.sock',
'login' => 'xxx',
'password' => 'xxx',
'database' => 'xxx',
'encoding' => 'UTF8',
'prefix' => ''
);
}
I had the same problem, when using MAMP and the Cake CLI. I'm running CakePHP 1.1xxx and MAMP 1.7.
The problem is, that the MySQL socket can't be found :D
Open Terminal and enter the following:
my-macbook:~ chris$ php -i | grep mysql.default_socket
mysql.default_socket => no value => no value
my-macbook:~ chris$ php -i -c /Applications/MAMP/conf/php5 | grep mysql.default_socket
mysql.default_socket => /Applications/MAMP/tmp/mysql/mysql.sock => /Applications/MAMP/tmp/mysql/mysql.sock
The catch is that without explicitly giving the php binary the path to its (read MAMP's) config file, the mysql.default_socket is not set.
Using that I did not need to change my database configuration whatsoever.
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
I had this exact same problem with mamp, and fixed it with the above command. I think you have to run this command every time you restart your computer. Maybe there is a better way to do it, but I use this with clix.app, so it is usually pretty fast. Also, change your host to localhost.
For me, I forgot to set the port on the host, since I was not using the default MySQL port in MAMP.
i.e. If your MySQL port is 8889, set host to localhost:8889.
Yeah had a similar issue, I think pointing to the the socket:portno as Vinko suggested might work - however if you use an ip address / localhost you'll be fine.
class DATABASE_CONFIG
{
public $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'account',
'password' => 'password',
'database' => 'database',
'prefix' => '',
'port' => '/var/mysql/mysql.sock'
);
}
This worked for me:
class DATABASE_CONFIG
{
public $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'account',
'password' => 'password',
'database' => 'database',
'prefix' => '',
'port' => '/Applications/MAMP/tmp/mysql/mysql.sock'
);
}
Try configuring your firewall... That was the case for me!
You can also make a symbolic link, bake is looking for mysql.sock in /tmp
like this:
ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
Cheers, Ebbot.

Categories