Can't get going with MySQL in CakePHP - mysql

This is my first time installing a framework, and I am pretty clueless.
I am on OSX 10.7 and I have the cakephp framework loaded into /Library/WebServer/Documents/cakephp and I have been able to load the test page and get rid of some of the errors and warnings. Right now I am trying to resolve this
Warning (2): PDO::__construct() [pdo.--construct]: [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) [CORE/Cake/Model/Datasource/Database/Mysql.php, line 160]
Cake is NOT able to connect to the database.
Database connection "SQLSTATE[HY000] [2002] No such file or directory" is missing, or could not be created.
I don't really know what to do here. I have installed MySQL. Does the MySQL PDO come installed on OSX by default? or do I need to install that? How can I check if that is installed if that seems to be the problem.
UPDATE:
The PDO Mysql driver is enabled.
Also the phpinfo() for pro_mysql looks like this:
Directive Local Value Master Value
pdo_mysql.default_socket /var/mysql/mysql.sock /var/mysql/mysql.sock
However the mysql directory doesn't appear in my filesystem. should I create it? or do I nee to change this path somewhere?
UPDATE:
I think the problem is that I haven't actually set up a database. kindof dumb of me not to set up the database.
I guess I will try to figure that out now.
UPDATE:
The thing that finally solved this was that cake was looking for the Unix socket to the database in /var/mysql/mysql.sock but mysql was using the socket in /tmp/mysql.sock
I fixed this by creating a symbolic link from the /var/mysql/mysql.sock to /tmp/mysql.sock.

It looks more like MySQL itself is not installed, but the PDO libraries are compiled with your webserver. I am not sure how to check this in OSX, but you can try checkign this link out: http://www.sequelpro.com/docs/Install_MySQL_on_Mac_OS_X
EDIT
Log into MySQL (mysql -u root -p) and create the databas:
create database cakephp
Then create a new username/password and grant them access to this database. Let's say you want to create the username cakephp and the password cakepass:
GRANT ALL ON cakephp.* TO cakephp#localhost IDENTIFIED BY 'cakepass';
FLUSH PRIVILEGES;
And now your database.php config file should look like this:
<?php
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'cakephp',
'password' => 'cakepass',
'database' => 'cakephp',
'prefix' => ''
);
}

No, MySQL does not come with OSX, you'll have to install it.
The easiest solution is to use XAMPP instead of compiling/installing MySQL yourself. It will also come with a separate Apache and PHP, if you don't want to mess with the native OSX versions.

Related

'MySQL server has gone away' [duplicate]

So I am using my cmd on my laravel folder and I tried to do (php artisan migrate:install). 2 errors came up.
[PDOException] SQLSTATE[HY000] [2006] MySQL server has gone away
[ErrorException] PDO::__construct(): MySQL server has gone away
Can anyone please explain what I did wrong?
You have Lost SQL connection to server during query. It is temporally issue. This is because of very low default setting of max_allowed_packet.
Raising max_allowed_packet in my.cnf (under [mysqld]) to 8 or 16M usually fixes it.
[mysqld]
max_allowed_packet=16M
NOTE: This can be set on your server as it's running. You need to restart the MySQL service once you are done.
Use: set global max_allowed_packet=104857600. My value sets it to 100MB.
This is not a Laravel issue, but a general MySQL Issue. Maybe the server is not running. Are you sure you're running MySQL in the background?
Check this link: MySQL Gone Away
Do the following checks in your system:
The Database Engine is running
You have created your database
You have created an user and granted permissions to the database
You have setup the user and the database in your Laravel's .env file.
After this, try to run the migrations command again, which is:
php artisan migrate
As explained Here
Let us know if that helps :).
For me the problem seemed to be that I assigned the wrong port to my Laravel project's .env file. Later, when I matched it with the my.cnf file, it worked.
I'm using Ubuntu 16.04 + nginx + MariaDB + Laravel project.
I encountered the same problem. The solution was to delete the mysql port number from 3306 or 80, and leave it empty
In files
.env DB_PORT=3306 to DB_PORT=
and on
database.php 'port' => env('DB_PORT', '3306'), to 'port' => env('DB_PORT', ''),
In my case the problem was I changed DB_HOST to localhost but it was fixed by keeping it default ie 127.0.0.1 and the port to default 3306.
Here is the configuration for localhost in xampp:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=hmb
DB_USERNAME=root
DB_PASSWORD=
Although I use localhost:8081/phpmyadmin to access my db
In addition to other answers:
Try changing localhost domain to 127.0.0.1 both in .env and config/database.php.
Also If you're using git then check git status and see if any unwanted files have been changed.
Because In my case options array for mysql connection configuration in file config/database.php was blank array somehow. I checked out git checkout config/database.php file and it starting to work fine.
If someone is looking for a solution, try to clean all cache you have, you can manually delete cache files in bootstrap/cache folder.
My solution is due to fix the DB_HOST parameter. Indeed I use PHPStorm and I set the public url in .env in order to use DB tools from the IDE and this broken Laravel.
So I changed DB_HOST to localhost and Laravel now works fine.

silverstripe install with mysql socket

I´m trying to install Silverstripe on a Server with the MySQL Connection Details like following:
Hostname localhost:/tmp/mysql5.sock
Socket /tmp/mysql5.sock
Entering one of these addresses in the Install Form for Database server brings up following error on submit for localhost:/tmp/mysql5.sock:
I couldn't find a database server on 'localhost:/tmp/mysql5.sock': Connection refused"
or for /tmp/mysql5.sock
I couldn't find a database server on '/tmp/mysql5.sock': php_network_getaddresses: getaddrinfo failed: Name or service not known
and just localhost shows this error:
I couldn't find a database server on 'localhost': Connecting to 3.22, 3.23 & 4.0 servers is not supported
Thanks,
Florian
Basically, there was something wrong with the server settings:
This is what I added to _config.php:
ini_set('mysqli.default_socket', '/tmp/mysql5.sock');
then the db connection looked like this:
$databaseConfig = array(
"type" => 'MySQLDatabase',
"server" => 'localhost',
"username" => 'myusername',
"password" => 'mypassword',
"database" => 'mydb',
"path" => '/tmp/mysql5.sock',
);
I have never tried to connect to a mysql server this way.
But maybe the installer fails to recognize a hostname like this, you could try to just write the _config.php file by hand and then run a /dev/build, maybe that works.
about:
I couldn't find a database server on 'localhost': Connecting to 3.22, 3.23 & 4.0 servers is not supported
SilverStripe requires a mysql database version 5, could it be that your mysql server is not up2date?
In setting up SilverStripe and MariaDB on a Mac (Mountain Lion), all that was necessary was to put this into the _ss_environment.php file:
define('SS_DATABASE_SERVER', 'localhost:/opt/local/var/run/mariadb/mysqld.sock');
It was not necessary to change the value of 'mysqli.default_socket'.

Cakephp Shell can't connect to mysql

I'm trying to get a crontab working for Cakephp (running on my shared Dreamhost server for now).
I have a shell set up as per the book, which works fine via either a cron run or manually executing (both via a shell file, e.g:
cron.sh
#########
cd /pathToApp/app/Console
/usr/local/bin/php cake.php updater list_reports
where updater is my shell and list_reports a method thereof.
The problem occurs when I try to access the database in the shell ("Using models in your shell", same link).
Error Message:
Error: Database connection "SQLSTATE[HY000] [2002] Can't connect to
local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)"
is missing, or could not be created.
My DB Set-up (site works fine for http, do I need to add unix_socket or something?
array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'mysql.mydomain.net',
'port' => '3306',
'login' => '*******',
'password' => '*******',
'database' => '*******',
'prefix' => '',
);
Thing I've tried:
Creating a symlink to /var/run/mysqld/mysqld.sock - permission denied
Can I even do this with shared hosting?
Tried various changes to the db settings above, no luck.
I note similar problems have been solved by people replacing localhost with 127.0.0.1, but I'm using a shared host, not local.
It's official! I'm a db!
I can confirm this problem was not caused by either Cakephp or Mysql, but rather for something I'd done myself and forgot about....
I had a little switch in database.php to automatically detect whether I was in the dev, test, staging environment etc. I had the default set to local, and since the bash script didn't pick up $_SERVER['SERVER_NAME'] which I was using for the switch, it defaulted to my local db.
Thanks to ifunk for spotting the clue which enabled me to unravel this and get back on track!
:)

bootstrap error trying to enable drupal module with drush

Trying to install my first drupal module. Following the book did not work because of an "FTP" error so I installed drush. Download seemed to work now the enable command gives me this error. I'm a noob with ubuntu and every part of drupal. Help. Thanks.
NNH#comp:/var/www/drupal$ sudo drush en module_filter
Command pm-enable needs a higher bootstrap level to run - you will [error]
need invoke drush from a more functional Drupal environment to run
this command.
The drush command 'en module_filter' could not be executed. [error]
Drush was not able to start (bootstrap) the Drupal database. [error]
Hint: This error often occurs when Drush is trying to bootstrap a
site that has not been installed or does not have a configured
database.and tried it.
Drush was attempting to connect to :
Drupal version : 7.14
Site URI : http: //default
Database driver : mysql
Database hostname : localhost
Database username : drupal7
Database name : drupal7
Default theme : garland
Administration theme: garland
PHP configuration :
Drush version : 5.3
Drush configuration:
Drupal root : /var/www/drupal
Site path : sites/default
Modules path : sites/all/modules
Themes path : sites/all/themes
File directory path: sites/default/files
%paths : Array
You can select another site with a working database setup by
specifying the URI to use with the --uri parameter on the command
line or $options['uri'] in your drushrc.php file.
Possibly related:
NNH#comp:~$ sudo dpkg-reconfigure -plow phpmyadmin
[sudo] password for NNH:
/usr/sbin/dpkg-reconfigure: phpmyadmin is broken or not fully installed
Drush is basically a command-line interface to interact, manage and configure Drupal and its modules. So if you like to use Drush for any operations pertaining database, you have to go through loop address ie., localhost. But it seems like Drush understands 127.0.0.1 as loop address but not localhost.
Simply by defining these in settings.php you are allowing the Drush to proceed with any database related operations.
Instead of just this
'host' => 'localhost',
use this,
'host' => php_sapi_name() == 'cli' ? '127.0.0.1' : 'localhost',
You typically recieve the error
Command pm-enable needs a higher bootstrap level to run - you will
[error] need invoke drush from a more functional Drupal environment to
run this command.
when you aren't in the root of the site when running the command.
If the error persist try connecting to MySQL straight from the prompt. Try connecting using the values from the Drush output, ie:
mysql -hlocalhost -udrupal7 -p drupal7
If it is working and the site is working then you most likely can rule out database connection problem. Assuming you are developing locally on your own box, you can try changing localhost to 127.0.0.1 in settings.php. This seem to have helped some people.
The site URI setting seems a bit odd.

How do I tell DBD::mysql where mysql.sock is?

Using DBD::mysql with DBI, I am getting the following error when attempting to connect to the database.
DBI connect('database=mydb:host=localhost','someuser',...) failed: Can't connect
to local MySQL server through socket '/tmp/mysql.sock' (2) at ./myscript.pl line 97
Yes MySQL is up and running. The problem is that mysql.sock is not in /tmp.
I know the location of mysql.sock and I currently have it hacked so that it works, I created a soft link to the current location of the mysql.sock file. I would rather not change the MySQL configuration, though this would probably be the easiest thing to do.
Is there a way to go into DBD::mysql and configure it to look for mysql.sock in the correct location?
You can specify the location of the socket in the connect method
my $dbh = DBI->connect("DBI:mysql:database=dbname;host=localhost;mysql_socket=/path/to/mysql.sock","someuser","somepassword", {'RaiseError' => 1});
For further information please have a look at the docs here
$dbh=DBI->connect("DBI:mysql:database=dbname;mysql_socket=/var/lib/mysql/mysql.sock;user=username;password=password");
You don't need to specify host since you're connecting to a socket file.