Fix the error There was a problem connecting to the server localhost

Fix the error There was a problem connecting to the server localhostIf you are developing your site first on a local computer, then when transferring to a hosting service you almost always encounter an error establishing a connection with the database.

But it is not necessary that this will be due to the transfer of the site, the settings for connecting to the database can be changed.

The user you are connected to is deleted, or the password is changed, or MySQL is moved to a separate server.

Also, the appearance of this error may mean the inaccessibility of the database server on your hosting. In any case, there is a problem, and it needs to be solved.

Make sure you have a valid backup of your site before any actions that will be offered below in the instructions!

Does the problem show up in wp-admin

The first step is to make sure that this error message is displayed both on the site and in the administrative panel. To do this, try to enter the admin panel of the site (wp-admin).

If the administrative panel displays a completely similar error message about establishing a connection with the database, you can safely proceed to the next step of this instruction.

If you received the message “One or several database tables are not available,” then you will need to perform an automatic repair of the tables by WordPress mechanisms.

To do this, follow these steps:

  • Open the wp-config.php file and add the following line to it:

 

     one  | define(‘WP_ALLOW_REPAIR’, true);

 

  • After that, go to http: //your-site.com/wp-admin/maint/repair.php
  • Click the “Repair Database” button and wait for the operation to complete.

This may take some time, depending on the size of the data in the tables in the base of your site.

 

Remember any user of your site can obtain that access to this page by accessing it at a direct address. Therefore, after correcting the error, be sure to remove the WP_ALLOW_REPAIR line from the wp-config.php file!

 

If after this step the site has become available and the error message no longer appears, you can safely finish reading the article and be glad that you have managed only with fright.

Otherwise, I recommend continuing reading the notes.

Check wp-config.php file

Check wp-config.php fileThe wp-config.php file is one of the most important files in WordPress.

It contains all the parameters for the normal operation of your site. All settings for connecting to the database are also in this file.

If suddenly you, or someone else (for example, a system administrator), changed your login or password to connect to the database.

Then you need to make changes to the wp-config.php file, remember this.

The following constants are responsible for setting up a connection to the MySQL database:

One  | define(‘DB_NAME’, ‘database name’);

2       | define (‘DB_USER’, ‘database user’);

3       | define (‘DB_PASSWORD’, ‘user password’);

Four | define(‘DB_HOST’, ‘localhost’);

Keep in mind that the constant DB_HOST will not always be localhost, it could be the server’s IP address, or some other address if you use hosting from MasterHost, for example.

In any case, you need to check this information with your hosting provider, or in your hosting account.

But for most hosts, the DB_HOST value will still be localhost, and most often it will not have to be changed.

It is worth mentioning that in some cases you will need to specify a non-standard port for connecting to MySQL, this is done by the following command in the wp-config.php file:

one  |  define(‘DB_HOST’, ‘127.0.0.1:3351’);

where 3351 is the port on which the MySQL server is listening. Check this value with your system administrator.

If you are convinced that all the settings in the file are correct, then you should assume that the problem with connecting to the database is somewhere deeper and you need to dig harder.

Test MySQL server

If your hosting provider allows you to use phpMyAdmin script – try to use it. To do this, go to your account, find the item in the menu mentioning the database and next to it will be a link to phpMyAdmin.

If you have a virtual server (VPS) and you use cPanel or ISPManager, then the link to phpMyAdmin will be on the main page of the server control panel.

In any case, you need to try to enter the database of your site. And, if everything was successful, then once again check the connection data that you entered into the wp-config.php file from the last step of this instruction.

If you are unable to connect to the database via phpMyAdmin, then try this move.

  • Create a file on your computer, call it test.php and add the following code to it:

 

One| <?php

    2| $ resource = mysql_connect (‘localhost’, ‘user’, ‘password’);

    3| if (!$resource) {

four| die (‘Error connecting:’. mysql_error ());

five| }

   6| echo ‘Connected successfully!’;

   7| mysql_close($resource);

 

Instead of “user” and “password,” enter your data to connect to the database. If you have a VPS, you can use the root account.

  • Upload this file to your hosting FTP
  • Open the browser address http: //your-say.com.en/test.php
  • If “Connection error” is displayed on the screen, then a message will be displayed next to it (usually in English), using which you can find some comments on Google or Yandex
  • If “Connected successfully” was displayed, then enter your login and password used to connect to the database in the wp-config.php file, as in the last step

 

If at the opening of this script you received the message: # 1045 – Access denied for user ‘foo’ @ ‘%’ (using password: YES), it means you are using the wrong login or password. Check again and try again.

If you could not connect to the database either through phpMyAdmin or through the test.php file.

I recommend contacting the support service of your hosting provider and figure out what the matter is on the phone. A normal ISP has a 24-hour support service.

Solutions that helped others

Use these recommendations at your own risk! In any case, before starting any edits you should have a working backup copy of your site.

 

Update settings in wp_options

Some users were helped by the following database request via phpMyAdmin:

One | UPDATE wp_options SET option_value = ‘your_site_address’ WHERE option_name = ‘siteurl’;

Instead of “your_addit,” specify the address of the site so that the request looks like this:

one | UPDATE wp_options SET option_value = ‘http: // yoursite.com’ WHERE option_name = ‘siteurl’;

Keep in mind that the wp_options table may be called differently if you changed the prefix of the WordPress tables. In this case, instead of wp_, specify your prefix.

 

Connect as root to the database

Connect as root to the databaseIf you have a VPS and managed to connect using the test.php file to the database as the root user. 

Then try using this data to connect to the database of your website via wp-config.php.

If everything goes well and the site works, then I recommend to go to phpMyAdmin.

Create a new user and specify the login and password of the new user in wp-config.php.

Never work under the superuser account on your battle server.

 

Conclusion

The solution to this problem can be both instantaneous and lengthy. It all depends on the complexity and the situation that caused the appearance of this message.

If you know any other solutions to this problem – write about them in the comments, and I will update the note.

In any case, you can always contact me for advice, and together we will solve the problem. Successes!

 

Leave a Comment