Connecting to remote MySQL server using PHP
- 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 thebind-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.