How can I link to a MySQL server on a web host?

When using PHP to connect to a MySQL database on a web hosting, what is the best way to access the server?

The MySQL administration page on my website says what the server’s IP address is, but can I use something 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:

Failed to connect: unable to connect to the local MySQL server through the socket '/var/lib/mysql/mysql.sock' (2)

+4
source share
6 answers

You can access the script system with "localhost" as the host.

 $con = mysql_connect("localhost", "user", "password"); 

However, the connection to the IP is most likely not done, because you have a lowercase L where you should have one digit (but I hope the IP address in the question was just an example).

Also, use mysqli or PDO instead of mysql, which is on the verge of obsolescence.

+2
source

You can specify the host name of the database server, for example. mydbbox.example.com . Using an IP address can be faster, since DNS lookups are not needed.

Also see this SO question .

0
source

The answer to this question depends on how your host (or their control panel) sets MySQL privileges. Privileges have an IP or hostname, so often an IP or hostname connection may not have the same connection result. If in doubt, I will stick with exactly the settings that they will give you.

More details: http://dev.mysql.com/doc/refman/5.0/en/account-names.html

0
source

If the database host is the same computer, use localhost . MySQL will connect with a local socket, not tcpip (network) when you use localhost with a port number or not.

Sometimes the server does not support socket connections, and you get an error, for example:

Failed to connect: unable to connect to local MySQL server via socket ...

EDIT / FIXED: If you cannot connect via a socket, use 127.0.0.1 . This will force conncetion tcpip.

If the database server is installed on another computer, you can use IP address or domain .

In most cases, the IP address is better because the connection can be established even when the DNS (domain name server) is not working correctly or after the domain expires.


And one piece of advice is to use mysqli_ instead of mysql_ functions in a new application. New features are very similar in use, but more secure.

The old mysql_ functions are deprecated, and they are not recommended for new applications. They are still available in PHP due to compatibility with old obsolete software (CMS systems, e-commerce systems, etc.

PHP help says (about mysql_connect):

Using this extension is not recommended. Instead, MySQLi or PDO_MySQL should use the extension.


Learn more about connecting with localhost - information from MySQL Reference

On Unix, MySQL programs handle the localhost hostname specifically, in a way that is likely different from what you expect compared to other network programs. To connect to the local host, MySQL programs try connecting to the local server using a Unix socket file. This happens even if the -port or -P option is specified to specify the port number. For the client to make a TCP / IP connection to the local server, use -host or -h to specify the host name value 127.0.0.1

Please note that the "-port" and "-P" options are described in a different context, except for PHP, but the connection mechanism works in a similar way, and localhost is a "special" name, it does not matter if it is PHP, the console or some other software security.

0
source

Have you tried to enter something like this on google?

php Failed to connect: unable to connect to local MySQL server via socket '/var/lib/mysql/mysql.sock' (2)

0
source

This mainly depends on how your host is configured. In my experience, you will use "localhost", the server IP address or a specific subdomain configured by the host.

I would start by testing "localhost" as this is the default value for most public hosting environments.

 $con = mysql_connect("localhost", "username", "password"); 
-1
source

Source: https://habr.com/ru/post/1441442/


All Articles