brint / wordpress-cookbook

Development repository for Opscode Cookbook wordpress
https://supermarket.chef.io/cookbooks/wordpress
Apache License 2.0
80 stars 174 forks source link

Error Establishing Database Connection (a possible solution to this) #66

Open tden012 opened 8 years ago

tden012 commented 8 years ago

I tried using the nginx recipe of this cookbook today on an ubuntu 14.04 machine and it all installed beautifully. BUT...

When I tried to run Wordpress nothing happened. This turned out to be an easy fix - nginx wasn't running, so I had to type:

sudo service nginx start

This started nginx, even though there was no output from the console.

The second problem I ran into was that on accessing the main web page, I got the error: "Error Establishing Database Connection"

Took me a while to figure out - and after some googling, I came across the cause. Turns out that Wordpress doesn't like the database host being referred to as 'localhost'. Instead, you have to use '127.0.0.1'.

Changing this is quite easy. You have to do this from within your cookbook:

If you are using your own cookbook, you can set the host in recipes/default.rb like this:

node.default['wordpress']['db']['host']  = "127.0.0.1"

or alternatively, you can set it in attributes/default.rb like this (useful if you follow the wrapper cookbook pattern):

default['wordpress']['db']['host'] = "127.0.0.1"

This allows everything to work as expected. But I'm curious - does anyone know why using 'localhost' doesn't work?

burtlo commented 8 years ago

I left everything standard. Applied the cookbook. See this error.

Wrapped the cookbook to replace the db host value to 127.0.0.1 and I still see this error.

burtlo commented 8 years ago

The user 'wordpressuser' was created in the database with no password. I was able to login without being prompted:

$ mysql -u wordpressuser
mysql> \q

So I updated the password for the 'wordpressuser' by setting to the password value I found in /var/www/wordpress/wp-config.php.

$ mysql -u wordpressuser
mysql> SET PASSWORD FOR 'wordpressuser'@'localhost' = OLD_PASSWORD('PASSWORD_FOUND_IN_WPCONFIG');
mysql> \q
$ mysql -u wordpressuser -p
burtlo commented 8 years ago

Actually upon further examination it looks like wordpres is still using the USER@'localhost' even if I specified the following default['wordpress']['db']['host'] = "127.0.0.1". So I tried to drop back to not specifying the value of the database host and it generated a user with no password again.

+---------+-----------+-------------------------------------------+
| User    | Host      | Password                                  |
+---------+-----------+-------------------------------------------+
| root    | localhost | *5AE22F70B5073B1582E0D3511BB225FB0E218E89 |
| root    | 127.0.0.1 | *5AE22F70B5073B1582E0D3511BB225FB0E218E89 |
| wp_user | localhost |                                           |
+---------+-----------+-------------------------------------------+
3 rows in set (0.00 sec)

Looking through the cookbooks I found that the database cookbook has the resource responsible for the generation of the user. When I debug and step my way through it it seems to create the correct repair sql here.

"CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'PASSWORD'"

Before the connection is closed on the mysql adapter I check to make sure that the user has been created with the password:

repair_client.query("SELECT User,Host,Password from mysql.user").to_a
=> [{"User"=>"root", "Host"=>"localhost", "Password"=>"*5AE22F70B5073B1582E0D3511BB225FB0E218E89"},
 {"User"=>"wp_user", "Host"=>"localhost", "Password"=>""},
 {"User"=>"root", "Host"=>"127.0.0.1", "Password"=>"*5AE22F70B5073B1582E0D3511BB225FB0E218E89"}]

It seems that the value is set correctly but then when the execution of the run completes it is no longer present. Which leads me to believe that something is taking place when it comes time to grant permissions to the user.

When granting permissions to the user it looks like the database password is necessary again. And because it is missing in the definition in the wordpress::database recipe it essentially clears that password.