The connection between php and localhost is divided into two types: IP and unix sock, that is, localhost, it is better to use localhost, which is also convenient for later Management. if it is used to connect to a non-local database, it will pass through the IP address.
Php connection to mysql code
$ Con = mysql_connect ('localhost', 'phpadmin', 'phptest ');
If (! $ Con)
{
Die ('cannot connect mysql'. mysql_error ());
}
Echo "mysql connect OK ";
Mysql_close ();
?>
The following code is displayed after the webpage is opened.
Cannot connect mysqlCan't connect to local MySQL server through socket '/opt/mysql/var/mysql. sock' (2)
1. check whether mysql has authorized the user to access the database through localhost. if the user has been authorized, check the database from another aspect.
2. check the mysql configuration file to see if sock has been configured. if not, add the configuration and specify the path so that php can be used later. if the mysql service cannot be restarted, run the command to find the directory where the sock is located. (if the mysql service is enabled, sock exists in the system and does not exist when the service is not enabled) # find/-name mysql. sock, you can see the scok directory, and then modify php. in the ini configuration file, find mysql. default_socket =, add the sock path found above after the equal sign, such as mysql. default_socket =/tmp/mysqld. sock and restart php-fpm.