1 year ago

#368628

test-img

ErIK

Can't connect to Mariadb using a wordpress instance with apache2 virtualhost

I am just having the worst time time making this connection work. At the moment, I am able to connect to my ipv4 and see a message that says "Error establishing database connection. I also created a test php file

Here is the setup for the test php file:

<?php
$servername = "localhost";
$username = "cr-user";
$password = "password";
$db = "tr-wordpress";
// Create connection
$conn = new mysqli($servername, $username, $password, $db);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>                                            

For my wp-admin:

define( 'DB_NAME', 'tr_wordpress' );
define( 'DB_USER', 'cr_user' );
define( 'DB_PASSWORD', 'password' );
define( 'DB_HOST', 'localhost' );
define( 'DB_CHARSET', 'utf8' );

I get a message saying that connection failed. My database has the "cr-user" user with granted privileges to the database under localhost. The way I did it was as so:

CREATE USER cr_user@localhost IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON tr-wordpress.* TO 'cr_user'@'localhost';    
FLUSH PRIVILEGES;

I then read a post about newer Mariadb versions having a bind to '127.0.0.1' line in the config file. I first tried commenting it out to see if it resolved anything. Didn't work. Then I uncommented it and set all my connections to '127.0.0.1', including test phpfile, wp-admin and mariadbuser host address.

$servername = "127.0.0.1";
define( 'DB_HOST', '127.0.0.1' );

And for my Mariadb I ran this command:

UPDATE mysql.user SET Host='127.0.0.1' WHERE Host='localhost' AND User='cr_user';
UPDATE mysql.db SET Host='127.0.0.1' WHERE Host='localhost' AND User='cr_user';
FLUSH PRIVILEGES;

It didnt work. I got the same message. Then I tried trying to connect via the ip externally by changing the previous areas from '127.0.0.1' to the ip. the change I got was that it then sais connection refused.

One thing to note is that if I run:

mysql -u cr_user -p'password'

in ubuntu's command line terminal, I can connect just fine using the username and password. It's only when going through PHP that something isn't working.

Would really appreciate some help here as I am out of ideas. Thank you!!!

php

wordpress

mariadb

apache2

virtualhost

0 Answers

Your Answer

Accepted video resources