skip to Main Content

I’m hosting a WordPress site on a subdomain and would like to access the database from the main site. I have added the main user to the WordPress database but can not seem to connect. The connection works fine for non-wordpress databases. Here is my config file:

$db_host = 'localhost';
$db_name = 'database_1';
$db_username = 'main_user';
$db_password = 'main_pass';

try{
$dsn = "mysql:host=$db_host;dbname=$db_name";
$db_connection = new PDO($dsn, $db_username, $db_password);

}catch(PDOException $e){

echo $e->getMessage();

}


$db_host_wp = 'localhost';
$db_name_wp = 'database_2_word_press';
$db_username_wp = 'main_user';
$db_password_wp = 'main_pass';

try{
$dsn_wp = "mysql:host=$db_host_wp;dbname=$db_name_wp";
$db_connection_wp = new PDO($dsn_wp, $db_username_wp, $db_password_wp);

}catch(PDOException $r){

echo $r->getMessage();

} 

The error I am getting is:

SQLSTATE[HY000] [1044] Access denied for user ‘main_user’@’localhost’ to database ‘database_2_word_press’

I have also tried changing the host variable without any luck:

$db_host_wp = 'localhost:3306';

The user has been given full access as shown below.

enter image description here

Is there a setting in WordPress that needs to be changed? Thank you in advance!

2

Answers


  1. its not WordPress problem, your database is not assign to this user. can you check your mysql database from cpanel

    create a user first and assign your database under this user ( you can find the option in mysql database option from capanel

    update user name and password to this field
    $db_username_wp = ‘main_user’;
    $db_password_wp = ‘main_pass’;

    and also your host will be localhost

    Login or Signup to reply.
  2. This definitely seems like an issue with the database user privileges on your database. If you are sure that you have set full permissions and assigned your user to the correct database, then I would definitely recommend reaching out to the tech support of your host to check this further, as they will be able to troubleshoot this much easier than anyone here lacking access to the environment. If they lack support, you might want to consider switching to a new one that could help in similar situations.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search