I have a connection.php file that is suppose to connect to a remote database.
Here is the code:
<?php
try {
$conn = new PDO('mysql:host=IP;port=PORT;dbname=DBNAME', 'USERNAME', 'PASSWORD');
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
Now all my variables I believe are correct since I can connect to the database through Toad. I used this same PDO format for my own database connection through localhost and it works fine. I am not sure what the problem is. Since i can use Toad i believe that the server already allows remote access to it, but i am not sure on that. Any input would be nice.
Also this is the Error that PDOException is coming back with:
SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'IP' (111)
111 means connection refused,
It probably means that your MySQL server is only listening the localhost interface.
If you have lines like this :
skip-networking
bind-address = 127.0.0.1
You should comment them In your my.cnf configuration file (add a # at the beginning of the lines), and restart MySQL.
well, in you /etc/mysql/my.cnf :
you should find out this:
skip-networking
bind-address = 127.0.0.1
You gotta comment the first one, and change the ip to 0.0.0.0 or to the ip of your remote host that you want to allow (recommended)
Also, you may don't have a user to connect to that host, i mean, mysql users are like 'root'#'192.168.1.1', so you may try to access with a user that doesn't exists. Try to create it and then grant to it all privilegies.
Just execute this couple commands on mysql:
CREATE USER 'root'#'192.168.1.100' IDENTIFIED BY '***';
GRANT ALL PRIVILEGES ON * . * TO 'root'#'192.168.1.100' IDENTIFIED BY '***';
Also, make sure that mysql is listening to in the right port, if using linux:
netstat -tlpn
should do the work, at least that's how i fixed similar problems.
I was facing the same error for a few hours now.
Turns out I had given the wrong port in my code.
My port to connect was the default port 3306 I was using 3327.
Hope it helps someone maybe.
Related
I am attempting to connect to a remote MySQL server from my local machine virtualhost using the following code:
$conn = mysql_connect("$dbhost", "$dbuser", "$dbpass") or die(mysql_error());
mysql_select_db($dbname, $conn) or die(mysql_error());
My problem is that I am unable to connect locally, receiving the error:
Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060)
This is not the case when I upload the same PHP file to the server. I am able to query the database with no problems at all.
I am unable to connect via command line either, but I can access cPanel which rules out the chance of my IP being banned accidentally.
My local server is running PHP 5.2.9, the remote server 5.2.12
firewall of the server must be set-up to enable incomming connections on port 3306
you must have a user in MySQL who is allowed to connect from % (any host) (see manual for details)
The current problem is the first one, but right after you resolve it you will likely get the second one.
It is very easy to connect remote MySQL Server Using PHP, what you have to do is:
Create a MySQL User in remote server.
Give Full privilege to the User.
Connect to the Server using PHP Code (Sample Given Below)
$link = mysql_connect('your_my_sql_servername or IP Address', 'new_user_which_u_created', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db('sandsbtob',$link) or die ("could not open db".mysql_error());
// we connect to localhost at port 3306
I just solved this kind of a problem.
What I've learned is:
you'll have to edit the my.cnf and set the bind-address = your.mysql.server.address under [mysqld]
comment out skip-networking field
restart mysqld
check if it's running
mysql -u root -h your.mysql.server.address –p
create a user (usr or anything) with % as domain and grant her access to the database in question.
mysql> CREATE USER 'usr'#'%' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON testDb.* TO 'monty'#'%' WITH GRANT OPTION;
open firewall for port 3306 (you can use iptables. make sure to open port for eithe reveryone, or if you're in tight securety, then only allow the client address)
restart firewall/iptables
you should be able to now connect mysql server form your client server php script.
This maybe not the answer to poster's question.But this may helpful to people whose face same situation with me:
The client have two network cards,a wireless one and a normal one.
The ping to server can be succeed.However telnet serverAddress 3306 would fail.
And would complain
Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060)
when try to connect to server.So I forbidden the normal network adapters.
And tried telnet serverAddress 3306 it works.And then it work when connect to MySQL server.
I am using below code to connect MySQL database in PHP.
try {
shell_exec("ssh -f -L 3307:127.0.0.1:3306 ronak#server_ip sleep 60 >> logfile");
$this->_conn = $this->dbh = new PDO('mysql:host=127.0.0.1;dbname=my_db', DB_USER, DB_PASS);
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Couldn't connect to database. Please try again!");
}
I want to direct connect with remote MySQL host. Server is on azure virtual machine. How can I do such a configurations?
I already opened 3306 port in azure portal for MySQL. I don't know how to use it in here without ssh tunnel.
Thanks.
1) change my.conf (whatever your mysql conf file is called). And set bind-address to 0.0.0.0 as it is prob 127.0.0.1
2) stop/restart mysql daemon
Connections now are not limited to those from localhost (what you are when you ssh). The default is localhost for obvious security reason until dev guy tweaks it
3) add database user perhaps coming in from diff ip addr
CREATE USER 'fred7'#'192.168.2.7' IDENTIFIED BY 'my_password';
4) grants
GRANT ALL PRIVILEGES ON test7db.* TO 'fred7'#'192.168.2.7';
5) firewall
For 3 and 4, let's say the mysql server is on aws ec2 and i am sitting at a public library using sqlyog or workbench. That prog will alert me if connect failure stating something like connection failed for user 'fred7'#'gfs6.nyc.comcastbusiness.net'. So it is pretty obvious how to do 3 and 4 then.
Good luck!
How to log with a diferent user in a MYSQL remote database?
Here's what I've done:
Logged as root in the MYSQL:
'create user 'user'#'%' IDENTIFIED BY 'password';
'grant select on *.* to 'user'#'%';
Then I setted a PHP script which connection is this one:
$con = mysql_pconnect("xxx.xx.xxx.xxx","user","password");
$selected = mysql_select_db("database",$con);
Aaaand it isn't working:
I'm using LAMP on a cloud server, by the way;
Warning: mysql_pconnect() [function.mysql-pconnect]: Can't connect to MySQL server on 'xxx.xx.xxx.xx' (10061) in D:\path\index.php on line 21
What am I doing wrong?
EDIT: Not a firewell issue;
You might want to check the MySQL documentation on this specific problem. If I had to guess, I would say that your MySQL server may be bound only to the local (127.0.0.1) address. To troubleshoot you should probably try connecting to the server using the command line MySQL client in order to get a better idea of why exactly the connection isn't being made.
Seems to me like a firewall issue. You should check if the machine hosting the MySQL server allows connection on the port 3306 from external IPs as well.
Check if the server you have to connect to has the firewall open on the port you are trying to connect... default port is 3306
you can use
mysqladmin -h localhost
to see check what is the port mysql is using
I need to connect to a remote MySQL database and have created the following connection code:
<?php
/* Set Variables */
$host="myipaddress";
$db="mydbname";
$username="dbuser";
$pass="mypass";
/* Attempt to connect */
$mysqli=new mysqli($host,$username,$pass,$db);
if (mysqli_connect_error()){
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
echo 'Success... ' . $mysqli->host_info . "\n";
$mysqli->close();
}
?>
For security reasons, I've not provided the actual variable values. When I run this on my development system, I receive
Connect Error (2003) Can't connect to MySQL server on 'myipaddress' (10061)
My PHP is a bit rusty, can someone identify where my code is faulty? Note that dbuser has select, insert and update privileges on the database name set as the variable.
Thanks,
Sid
Edit
I made changes to my.cnf and restarted mysql. I now receive access denied for user 'dbuser'#'mycurrenthostname' (using password YES). When I use mysql -u dbuser -p from command line, I can login. I granted insert, update and select to dbuser with host '%' so that dbuser could connect from anywhere.
I've read the MySQL Reference guide about this error, but am still stuck. Is there a problem with my code, now that my.cnf has been fixed?
Check that your firewall is allowing connections through on port 3306.
Check the MySQL configuration parameter bind-address in my.cnf to ensure that it is allowing remote connections.
There's information and troubleshooting tips here:
http://dev.mysql.com/doc/refman/5.1/en/can-not-connect-to-server.html
According to the MySql documentation , The error (2003) Can't connect to MySQL server on 'server' (10061) indicates that the network connection has been refused. You should check that there is a MySQL server running, that it has network connections enabled, and that the network port you specified is the one configured on the server.
I'm not familiar with php , but the problem might not be in your code.
If your getting the above error even after putting the Mysql port 3306 open access,
Just change bind address of mysql file my.cnf which is located at /etc/mysql/my.cnf
Update the bind address as given below
bind-address = 0.0.0.0
This will allow access to all ips remotely
On AWS ec2 it is working.
I am attempting to connect to a remote MySQL server from my local machine virtualhost using the following code:
$conn = mysql_connect("$dbhost", "$dbuser", "$dbpass") or die(mysql_error());
mysql_select_db($dbname, $conn) or die(mysql_error());
My problem is that I am unable to connect locally, receiving the error:
Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060)
This is not the case when I upload the same PHP file to the server. I am able to query the database with no problems at all.
I am unable to connect via command line either, but I can access cPanel which rules out the chance of my IP being banned accidentally.
My local server is running PHP 5.2.9, the remote server 5.2.12
firewall of the server must be set-up to enable incomming connections on port 3306
you must have a user in MySQL who is allowed to connect from % (any host) (see manual for details)
The current problem is the first one, but right after you resolve it you will likely get the second one.
It is very easy to connect remote MySQL Server Using PHP, what you have to do is:
Create a MySQL User in remote server.
Give Full privilege to the User.
Connect to the Server using PHP Code (Sample Given Below)
$link = mysql_connect('your_my_sql_servername or IP Address', 'new_user_which_u_created', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db('sandsbtob',$link) or die ("could not open db".mysql_error());
// we connect to localhost at port 3306
I just solved this kind of a problem.
What I've learned is:
you'll have to edit the my.cnf and set the bind-address = your.mysql.server.address under [mysqld]
comment out skip-networking field
restart mysqld
check if it's running
mysql -u root -h your.mysql.server.address –p
create a user (usr or anything) with % as domain and grant her access to the database in question.
mysql> CREATE USER 'usr'#'%' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON testDb.* TO 'monty'#'%' WITH GRANT OPTION;
open firewall for port 3306 (you can use iptables. make sure to open port for eithe reveryone, or if you're in tight securety, then only allow the client address)
restart firewall/iptables
you should be able to now connect mysql server form your client server php script.
This maybe not the answer to poster's question.But this may helpful to people whose face same situation with me:
The client have two network cards,a wireless one and a normal one.
The ping to server can be succeed.However telnet serverAddress 3306 would fail.
And would complain
Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060)
when try to connect to server.So I forbidden the normal network adapters.
And tried telnet serverAddress 3306 it works.And then it work when connect to MySQL server.