CakePHP 2 cannot connection with MySQL - php

I am trying to set up CakePHP 2 in my virtually owned server. The only step missing is connecting MySQL. I've edited my database.php file to include the following.
'datasource' => 'Database/Mysql',
'persistent' => false,
'database' => 'paulncks_money',
'host' => 'server189.web-hosting.com',
'login' => 'paulncks_teller',
'password' => '**********',
However, cake php gives me this error
Error: A Database connection using "Mysql" was missing or unable to connect.
The database server returned this error: SQLSTATE[HY000] [2006] MySQL server has gone away
I've migrated my database onto the server and tried every possible hostname.
One possible fix
I tried being more specific and included the port on my hostname.
'host' => 'server189.web-hosting.com',
This change returned a new set of errors. I don't know if I am any closer to solving my problem, but these errors are so bizzare.
Warning (2): Packets out of order. Expected 0 received 80. Packet
size=5526600 [CORE/Cake/Model/Datasource/Database/Mysql.php, line 184]
Warning (2): PDO::__construct() [<a
href='http://php.net/pdo.construct'>pdo.construct</a>]: MySQL server has
gone away [CORE/Cake/Model/Datasource/Database/Mysql.php, line 184]
Warning (2): PDO::__construct() [<a
href='http://php.net/pdo.construct'>pdo.construct</a>]: Error while reading
greeting packet. PID=294504 [CORE/Cake/Model/Datasource/Database/Mysql.php,
line 184]
Am I closer to connecting?

May be i'm too late but this can help some one else
Possible reasons for SQLSTATE[HY000] [2006]
The most common of them is (point 4 from the link) :
max_allowed_packet = 1M
Try to increase to 4M, 8M or 16M (to do that you need access to mysql config file)
One advice don't show mysql server settings (host, login and so on)

Related

connect external db with codeIgniter

i am new in code igniter,
i tried to connect with local db then successfully connected,but while connecting with external db it resulting an error
A PHP Error was encountered
Severity: Warning
Message: mysqli::real_connect(): (HY000/2003): Can't connect to MySQL server on 'xxxxx.gridserver.com' (110)
Filename: mysqli/mysqli_driver.php
Line Number: 202
Backtrace:
File: /var/www/html/restapi/application/libraries/REST_Controller.php
Line: 375
Function: __construct
File: /var/www/html/restapi/application/controllers/Project.php
Line: 26
Function: __construct
File: /var/www/html/restapi/index.php
Line: 292
Function: require_once
i am not familiar with mysql,
i searched more for it, but i have no idea what they speaking about it,
is there anything to do for external db connecton
Thank you in advance!
Check your config->database.php to ensure the
$db['default']['hostname'],$db['default']['username'],$db['default']['password'],$db['default']['database'] are set correctly.
Anther possibility is you're trying to connect a MySQL server remotely but the server itself is blocking remote connection. So make sure that you are able to connect your mysql database using the same set of host, username, password through Mysql client software such as "MySQL Workbench"
As stated by Tan See Youu, it's likely that you may have incorrect credentials in the database.php or indeed named remote connection is being blocked by the server. Though in some cases hosts offer tunnles to be used to allow remote connections through a secured connection. You might want to ask your hosting if they have a SSH tunnel that you could use if they do block direct connections.
1.Check your application/config/database.php
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost', // your database machine name, this could have localhost, ip address or domain name.
'username' => 'yourmysluser',
'password' => 'yourmysqlpassword',
'database' => 'yourdb',
'dbdriver' => 'mysqli',
Check your mysql database, by default remote access to the MySQL database server is disabled for security reason. However, some time you nee to provie remote access to database server from home or a web server. You can enable remote mysql access with mysql console or phpmyadmin or another database tool dependent your host.
refer to this :
Enable remote MySQL connection: ERROR 1045 (28000): Access denied for user
and
https://www.youtube.com/watch?v=xXOq9U9fzOo

Drupal: PDOException: SQLSTATE[HY000] [2002] Connection refused in lock_may_be_available()

I tried installing Drupal on my local server, and everything worked fine. I installed Drupal on my localhost, and then I tried to transfer the same Drupal directory on my server using FileZilla. Changed my settings.php file in accordance to my server MySql settings as follows:
$databases = array (
'default' =>
array (
'default' =>
array (
'database' => 'manas_drupal',
'username' => 'XXXXXXX',
'password' => 'XXXXXXX',
'host' => '127.0.0.1',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
);
However, when I tried accessing the website where the Drupal installation should have been present, I encounter the following error:
PDOException: SQLSTATE[HY000] [2002] Connection refused in lock_may_be_available() (line 167 of /home/manasge/manas.getevangelized.com/drupal/includes/lock.inc).
Now, line 167 of lock.inc contains the following function:
function lock_may_be_available($name) {
$lock = db_query('SELECT expire, value FROM {semaphore} WHERE name = :name', array(':name' => $name))->fetchAssoc();
if (!$lock) {
return TRUE;
}
$expire = (float) $lock['expire'];
$now = microtime(TRUE);
if ($now > $expire) {
// We check two conditions to prevent a race condition where another
// request acquired the lock and set a new expire time. We add a small
// number to $expire to avoid errors with float to string conversion.
return (bool) db_delete('semaphore')
->condition('name', $name)
->condition('value', $lock['value'])
->condition('expire', 0.0001 + $expire, '<=')
->execute();
}
return FALSE;
}
What seems to be wrong here? Everything works fine on my localhost, but I can't seem to get this thing working on my main host. For reference, this is the link where my Drupal directory is hosted: http://manas.getevangelized.com/drupal/
Also, I have an empty database named manas_drupal defined in PHPMyAdmin already. Also, I made sure that the username and password for MySQL in settings.php were entered correctly.
Check MySQL is running correctly.
This happened to me because the server that was running MySQL had killed off the process, although the specifics of why this happened are not relevant. When my instance of drupal went to connect to the database it was given 'Connection Refused'.
I then attempted to directly connect to the database but I had similar issues connecting. A thread I found here suggested restarting MySQL. When I checked the status of MySQL it gave me the following error:
$ sudo service mysql status
MySQL is not running, but lock file (/var/lock/subsys/mysql[FAILED]
After restarting it, my drupal instance worked as normal again.
Your database shouldn't be empty, export your database from localhost and import it in your empty database in your online server.

Error on PDO SSL connection

I am getting the following error when trying to connect from my Windows box to my Linux box:
Warning: PDO::__construct(): SSL operation failed with code 1. OpenSSL Error messages: error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number
This is the connection code:
$dbE = new PDO(
'mysql:dbname=db_name;host=host_ip;',
'username',
'password',
array(
PDO::MYSQL_ATTR_SSL_KEY =>'client_side_path\client-key.pem',
PDO::MYSQL_ATTR_SSL_CERT =>'client_side_path\client-cert.pem',
PDO::MYSQL_ATTR_SSL_CA =>'client_side_path\ca-cert.pem'
)
);
$dbE->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'Connected';
Any ideas welcome.
Fixed it. The reason was because the path in Server B to the CA was incorrect. Its worth noting that to fix this I tried a connection outside of PHP directly from server A and it came up with an error "bad other signature confirmation" - I then loaded the CA cert and found it was empty. Rebooted the server, set the correct paths in both my.cnf and the server start call and all fixed. Really worth trying the mysql direct connection as it gave a very different error which was much more useful than the PDO one.

mysqli::mysqli(): (HY000/2002) localhost not working

I am trying to connect to my data base and i get an error when I use 127.0.0.1 instead of localhost.
Warning: mysqli::mysqli(): (HY000/2002): 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.
If I use localhost instead of 127.0.0.1 it works fine. Anyone have any insight into this?
the code used to connect is
<?php
$db = new mysqli('127.0.0.1', 'root', '', 'dbname');
?>
Check the sql configurations. I'm going to list a few that should be checked that could cause this option. I do recommend sharing what version of php and sql you are using. Are you using LAMP? It is the same problem listed in the above comments. Newer versions of LAMP have changed the default to 127.0.0.1 for localhost. This is why it comes across as a different problem and you do need a different approach to fix it.
1) if my.cnf has a line reading skip-networking comment it out.
2) check the ect/hosts.allow file make sure there is a line that says
mysqld: ALL
3) check the ect/hosts.deny file and make sure there is only 1 line stating
ALL:ALL
Let us know if that fixed the problem.

MySql Error: cannot connect to local MySql server through socket '/var/run/mysqld/mysqld.sock' (2), while other pages do connect correctly

I understand this problem has been a recurring problem on this site, but my issue is a little different than the previous ones.
PROBLEM
Some pages use the correct socket directory, while other pages try and connect through an incorrect socket directory or this is what I believe the problem is based on the error i am receiving.
DETAILS
HOST: example.com
cakePHP version: 1.3.2 (Not my choice).
Page's content comes from database.
URL: http://example.com
My website has 2 sections:
anonymous section
login section for members or admin
The anonymous section works. It accesses the database, adds the content, and funcitons as it should.
ERROR
The error occurs when I click a link "view more.." under "Job Links" on the home page. A login form should pop up, instead i receive the error "cannot connect to local MySql server through socket '/var/run/mysqld/mysqld.sock' (2)".
In addition, after I login via the "Members login" button, also on the home page, with the correct credentials, it also produces the same error.
QUESTION
Why would different sections on my webpage try to access the sockets through different directories?
ADDITIONAL STUFF
I signed up today and this is my first post, so feedback on my post regarding enough information would be helpful for future posts.
Thanks for your time.
UPDATE
Upon further research, MySql has been using the socket directory /var/run/mysqld/mysqld.sock from the start. Not sure what this means yet, but continuing research..
database.php file
class DATABASE_CONFIG {
var $default = array(
'driver' => 'mysqli',
'persistent' => true,
'host' => 'redlabelcom.netfirmsmysql.com',
'login' => 'bcp',
'password' => '********',
'database' => 'bcp',
'prefix' => '',
'encoding' => 'UTF8',
//'socket' => '/var/run/mysqld/mysqld.sock', // I've tried commenting out all variations of socket and port
//'port' => '/var/run/mysqld/mysqld.sock', // nothing works.
);
var $test = array(
'driver' => 'mysqli',
'persistent' => false,
'host' => 'redlabelcom.netfirmsmysql.com',
'login' => 'bcp',
'password' => '********',
'database' => 'bcp',
'prefix' => '',
'encoding' => 'UTF8',
//'port' => '/var/run/mysqld/mysqld.sock',
);
}
?>
This problem is really strange. I guess this has something to do with mysql connection. MySQL is a daemon, usually configured in /etc/mysql/my.cnf. There you define how the client will connect the MySQL server:
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
If your socket is wrong (there's no such socket on the server), you are probably connecting different mysql host/port from anonymous/secured parts of application. This is possible (I mean different db connections from different application parts). Check your configuration (host, port, etc.) and try to connect from MySQL console:
$ mysql -h hostname -u username -p dbname # enter password
and check if you can connect. If you can connect from console, you'll surely connect from any server-side application (php, python, whatever).
It's an application that someone else was developing, right, and now you have to maintain it?
PROBLEM SOLVED!
Thanks everyone for your support!
This was an interesting issue that initially looked more complicated than the actual problem.
cakephp version 1.3.2 is set up to make a new connection to the database if you need to access the phpbb data fields. The new connection uses a different configuration than what is set up in the database.php file.
Below is a detailed description with file locations.
the 'app/webroot/discussion/common.php' file makes a NEW connection to the database using a different set of MySql parameters than the database.php file. Again, it does NOT use database.php's MySql configuration.
Instead, cakephp defines new connection variables located at:
'app/webroot/discussion/config.php'
To solve the problem, simply change the
$dbhost => localhost -> $dbhost => yourservername
$dbname => wrongname -> $dbname => rightname
etc..
Keep in mind that it is possible the previous developer changed these values, so if this doesn't help you then good luck.
BTW, the error message above was the result of trying to connect to localhost.
To resolve this issue, I ran the following commands:
mysqld --tc-heuristic-recover=ROLLBACK
systemctl start mariadb.service

Categories