Amazon AWS database connection error with valid settings - php

I'm running both a wordpress site as well as a mediawiki off of the same web servers. The mediawiki site works great! The wordpress site, meh. Not so much. I keep getting the common database connection error:
Error establishing a database connection
And as far as I can tell the settings between the mediawiki site and the wordpress site are nearly identical.
Here's the media wiki config first since that one's working:
## Database settings
$wgLBFactoryConf['class'] = 'LBFactorySimple';
$wgDBtype = "mysql";
$wgDBservers = '';
$wgDBserver = "db.example.com";
$wgDBssl = true;
$wgDBname = "jfwiki";
$wgDBuser = "admin_ssl";
$wgDBpassword = "secret";
And here's what the wordpress database connection settings look like since they are not:
/** MySQL database username */
define('DB_NAME', 'jokefire');
define('DB_USER', 'admin_ssl');
/** MySQL database password */
define('DB_PASSWORD', 'secret');
/** MySQL hostname */
define('DB_HOST', 'db.example.com');
/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');
/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');
/** Contact the database over a secure connection */
define('DB_SSL', true);
I realize that they're not exactly the same. But I think you can make an easy correlation between the mediawiki settings and the settings for wordpress. And they look similar enough to think that wordpress should be working. Right?
The only real other difference is the name of the database each site is using, which I guess makes sense.
But the fact that medawiki works fine tells me that the user and password set for both sites has access to the database.
Just for laughs I use the account settings from the wordpress config to demonstrate that I can connect to the DB on the command line. Again, it's the same account info that I have in the wik site:
#mysql -uadmin_ssl -p -h db.example.com -D jokefire -e "show tables" | head -5
Enter password:
Tables_in_jokefire
wp_bp_activity
wp_bp_activity_meta
wp_bp_chat_channel_users
wp_bp_chat_channels
Also, I created a basic php script to see if it could connect to the database
<?php
$link = mysql_connect('db.example.com', 'admin_ssl', 'secret');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
And to my surprise it can't connect!
php testconnect.php
Could not connect: Access denied for user 'admin_ssl'#'ec2-54-86-143-49.compute-1.amazonaws.com' (using password: YES)
Why am I surprised that it can't? Because again 1) the wiki can connect to the database no problem. And 2) I can connect to the db on the command line using the same credentials.
My API MySQL Client version is:
Client API version mysqlnd 5.0.10 - 20111026 - $Id: c85105d7c6f7d70d609bb4c000257868a40840ab $
Hope this helps narrow down the problem. Thanks!

The host name could easily be the issue.
Try select user,host from mysql.user where user='admin_ssl' order by host;
As is often the case on AWS servers, what works on localhost or 127.0.0.1 as a host is quite different than a host coming in from elsewhere.
From your PHP sample, the mysql server cannot authenticate the user and host. My bet it is the ec2-54-86-143-49.compute-1.amazonaws.com part of it.
A quick test would be to go on a mysql prompt on the server in question, and perform a create user and grant for such a user. Other people would recommend creating a 'admin_ssl'#'%' user so it works regardless of hostname coming in. I would be careful with that approach.
In order to lock down security, I am willing to create a user on AWS such as create user 'dbAdmin'#'mc83.newyork.comcastbusiness.com' IDENTIFIED BY ... to ensure I don't user wildcards like '%'.
Don't forget the grants. But that is getting ahead of the issue at hand, which is a connect.
good luck.

Related

Cannot connect to MySQL database on remote server using php mysqli

I am trying to use mysqli to insert some data into a MySQL database (let's call the schema myDatabase), but cannot successfully connect. Here's the code snippet to connect:
...
$config = parse_ini_file('../includes/config.ini');
$username = $config['username'];
$password = $config['password'];
$dbname = $config['dbname'];
$server = $config['server'];
$conn = new mysqli($server, $username, $password, $dbname);
if (!$conn || $conn->connect_error) {
die( 'Connection Failed: ('.$conn->connect_errno.') '.$conn->connect_error);
}
...
I get the following result:
Connection Failed: (1045) Access denied for user 'myUser'#'my.laptop.ip.address' (using password: YES)
Here's some details on the set-up, in case they are relevant:
The code is on my laptop running Windows 7 and using PHP 5.3.5 that came with xammpp.
The database is hosted on a remote server with MySQL5.1.52. I created a user to which I granted all privileges on myDatabase.*. No host was specified for the user (e.g. 'myUser'#'%'), as I am still in development and don't know the ip address where the code for the live application will be hosted.
If I ssh onto the database server, I can connect to mysql using the credentials for myUser and access the tables in myDatabase. I have another schema on this same server which is accessed by a different user, and have been able to use mysqli to connect without any problems.
Just to be sure it wasn't a typo, I dropped the user, and created it again, copying and pasting the username and password from the config.ini file used in my php code (and flushed privileges, of course). I did this again, except this time the host was specified, e.g. CREATE USER 'myUser'#'my.laptop.ip.address' IDENTIFIED BY 'myPassword'. I keep getting the same error and now I'm completely stumped.
Help, please.
On your mysql machine hit:
GRANT ALL PRIVILEGES ON database.* TO 'myUser'#'%' IDENTIFIED BY 'newpassword';
FLUSH PRIVILEGES;
This will allow the user to connect from any host. Once it works, you can limit it to just a specific host and database.
Okay, this is strange, but it appears the problem had to do with the password I was using. The original one contained some special characters ($, & +). When I changed it so that it only contained numbers, letters and underscore, it worked.
Is this real, or did I accidentally do something else without realizing that turned out to be the actual solution?

Error establishing a database connection with WordPress and Azure

I have hosted a WordPress site, http://edufunweb.azurewebsites.net/, on Azure. It was working fine until I wanted to host another site. So I went to the edufun wp-config.php file in Microsoft Webmatrix and changed the line
$table_prefix = 'wp_';
to
$table_prefix = 'wp_2';
(or maybe it was the other way around) in order to have different tables in the database for each of the sites. Then I deleted the other site and now with whichever codes above I get a
Warning: mysql_connect(): Access denied for user 'abcd'#'xxx.yyy.zzz.xx' (using password: YES) in D:\home\site\wwwroot\wp-includes\wp-db.php on line 1461
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 eu-cdbr-azure-north-c.cloudapp.net. 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.
Moreover, if I connect to http://edufunweb.azurewebsites.net/wp-admin/
I still get the same error.
Your help is highly appreciated as I need my website back on track!
I don't think this is a WP multi-site issue. This typically is caused when the connection string to the host mysql server is not correct.
Here's an easy way to test this.
Open wp-config.php and locate the following lines.
/** MySQL database username */
define('DB_USER', 'user_name');
/** MySQL database password */
define('DB_PASSWORD', 'user_password');
/** MySQL hostname */
define('DB_HOST', 'some_hostname');
Next, open your WP site in the Azure Management Portal. Go to the Configure tab and scroll down to Connection Strings. Click "Show Connection Strings". Copy it into a text editor and compare the values to what you see in wp-config.php. If those values are not the same then keep both copies and do the next step.
Next, open your local shell and open mysqladmin by typing in the following, replacing the values from above here below (without single quotes), note there is no space between the -p parameter and the password value itself.
mysqladmin -u user_name -puser_password -h some_hostname
One of those connection values should be correct and if so replace the one that is not correct with the one that is. If neither work, then go back to the Azure Management Portal to your site, click on Linked Resources, click the item that appears and you should be in the Clear DB management portal where you can get the correct connection information for your MySQL Database.
After you have sorted you can go configure your WP Multi-site. Here is an article on how to do that for Azure Web Apps, http://azure.microsoft.com/en-us/documentation/articles/web-sites-php-convert-wordpress-multisite/
hope that helps.
define( 'WP_ALLOW_MULTISITE', true );
define('MULTISITE', true);
define('SUBDOMAIN_INSTALL', false);
define('DOMAIN_CURRENT_SITE', '');
define('PATH_CURRENT_SITE', '');
define('SITE_ID_CURRENT_SITE', 1);
define('BLOG_ID_CURRENT_SITE', 1);
those lines add to config.php and check wp-admin, there is an option network admin. Go to the network admin and after that go to the settings and setup network from Network Setup option.
Go to the Site option and Add New Site.

MySQL remote access trouble?

I have a database set up on a godaddy server. It is configured to allow remote access, and there are a couple of websites I'm running which need to access this data. It works when accessed from another godaddy site, and I can connect from my development environment both at work and home. We recently set up hosting with mydomain.com.
Here is the code block that triggered it:
function connect(){
$servername = "XX.XX.XXX.XX";
$dbusername = "databaseusername";
$dbpassword = "mahpassword";
$dbname = "databasename";
try{
$newMysql = new PDO("mysql:host=".$servername.";dbname=".$dbname, $dbusername, $dbpassword);
}
catch(PDOException $e){
echo 'connection Failed: '. $e->getMessage();
die;
}
}
and now I'm getting this error message on the new site:
connection Failed: SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'XX.XX.XXX.XX' (111)
The only problems I can think of is that either for some reason there are a limited number of IP addresses the MySQL database will connect to by default (which seems squirrely), I'm getting blocked by a firewall on the MySQL server (again.. doesn't make sense to me), or there is some setting on the mydomain hosting server disallowing remote requests (?)
I'm new to this kind of thing, so I'm open to any suggestions. I could probably just set up another database on the new site, but I don't want the hassle of keeping them synchronized if I don't need to. What might be wrong? Are there any workarounds?
[edit]
connected to remote database via console (mysql -h XX.XX.XXX.XX ...), the privileges were found under the information_schema database, a quick select * from SCHEMA_PRIVILEGES and select * from USER_PRIVILEGES shows that 'databaseusername'#'%' has sufficient privileges. Not that it helped me any, but maybe it'll help someone down the road.
[/edit]
As it has been more than a year since I asked this question, I suppose I need to answer it just to close it.
It turns out that godaddy had blocked mydomain.com servers via firewall ("Remote access" was limited). so in order to accomplish what I wanted to do, I had to copy and store the database on both sites.

Connect to Amazon RDS with PHP

I am trying to connect my RDS Instance with my PHP connection file.
This is what I have in my file:
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'User Name');
define('DB_PASSWORD', 'Password');
define('DB_DATABASE', 'DATABASE');
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());
I replaced localhost with my endpoint (rds thing) url, username and password with my RDS Instance user and pass and database name the one I've set when I created the instance. But it doesn't seem to work.
Is there anything else I have to do that I am not aware of or should it work?
RDS instances do not have a static IP address attached to them, you always use the endpoint for the host. For example:
database1.jlsdfjhgsdf.us-east-1.rds.amazonaws.com
If you are connecting to the instance with a username other than the root database account, you will need to grant the user privileges.
Check security group settings. If you are connecting from another EC2 instance on Amazon you will need to attach that security group. If you are trying to connect from outside AWS, you will need to whitelist the IP address you are accessing from.
Some ideas:
Try using the actual IP of the instance, then it should work.
Did you authorized access to your DB instance?
You may want to have a look at Get Started with Amazon RDS to properly setup your RDS instance
I was facing a similar issue whilst trying to connect an EC2 Apache server using PHP to the RDS MySQL instance.
Weirdly I could establish a connection via CLI - once in mysql running status will tell you which user youre logged in with, plus the port, server name etc.
Turned out some AMI images have SELinux enforcement - meaning the apache server cant send network requests as pointed out by this gentlemen (http://www.filonov.com/2009/08/07/sqlstatehy000-2003-cant-connect-to-mysql-server-on-xxx-xxx-xxx-xxx-13/)
Other points:
Make sure inbound ports are set for your RDS DB
In MySQL make sure the host is set to '%' as opposed to localhost
Always use the endpoint string to connect as the RDS IP changes
I was recently having a lot of trouble with this also but was able to fix it. I made sure my security groups (for the RDS and for EC2) were allowing each other. I was able to run my script from the terminal and connect to my database also from the terminal, but I couldn't get the script to run/connect to MySQL from a browser. It turns out I did not have mysql-server installed-- once I installed that and restarted httpd and mysqld it worked like a charm.
This article is what led me to installing mysql-server and the service starts/restarts. Hope it helps! -- http://www.rndmr.com/amazon-aws-ec2-easy-web-serverset-up-guide-with-a-mac-and-coda-2-256/
Just accepts all incoming connections.
I also had the connection problem between the ec2 (apache + php server) and the RDS (Mysql server) when following the tutorial at http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Tutorials.WebServerDB.CreateDBInstance.html.
I solved it by using the double quote when specifying the connection value while the guideline is using single quote.
define('DB_SERVER', "localhost");
define('DB_USERNAME', "User Name");
define('DB_PASSWORD', "Password");
define('DB_DATABASE', "DATABASE");
I was trying to connect to my DB instance using node-mysql. I found that I the endpoint that RDS provided me with did a DNS lookup. Followed that up and changed the URL to that one. I was only able to connect with mysql via command line until then. When I changed it to the resulting endpoint after the lookup, node-mysql was finally able to connect.

Error establishing a database connection on wordpress

i have a blog with wordpress but sometimes i have problem with that
i got blow error
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 %s. 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
and this is my config file
define('WP_HOME','http://www.mysite.com');
define('WP_SITEURL','http://www.mysite.come');
define('DB_NAME', 'mydbname');
/** MySQL database username */
define('DB_USER', 'dbusername');
/** MySQL database password */
define('DB_PASSWORD', 'dbpassword');
/** MySQL hostname */
define('DB_HOST', 'mysite.com');
why i got this error sometimes what do you think about this problem?
i contact with my admins server and they said "we dont have any problem with server and mysql service"
does my server have problem or my config have problem?
If your are not using default port 3306 for MySql, do give the host name with port number.
define('WP_HOME','http://www.mysite.com');
define('WP_SITEURL','http://www.mysite.come');
define('DB_NAME', 'mydbname');
/** MySQL database username */
define('DB_USER', 'dbusername');
/** MySQL database password */
define('DB_PASSWORD', 'dbpassword');
/** MySQL hostname */
define('DB_HOST', 'localhost:3307'); //If you are not using default port number, do give post number along with localhost.
This would work
why i got this error sometimes what do you think about this problem?
That's the key; if it only happens sometimes, it's an intermittent issue at the webhost. The webhost needs to look in the MySQL logs and PHP logs for connection errors, timeouts, MySQL crashes, etc, anything that will point to the cause of the intermittent issues.
If you had "Error establishing a database connection" all the time, that points to a definite error you made in wp-config.php.
Tell the webhost and administrator to look at their logs to see when the MySQL server is going down and why.
If this is your own server, you need to know the OS and other details so you can search Stack Overflow for the information. Log locations vary, depending on the OS.
If you get it 'sometimes' then it means the PHP Scrpt wasn't able to connect to the database. One of the various reasons could be database server down for a while.
first type mysql -u root -p and enter the your mysql password
then type GRANT ALL PRIVILEGES ON mydbname.* TO dbusername#localhost;
then FLUSH PRIVILEGES;
and finally exit
Try changing your DB_HOST to localhost
** MySQL hostname */
define('DB_HOST', 'localhost');
Furthermore, have you set up the database and filled in the username, password & database in your config?
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.
You can do this by adding the following line in your wp-config.php file:
define('WP_ALLOW_REPAIR', true);
http://cdn4.wpbeginner.com/wp2-9/dbrepair.gif
Remember, the user does not need to be logged in to access this functionality when this define is set. This is because its main intent is to repair a corrupted database, Users can often not login when the database is corrupt. So once you are done repairing and optimizing your database, make sure to remove this from your wp-config.php.
Regard
LatestTutorial.com
get the correct DB host name from the database
use following quesry for mysql
select ##hostname;
change the value of the DB_HOST to output of above query
Some time this happen , just because of incorrect credentials so check your credentials or try to reset your user name and passward , some time host issue or you can repair the database just go to your wp-config file and write
define ( 'WP_ALLOW_REPAIR' , true);
and repair it and don't forget to remove this line after repair.
There are many reason for the issue if you get the error only sometimes
If your database is very big, it can not handle the queries. ( You probably see 'too many database connections' or 'user already has more than 'max_user_connections' active connections' errors on error logs. So try to reduce database tables size.
If you got the error on both backend and frontend, check your database credentials are correct.
If the error only on the front end. try to repair database by adding the following line of code in wp-config.php
define('WP_ALLOW_REPAIR', true);
Then go to 'http://www.yoursite.com/wp-admin/maint/repair.php' and click 'repair and optimize database' button.

Categories