php没法使用localhost连接mysql服务器
程序员文章站
2022-06-01 17:52:10
...
php无法使用localhost连接mysql服务器
在redhat下安装了mysql,在mysql控制台下,可以使用localhost连接,但是在php中连接时,却不行,而使用127.0.0.1能连上,查看了hosts文件,发现有127.0.0.1和localhost的对应
哪位大侠知道是怎么回事
------解决方案--------------------
是不是你在建站点的时候设置了站点为http://127.0.0.1了呢?
------解决方案--------------------
看一看你的mysql是怎么配置的,
检查mysql启动后/usr/local/mysql/var/mysql.sock是否存在。
------解决方案--------------------
Description
A frequent error message received when using the mysql command line utility is: Can 't connect to local MySQL server through socket '/tmp/mysql.sock ' While this error message can be frustrating, the solution is simple.
Directions
When connecting to a MySQL server located on the local system, the mysql client connects thorugh a local file called a socket instead of connecting to the localhost loopback address 127.0.0.1. For the mysql client, the default location of this socket file is /tmp/mysql.sock. However, for a variety of reasons, many MySQL installations place this socket file somewhere else like /var/lib/mysql/mysql.sock.
While it is possible to make this work by specifying the socket file directly in the mysql client command
mysql --socket=/var/lib/mysql/mysql.sock ...
it is painful to type this in every time. If you must do so this way (because you don 't have permissions to the file in the solution below), you could create an alias in your shell to make this work
(like alias mysql= "mysql --socket=/var/lib/mysql/mysql.sock " depending on your shell).
To make your life easier, you can make a simple change to the MySQL configuration file /etc/my.cnf that will permanently set the socket file used by the mysql client. After making a backup copy of /etc/my.cnf, open it in your favorite editor. The file is divided into sections such as
[mysqld]
datadir=/usr/local/mysql/data
socket=/var/lib/mysql/mysql.sock
[mysql.server]
user=mysql
basedir=/usr/local/mysql
If there is not currently a section called [client], add one at the bottom of the file and copy the socket= line under the [mysqld] section such as:
[client]
socket=/var/lib/mysql/mysql.sock
If there is already a [client] section in the my.cnf file, add or edit the socket line as appropriate. You won 't need to restart your server or any other processes. Subsequent uses of the mysql client will use the proper socket file.
在redhat下安装了mysql,在mysql控制台下,可以使用localhost连接,但是在php中连接时,却不行,而使用127.0.0.1能连上,查看了hosts文件,发现有127.0.0.1和localhost的对应
哪位大侠知道是怎么回事
------解决方案--------------------
是不是你在建站点的时候设置了站点为http://127.0.0.1了呢?
------解决方案--------------------
看一看你的mysql是怎么配置的,
检查mysql启动后/usr/local/mysql/var/mysql.sock是否存在。
------解决方案--------------------
Description
A frequent error message received when using the mysql command line utility is: Can 't connect to local MySQL server through socket '/tmp/mysql.sock ' While this error message can be frustrating, the solution is simple.
Directions
When connecting to a MySQL server located on the local system, the mysql client connects thorugh a local file called a socket instead of connecting to the localhost loopback address 127.0.0.1. For the mysql client, the default location of this socket file is /tmp/mysql.sock. However, for a variety of reasons, many MySQL installations place this socket file somewhere else like /var/lib/mysql/mysql.sock.
While it is possible to make this work by specifying the socket file directly in the mysql client command
mysql --socket=/var/lib/mysql/mysql.sock ...
it is painful to type this in every time. If you must do so this way (because you don 't have permissions to the file in the solution below), you could create an alias in your shell to make this work
(like alias mysql= "mysql --socket=/var/lib/mysql/mysql.sock " depending on your shell).
To make your life easier, you can make a simple change to the MySQL configuration file /etc/my.cnf that will permanently set the socket file used by the mysql client. After making a backup copy of /etc/my.cnf, open it in your favorite editor. The file is divided into sections such as
[mysqld]
datadir=/usr/local/mysql/data
socket=/var/lib/mysql/mysql.sock
[mysql.server]
user=mysql
basedir=/usr/local/mysql
If there is not currently a section called [client], add one at the bottom of the file and copy the socket= line under the [mysqld] section such as:
[client]
socket=/var/lib/mysql/mysql.sock
If there is already a [client] section in the my.cnf file, add or edit the socket line as appropriate. You won 't need to restart your server or any other processes. Subsequent uses of the mysql client will use the proper socket file.
相关文章
相关视频
上一篇: PHP查询数据库中满足条件的记录条数(两种实现方法)
下一篇: 关于PHP伪静态的研讨
推荐阅读
-
MySQL使用innobackupex备份连接服务器失败的解决方法
-
PHP下使用mysqli的函数连接mysql出现warning: mysqli::real_connect(): (hy000/1040): ...
-
MySQL使用innobackupex备份连接服务器失败的解决方法
-
php使用mysqli和pdo扩展,测试对比连接mysql数据库的效率完整示例
-
php中mysql连接和基本操作代码(快速测试使用,简单方便)
-
如何使用php判断服务器是否是HTTPS连接
-
PHP持久连接mysql_pconnect()函数使用介绍
-
深入探讨:PHP使用数据库永久连接方式操作MySQL的是与非
-
使用PHP连接LDAP服务器
-
php中mysql连接方式PDO使用详解