简体   繁体   中英

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

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, eg 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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM