How should I refer to a MySQL server on a web host? - php

When using PHP to connect to a MySQL database on a web host, what is the best way to refer to the server?
The MySQL admin page on my web host says what the IP address of the server is, but can I use something else other than the IP number?
Eg.
$con = mysql_connect("l00.50.10.10","user","password");
if (!$con) { die('Could not connect: ' . mysql_error()); }
When I use localhost I get this output:
Could not connect: Can't connect to local MySQL server through socket
'/var/lib/mysql/mysql.sock' (2)

You can refer to the script's system with "localhost" as the host.
$con = mysql_connect("localhost", "user", "password");
However, connecting to the IP is most likely failing because you have a lowercase L where you should have a one digit (but I'm hoping the IP in the question was just an example).
Also, use mysqli or PDO instead of mysql, which is on the verge of deprecation.

You can the database server's host name, e.g. mydbbox.example.com. Using the IP address may be faster, as no DNS lookup will be necessary.
Also see this SO question.

The answer to this question depends on how your host (or their control panel) configures MySQL privileges. The privileges are IP or hostname specific, so often connecting by IP or by hostname may not have the same connection results. If in doubt, I would stick to exactly the settings they give you.
Read more: http://dev.mysql.com/doc/refman/5.0/en/account-names.html

If database host is same computer - use localhost. MySQL will connect by local socket, not tcpip (network) when you use localhost with port number or not.
Sometimes server does not support socket connections, and you will get error like this:
Could not connect: Can't connect to local MySQL server through socket ...
EDIT/CORRECTED:
If you cant connect by socket - use 127.0.0.1. It will force conncetion by tcpip.
If database server is installed on other machine - you can use IP address or domain.
IP address is better in most cases, because connection can be established even when DNS (domain name server) is not working correctly or when domain expired.
And one advice - use mysqli_ instead of mysql_ functions in new application. New functions are very similar in use, but more secure.
Old mysql_ functions are obsolete and they are not recommended for new applications. They are still available in PHP, because of compatibility with old, outdated software (CMS systems, e-commerce systems etc
PHP Help says (about mysql_connect):
Use of this extension is discouraged. Instead, the MySQLi or PDO_MySQL
extension should be used.
More about connecting with localhost - info from MySQL Reference
On Unix, MySQL programs treat the host name localhost specially, in a
way that is likely different from what you expect compared to other
network-based programs. For connections to localhost, MySQL programs
attempt to connect to the local server by using a Unix socket file.
This occurs even if a --port or -P option is given to specify a port
number. To ensure that the client makes a TCP/IP connection to the
local server, use --host or -h to specify a host name value of
127.0.0.1
Note, that "--port" and "-P" options are decribed in other context than PHP, but connection mechanism works similar and localhost is "special" name, doesnt matter is this PHP, console or some other software.

Have you tried to type in google something like this?
php Could not connect: Can't connect to local MySQL server through
socket '/var/lib/mysql/mysql.sock' (2)

This depends mostly on how your host has things configured. From my experience you will use "localhost", the server's IP address or a subdomain of some sort set up by the host.
I'd start by testing "localhost" as this is the default for most shared hosting environments.
$con = mysql_connect("localhost", "username", "password");

Related

How to connect a PHP app to MySQL via pipes?

I have found glimpses of the fact that it is possible to connect PHP to MySQL via pipes in their documentation of MySQLi, but I cannot, for the life of me, find anyone explaining what is needed.
The host parameter claims:
When possible, pipes will be used instead of the TCP/IP protocol.
But when is it "possible"? I have my own machine, and I definitely have the necessary privileges to achieve this, I just don't know how. Connecting to the host localhost reports "Localhost via UNIX socket" when examining the host_info.
Trying to follow one (downvoted) comment from that page, and connecting to host ., with socket parameter set to mysql, causes a 2002 connection error.
How do I tell it to (always) connect via a pipe instead?
Today I had the same issue and it required much time to solve this on Windows.
I can establish a named pipe connection with the following code:
$connection = new mysqli('.', 'user', 'pass', 'database', null, '\\\\.\\pipe\\name_of_pipe');
The server, where I want to connect to, has the following configuration:
[mysqld]
skip-networking
enable-named-pipe
socket=name_of_pipe
Using '127.0.0.1', 'localhost' or NULL as hostname doesn't work. Also you must specify a path to the named pipe and not just the name of the pipe.
Unfortunately the PHP documentation is a little bit weak here...
Named Pipes only work under Windows.
Also
Whenever you specify "localhost" or "localhost:port" as server, the MySQL client library will override this and try to connect to a local socket (named pipe on Windows). If you want to use TCP/IP, use "127.0.0.1" instead of "localhost". If the MySQL client library tries to connect to the wrong local socket, you should set the correct path as in your PHP configuration and leave the server field blank.
It is not mentioned in the actual PHP documentation, but it should be still valid.

Can't connect to MySQL server on 'ipaddress' (110)

So I have a PHP file hosted on Namecheap server.
$db=mysql_connect ("ipaddress", "user", "pass") or die ('I cannot connect to the database because: ' . mysql_error());
and it gives this error:
I cannot connect to the database because: Can't connect to MySQL server on 'ipaddress' (110)
I CAN connect to this DB using mysql workbench outside of the network just fine.
I have experienced this issue. What I did was use the internet address instead of your public IP/DNS.
Since, I'm using Linux I do ifconfig and you will see inet addr: xxx.xxx.xxx.xxx and use that IP as your host instead of the public IP/DNS. On Windows, Simply use your local IP address.
That's it!
If you are using MySQL for your database solution (which seems odd due to the usage of IIS on a Windows Server operating system)
Try running (As Root):
GRANT ALL ON Database.* TO Username#'IPAddress' IDENTIFIED BY 'PASSWORD';
Where the second is the permissions that you are granting on, this is a place holder for all
This will allow a connection from the IP you specify
also A problem is with connecting to your MySQL engine from inside your network, you will naturally connect from an internal IPV4 Address (192.168.0.x for example) this does not require portforwarding. BUT if you are using:
mysql_connect('WANIP', 'User', 'password'); you will have to forward port 3306 to your server. http://www.portforward.com for assistance.
Edit:
http://dev.mysql.com/doc/refman/5.0/en/can-not-connect-to-server.html
The manual for this subject, this may provide some assistance
If you are using Microsoft SQL Server check this link out:
http://blogs.msdn.com/b/walzenbach/archive/2010/04/14/how-to-enable-remote-connections-in-sql-server-2008.aspx
I have faced this issue when installing opencart on my server. When i was using Windows server it requires IP address of website/domain. Now when shifted to linux hosting, linux hosting accpted as localhost And done. I could install opencart

Have two websites on different server but need one database using php and mysql

I want one website on one server and another website on another server, but only 1 database for both using php and mysql.
is it possible? if yes then how?
Yes. its possible. Normally you dont put your db on distant server. You should keep the db server on same data center so you avail high speed internal network link.
If your websites are in host web1 and web2 and database is in dbhost1, then in web1 and web2 connect to mysql with dbhost1 as host name.
mysqli
$mysqli = new mysqli("dbhost1", "user", "pass");
PDO
$dbh = new PDO('mysql:dbname=dbname;host=dbhost1', "user", "pass");
Legacy mysql extension
mysql_connect("dbhost1", "user", "pass");
Note: Make sure in the database user#web1 (on host web1) or user#web2 (on host web2) has access.
You can connect to a remote database given the correct settings.
Failing that, you could write a service layer that could be called from both servers.
while connecting to mysql database use same database connection credential on both servers.
Yes why not. But for security and performance issues i wouldn't do that.
The question is what do you understand under on another server is the server direct connected over LAN or is the server in another Data center. When the server is in another Data center you can get a lot of traffic and performance issues.
When the server is direkt connected you can change the my.cnf and change the line:
bind-address 127.0.0.1
then the server is reachable from outside. And you should give the user enough rights to connect from outside.
yes, it's possibile.
you have to use a mysql server which is reachable from both servers. and you simply connect with the same host/user/pass from both servers:
define("WEBDOMAIN", "94.145.22.15"); //some fake data
define("DEFDBNAME", "my_db");
define("DEFUSERNAME", "my_user");
define("DEFDBPWD", "my_pass");
mysql_connect( WEBDOMAIN, DEFUSERNAME, DEFDBPWD );
mysql_select_db( DEFDBNAME);
That should be enaugh. You coud have some problems if your hosting providers doesn't allow servers to connect to external servers (sometimes ports are closed).
For security reasons it is common, that You can't connect to a mysql server that has not authorized your hosting server.
So in mysql, You have to allow a user to connect not only from localhost, but also from other host (if You have enought priviliges, You can do it from phpMyAdmin by editing user priviliges afair).

Cannot connect to mysql with 127.0.0.1

With the following code I can connect to mysql:
mysql_connect("localhost","username","");
But if I change localhost to 127.0.0.1 I get the following error:
Can't connect to MySQL server on '127.0.0.1' (13)
Why doesn't it work with 127.0.0.1?
localhost is special cased and uses UNIX sockets instead of TCP/IP. 127.0.0.1 doesn't get that special handling.
See the documentation:
On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option.
If it doesn't work when you use TCP/IP then the database probably isn't listening on the network. This is generally a good thing as it enhances security (not that listening on 127.0.0.1 exposes any problems, but listening on all interfaces gives more opportunity for attacks).
If you really want to allow connections via the network, then see skip-networking.
have you got an entry in your hosts file mapping 127.0.0.7 to localhost?
Do you have more than 1 mysql servers installed/running on your system? If so, please specify the port number of the mysql server you are trying to access like 127.0.0.1:3306,127.0.0.1:8889 etc.
If you do not know whether there are any other mysql server instances running on your system also, please specify the port.
You will be able to access it when you add the privileges for 'root'#'127.0.0.1' in the "USER_PRIVILEGES" table in the "information_schema" database
You might also try disabling SELINUX

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