PHP/MySQL simple connection won't work - php

I'm hosting a website on Zymic (free host) that utilizes MySQL. I opened an account, and wrote the SIMPLEST function to connect to the DB. It looks like this:
<?php
$conn = mysql_connect("uuuq.com","paulasplace_sudo","mypassword");
if(!$con)
{
die("Could not connect: " . mysql_error());
}
else
{
echo("mysql connected successfully!");
}
?>
but it throws this error:
Warning: mysql_connect() [function.mysql-connect]: Lost connection to MySQL server at 'reading initial communication packet', system error: 111 in /www/uuuq.com/p/a/u/paulasplace/htdocs/index.php on line 9
Could not connect: Lost connection to MySQL server at 'reading initial communication packet', system error: 111
Any ideas what might be wrong?

This may be just from when you copied the code into your post, but you store the results of mysql_connect() into $conn, but the if statement checks a different variably $con...

For those like me that had this same problem with a virtual machine or where you have all the rights (priviliges as read in phpmyadmin) to make users, the problem for me was that my user for the database was created incorrectly . While creating my DB user i set incorrectly one the parametres right after the name there is an selector where you can choose the server: chosen as default was my error, that says "any server" and is set as % , i had to change it , infact , i had to create a new user with the same name and different server, there i set it to localhost. My problem got solved , i deleted my other user, restarted the server and it worked fine.

I suspect the hostname is "localhost" and not uuuq.com.

Often times these free hosts have their MySQL servers at different addresses altogether as opposed to simply localhost or the site address itself.
If the host provides access to phpMyAdmin, open that then look at the top of the page and you should see something along the lines of
Server: s1.mysqlserver.com
That is the address you want.

Related

unable to connect external server database

how to access cpanel database access in godady server using mysql php connectivity
i have tried a lot but last long it gives me
Warning: mysql_connect() [function.mysql-connect]: Lost connection to
MySQL server at 'reading initial communication packet', system error:
111 in /home/content/q/u/i/quickandkosh/html/searchdata.php on line 6
Could not connect: Lost connection to MySQL server at 'reading initial
communication packet', system error: 111
I'm not sure about go daddy but I know some web hosts need a specific path to mysql server. Most us localhost but others, such as ipage use a full path through the virtual server. Go into your cpanel at godaddy and then "mysql databases" and it should have a "server" title with address below it somewhere in the page.
Just to note, you should also be using mysqli_connect.
I think your host could be wrong, wrong port, a firewall blocked the connection. So I would double check your connection settings.

Remote mySQL connection error

I posted this question earlier but only provided a link to the output instead of posting the output here. With the additional details, I trust there will be enough info.
This script works locally on my server but when loaded on a remote server the error shown after the code below is given... Can someone tell me why it works locally and not remotely. A % wildcard has been set for allowed hosts.
<?php
$version_link = mysql_connect('gjinternetsolutions.com', 'gj_Guest', 'Password1');
mysql_select_db("gj_Software", $version_link);
if (mysql_errno())
{
$error = "<p>MySQL error ".mysql_errno().": ".mysql_error()."\n</p>";
die($error);
}
$version_query = "SELECT * FROM `VersionCheck` where `Software`='RedemptionFee'";
$version_result = mysql_query($version_query);
$version_row = mysql_fetch_array($version_result);
if (mysql_errno())
{
$error = "<p>MySQL error ".mysql_errno().": ".mysql_error()."\n<br>When executing:<br>\n$version_query\n</p>";
die($error);
}
$new_version=$version_row['Version'];
$new_URL=$version_row['URL'];
echo "The latest version of the Redemption Fee Module is: ".$new_version;
?>
This is the error that is given from the above script...
Warning: mysql_connect(): Can't connect to MySQL server on 'gjinternetsolutions.com' (4) in /home/nicedayh/public_html/CheckRemoteMySQL.php on line 3
Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in /home/nicedayh/public_html/CheckRemoteMySQL.php on line 4
MySQL error 2003: Can't connect to MySQL server on 'gjinternetsolutions.com' (4)
At first I thought something was wrong with my script but after seeing it work perfectly on the local machine, I am not certain. Several people have tried loading it remotely and had no success so it is not just one remote machine that has an issue.
I think you have to put :3306 after your database location or whatever port your MySQL is on
Are you able to ping gjinternetsolutions.com?
My server in New Zealand recognises this host name, however my server in the US does not. Your A records may have not propagated to your computer yet, or your name servers NS1.GJINTERNETSOLUTIONS.COM and NS2.GJINTERNETSOLUTIONS.COM might be having issues.
A. Is your mysql database listening on the same server/IP that gjinternetsolutions.com is hosted on since that is what you are using to connect to?
B. If it is, is mysql service running on the db server?
C. If it is, is port 3306 open on the firewall to let traffic through?
D. If it is, is your my.cnf file on the db server set to localhost only using bind-address = 127.0.0.1? If so, comment that out.
E. If it's not, do you have mysql database setup to accept connections from a remote server i.e. not localhost?
http://www.rackspace.com/knowledge_center/article/mysql-connect-to-your-database-remotely
or if you use a cpanel
http://my.kualo.com/knowledgebase/1059-how-to-set-your-whm-server-to-use-a-remote-mysql-server.html
One of these is probably the issue. This at least should get somewhere to look.

mysql_connect() using http protocol

I am trying to connect to a MySql server (not localhost) from my computer using the code below. It is giving this error:
Warning: mysql_connect() [function.mysql-connect]: [2002] Connection refused (trying to connect via tcp://10.6.3.6:3306) in on line 7
I wonder if we can use the http protocol to connect instead of tcp that is being used by default? I searched quite a bit on how to change the protocol, but most of the answers were describing how to connect to localhost, and not much about how to connect to another server. Please help.
PS: I am able to connect to the server by going to http://10.6.3.6/phpmyadmin/...). So I am sure the server is up.
My Code
<?php
$db_hostname = '10.6.3.6';
$db_database = 'db_user11';
$db_username = 'db_user11';
$db_password = '########';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database, $db_server)
or die("Unable to select database: " . mysql_error());
?>
You can't. HTTP is not a protocol suitable for this.
You are talking about 2 processes communicating here (MySQL Server and your app) and they either do it via shared memory, pipes or sockets. Those are the 3 ways that processes communicate with each other.
First of all, HTTP is layered on top of TCP/IP.
In order to connect to something via HTTP, it needs to run an HTTP server. MySQL does not run an HTTP server and there's no current/realistic/supported way to tunnel an SQL connection through HTTP. Even if there was, HTTP is not exactly the best protocol for this.
In short: no. You're trying to solve the wrong problem. You need to configure your MySQL server to allow connections from other machines over the network, give the user you're trying to connect with appropriate permissions to connect from other machines and make sure the MySQL server is reachable from other machines.
Just because the phpmyadmin is hosted on that server, does not neccesarily mean that the database server is in the same IP address. Please ask the domain administrator to give you the details of database server.
Maybe just check and make sure the mysql user account you are connecting with has proper permissions set for remote access.
In other words, this may be obvious but make sure the user has valid remote credentials.
On the command-line ( if you can relate to this ) , one can do this in mysql to add a user allowed to connect from remote connexion:
mysql> CREATE USER 'monty'#'%' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'#'%'
-> WITH GRANT OPTION;
This would create a user, monty with password some_pass, with FULL privileges on all databases, all tables from anywhere. The % is the key here, and is needed for remote access.
MySQL users created through gui tools often set the host to 'localhost' and this would not allow a remote connexion.
Hope this helps. Good-luck.

MySQL Connection String Using PHP

I am trying to connect to the database that is hosted on my server through my local machine.
my server has cPanel 11, and it is a typical shared server, powered by CentOS, PHP and MySQL installed.
to be precise i am holding the reseller account in the same server. i want to access the database between different accounts or domains.
in MySQL connection String i tried defining the domain name, and it isn't working. here is what i tried.
<?php
$link = mysql_connect('mydomain.com', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
this is giving the following error
Warning: mysql_connect() [function.mysql-connect]: [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://bhatkalnews.com:3306) in C:\wamp\www\test\conn.php on line 4
Warning: mysql_connect() [function.mysql-connect]: 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:\wamp\www\test\conn.php on line 4
Fatal error: Maximum execution time of 60 seconds exceeded in C:\wamp\www\test\conn.php on line 4
what do i have to define in connection string to make it work?
Firewall ?
try
telnet mydomain.com 3306
on the command line.
If that doesn't work, the connection is blocked by a firewall, most likely
Did you try adding the port explicitly:
mysql_connect('mydomain.com:3306', 'mysql_user', 'mysql_password');
or the port youre running on if not the default 3306. This of course assumes your server allows remote connections.
Also make sure the user in quest has access from the IP address youre connecting from. This isnt youre issue right now, but it may be the next question you have after you get the server to respond :-)
Please refer,
http://forums.mysql.com/read.php?52,294772
You need to add your IP address/range on Access Host list in Remote MySQL in cPanel.

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