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

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.

Related

Copying Drupal website and transfer to another hosting and domain

How to copy the site on the CMS Drupal? At the moment, I just completed the export of the database, then made a copy of the site from the hosting. And uploaded to the new hosting, in the domain directory, the copied site, and through phpmyadmin imported the database. I got this error ...
Error The website encountered an unexpected error. Please try again
later. Error messagePDOException: SQLSTATE[HY000] [1045] Access denied
for user 'rvsrru_c1035'#'localhost' (using password: YES) in
lock_may_be_available() (line 167 of
/home/aufhcrw3/public_html/includes/lock.inc).
You can use this tutorial
Copy project
Create user and add permission. It's you can change on phpmyadmin
Change settings.php
$databases = array (
'default' =>
array (
'default' =>
array (
'database' => 'yourdatabasename',
'username' => 'databaseusername',
'password' => 'databasepassword',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
);
Import DB. As I understand you have already done :)
You need to update the settings.php file which is located in sites/default, and add the right database connection credentials. You'll also need to search and replace any references to the old URL to the new URL in your database, but that's not the problem you're facing right now and is best addressed by another question / google search
Have you read the documentation on this? Migrating a site
#Gariko, please change database credentials in settings.php file. Update database name, database user and password of new environment. Check for host also.
After this clear cache by drush, via db tables or admin.
If who have this promblem. Reading please this is site - https://www.drupal.org/docs/7/backing-up-and-migrating-a-site/migrating-a-site
Next step. Create new user for DB in phpmyadmin or cpanel. And change setting in file - seting.php. See above!

CakePHP 2 cannot connection with MySQL

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)

Laravel Homestead SQLSTATE[HY000] [2002] Connection refused after server move

I moved an application that I'm working on into a new dev server, a Laravel Homestead VagrantBox on a Mac OSX host. Upon doing so, I ran php artisan:migrate to update my database and this went through without a hitch.
I decided to create a new user to continue testing, so I created the route
Route::get('/newuser', function()
{
User::create([
'username' => 'someone',
'email' => 'someone#someone.com',
'password' => Hash::make('password')
]);
return 'Done.';
});
When I visit /newuser, however. I am getting the following message.
SQLSTATE[HY000] [2002] Connection refused
Now, I know that my database config must be correct, as I received no errors when I ran php artisan migrate and my database migrated successfully. However, just to be safe, I checked my database config.
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost:33060',
'database' => 'site',
'username' => 'root',
'password' => 'apassword',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
I figured that maybe there was an issue with the way I defined the port I was on, so I added the parameter:
'port' => '33060'
Upon doing this, the error message changed from "Connection refused" to "No such file or directory"
I'm at a loss. Does anyone have any pointers?
Now, I know that my database config must be correct, as I received no errors when I ran php artisan migrate
It's a better than even money bet when you're stuck on something that it's one of your assumptions that's the problem. It's possible your Laravel application is reading different credentials during a command line run, or that the migration had nothing to do, or for some weird PHP reason the errors were suppressed during your migration run. I'd check the credentials Laravel's using during the context your errors are cropping up. Add the following code to your newuser route to see what Laravel's reading.
$default = Config::get('database.default');
var_dump($default);
$config = Config::get('database.connections.'.$default);
var_dump($config);
I had same problem and i saw my database server was unresponsive i restarted and it works... sometime it cause when wrong configuration.
I posted this answer if someone will have same issue and mysql unresponsive :)

Connect to an external Database (other Host) on Drupal 7

I want to connect to an external Database on another Host (not localhost) on Drupal because i want to migrate Content with the Migrate Module.
thats in my settings.php
$databases['for_migration']['default'] = array(
'driver' => 'mysql',
'database' => 'dbname',
'username' => 'username',
'password' => 'password',
'host' => 'other-host.com',
'prefix' => '',
'port' => '',
);
And in another method im calling the database via:
$query = Database::getConnection('default', 'for_migration')
thats the error which appears:
PDOException: SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'host.com' (13) in MigrateMigration->__construct() (Zeile 16 von /var/www/html/x/sites/all/modules/migrate_x/x.inc).
So if i call a database on localhost, there are no errors and all migrates perfectly. But i cant connect to a database on an external host.
That could be due to:
1) connection limitation (firewall block, for example).
2) the account you are using does not allows logins outside of "localhost". In this case, you will have to edit your account hostname mask on the 'user' table inside the 'mysql' database for '%' (which means, any address) or the machine IP address you are accessing from.
It was my fault. I have set httpd_can_network_connect on the remote server to on.
But the solution was to set it on my local machine. After that i can connect to an external Host. So it was a SELinux Problem.
Thanks to #fobia

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