Home  >  Article  >  Database  >  关于php中使用IP和localhost连接问题

关于php中使用IP和localhost连接问题

WBOY
WBOYOriginal
2016-05-31 08:47:51983browse

php连接数据库分两种,一种是IP,一种是unix sock,即localhost,如果是连接本地mysql,用localhost较好,也方便后期的管理,如果用于连接非本地数据库,则要通过IP了。

php连接mysql代码

$con = mysql_connect('localhost','phpadmin','phptest');

if (!$con)

{

die('cannot connect mysql' . mysql_error());

}

echo "mysql connect OK";

mysql_close();

?>

打开网页后出现下面代码

cannot connect mysqlCan't connect to local MySQL server through socket '/opt/mysql/var/mysql.sock' (2)

1、看mysql有没有授权用户通过localhost访问数据库,如果已经有授权了,就得从另外一方面来排查。

2、查看mysql的配置文件,看看sock是否有配置,如果没有,添加配置,指定路径,方便一会从php.ini文件来指定,如果不能随便重启mysql服务,通过命令来查找sock所在目录(此时mysql服务已经开启,会有sock存在系统中,未开启服务时不存在)#find / -name mysql.sock ,这样可以看到scok所在目录,然后修改php.ini配置文件,找到mysql.default_socket = ,在等号后面添加上面找到的sock路径,如mysql.default_socket = /tmp/mysqld.sock,然后重启php-fpm,应该就可以了。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn