Unknown MySQL server host - php

When trying to connect to my database server, i encounter the problem of unknown host:
Warning: mysqli::mysqli() [mysqli.mysqli]: (HY000/2005): Unknown MySQL server host 'xxxxxxxxxxxxx:port' in index.php on line 18
the line 18 is that line where i try to request to connection the the MySQL server:
$this->db = new mysqli($db_host, $db_user, $db_psw, $db_name);
I host my database on the 1&1 website hosting company.

This is usually the case when name resolving doesn't work on the host. If your connect destination is always the same, you might want to use its IP address for connecting instead.

If you use this code:
$Mysqli= new mysqli('mysql2.servidoreswindows.net:3306',
'usu', 'pass', 'dbname');
you can try to write host without port
That is:
$Mysqli= new mysqli('mysql2.servidoreswindows.net', 'usu', 'pass', 'dbname');

Please pay attention with AWS security groups:
In my case I can connect to RDS from my computer through Telnet and
I can connect through Mysql Workbench also but I cant connect from
my EC2 instance that is within the same VPC as the RDS.
The solution was:
I have created a security group (exampl1) for RDS and assigned to it.
I have created a security group (exampl2) for EC2 and assigned to it.
and I have assigned the RDS security group (exampl1) to the EC2 too. << this saves me.
Info: If your EC2 has assigned 2 or more security groups, then in the RDS security group inbound source has to create rules as many security groups has your EC2 assigned.
Amazon docs says:
The EC2 instance in the VPC shares the same VPC security group with the DB instance.
http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Overview.RDSSecurityGroups.html

I know this is an old question. But I ran into this same issue today, and none of the answers I found were the right one.
The issue in my case ended up being that port 3306 is not open on our firewall, and I was attempting to connect to an internal mysql database from an external server.
I'm not sure why the error it gives you is "Unknown Host" in this case. I would have expected something more like "Unable to connect." or "Connection refused.", but using the exact same code from an internal server worked fine, leading me to this conclusion.

Make sure you're not including the "http://" part. I was using http://example.com in my .env file. When I left it as example.com it worked.

Related

Can't connect to database with error (HY000/2002): No connection could be made because the target machine actively refused it

I'm kind of new to programming and I'm only trying to connect to database using xampp but it gives me this error.
Warning: mysqli_connect(): (HY000/2002): No connection could be made
because the target machine actively refused it.
$conn = mysqli_connect('localhost', 'root', '', 'db');
I tried running the same code on a different PC and it works fine.
so i guess the problem lies within PC that I'm working on.
Any suggestions what to configure?
If you are sure that login and password are all correct, then check port number.
Default MySQL port is 3306. Maybe you've got different one on this machine.
Or, it is also possible, default port in PHP may be changed.
See XAMPP configuration to see MySQL port and add it to your server address.
PS: you may also see privileges for "localhost" connection. See 'user' table in 'mysql' database. As you have XAMPP installed, you should have phpmyadmin as well.

Can't access my AWS hosted database using PHP

I am having a huge issue with accessing my AWS database from within the php code for a site i am building. I have tried many different connection methods, and all seem to give an error that says
Warning: mysqli_connect(): (HY000/2003): Can't connect to MySQL server on '(the endpoint of my hosted DB)' (111)
I've looked up error 111, but I can not seem to understand why it applies to me in this scenario. I can access my database from any computer, using the same information I'm using here to try to connect. I'm starting to think it's the host I'm using for my site that's the problem. I'm using the free biz.nf host just for testing, but I'm not sure my site host really should have any effect on the php inside my site. Here's my connect statement
$mysql_host="DB endpoint";
$mysql_user="myusername";
$mysql_pass="mypassword";
$my_db="myDBname";
$con = mysqli_connect($mysql_host,$mysql_user,$mysql_pass);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Thanks for the help!
You likely need to update your security group and route tables for your subnets to allow the host on biz.nf to access the RDS instance.
Check your RDS dashboard for the given instance, and examine the settings on your security group and subnets.

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.

PHP 5.3 Upgrade - Can't Connect to MySQL Server

I've done a bit of research on this today, but nothing seems to address this issue. I recently upgraded to PHP 5.3.3 from 5.1.6, as well as upgraded MySQL to 5.5 from 5.0. Afterwards, the following code generates an error saying "Can't connect to mysql database":
$connection = mysql_pconnect($dbhost, $dbusername, $dbpassword);
if (!$connection) {
//Can't connect
die('Could not connect: ' . mysql_error());
return;
}
And get the following error:
Warning: mysql_pconnect(): Can't connect to MySQL server on '199.59.157.103' (13) in /var/www/html/ws/Cust/customerWS_1_1.php on line 19 Could not connect: Can't connect to MySQL server on '199.59.157.103' (13)
I am able to connect to the remote host via the command line, and have tried everything from resetting the password to shutting down IP Tables. I'm kind of at a loss - so any help would be appreciated.
We had a similar problem with the same error message "Can't connect to mysql database" and maybe the following points will be also usefull:
check mysql user privileges (in our case and with external connections we had the old server IP address (allow access) in the table) GRANT ALL PRIVILEGES ON tablename.* TO 'username'#'145.1.1.2';
-> reload privileges after changes (FLUSH PRIVILEGES;)
disable the SELinux parameter. In new Plesk versions (>10?) this would be set enabled automatically...
--> in /etc/selinux/config change the line that says:
SELINUX=enforcing to SELINUX=disabled
See further details: http://googolflex.com/?p=482
There is a PHP 5.3 mysql driver restriction related to old authentication scheme hash stored in the database by previous mysql server. This could cause a problem with stored passwords so the databases will be unaccessible with old passwords.
To solve the problem you should recreate the mysql users with the same login and same password.
When you say "I am able to connect to the remote host via the command line" do you mean, you are connecting to the MySQL server REMOTELY? Or that you are SSHing to your MySQL server and connecting to it from the MySQL server's command line? If the latter, it's possible that when you upgraded to MySQL 5.5 you accidentally stopped MySQL from binding / listening on a public IP. Check your my.cnf.
Otherwise: is your PHP code being run on the same server as your MySQL server? It's possible that your permissions could be off. MySQL permissions are very particular about connecting to the MySQL server via it's IP address if the client only has access to connect from 'localhost', and vice-versa.
First you should make sure that the connect parameters $dbhost, $dbusername and $dbpassword are correct. Assuming these are correct, you should check your mysql ini settings. I would assume that the bind-address is set to 127.0.0.1 or localhost, but not to a public IP address.
Also note that it's a security risk if you can connect to mysql from a public IP address. You may want to look into a solution that does not require such connection.

mysql_connect doesn't work on a certain host

I get this everytime I use mysql_connect() no matter what database I choose:
Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'IP' (111) in filename.php on line 17
A MySQL error has occurred: Can't connect to MySQL server on 'IP' (111)
The exact same file works on my personal website fine. I have tried multiple databases hosted on different servers and it always gives that output.
The database itself is hosted on the same server, but using its full IP in mysql_connect(). Using localhost:port doesn't work either as it says:
Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in filename.php on line 17
A MySQL error has occurred: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
But using the IP should work as it has worked calling it via the same file hosted on other servers.
This is the code:
$connect = mysql_connect($db_url,$db_user,$db_pass); // connects
if ($connect == false) exit("A MySQL error has occurred: " . mysql_error());
Now since the file works on other servers i am guessing it is something to do with the server it is on and might need something changed. I don't personally have root access to the server (just my part of the shared host). Is there anything I can do i php, editing the php.ini file or something I should pass on to someone with root access?
Edit: Ok it turns out that the server doesn't have access to outside databases, so thats why the IP didn't work. Thanks for all your answers but we have decided simply to change hosting provider. We need to be able to access an outside database.
This is on a hosting service? Check their documentation, there will be something that tells you where to find mysql. It isn't necessarily localhost.
For example, on startlogic.com, you use: yourdomain.startlogicmysql.com
Can you connect using mysqladmin using the same host, username and password?
mysqladmin -h $db_url -u $db_user -p $db_pass
Replace $db_xxxx with real values.
If that works from the same host as your php script, then sudo to the apache User and try the same test. One of those must be failing.
EDIT: nevermind on sudo part, I noticed that you don't have root access.
Something else to try: Use '127.0.0.1' instead of 'localhost'. I have had issues before where mysql stupidly assumed it could silently change 'localhost:' to '/var/run/mysqld/mysqld.sock'.
Your wording is not very clear, I hope you are not thinking you can connect to the same mysql server from any old web server just because you know the IP address and port number. If the web host is at all competent, they have probably firewalled mysql so it is only accessible through their own web servers.

Categories