PHP: connect with external mysql database - php

I want to connect with external database using PHP. But, it gives this error
Warning: mysql_connect(): Can't connect to MySQL server on 'SERVER_IP_ADDRESS' (110) in /home/<username>/public_html/index.php on line 7
Can't connect to MySQL server on 'SERVER_IP_ADDRESS' (110)
Here is my code.
define("_CONFIG_DB_HOST", "SERVER_IP_ADDRESS"); /* database host name */
define("_CONFIG_DB_NAME", "DATABASE_NAME"); /* database name */
define("_CONFIG_DB_USER", "DATABASE_USERNAME"); /* database username */
define("_CONFIG_DB_PASS", "DATABASE_PASSWORD"); /* database password */
$con = mysql_connect(_CONFIG_DB_HOST,_CONFIG_DB_USER,_CONFIG_DB_PASS) or die(mysql_error());
mysql_select_db(_CONFIG_DB_NAME,$con) or die(mysql_error());
I've also added entry for Remote Database Access Hosts in Host Server
Please guide me if I'm doing anything wrong.

Make sure DATABASE_USERNAME has access from you host (the machine running this script) has access to DATABASE_NAME on SERVER_IP_ADDRESS.
When you grant access on database to use you would use USERNAME#HOST form. Here the HOST means the host where this USERNAME would be connecting from. Usually we keep it localhost. becasue we want to connect by a user who is in localhost according to mysql server. But your local host is not mysql servers localhost. That's why that host part needs to be changed.

Related

connect live server from local server in php mysql [duplicate]

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.

How to make database connection from different website

I have a database in a website called booksiders.com and then I have a new website called kryptotech.co.in in which i am looking to use the database of the first website. So how can connect with the database which exists on another hosting server.
This is the code i am using in kryptotech.co.in for connecting to the database of booksiders.com from kryptotech.co.in which is hosted on another server.
<?php
$con = mysql_connect("booksiders.com","bookside","******");
if(!mysql_select_db("bookside_kryptoDB",$con))
echo "Unable to connect to database";
?>
Please tell me what hostname should be used for doing this.
You will have to allow your server IP in allow remote host access list on remote server and ask them to allow port 3306 in their firewall so that you can connect mysql databases from your hosting server.

mysql_connect(): Connection refused

I was just wondering if it was possible for certain databases to block SQL accesses through mysql_connect. I recently downloaded XAmpp and created a SQL database using my own computer as a local host and wrote a PHP file that accessed that database.
However, when I tried to change the username and host settings so that Xampp would run the PHP files that would then connect to the external SQL database of a free hosting site that I'm trying to use (biz.co.nf), I got the following error message:
Warning: mysql_connect(): Connection refused in /Applications/XAMPP/xamppfiles/htdocs/...
Right now I'm thinking either:
My login credentials to that database are incorrect, or
Somehow the host blocks SQL accesses from external users, but if I were to load my php code into the server using FileZilla, it should work okay?
Please let me know if this is the case.
I'm also fairly certain I have the right login credentials.
Here's what I have:
$host = "fdb13.biz.nf";
$username = "1764941_login";
$password = ________;
$db_name = "1764941_Login";!
$tbl_name = "Members";
//Connect to server
mysql_connect($host, $username, $password)or die("Cannot Connect!");
mysql_select_db($db_name)or die("Cannot select Database");
with my server settings according to my website...
Of course, I omitted my password.
Connections are refused from remote locations to MySQL for security reasons. You can add your IP Address to enable MySQL to listen from your database by following the instructions in this link - Cyberciti biz tips on enabling remote access to mysql database server In your case, add the IP address of the location where your PHP script.
I would also say that
mysql_connect
is deprecated in PHP. Use mysqli instead. Check the comments in the link - PHP original MySQL API

Difficulty connecting to MySQL remotely

When I am connected to the internet, I can connect to my local MySQL server with my public/internet IP address, using MySQL workbench.
However, using the same parameters, I can not connect to the same MySQL server from a website running on PHP. I use the code line below:
new mysqli("41.58.XX.XX", "username", "mysql_pswd", "db_name");
I get the following error message:
(HY000/2003): Can't connect to MySQL server on '41.58.xx.xx'
Note: My configuration includes granting all privileges to the user on all host '%'
Please how can I do this correctly?

Why am I getting mysql_connect(): Lost connection to MySQL server and how can I fix it?

My php code fails to connect to the mysql database on my web server. I get the following error:
mysql_connect(): Lost connection to MySQL server at 'reading initial
communication packet', system error: 110 in filename at line 71.
The same code works fine when run from my development machine pointed at the mysql database on the server I'm trying to run this from (I copied the file up to the server and I'm trying to run it via ssh on that server). I have verified that the mysql user in the script can connect to the database from the server by running mysql from the ssh command line on that server using the same user name and password as specified in my php script.
here is my code:
function jsw_set_cnxn($env){
global $env;
$cnxn = mysql_connect($env['db_svr'], $env['db_usr'], $env['db_pwd'])
or die ('DB Connection Error: ' . mysql_error());
mysql_select_db ($env['db'], $cnxn);
return $cnxn;
}
I've seen this error caused when using the servers IP address when it expects localhost as the server address.
This is because the server is the machine mysql expects but the wrong host coming in. Try using localhost.

Categories