I'm trying to connect to a remote MySql database and I get this error message:
Warning: mysqli_connect(): (HY000/2002): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\myLocalDiectory\RemoteConn.php on line 9 "Resource not found" error 404.
Here's my php file that I'm trying to use:
<?php
define("DB_SERVER", "serverName");
define("DB_USER", "userName");
define("DB_PASS", "password");
define("DB_NAME", "dbName");
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
Am I missing something? Thanks for any help!
The first thing that I would check (if you haven't done so) is that you can in fact connect to the database from the computer that runs your PHP script. This to rule out a network or firewall problem.
The first thing would be pinging the server. In a DOS prompt run:
ping servername
Where "servername" is the same string that you put in your PHP script above. If this does not reply with a string similar to the one below, specifically, the first word is not "Reply":
Reply from 192.168.239.132: bytes=32 time=101ms TTL=124
This means that there is most likely no connectivity between the computer running the PHP script an the mysql server. I would then check if the server and the computer are properly connected to the network, if the server is up, an if there is not firewall in your computer running the PHP script or on the server.
Now, if your test above shows "Reply" to the ping, you can test if you can connect to the Mysql service from your php server. For this you can use Mysql workbench (http://dev.mysql.com/downloads/workbench/) and from there create a connection with the database parameters that you are giving to your script. If you cannot connect with Mysql workbench, you might need to disable a firewall in your Mysql server, a firewall in your computer running PHP, or enable the Mysql server to accept remote connections for the database and username that you use in your PHP script (some distributions Mysql server are installed to only accept local connections for safety).
If the problem is a permission in the server (the user can only connect locally but not from a remote computer for instance), you can enable the permission in the Mysql server with the GRANT command: http://dev.mysql.com/doc/refman/5.1/en/grant.html
define("DB_USER", "userName");
Try root instead of userName and define DB_HOST.
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 running an apache webserver on a server on my local network.
I have a microsoft SQL server 2008 r2 database running on a different machine on the same network. My webserver is hosting a lot of pages, and are all fine and can be accessed locally and externally. However, when I try to connect to the SQL database using php, I get hit with the following error.:
Failed to connect to MySQL: No connection could be made because the target machine actively refused it.
I am at a total loss at this point as I have opened port 1433 and have spent hours trying to figure this out.
The code that I am using is a very basic connect script:
<?php
// Create connection
$con = mysqli_connect("mycomputer", "myname", "password", "my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
If anyone can help, I would sincerely appreciate it as iIam about to give up on this project!!!
Thank you in advance.
As i understand - you use MS SQL , so you must use mssql_connect not as mysqli_connect
Check here
Cause its another database and another driver (extension ) using to connect
I'm pretty new at php and I'm getting an error whenever I try to load a php page that requires access to the database I set up on phpMyAdmin
Here is the error:
Database connection failed: Can't connect to local MySQL server
through socket '/tmp/mysql.sock' (46) (2002)
The code I'm using for the connection is this:
<?php
define("DB_SERVER", "localhost");
define("DB_USER", "xxxx");
define("DB_PASS", "xxxx");
define("DB_NAME", "tester");
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
// Test if connection occurred.
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
I set up phpMyAdmin on my server by following the directions from the site that hosts my website, so I'm pretty sure I did that correctly.
I've been able to get databases to work before on my computer by using WAMP, but this is the first time I've actually tried getting everything working online, so I don't know if this is a stupid error on my part, or if it's something bigger.
I don't know what a socket is either so I'm not sure how to troubleshoot this.
Thanks for your help!!
You have to find a way to connect to your MySQL instance. There are two ways, TCP (networking, even if that network address is 127.0.0.1) and sockets (a file such as /var/run/mysqld/mysql.sock). Generally, MySQL is configured out of the box to listen on both, and again in general there's little difference between the two. You have to be careful when creating or editing permissions that they match the connection type, user bob#localhost is different from user bob#127.0.0.1. I tend to use sockets and that's also what your script is trying to do by default. You could tell it to connect via TCP, but it's just as easy to tell it the proper path to the socket.
Anyway, for me the quickest way to figure it out is to try the command line tool. If you can do mysql -u root -p at the command line and get a MYSQL> shell prompt, type in "STATUS" and look for the Connection line, which might read Localhost via UNIX socket; and a bit further down you might see a line like "UNIX socket: /var/run/mysqld/mysql.sock" in which case you just tell your PHP script or global PHP configuration about the socket path, because right now it's looking in /tmp/mysql.sock which doesn't exist.
You can also see this when you log in to phpMyAdmin, it should display on the right hand side of the page some "Database Server" information -- look here for the "Server" line which might read something like "phpMyAdmin demo - MySQL (192.168.30.23 via TCP/IP" (a clear indication you're connecting over TCP).
Anyway, whichever method you use to find the path to the socket or deciding to use TCP networking, see the PHP manual page for information about using that to connect. You can also just set the global path in php.ini for the socket (which is what I'd suggest you do), then you don't have to set it manually in each script as it just uses the php.ini setting as a default. You may need to set each of the values for pdo_mysql.default_socket, mysql.default_socket, and mysqli.default_socket (though in your code you're only using mysqli, so technically you only need to set the last value...but why risk confusing your future self with questions like "why would mysqli work but pdo fail?" -- just set them all now and don't worry about it).
Hope this helps.
That either means you don't have the privileges to access phpMyAdmin or that socket isn't there. Try reinstalling phpMyAdmin.
I'm new to php, and while trying to make a connection on one of my pages to the database I set up on the phpMyAdmin page of my site.
I get this error:
"Database connection failed: Can't connect to local MySQL server
through socket '/tmp/mysql.sock' (46) (2002)"
I don't know what a socket is, or why it's trying to go to what looks like a temp file, so I don't even know where to being to troubleshoot this.
The code I'm using to make the initial connection is this:
<?php
define("DB_SERVER", "localhost");
define("DB_USER", "xxxx");
define("DB_PASS", "xxxx");
define("DB_NAME", "tester");
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
// Test if connection occurred.
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
I know I should have access to the MySQL databases, since I logged on and made them myself.
I previously set the page up by using WAMP on my computer, and everything worked fine.
It's just when I tried making it live on the site that I ran into this error.
Any help would be awesome!!
Try using 127.0.0.1 instead of localhost.
If that does not solve it, and you have root access to your server, try the following command
service mysql restart
To restart the mysql server.
The first option will probably work. Again, if you have root access to your server, you should change the mysql config to support sockets, since it's better than the TCP-ip connection.
After contacting my hosting service several times, it seems the error occurred because my hosting service recently changed their specifications and now uses "mysql" instead of "localhost" in the host and server fields. I had used "localhost" before with another host service, so I didn't think to change this, and the latest help articles on my host's website had not updated to reflect this.
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.