Jump to content

Error establishing a database connection - wordpress-issues


dil_bert

Recommended Posts

 
hello dear phpexperts, 
 
i thought that during the installation process the file wp-config-sample.php  gets to 
wp-config.php - but this is not happening. 
 
 
 
Quote

Error establishing a database connection

This either means that the username and password information in your wp-config.php file is incorrect or we can’t contact the database server at localhost. This could mean your host’s database server is down.
 
  • Are you sure you have the correct username and password?
  • Are you sure that you have typed the correct hostname?
  • Are you sure that the database server is running?
If you’re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress Support Forums.
 
 
guess that i have to edit all the stuff manually now.. 
 
 
 
i want to edit the file now and i will afterwards store it as wp_config.php on the server ab.  
Btw: i am still wondering why this was not happening  so far
 
additionally - i will do the following 
 
First thing you should do is to make sure that you are getting the same error on both the front-end of the site, and the back-end of the site (wp-admin). If the error message is the same on both pages “Error establishing a database connection”, then proceed onto the next step. If you are getting a different error on the wp-admin for instance something like “One or more database tables are unavailable. The database may need to be repaired”, then you need to repair your database.
 
gem. https://www.wpbeginner.com/wp-tutorials/how-to-fix-the-error-establishing-a-database-connection-in-wordpress/
 
 
	

	
define('WP_ALLOW_REPAIR', true);
 
[/CODE]
 
[QUOTE]
once you have done that, you can see the settings by visiting this page: 
[/QUOTE]
 
 
i come back and report all the findings
Edited by dil_bert
Link to comment
Share on other sites

hello again - did not help so far...

 

as all the trials failed - i tried the following: 

https://www.wpbeginner.com/wp-tutorials/how-to-fix-the-error-establishing-a-database-connection-in-wordpress/

Quote

 

Often you will notice this Error establishing database connection when your site gets swarmed with a lot of traffic. Basically, your host server just cannot handle the  load (specially when you are on shared hosting). Your site will get really slow and for some users even output the error. So the best thing you should do is get on the  phone or livechat with your hosting provider and ask them if your MySQL server is responsive. For those users who want to test if MySQL server is running yourself, you can do a few things. Test other sites on the same server to see if they are having the issue. If they are also getting the same error, then most definitely there is something wrong with your MySQL server. If you do not have any other site on this same hosting account
 simply go to your cPanel and try to access phpMyAdmin and connect the database. If you can connect, then we need to verify if your database user has sufficient permission.  Create a new file called testconnection.php and paste the following code in it:

 

 

 

<?php
$link = mysqli_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>

 

Make sure to replace the username and password. If the script connected successfully, then it means that your user has sufficient permission, and there is  something else that is wrong. Go back to your wp-config file to make sure that everything there is correct (re-scan for typos).

well see what i get back here: 

 

Warning: mysqli_connect(): (HY000/2002): No such file or directory in /sites/www.job-starter.de/testconnection.php on line 2
Warning: mysqli_error() expects exactly 1 parameter, 0 given in /sites/www.job-starter.de/testconnection.php on line 4
Could not connect:

 

well i have to muse _ what goes on here..  something goes wrong... 

 

i will digg deeper - and come back again and report all the findings. 
 

 

Link to comment
Share on other sites

hello dear all ,

 

 

well i have still issues with the access of the db

 

Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /sites/www.mysite.de/testconnection.php on line 3
Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known in /sites/www.mysite.de/testconnection.php on line 3

Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in /sites/www.mysite.de/testconnection.php on line 4
could not connect:

 

<?php
if(function_exists('mysqli_connect')){
if(!($link = mysqli_connect('localhost:8005','user','passwd','db-name'))){
die('could not connect: ' . mysqli_error($link));
}
} else {
die("don't have mysqli");
}
echo 'connect successfully';
mysqli_close($link);

 

and i get back the following 

 

 

<?php
if(function_exists('mysqli_connect')){
if(!($link = mysqli_connect('localhost:8005','username','passwd','db_name'))){
die('could not connect: ' . mysqli_error($link));
}
} else {
die("don't have mysqli");
}
echo 'connect successfully';
mysqli_close($link);
	Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /sites/www.mysite.de/testconnection.php on line 3
Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known in /sites/www.mysite.de/testconnection.php on line 3
Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in /sites/www.job-starter.de/testconnection.php on line 4
could not connect:
	

well i wonder what is going on here

 

Link to comment
Share on other sites

hello dear all ,

 

 

well i have still issues with the access of the db

 

Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /sites/www.mysite.de/testconnection.php on line 3
Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known in /sites/www.mysite.de/testconnection.php on line 3

Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in /sites/www.mysite.de/testconnection.php on line 4
could not connect:

 

 

<?php
if(function_exists('mysqli_connect')){
if(!($link = mysqli_connect('localhost:8005','jobstarter','susi19','jobstarter'))){
die('could not connect: ' . mysqli_error($link));
}
} else {
die("don't have mysqli");
}
echo 'connect successfully';
mysqli_close($link);

 

and i get back the following 

Link to comment
Share on other sites

    Could not connect:


**update:** 


i just use this...

     

 


     * @link https://codex.wordpress.org/Editing_wp-config.php
     *
     * @package WordPress
     */
    
    // ** MySQL settings - You can get this info from your web host ** //
    /** The name of the database for WordPress */
    define('DB_NAME', 'my_db_name');
    
    /** MySQL database username */
    define('DB_USER', 'my_user_name');
    
    /** MySQL database password */
    define('DB_PASSWORD', 'mypasswd');
    
    /** MySQL hostname */
    define('DB_HOST', 'localhost');
    
    /** Database Charset to use in creating database tables. */
    define('DB_CHARSET', 'utf8');


and yes:_ i also enabled the debugging mode 

 

    define('WP_DEBUG', false);
    define('WP_ALLOW_REPAIR', true);

 

Link to comment
Share on other sites

hello again dear friends hello dear MaxDD and Barand

 

first of all - many many thanks for the quick reply - great to hear from you. i will check the ressources that you adviced to have a closer look at. 

 

- the php-ressource

 - the wp-class....

 

besides that : my serveradmin told me that i have to be careful with the path / the pipe-path on the server.... 

 

well i think that it has to do with the pipe path to mysql .... 

 

i will digg deeper and i of course will keep you informed 

stay tuned and have a grat day

 

btw: see the images ..that i have attached 

mysql_path_pipe.png

mysql-connections-setup-new-connection.png

Edited by dil_bert
Link to comment
Share on other sites

 

probably the paths are not correct...


 



/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
    define( 'ABSPATH', dirname( _FILE_ ) . '/' );
}
[/CODE]

see here the errors 

 

[CODE]Warning: mysqli_connect(): (HY000/2002): No such file or directory in 
/sites/www.mysite.de/testconnection.php on line 2
Warning: mysqli_error() expects exactly 1 parameter,
0 given in /sites/www.mysite.de/testconnection.php on line 4
Could not connect:
[/CODE]

 

i think that i have to understand the errors...

Edited by dil_bert
Link to comment
Share on other sites

hello again - i have gathered some more infos and insights due to some more tests. 


see the following data: - the outcome of a testscript: 
 

Warning: mysqli_connect(): (HY000/2002): No such file or directory in /sites/www.mysite.de/tests.php on line 3
Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in /sites/www.mysite.de/tests.php on line 4
could not connect

see the script: 

<?php
if(function_exists('mysqli_connect')){
if(!($link = mysqli_connect('localhost','user','passwd','my_db'))){
die('could not connect: ' . mysqli_error($link));
}
} else {
die("don't have mysqli");
}
echo 'connect successfully';
mysqli_close($link);


some ideas:

the paths seem to be a problem - in the config
by the way: the character-code is 1252 ANSI L

any idea and help - i look forward to hear from you

regards

Link to comment
Share on other sites

on a sidenote,. ...

 


<?php
if(function_exists('mysqli_connect')){
if(!($link = mysqli_connect('localhost','user','passwd','my_db'))){
die('could not connect: ' . mysqli_error($link));
}
} else {
die("don't have mysqli");
}
echo 'connect successfully';
mysqli_close($link);

[/CODE]

well i guess that this is saying that 1 parameter was required, 0 were provided. and besides this we surely can say that there absolutly no error checking is being done before calling the function, it's blindly passing a variable that was collected.

interesting: This actually makes sense, given i am probably failing to connect to mysql. 

What we don't see is validation that the credentials work, & i can connect. 

one option would be to ssh to the php server, & then connect over the cli to mysql. Otherwise, we´re just guessing.
Above all - we can say that there probably is no pathing issue. honestly - there are serious doubts that wordpress has a pathing issue, or this is anyway code related.


[CODE]active_connections  18446744073709551563
is this number normal? If not how can I reduce it?

[/CODE]


If this were the cause of the issue, none of the sites would work,  assuming they are all using the same mysql server. The quickest way to reduce it would be to 
reboot the server. Just posting the number alone doesn't tell us enough. best  thing would be to verify with netstat. If they are active connections, then i probably should have a closer look a the proper firewall rules in place blocking outside connections. 
 

hmm - i will have a closer look at all the condidions and settings - and will come back and report all the findings. 

 

have a great day 

Link to comment
Share on other sites

On 6/8/2019 at 8:52 AM, Barand said:

hello dear Barand hello dear MaxDD 

many many thanks for the quick reply - 

@Barand - yo told me to do /use the following:

 


to run the following command: mysqli_connect_error (PHP 5, PHP 7)

https://www.php.net/manual/en/mysqli.connect-error.php
mysqli::$connect_error -- mysqli_connect_error — Returns a string description of the last connect error


Object oriented style

string $mysqli->connect_error;
Procedural style

mysqli_connect_error ( void ) : string
Returns the last error message string from the last call to mysqli_connect().

Return Values ¶
A string that describes the error. NULL is returned if no error occurred.

Examples ¶
Example #1 $mysqli->connect_error example

Object oriented style

<?php

$mysqli = @new mysqli('localhost', 'fake_user', 'my_password', 'my_db');

// Works as of PHP 5.2.9 and 5.3.0.
if ($mysqli->connect_error) {
    die('Connect Error: ' . $mysqli->connect_error);
}
?>

Procedural style

<?php
$link = @mysqli_connect('localhost', 'fake_user', 'my_password', 'my_db');

if (!$link) {
    die('Connect Error: ' . mysqli_connect_error());
}
?>

The above examples will output:

Connect Error: Access denied for user 'fake_user'@'localhost' (using password: YES)
 

Link to comment
Share on other sites

So, clearly you're working with WordPress for this project. WordPress has enough issues and pain points by and of itself - why are you making it harder on yourself? If you're actually (as it seems) working within the WP infrastructure, why not just use the WordPress database object? The connection to the database is already made, you can use it like an ORM (kinda), and depending on how you use it, it kinda sorta sanitizes database interactions by default.

The reason you'll see a check in most WP theme and/or plugin files for the ABSPATH constant is to make sure the WordPress core has been loaded. If you're trying to do something within WordPress before the core is loaded, I'd recommend looking at why you're trying to do it and why it needs to be done exactly then.

I hope that makes sense, and sorry if I'm missing the point of what you're actually trying to do, but it really seems like you're making things much more difficult than they need to or should be.

Link to comment
Share on other sites

hello dear MaxxD and Barand, 

first of all - many many thanks for the reply with all the ideas and suggestions. 
many many thanks for the quick answer with all the ideas, that are very interesting. 


here a little update and more findings: 

by the way: if i do not use USE <db-name> .... then it works propperly


conclusio: if i run 

SHOW GLOBAL VARIABLES LIKE 'PORT';
[/CODE]

i get a concrete output from SQL command SHOW GLOBAL VARIABLES LIKE 'PORT'; ..

or if i run 
[CODE]
    SHOW GRANTS FOR CURRENT_USER;
[/CODE]
i get outputs like this one

[CODE]
    Output from SQL command SHOW GRANTS FOR CURRENT_USER; ..
    Grants for root@localhost
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*4444444rrrrrrwwwww222222444443660283C379783ED8EF54B6EC01DAF8374444444rrrrrrrrrrrreeeeewwwww2CeC3C474F4' WITH GRANT OPTION
    GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION [/CODE]

and now i have to rethink what this does want to say in the context of a - error in trying to establish a db-connection - while installing a wordpress on the frontend


look forward to hear from you 

greetings 
Link to comment
Share on other sites

 

first of all - many thanks dear Barand and Maxxd for the reply and your continued support: 

here some more insights and Output from SQL command SHOW GRANTS FOR 'jo'@'localhost'; ..


Output from SQL command SHOW GRANTS FOR 'jo'@'localhost'; ..
Grants for jo@localhost
GRANT USAGE ON *.* TO 'jo'@'localhost' IDENTIFIED BY PASSWORD '*93C1B40FA5F0FED10E5A0CE946A5E71D59B00860'
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP ON `jo`.* TO 'jo'@'localhost'

 


hmmm - running the SQL-Request works now - without any issue. but for the results : i try to figure out what these results mean. Guess that i can verify the issues. 
 

Link to comment
Share on other sites

BY THE WAY: - My Server admin adviced me to do some extra tests;

- running an installation with another script - a other than wordpress - to see if the installation probably would succeed with another script. 

well i am musing bout these advices - and will follow them.  I must have more insights into the magic. 


will come back and report all findings. 
greetings

Link to comment
Share on other sites

  • 6 months later...
  • 2 weeks later...

When you install a fresh wordpress website, the CMS by default create TWO config-php files which are:

  • wp-config-sample.php
  • wp-config.php

Once, installation is done, then it is always recommended to remove "wp-config-sample.php" from the server, so that hackers can't use it to hack your website.

Now coming, to your question, where did you added the DB Name, ID and Password?

But, if your website was already live and shifted your wordpress website from one server to another then you may notice an 404 error all across the pages. This may be because of SSL issue and .htaccess. 

You may have to install and activate ssl on your website using cpanel read this

My website issue fixed using ssl installation.

 

 

 

ssl.jpg

  • Like 1
Link to comment
Share on other sites

On 1/9/2020 at 2:45 PM, isabelwilliams said:

 

Hello dear Isabellwilliiams and hello dear dip7 

 

many many thanks for the reply and all your tipps and the shared insights.  I am happy to hear from you.  I will follow your advices and will do all you mention.

The SSL-Ideas were very interesting and i like them.  And also the htaccess-things. 

I will digg deeper into these techniques. 

 

Again - many thanks for all you do

have a great weekend 

cheers 

dil_bert

 

On 1/9/2020 at 2:45 PM, isabelwilliams said:

When you install a fresh wordpress website, the CMS by default create TWO config-php files which are:

  • wp-config-sample.php
  • wp-config.php

Once, installation is done, then it is always recommended to remove "wp-config-sample.php" from the server, so that hackers can't use it to hack your website.

Now coming, to your question, where did you added the DB Name, ID and Password?

But, if your website was already live and shifted your wordpress website from one server to another then you may notice an 404 error all across the pages. This may be because of SSL issue and .htaccess. 

You may have to install and activate ssl on your website using cpanel read this

My website issue fixed using ssl installation.

 

 

 

ssl.jpg

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.